1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
|
/*
* linux/fs/hfs/file_hdr.c
*
* Copyright (C) 1995-1997 Paul H. Hargrove
* This file may be distributed under the terms of the GNU Public License.
*
* This file contains the file_ops and inode_ops for the metadata
* files under the AppleDouble and Netatalk representations.
*
* The source code distributions of Netatalk, versions 1.3.3b2 and
* 1.4b2, were used as a specification of the location and format of
* files used by Netatalk's afpd. No code from Netatalk appears in
* hfs_fs. hfs_fs is not a work ``derived'' from Netatalk in the
* sense of intellectual property law.
*
* "XXX" in a comment is a note to myself to consider changing something.
*
* In function preconditions the term "valid" applied to a pointer to
* a structure means that the pointer is non-NULL and the structure it
* points to has all fields initialized to consistent values.
*
* XXX: Note the reason that there is not bmap() for AppleDouble
* header files is that dynamic nature of their structure make it
* very difficult to safely mmap them. Maybe in the distant future
* I'll get bored enough to implement it.
*/
#include "hfs.h"
#include <linux/hfs_fs_sb.h>
#include <linux/hfs_fs_i.h>
#include <linux/hfs_fs.h>
/* prodos types */
#define PRODOSI_FTYPE_DIR 0x0F
#define PRODOSI_FTYPE_TEXT 0x04
#define PRODOSI_FTYPE_8BIT 0xFF
#define PRODOSI_FTYPE_16BIT 0xB3
#define PRODOSI_AUXTYPE_DIR 0x0200
/*================ Forward declarations ================*/
static hfs_rwret_t hdr_read(struct file *, char *, hfs_rwarg_t, loff_t *);
static hfs_rwret_t hdr_write(struct file *, const char *,
hfs_rwarg_t, loff_t *);
static void hdr_truncate(struct inode *);
/*================ Global variables ================*/
static struct file_operations hfs_hdr_operations = {
NULL, /* lseek - default */
hdr_read, /* read */
hdr_write, /* write */
NULL, /* readdir - bad */
NULL, /* select - default */
NULL, /* ioctl - default */
NULL, /* mmap - XXX: not yet */
NULL, /* no special open code */
NULL, /* flush */
NULL, /* no special release code */
file_fsync, /* fsync - default */
NULL, /* fasync - default */
NULL, /* check_media_change - none */
NULL, /* revalidate - none */
NULL /* lock - none */
};
struct inode_operations hfs_hdr_inode_operations = {
&hfs_hdr_operations, /* default file operations */
NULL, /* create */
NULL, /* lookup */
NULL, /* link */
NULL, /* unlink */
NULL, /* symlink */
NULL, /* mkdir */
NULL, /* rmdir */
NULL, /* mknod */
NULL, /* rename */
NULL, /* readlink */
NULL, /* follow_link */
NULL, /* get_block - XXX: not available since
header part has no disk block */
NULL, /* readpage */
NULL, /* writepage */
NULL, /* flushpage */
hdr_truncate, /* truncate */
NULL, /* permission */
NULL, /* smap */
NULL /* revalidate */
};
const struct hfs_hdr_layout hfs_dbl_fil_hdr_layout = {
__constant_htonl(HFS_DBL_MAGIC), /* magic */
__constant_htonl(HFS_HDR_VERSION_2), /* version */
6, /* entries */
{ /* descr[] */
{HFS_HDR_FNAME, offsetof(struct hfs_dbl_hdr, real_name), ~0},
{HFS_HDR_DATES, offsetof(struct hfs_dbl_hdr, create_time), 16},
{HFS_HDR_FINFO, offsetof(struct hfs_dbl_hdr, finderinfo), 32},
{HFS_HDR_MACI, offsetof(struct hfs_dbl_hdr, fileinfo), 4},
{HFS_HDR_DID, offsetof(struct hfs_dbl_hdr, cnid), 4},
{HFS_HDR_RSRC, HFS_DBL_HDR_LEN, ~0}
},
{ /* order[] */
(struct hfs_hdr_descr *)&hfs_dbl_fil_hdr_layout.descr[0],
(struct hfs_hdr_descr *)&hfs_dbl_fil_hdr_layout.descr[1],
(struct hfs_hdr_descr *)&hfs_dbl_fil_hdr_layout.descr[2],
(struct hfs_hdr_descr *)&hfs_dbl_fil_hdr_layout.descr[3],
(struct hfs_hdr_descr *)&hfs_dbl_fil_hdr_layout.descr[4],
(struct hfs_hdr_descr *)&hfs_dbl_fil_hdr_layout.descr[5]
}
};
const struct hfs_hdr_layout hfs_dbl_dir_hdr_layout = {
__constant_htonl(HFS_DBL_MAGIC), /* magic */
__constant_htonl(HFS_HDR_VERSION_2), /* version */
5, /* entries */
{ /* descr[] */
{HFS_HDR_FNAME, offsetof(struct hfs_dbl_hdr, real_name), ~0},
{HFS_HDR_DATES, offsetof(struct hfs_dbl_hdr, create_time), 16},
{HFS_HDR_FINFO, offsetof(struct hfs_dbl_hdr, finderinfo), 32},
{HFS_HDR_MACI, offsetof(struct hfs_dbl_hdr, fileinfo), 4},
{HFS_HDR_DID, offsetof(struct hfs_dbl_hdr, cnid), 4}
},
{ /* order[] */
(struct hfs_hdr_descr *)&hfs_dbl_dir_hdr_layout.descr[0],
(struct hfs_hdr_descr *)&hfs_dbl_dir_hdr_layout.descr[1],
(struct hfs_hdr_descr *)&hfs_dbl_dir_hdr_layout.descr[2],
(struct hfs_hdr_descr *)&hfs_dbl_dir_hdr_layout.descr[3],
(struct hfs_hdr_descr *)&hfs_dbl_dir_hdr_layout.descr[4]
}
};
const struct hfs_hdr_layout hfs_nat2_hdr_layout = {
__constant_htonl(HFS_DBL_MAGIC), /* magic */
__constant_htonl(HFS_HDR_VERSION_2), /* version */
9, /* entries */
{ /* descr[] */
{HFS_HDR_FNAME, offsetof(struct hfs_dbl_hdr, real_name), ~0},
{HFS_HDR_COMNT, offsetof(struct hfs_dbl_hdr, comment), 0},
{HFS_HDR_DATES, offsetof(struct hfs_dbl_hdr, create_time), 16},
{HFS_HDR_FINFO, offsetof(struct hfs_dbl_hdr, finderinfo), 32},
{HFS_HDR_AFPI, offsetof(struct hfs_dbl_hdr, fileinfo), 4},
{HFS_HDR_DID, offsetof(struct hfs_dbl_hdr, cnid), 4},
{HFS_HDR_SNAME, offsetof(struct hfs_dbl_hdr, short_name), ~0},
{HFS_HDR_PRODOSI, offsetof(struct hfs_dbl_hdr, prodosi), 8},
{HFS_HDR_RSRC, HFS_NAT_HDR_LEN, ~0}
},
{ /* order[] */
(struct hfs_hdr_descr *)&hfs_nat_hdr_layout.descr[0],
(struct hfs_hdr_descr *)&hfs_nat_hdr_layout.descr[1],
(struct hfs_hdr_descr *)&hfs_nat_hdr_layout.descr[2],
(struct hfs_hdr_descr *)&hfs_nat_hdr_layout.descr[3],
(struct hfs_hdr_descr *)&hfs_nat_hdr_layout.descr[4],
(struct hfs_hdr_descr *)&hfs_nat_hdr_layout.descr[5],
(struct hfs_hdr_descr *)&hfs_nat_hdr_layout.descr[6],
(struct hfs_hdr_descr *)&hfs_nat_hdr_layout.descr[7],
(struct hfs_hdr_descr *)&hfs_nat_hdr_layout.descr[8]
}
};
const struct hfs_hdr_layout hfs_nat_hdr_layout = {
__constant_htonl(HFS_DBL_MAGIC), /* magic */
__constant_htonl(HFS_HDR_VERSION_1), /* version */
5, /* entries */
{ /* descr[] */
{HFS_HDR_FNAME, offsetof(struct hfs_dbl_hdr, real_name), ~0},
{HFS_HDR_COMNT, offsetof(struct hfs_dbl_hdr, comment), 0},
{HFS_HDR_OLDI, offsetof(struct hfs_dbl_hdr, create_time), 16},
{HFS_HDR_FINFO, offsetof(struct hfs_dbl_hdr, finderinfo), 32},
{HFS_HDR_RSRC, HFS_NAT_HDR_LEN, ~0},
},
{ /* order[] */
(struct hfs_hdr_descr *)&hfs_nat_hdr_layout.descr[0],
(struct hfs_hdr_descr *)&hfs_nat_hdr_layout.descr[1],
(struct hfs_hdr_descr *)&hfs_nat_hdr_layout.descr[2],
(struct hfs_hdr_descr *)&hfs_nat_hdr_layout.descr[3],
(struct hfs_hdr_descr *)&hfs_nat_hdr_layout.descr[4]
}
};
/*================ File-local variables ================*/
static const char fstype[16] =
{'M','a','c','i','n','t','o','s','h',' ',' ',' ',' ',' ',' ',' '};
/*================ File-local data types ================*/
struct hdr_hdr {
hfs_lword_t magic;
hfs_lword_t version;
hfs_byte_t filler[16];
hfs_word_t entries;
hfs_byte_t descrs[12*HFS_HDR_MAX];
};
/*================ File-local functions ================*/
/*
* dlength()
*/
static int dlength(const struct hfs_hdr_descr *descr,
const struct hfs_cat_entry *entry)
{
hfs_u32 length = descr->length;
/* handle auto-sized entries */
if (length == ~0) {
switch (descr->id) {
case HFS_HDR_DATA:
if (entry->type == HFS_CDR_FIL) {
length = entry->u.file.data_fork.lsize;
} else {
length = 0;
}
break;
case HFS_HDR_RSRC:
if (entry->type == HFS_CDR_FIL) {
length = entry->u.file.rsrc_fork.lsize;
} else {
length = 0;
}
break;
case HFS_HDR_FNAME:
length = entry->key.CName.Len;
break;
case HFS_HDR_SNAME:
default:
length = 0;
}
}
return length;
}
/*
* hdr_build_meta()
*/
static void hdr_build_meta(struct hdr_hdr *meta,
const struct hfs_hdr_layout *layout,
const struct hfs_cat_entry *entry)
{
const struct hfs_hdr_descr *descr;
hfs_byte_t *ptr;
int lcv;
hfs_put_nl(layout->magic, meta->magic);
hfs_put_nl(layout->version, meta->version);
if (layout->version == htonl(HFS_HDR_VERSION_1)) {
memcpy(meta->filler, fstype, 16);
} else {
memset(meta->filler, 0, 16);
}
hfs_put_hs(layout->entries, meta->entries);
memset(meta->descrs, 0, sizeof(meta->descrs));
for (lcv = 0, descr = layout->descr, ptr = meta->descrs;
lcv < layout->entries; ++lcv, ++descr, ptr += 12) {
hfs_put_hl(descr->id, ptr);
hfs_put_hl(descr->offset, ptr + 4);
hfs_put_hl(dlength(descr, entry), ptr + 8);
}
}
/*
* dup_layout ()
*/
static struct hfs_hdr_layout *dup_layout(const struct hfs_hdr_layout *old)
{
struct hfs_hdr_layout *new;
int lcv;
if (HFS_NEW(new)) {
memcpy(new, old, sizeof(*new));
for (lcv = 0; lcv < new->entries; ++lcv) {
(char *)(new->order[lcv]) += (char *)new - (char *)old;
}
}
return new;
}
/*
* init_layout()
*/
static inline void init_layout(struct hfs_hdr_layout *layout,
const hfs_byte_t *descrs)
{
struct hfs_hdr_descr **base, **p, **q, *tmp;
int lcv, entries = layout->entries;
for (lcv = 0; lcv < entries; ++lcv, descrs += 12) {
layout->order[lcv] = &layout->descr[lcv];
layout->descr[lcv].id = hfs_get_hl(descrs);
layout->descr[lcv].offset = hfs_get_hl(descrs + 4);
layout->descr[lcv].length = hfs_get_hl(descrs + 8);
}
for (lcv = layout->entries; lcv < HFS_HDR_MAX; ++lcv) {
layout->order[lcv] = NULL;
layout->descr[lcv].id = 0;
layout->descr[lcv].offset = 0;
layout->descr[lcv].length = 0;
}
/* Sort the 'order' array using an insertion sort */
base = &layout->order[0];
for (p = (base+1); p < (base+entries); ++p) {
q=p;
while ((*q)->offset < (*(q-1))->offset) {
tmp = *q;
*q = *(q-1);
*(--q) = tmp;
if (q == base) break;
}
}
}
/*
* adjust_forks()
*/
static inline void adjust_forks(struct hfs_cat_entry *entry,
const struct hfs_hdr_layout *layout)
{
int lcv;
for (lcv = 0; lcv < layout->entries; ++lcv) {
const struct hfs_hdr_descr *descr = &layout->descr[lcv];
if ((descr->id == HFS_HDR_DATA) &&
(descr->length != entry->u.file.data_fork.lsize)) {
entry->u.file.data_fork.lsize = descr->length;
hfs_extent_adj(&entry->u.file.data_fork);
} else if ((descr->id == HFS_HDR_RSRC) &&
(descr->length != entry->u.file.rsrc_fork.lsize)) {
entry->u.file.rsrc_fork.lsize = descr->length;
hfs_extent_adj(&entry->u.file.rsrc_fork);
}
}
}
/*
* get_dates()
*/
static void get_dates(const struct hfs_cat_entry *entry,
const struct inode *inode, hfs_u32 dates[3])
{
dates[0] = hfs_m_to_htime(entry->create_date);
dates[1] = hfs_m_to_htime(entry->modify_date);
dates[2] = hfs_m_to_htime(entry->backup_date);
}
/*
* set_dates()
*/
static void set_dates(struct hfs_cat_entry *entry, struct inode *inode,
const hfs_u32 *dates)
{
hfs_u32 tmp;
tmp = hfs_h_to_mtime(dates[0]);
if (entry->create_date != tmp) {
entry->create_date = tmp;
hfs_cat_mark_dirty(entry);
}
tmp = hfs_h_to_mtime(dates[1]);
if (entry->modify_date != tmp) {
entry->modify_date = tmp;
inode->i_ctime = inode->i_atime = inode->i_mtime =
hfs_h_to_utime(dates[1]);
hfs_cat_mark_dirty(entry);
}
tmp = hfs_h_to_mtime(dates[2]);
if (entry->backup_date != tmp) {
entry->backup_date = tmp;
hfs_cat_mark_dirty(entry);
}
}
/*
* hdr_read()
*
* This is the read field in the inode_operations structure for
* header files. The purpose is to transfer up to 'count' bytes
* from the file corresponding to 'inode', beginning at
* 'filp->offset' bytes into the file. The data is transfered to
* user-space at the address 'buf'. Returns the number of bytes
* successfully transfered.
*/
/* XXX: what about the entry count changing on us? */
static hfs_rwret_t hdr_read(struct file * filp, char * buf,
hfs_rwarg_t count, loff_t *ppos)
{
struct inode *inode = filp->f_dentry->d_inode;
struct hfs_cat_entry *entry = HFS_I(inode)->entry;
const struct hfs_hdr_layout *layout;
off_t start, length, offset;
off_t pos = *ppos;
int left, lcv, read = 0;
if (!S_ISREG(inode->i_mode)) {
hfs_warn("hfs_hdr_read: mode = %07o\n",inode->i_mode);
return -EINVAL;
}
if (HFS_I(inode)->layout) {
layout = HFS_I(inode)->layout;
} else {
layout = HFS_I(inode)->default_layout;
}
/* Adjust count to fit within the bounds of the file */
if ((pos >= inode->i_size) || (count <= 0)) {
return 0;
} else if (count > inode->i_size - pos) {
count = inode->i_size - pos;
}
/* Handle the fixed-location portion */
length = sizeof(hfs_u32) + sizeof(hfs_u32) + 16 +
sizeof(hfs_u16) + layout->entries * (3 * sizeof(hfs_u32));
if (pos < length) {
struct hdr_hdr meta;
left = length - pos;
if (left > count) {
left = count;
}
hdr_build_meta(&meta, layout, entry);
left -= copy_to_user(buf, ((char *)&meta) + pos, left);
count -= left;
read += left;
pos += left;
buf += left;
}
if (!count) {
goto done;
}
/* Handle the actual data */
for (lcv = 0; count && (lcv < layout->entries); ++lcv) {
const struct hfs_hdr_descr *descr = layout->order[lcv];
struct hfs_fork *fork;
char tmp[16], *p;
off_t limit;
/* stop reading if we run out of descriptors early */
if (!descr) {
break;
}
/* find start and length of this entry */
start = descr->offset;
length = dlength(descr, entry);
/* Skip to next entry if this one is empty or isn't needed */
if (!length || (pos >= start + length)) {
continue;
}
/* Pad with zeros to the start of this entry if needed */
if (pos < start) {
left = start - pos;
if (left > count) {
left = count;
}
clear_user(buf, left);
count -= left;
read += left;
pos += left;
buf += left;
}
if (!count) {
goto done;
}
/* locate and/or construct the data for this entry */
fork = NULL;
p = NULL;
switch (descr->id) {
case HFS_HDR_DATA:
fork = &entry->u.file.data_fork;
limit = fork->lsize;
break;
case HFS_HDR_RSRC:
fork = &entry->u.file.rsrc_fork;
limit = fork->lsize;
break;
case HFS_HDR_FNAME:
p = entry->key.CName.Name;
limit = entry->key.CName.Len;
break;
case HFS_HDR_OLDI:
case HFS_HDR_DATES:
get_dates(entry, inode, (hfs_u32 *)tmp);
if (descr->id == HFS_HDR_DATES) {
/* XXX: access date. hfsplus actually
has this. */
memcpy(tmp + 12, tmp + 4, 4);
} else if ((entry->type == HFS_CDR_FIL) &&
(entry->u.file.flags & HFS_FIL_LOCK)) {
hfs_put_hl(HFS_AFP_RDONLY, tmp + 12);
} else {
hfs_put_nl(0, tmp + 12);
}
p = tmp;
limit = 16;
break;
case HFS_HDR_FINFO:
p = (char *)&entry->info;
limit = 32;
break;
case HFS_HDR_AFPI:
/* XXX: this needs to do more mac->afp mappings */
hfs_put_ns(0, tmp);
if ((entry->type == HFS_CDR_FIL) &&
(entry->u.file.flags & HFS_FIL_LOCK)) {
hfs_put_hs(HFS_AFP_RDONLY, tmp + 2);
} else {
hfs_put_ns(0, tmp + 2);
}
p = tmp;
limit = 4;
break;
case HFS_HDR_PRODOSI:
/* XXX: this needs to do mac->prodos translations */
memset(tmp, 0, 8);
#if 0
hfs_put_ns(0, tmp); /* access */
hfs_put_ns(0, tmp); /* type */
hfs_put_nl(0, tmp); /* aux type */
#endif
p = tmp;
limit = 8;
break;
case HFS_HDR_MACI:
hfs_put_ns(0, tmp);
if (entry->type == HFS_CDR_FIL) {
hfs_put_hs(entry->u.file.flags, tmp + 2);
} else {
hfs_put_ns(entry->u.dir.flags, tmp + 2);
}
p = tmp;
limit = 4;
break;
case HFS_HDR_DID:
/* if it's rootinfo, stick the next available did in
* the did slot. */
limit = 4;
if (entry->cnid == htonl(HFS_ROOT_CNID)) {
struct hfs_mdb *mdb = entry->mdb;
const struct hfs_name *reserved =
HFS_SB(mdb->sys_mdb)->s_reserved2;
while (reserved->Len) {
if (hfs_streq(reserved->Name,
reserved->Len,
entry->key.CName.Name,
entry->key.CName.Len)) {
hfs_put_hl(mdb->next_id, tmp);
p = tmp;
goto hfs_did_done;
}
reserved++;
}
}
p = (char *) &entry->cnid;
hfs_did_done:
break;
case HFS_HDR_SNAME:
default:
limit = 0;
}
/* limit the transfer to the available data
of to the stated length of the entry. */
if (length > limit) {
length = limit;
}
offset = pos - start;
left = length - offset;
if (left > count) {
left = count;
}
if (left <= 0) {
continue;
}
/* transfer the data */
if (p) {
left -= copy_to_user(buf, p + offset, left);
} else if (fork) {
left = hfs_do_read(inode, fork, offset, buf, left,
filp->f_reada != 0);
if (left > 0) {
filp->f_reada = 1;
} else if (!read) {
return left;
} else {
goto done;
}
}
count -= left;
read += left;
pos += left;
buf += left;
}
/* Pad the file out with zeros */
if (count) {
clear_user(buf, count);
read += count;
pos += count;
}
done:
if (read) {
inode->i_atime = CURRENT_TIME;
*ppos = pos;
mark_inode_dirty(inode);
}
return read;
}
/*
* hdr_write()
*
* This is the write() entry in the file_operations structure for
* header files. The purpose is to transfer up to 'count' bytes
* to the file corresponding to 'inode' beginning at offset
* '*ppos' from user-space at the address 'buf'.
* The return value is the number of bytes actually transferred.
*/
static hfs_rwret_t hdr_write(struct file *filp, const char *buf,
hfs_rwarg_t count, loff_t *ppos)
{
struct inode *inode = filp->f_dentry->d_inode;
struct hfs_cat_entry *entry = HFS_I(inode)->entry;
struct hfs_hdr_layout *layout;
off_t start, length, offset;
int left, lcv, written = 0;
struct hdr_hdr meta;
int built_meta = 0;
off_t pos;
if (!S_ISREG(inode->i_mode)) {
hfs_warn("hfs_hdr_write: mode = %07o\n", inode->i_mode);
return -EINVAL;
}
if (count <= 0) {
return 0;
}
pos = (filp->f_flags & O_APPEND) ? inode->i_size : *ppos;
if (!HFS_I(inode)->layout) {
HFS_I(inode)->layout = dup_layout(HFS_I(inode)->default_layout);
}
layout = HFS_I(inode)->layout;
/* Handle the 'magic', 'version', 'filler' and 'entries' fields */
length = sizeof(hfs_u32) + sizeof(hfs_u32) + 16 + sizeof(hfs_u16);
if (pos < length) {
hdr_build_meta(&meta, layout, entry);
built_meta = 1;
left = length - pos;
if (left > count) {
left = count;
}
left -= copy_from_user(((char *)&meta) + pos, buf, left);
layout->magic = hfs_get_nl(meta.magic);
layout->version = hfs_get_nl(meta.version);
layout->entries = hfs_get_hs(meta.entries);
if (layout->entries > HFS_HDR_MAX) {
/* XXX: should allocate slots dynamically */
hfs_warn("hfs_hdr_write: TRUNCATING TO %d "
"DESCRIPTORS\n", HFS_HDR_MAX);
layout->entries = HFS_HDR_MAX;
}
count -= left;
written += left;
pos += left;
buf += left;
}
if (!count) {
goto done;
}
/* We know for certain how many entries we have, so process them */
length += layout->entries * 3 * sizeof(hfs_u32);
if (pos < length) {
if (!built_meta) {
hdr_build_meta(&meta, layout, entry);
}
left = length - pos;
if (left > count) {
left = count;
}
left -= copy_from_user(((char *)&meta) + pos, buf, left);
init_layout(layout, meta.descrs);
count -= left;
written += left;
pos += left;
buf += left;
/* Handle possible size changes for the forks */
if (entry->type == HFS_CDR_FIL) {
adjust_forks(entry, layout);
hfs_cat_mark_dirty(entry);
}
}
/* Handle the actual data */
for (lcv = 0; count && (lcv < layout->entries); ++lcv) {
struct hfs_hdr_descr *descr = layout->order[lcv];
struct hfs_fork *fork;
char tmp[16], *p;
off_t limit;
/* stop writing if we run out of descriptors early */
if (!descr) {
break;
}
/* find start and length of this entry */
start = descr->offset;
if ((descr->id == HFS_HDR_DATA) ||
(descr->id == HFS_HDR_RSRC)) {
if (entry->type == HFS_CDR_FIL) {
length = 0x7fffffff - start;
} else {
continue;
}
} else {
length = dlength(descr, entry);
}
/* Trim length to avoid overlap with the next entry */
if (layout->order[lcv+1] &&
((start + length) > layout->order[lcv+1]->offset)) {
length = layout->order[lcv+1]->offset - start;
}
/* Skip to next entry if this one is empty or isn't needed */
if (!length || (pos >= start + length)) {
continue;
}
/* Skip any padding that may exist between entries */
if (pos < start) {
left = start - pos;
if (left > count) {
left = count;
}
count -= left;
written += left;
pos += left;
buf += left;
}
if (!count) {
goto done;
}
/* locate and/or construct the data for this entry */
fork = NULL;
p = NULL;
switch (descr->id) {
case HFS_HDR_DATA:
#if 0
/* Can't yet write to the data fork via a header file, since there is the
* possibility to write via the data file, and the only locking is at the
* inode level.
*/
fork = &entry->u.file.data_fork;
limit = length;
#else
limit = 0;
#endif
break;
case HFS_HDR_RSRC:
fork = &entry->u.file.rsrc_fork;
limit = length;
break;
case HFS_HDR_OLDI:
case HFS_HDR_DATES:
get_dates(entry, inode, (hfs_u32 *)tmp);
if (descr->id == HFS_HDR_DATES) {
memcpy(tmp + 12, tmp + 4, 4);
} else if ((entry->type == HFS_CDR_FIL) &&
(entry->u.file.flags & HFS_FIL_LOCK)) {
hfs_put_hl(HFS_AFP_RDONLY, tmp + 12);
} else {
hfs_put_nl(0, tmp + 12);
}
p = tmp;
limit = 16;
break;
case HFS_HDR_FINFO:
p = (char *)&entry->info;
limit = 32;
break;
case HFS_HDR_AFPI:
hfs_put_ns(0, tmp);
if ((entry->type == HFS_CDR_FIL) &&
(entry->u.file.flags & HFS_FIL_LOCK)) {
hfs_put_hs(HFS_AFP_RDONLY, tmp + 2);
} else {
hfs_put_ns(0, tmp + 2);
}
p = tmp;
limit = 4;
break;
case HFS_HDR_PRODOSI:
/* XXX: this needs to do mac->prodos translations */
memset(tmp, 0, 8);
#if 0
hfs_put_ns(0, tmp); /* access */
hfs_put_ns(0, tmp); /* type */
hfs_put_nl(0, tmp); /* aux type */
#endif
p = tmp;
limit = 8;
break;
case HFS_HDR_MACI:
hfs_put_ns(0, tmp);
if (entry->type == HFS_CDR_FIL) {
hfs_put_hs(entry->u.file.flags, tmp + 2);
} else {
hfs_put_ns(entry->u.dir.flags, tmp + 2);
}
p = tmp;
limit = 4;
break;
case HFS_HDR_FNAME: /* Can't rename a file this way */
case HFS_HDR_DID: /* can't specify a did this way */
default:
limit = 0;
}
/* limit the transfer to the available data
of to the stated length of the entry. */
if (length > limit) {
length = limit;
}
offset = pos - start;
left = length - offset;
if (left > count) {
left = count;
}
if (left <= 0) {
continue;
}
/* transfer the data from user space */
if (p) {
left -= copy_from_user(p + offset, buf, left);
} else if (fork) {
left = hfs_do_write(inode, fork, offset, buf, left);
}
/* process the data */
switch (descr->id) {
case HFS_HDR_OLDI:
set_dates(entry, inode, (hfs_u32 *)tmp);
if (entry->type == HFS_CDR_FIL) {
hfs_u8 new_flags = entry->u.file.flags;
if (hfs_get_nl(tmp+12) & htonl(HFS_AFP_WRI)) {
new_flags |= HFS_FIL_LOCK;
} else {
new_flags &= ~HFS_FIL_LOCK;
}
if (new_flags != entry->u.file.flags) {
entry->u.file.flags = new_flags;
hfs_cat_mark_dirty(entry);
hfs_file_fix_mode(entry);
}
}
break;
case HFS_HDR_DATES:
set_dates(entry, inode, (hfs_u32 *)tmp);
break;
case HFS_HDR_FINFO:
hfs_cat_mark_dirty(entry);
break;
case HFS_HDR_MACI:
if (entry->type == HFS_CDR_DIR) {
hfs_u16 new_flags = hfs_get_ns(tmp + 2);
if (entry->u.dir.flags != new_flags) {
entry->u.dir.flags = new_flags;
hfs_cat_mark_dirty(entry);
}
} else {
hfs_u8 new_flags = tmp[3];
hfs_u8 changed = entry->u.file.flags^new_flags;
if (changed) {
entry->u.file.flags = new_flags;
hfs_cat_mark_dirty(entry);
if (changed & HFS_FIL_LOCK) {
hfs_file_fix_mode(entry);
}
}
}
break;
case HFS_HDR_DATA:
case HFS_HDR_RSRC:
if (left <= 0) {
if (!written) {
return left;
} else {
goto done;
}
} else if (fork->lsize > descr->length) {
descr->length = fork->lsize;
}
break;
case HFS_HDR_FNAME: /* Can't rename a file this way */
case HFS_HDR_DID: /* Can't specify a did this way */
case HFS_HDR_PRODOSI: /* not implemented yet */
case HFS_HDR_AFPI: /* ditto */
default:
break;
}
count -= left;
written += left;
pos += left;
buf += left;
}
/* Skip any padding at the end */
if (count) {
written += count;
pos += count;
}
done:
*ppos = pos;
if (written > 0) {
if (pos > inode->i_size)
inode->i_size = pos;
inode->i_mtime = inode->i_atime = CURRENT_TIME;
mark_inode_dirty(inode);
}
return written;
}
/*
* hdr_truncate()
*
* This is the truncate field in the inode_operations structure for
* header files. The purpose is to allocate or release blocks as needed
* to satisfy a change in file length.
*/
static void hdr_truncate(struct inode *inode)
{
struct hfs_cat_entry *entry = HFS_I(inode)->entry;
struct hfs_hdr_layout *layout;
size_t size = inode->i_size;
int lcv, last;
if (!HFS_I(inode)->layout) {
HFS_I(inode)->layout = dup_layout(HFS_I(inode)->default_layout);
}
layout = HFS_I(inode)->layout;
last = layout->entries - 1;
for (lcv = 0; lcv <= last; ++lcv) {
struct hfs_hdr_descr *descr = layout->order[lcv];
struct hfs_fork *fork;
hfs_u32 offset;
if (!descr) {
break;
}
if (descr->id == HFS_HDR_RSRC) {
fork = &entry->u.file.rsrc_fork;
#if 0
/* Can't yet truncate the data fork via a header file, since there is the
* possibility to truncate via the data file, and the only locking is at
* the inode level.
*/
} else if (descr->id == HFS_HDR_DATA) {
fork = &entry->u.file.data_fork;
#endif
} else {
continue;
}
offset = descr->offset;
if ((lcv != last) && ((offset + descr->length) <= size)) {
continue;
}
if (offset < size) {
descr->length = size - offset;
} else {
descr->length = 0;
}
if (fork->lsize != descr->length) {
fork->lsize = descr->length;
hfs_extent_adj(fork);
hfs_cat_mark_dirty(entry);
}
}
}
|