]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_open.c
use macros ad_xxmetada when we only want files' metadata.
[netatalk.git] / libatalk / adouble / ad_open.c
1 /*
2  * $Id: ad_open.c,v 1.34 2005-05-14 12:54:55 didg Exp $
3  *
4  * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
5  * Copyright (c) 1990,1991 Regents of The University of Michigan.
6  * All Rights Reserved.
7  *
8  * Permission to use, copy, modify, and distribute this software and
9  * its documentation for any purpose and without fee is hereby granted,
10  * provided that the above copyright notice appears in all copies and
11  * that both that copyright notice and this permission notice appear
12  * in supporting documentation, and that the name of The University
13  * of Michigan not be used in advertising or publicity pertaining to
14  * distribution of the software without specific, written prior
15  * permission. This software is supplied as is without expressed or
16  * implied warranties of any kind.
17  *
18  *      Research Systems Unix Group
19  *      The University of Michigan
20  *      c/o Mike Clark
21  *      535 W. William Street
22  *      Ann Arbor, Michigan
23  *      +1-313-763-0525
24  *      netatalk@itd.umich.edu
25  * 
26  * NOTE: I don't use inline because a good compiler should be
27  * able to optimize all the static below. Didier
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif /* HAVE_CONFIG_H */
33
34 #include <errno.h>
35
36 #include <atalk/adouble.h>
37 #include <sys/param.h>
38 #include <atalk/logger.h>
39
40 #include <atalk/util.h>
41 #include <string.h>
42
43 #include "ad_private.h"
44 #include <stdlib.h>
45
46 #ifndef MAX
47 #define MAX(a, b)  ((a) < (b) ? (b) : (a))
48 #endif /* ! MAX */
49
50 /*
51  * AppleDouble entry default offsets.
52  * The layout looks like this:
53  *
54  * this is the v1 layout:
55  *        255     200             16      32              N
56  *      |  NAME |    COMMENT    | FILEI |    FINDERI    | RFORK |
57  *
58  * we need to change it to look like this:
59  *
60  * v2 layout:
61  * field       length (in bytes)
62  * NAME        255
63  * COMMENT     200
64  * FILEDATESI  16     replaces FILEI
65  * FINDERI     32  
66  * DID          4     new
67  * AFPFILEI     4     new
68  * SHORTNAME   12     8.3 new
69  * RFORK        N
70  * 
71  * so, all we need to do is replace FILEI with FILEDATESI, move RFORK,
72  * and add in the new fields.
73  *
74  * NOTE: the HFS module will need similar modifications to interact with
75  * afpd correctly.
76  */
77  
78 #define ADEDOFF_MAGIC        (0)
79 #define ADEDOFF_VERSION      (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
80 #define ADEDOFF_FILLER       (ADEDOFF_VERSION + ADEDLEN_VERSION)
81 #define ADEDOFF_NENTRIES     (ADEDOFF_FILLER + ADEDLEN_FILLER)
82
83 /* initial lengths of some of the fields */
84 #define ADEDLEN_INIT     0
85
86 /* make sure we don't redefine ADEDOFF_FILEI */
87 #ifdef ADEDOFF_FILEI
88 #undef ADEDOFF_FILEI
89 #endif /* ADEDOFF_FILEI */
90
91 #define ADEDOFF_NAME_V1      (AD_HEADER_LEN + ADEID_NUM_V1*AD_ENTRY_LEN)
92 #define ADEDOFF_COMMENT_V1   (ADEDOFF_NAME_V1 + ADEDLEN_NAME)
93 #define ADEDOFF_FILEI        (ADEDOFF_COMMENT_V1 + ADEDLEN_COMMENT)
94 #define ADEDOFF_FINDERI_V1   (ADEDOFF_FILEI + ADEDLEN_FILEI)
95 #define ADEDOFF_RFORK_V1     (ADEDOFF_FINDERI_V1 + ADEDLEN_FINDERI)
96
97 /* i stick things in a slightly different order than their eid order in 
98  * case i ever want to separate RootInfo behaviour from the rest of the 
99  * stuff. */
100 #define ADEDOFF_NAME_V2      (AD_HEADER_LEN + ADEID_NUM_V2*AD_ENTRY_LEN)
101 #define ADEDOFF_COMMENT_V2   (ADEDOFF_NAME_V2 + ADEDLEN_NAME)
102 #define ADEDOFF_FILEDATESI   (ADEDOFF_COMMENT_V2 + ADEDLEN_COMMENT)
103 #define ADEDOFF_FINDERI_V2   (ADEDOFF_FILEDATESI + ADEDLEN_FILEDATESI)
104 #define ADEDOFF_DID          (ADEDOFF_FINDERI_V2 + ADEDLEN_FINDERI)
105 #define ADEDOFF_AFPFILEI     (ADEDOFF_DID + ADEDLEN_DID)
106 #define ADEDOFF_SHORTNAME    (ADEDOFF_AFPFILEI + ADEDLEN_AFPFILEI)
107 #define ADEDOFF_PRODOSFILEI  (ADEDOFF_SHORTNAME + ADEDLEN_SHORTNAME)
108 #define ADEDOFF_PRIVDEV      (ADEDOFF_PRODOSFILEI + ADEDLEN_PRODOSFILEI)
109 #define ADEDOFF_PRIVINO      (ADEDOFF_PRIVDEV + ADEDLEN_PRIVDEV)
110 #define ADEDOFF_PRIVSYN      (ADEDOFF_PRIVINO + ADEDLEN_PRIVINO)
111 #define ADEDOFF_PRIVID       (ADEDOFF_PRIVSYN + ADEDLEN_PRIVSYN)
112
113 #define ADEDOFF_RFORK_V2     (ADEDOFF_PRIVID + ADEDLEN_PRIVID)
114
115 #define ADEID_NUM_OSX        2
116 #define ADEDOFF_FINDERI_OSX  (AD_HEADER_LEN + ADEID_NUM_OSX*AD_ENTRY_LEN)
117 #define ADEDOFF_RFORK_OSX    (ADEDOFF_FINDERI_OSX + ADEDLEN_FINDERI)
118
119 /* we keep local copies of a bunch of stuff so that we can initialize things 
120  * correctly. */
121
122 /* invisible bit for dot files */
123 #define ATTRBIT_INVISIBLE     (1 << 0)
124
125 /* this is to prevent changing timezones from causing problems with
126    localtime volumes. the screw-up is 30 years. we use a delta of 5
127    years.  */
128 #define TIMEWARP_DELTA 157680000
129
130
131 struct entry {
132   u_int32_t id, offset, len;
133 };
134
135 static const struct entry entry_order1[ADEID_NUM_V1 +1] = {
136   {ADEID_NAME,    ADEDOFF_NAME_V1,    ADEDLEN_INIT},      /* 3 */
137   {ADEID_COMMENT, ADEDOFF_COMMENT_V1, ADEDLEN_INIT},      /* 4 */
138   {ADEID_FILEI,   ADEDOFF_FILEI,      ADEDLEN_FILEI},     /* 7 */
139   {ADEID_FINDERI, ADEDOFF_FINDERI_V1, ADEDLEN_FINDERI},   /* 9 */
140   {ADEID_RFORK,   ADEDOFF_RFORK_V1,   ADEDLEN_INIT},      /* 2 */
141   {0, 0, 0}
142 };
143
144 #if AD_VERSION == AD_VERSION1 
145 #define DISK_EID(ad, a) (a)
146
147 #else /* AD_VERSION == AD_VERSION2 */
148
149 static u_int32_t get_eid(struct adouble *ad, u_int32_t eid) 
150 {
151     if (eid <= 15)
152         return eid;
153     if (ad->ad_version == AD_VERSION1)
154         return 0;
155     if (eid == AD_DEV)
156         return ADEID_PRIVDEV;
157     if (eid == AD_INO)
158         return ADEID_PRIVINO;
159     if (eid == AD_SYN)
160         return ADEID_PRIVSYN;
161     if (eid == AD_ID)
162         return ADEID_PRIVID;
163
164     return 0;
165 }
166
167 #define DISK_EID(ad, a) get_eid(ad, a)
168
169 static const struct entry entry_order2[ADEID_NUM_V2 +1] = {
170   {ADEID_NAME, ADEDOFF_NAME_V2, ADEDLEN_INIT},
171   {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
172   {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
173   {ADEID_FINDERI, ADEDOFF_FINDERI_V2, ADEDLEN_FINDERI},
174   {ADEID_DID, ADEDOFF_DID, ADEDLEN_DID},
175   {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
176   {ADEID_SHORTNAME, ADEDOFF_SHORTNAME, ADEDLEN_INIT},
177   {ADEID_PRODOSFILEI, ADEDOFF_PRODOSFILEI, ADEDLEN_PRODOSFILEI},
178   {ADEID_PRIVDEV,     ADEDOFF_PRIVDEV, ADEDLEN_INIT},
179   {ADEID_PRIVINO,     ADEDOFF_PRIVINO, ADEDLEN_INIT},
180   {ADEID_PRIVSYN,     ADEDOFF_PRIVSYN, ADEDLEN_INIT},
181   {ADEID_PRIVID,     ADEDOFF_PRIVID, ADEDLEN_INIT},
182   {ADEID_RFORK, ADEDOFF_RFORK_V2, ADEDLEN_INIT},
183
184   {0, 0, 0}
185 };
186
187 /* OS X adouble finder info and resource fork only
188 */
189 static const struct entry entry_order_osx[ADEID_NUM_OSX +1] = {
190   {ADEID_FINDERI, ADEDOFF_FINDERI_OSX, ADEDLEN_FINDERI},
191   {ADEID_RFORK, ADEDOFF_RFORK_OSX, ADEDLEN_INIT},
192
193   {0, 0, 0}
194 };
195
196 #endif /* AD_VERSION == AD_VERSION2 */
197
198 #if AD_VERSION == AD_VERSION2
199
200 /* update a version 2 adouble resource fork with our private entries */
201 static int ad_update(struct adouble *ad, const char *path)
202 {
203   struct stat st;
204   u_int16_t nentries = 0;
205   off_t     off, shiftdata=0;
206   const struct entry  *eid;
207   static off_t entry_len[ADEID_MAX];
208   static char  databuf[ADEID_MAX][256], *buf;
209   int fd;
210   int ret = -1;
211
212   /* check to see if we should convert this header. */
213   if (!path || ad->ad_flags != AD_VERSION2)
214     return 0;
215   
216   if (!(ad->ad_hf.adf_flags & O_RDWR)) {
217       /* we were unable to open the file read write the last time */
218       return 0;
219   }
220
221   if (ad->ad_eid[ADEID_RFORK].ade_off) {
222       shiftdata = ADEDOFF_RFORK_V2 -ad->ad_eid[ADEID_RFORK].ade_off;
223   }
224
225   memcpy(&nentries, ad->ad_data + ADEDOFF_NENTRIES, sizeof( nentries ));
226   nentries = ntohs( nentries );
227
228   if ( shiftdata == 0 && nentries == ADEID_NUM_V2)
229     return 0;
230
231   memset(entry_len, 0, sizeof(entry_len));
232   memset(databuf, 0, sizeof(databuf));
233
234   /* bail if we can't get a lock */
235   if (ad_tmplock(ad, ADEID_RFORK, ADLOCK_WR, 0, 0, 0) < 0)
236     goto bail_err;
237
238   fd = ad->ad_hf.adf_fd;
239
240   if (fstat(fd, &st)) {
241     goto bail_lock;
242   }
243
244   if (st.st_size > 0x7fffffff) {
245       LOG(log_debug, logtype_default, "ad_update: file '%s' too big for update.", path);
246       errno = EIO;
247       goto bail_lock;
248   }
249
250   off = ad->ad_eid[ADEID_RFORK].ade_off;
251   if (off > st.st_size) {
252       LOG(log_error, logtype_default, "ad_update: invalid resource fork offset. (off: %u)", off); 
253       errno = EIO;
254       goto bail_lock;
255   }
256
257   if (ad->ad_eid[ADEID_RFORK].ade_len > st.st_size - off) {
258       LOG(log_error, logtype_default, "ad_update: invalid resource fork length. (rfork len: %u)", ad->ad_eid[ADEID_RFORK].ade_len); 
259       errno = EIO;
260       goto bail_lock;
261   }
262   
263   if ((void *) (buf = (char *)
264                 mmap(NULL, st.st_size + shiftdata,
265                      PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
266           MAP_FAILED) {
267     goto bail_lock;
268   }
269
270   /* last place for failure. */
271   if (sys_ftruncate(fd, st.st_size + shiftdata) < 0) {
272     goto bail_lock;
273   }
274
275   /* move the RFORK. this assumes that the RFORK is at the end */
276   if (off) {
277     memmove(buf + ADEDOFF_RFORK_V2, buf + off, ad->ad_eid[ADEID_RFORK].ade_len);
278   }
279
280   munmap(buf, st.st_size + shiftdata);
281
282   /* now, fix up our copy of the header */
283   memset(ad->ad_filler, 0, sizeof(ad->ad_filler));
284  
285   /* save the header entries */ 
286   eid = entry_order2;
287   while (eid->id) {
288     if( ad->ad_eid[eid->id].ade_off != 0) {
289       if ( eid->id > 2 && ad->ad_eid[eid->id].ade_len < 256)
290         memcpy( databuf[eid->id], ad->ad_data +ad->ad_eid[eid->id].ade_off, ad->ad_eid[eid->id].ade_len);
291       entry_len[eid->id] = ad->ad_eid[eid->id].ade_len;
292     }
293     eid++;
294   }
295
296   memset(ad->ad_data + AD_HEADER_LEN, 0, AD_DATASZ - AD_HEADER_LEN);
297
298   /* copy the saved entries to the new header */
299   eid = entry_order2;
300   while (eid->id) {
301     if ( eid->id > 2 && entry_len[eid->id] > 0) {
302       memcpy(ad->ad_data+eid->offset, databuf[eid->id], entry_len[eid->id]);
303     }
304     ad->ad_eid[eid->id].ade_off = eid->offset;
305     ad->ad_eid[eid->id].ade_len = entry_len[eid->id];
306     eid++;
307   }
308
309   /* rebuild the header and cleanup */
310   LOG(log_debug, logtype_default, "updated AD2 header %s", path);
311   ad_flush(ad, ADFLAGS_HF );
312   ret = 0;
313
314 bail_lock:
315   ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0, 0);
316 bail_err:
317   return ret;
318 }
319
320 /* ------------------------------------------
321    FIXME work only if < 2GB 
322 */
323 static int ad_convert(struct adouble *ad, const char *path)
324 {
325   struct stat st;
326   u_int16_t attr;
327   char *buf;
328   int fd, off;
329   int ret = -1;
330   /* use resource fork offset from file */
331   int shiftdata;
332   int toV2;
333   int toV1;
334   
335   if (!path) {
336       return 0;
337   }
338   
339   if (!(ad->ad_hf.adf_flags & ( O_RDWR))) {
340       /* we were unable to open the file read write the last time */
341       return 0;
342   }
343
344   /* check to see if we should convert this header. */
345   toV2 = ad->ad_version == AD_VERSION1 && ad->ad_flags == AD_VERSION2;
346   toV1 = ad->ad_version == AD_VERSION2 && ad->ad_flags == AD_VERSION1;
347
348   if (!toV2 && !toV1)
349       return 0;
350
351   /* convert from v1 to v2. what does this mean?
352    *  1) change FILEI into FILEDATESI
353    *  2) create space for SHORTNAME, AFPFILEI, DID, and PRODOSI
354    *  3) move FILEI attributes into AFPFILEI
355    *  4) initialize ACCESS field of FILEDATESI.
356    *  5) move the resource fork
357    */
358   
359   /* bail if we can't get a lock */
360   if (ad_tmplock(ad, ADEID_RFORK, ADLOCK_WR, 0, 0, 0) < 0) 
361     goto bail_err;
362
363   /* we reuse fd from the resource fork */
364   fd = ad->ad_hf.adf_fd;
365
366   if (ad->ad_eid[ADEID_RFORK].ade_off) {
367       shiftdata = ADEDOFF_RFORK_V2 -ad->ad_eid[ADEID_RFORK].ade_off;
368   }
369   else {
370       shiftdata = ADEDOFF_RFORK_V2 -ADEDOFF_RFORK_V1; /* 136 */
371   }
372
373   if (fstat(fd, &st)) { 
374       goto bail_lock;
375   }
376
377   if (st.st_size > 0x7fffffff -shiftdata) {
378       LOG(log_debug, logtype_default, "ad_v1tov2: file too big."); 
379       errno = EIO;
380       goto bail_lock;
381   }
382   
383   off = ad->ad_eid[ADEID_RFORK].ade_off;
384
385   if (off > st.st_size) {
386       LOG(log_error, logtype_default, "ad_v1tov2: invalid resource fork offset. (off: %u)", off); 
387       errno = EIO;
388       goto bail_lock;
389   }
390
391   if (ad->ad_eid[ADEID_RFORK].ade_len > st.st_size - off) {
392       LOG(log_error, logtype_default, "ad_v1tov2: invalid resource fork length. (rfork len: %u)", ad->ad_eid[ADEID_RFORK].ade_len); 
393       errno = EIO;
394       goto bail_lock;
395   }
396   
397   if ((void *) (buf = (char *) 
398                 mmap(NULL, st.st_size + shiftdata,
399                      PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == 
400           MAP_FAILED) {
401     goto bail_lock;
402   }
403
404   /* last place for failure. */
405
406   if (sys_ftruncate(fd, st.st_size + shiftdata) < 0) {
407       goto bail_lock;
408   }
409   
410   /* move the RFORK. this assumes that the RFORK is at the end */
411   if (off) {
412       memmove(buf + ADEDOFF_RFORK_V2, buf + off, ad->ad_eid[ADEID_RFORK].ade_len);
413   }
414
415   munmap(buf, st.st_size + shiftdata);
416
417   /* now, fix up our copy of the header */
418   memset(ad->ad_filler, 0, sizeof(ad->ad_filler));
419   
420   /* replace FILEI with FILEDATESI */
421   ad_getattr(ad, &attr);
422   ad->ad_eid[ADEID_FILEDATESI].ade_off = ADEDOFF_FILEDATESI;
423   ad->ad_eid[ADEID_FILEDATESI].ade_len = ADEDLEN_FILEDATESI;
424   ad->ad_eid[ADEID_FILEI].ade_off = 0;
425   ad->ad_eid[ADEID_FILEI].ade_len = 0;
426   
427   /* add in the new entries */
428   ad->ad_eid[ADEID_DID].ade_off = ADEDOFF_DID;
429   ad->ad_eid[ADEID_DID].ade_len = ADEDLEN_DID;
430   ad->ad_eid[ADEID_AFPFILEI].ade_off = ADEDOFF_AFPFILEI;
431   ad->ad_eid[ADEID_AFPFILEI].ade_len = ADEDLEN_AFPFILEI;
432   ad->ad_eid[ADEID_SHORTNAME].ade_off = ADEDOFF_SHORTNAME;
433   ad->ad_eid[ADEID_SHORTNAME].ade_len = ADEDLEN_INIT;
434   ad->ad_eid[ADEID_PRODOSFILEI].ade_off = ADEDOFF_PRODOSFILEI;
435   ad->ad_eid[ADEID_PRODOSFILEI].ade_len = ADEDLEN_PRODOSFILEI;
436   
437   ad->ad_eid[ADEID_PRIVDEV].ade_off = ADEDOFF_PRIVDEV;
438   ad->ad_eid[ADEID_PRIVDEV].ade_len = ADEDLEN_INIT;
439   ad->ad_eid[ADEID_PRIVINO].ade_off = ADEDOFF_PRIVINO;
440   ad->ad_eid[ADEID_PRIVINO].ade_len = ADEDLEN_INIT;
441   ad->ad_eid[ADEID_PRIVSYN].ade_off = ADEDOFF_PRIVSYN;
442   ad->ad_eid[ADEID_PRIVSYN].ade_len = ADEDLEN_INIT;
443   ad->ad_eid[ADEID_PRIVID].ade_off  = ADEDOFF_PRIVID;
444   ad->ad_eid[ADEID_PRIVID].ade_len =  ADEDLEN_INIT;
445   
446   /* shift the old entries (NAME, COMMENT, FINDERI, RFORK) */
447   ad->ad_eid[ADEID_NAME].ade_off = ADEDOFF_NAME_V2;
448   ad->ad_eid[ADEID_COMMENT].ade_off = ADEDOFF_COMMENT_V2;
449   ad->ad_eid[ADEID_FINDERI].ade_off = ADEDOFF_FINDERI_V2;
450   ad->ad_eid[ADEID_RFORK].ade_off = ADEDOFF_RFORK_V2;
451   
452   /* switch to dest version */
453   ad->ad_version = (toV2)?AD_VERSION2:AD_VERSION1;
454   
455   /* move our data buffer to make space for the new entries. */
456   memmove(ad->ad_data + ADEDOFF_NAME_V2, ad->ad_data + ADEDOFF_NAME_V1,
457           ADEDOFF_RFORK_V1 - ADEDOFF_NAME_V1);
458   
459   /* now, fill in the space with appropriate stuff. we're
460      operating as a v2 file now. */
461   ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
462   memset(ad_entry(ad, ADEID_DID), 0, ADEDLEN_DID);
463   memset(ad_entry(ad, ADEID_AFPFILEI), 0, ADEDLEN_AFPFILEI);
464   ad_setattr(ad, attr);
465   memset(ad_entry(ad, ADEID_SHORTNAME), 0, ADEDLEN_SHORTNAME);
466   memset(ad_entry(ad, ADEID_PRODOSFILEI), 0, ADEDLEN_PRODOSFILEI);
467   
468   /* rebuild the header and cleanup */
469   ad_flush(ad, ADFLAGS_HF );
470   ret = 0;
471   
472 bail_lock:
473   ad_tmplock(ad, ADEID_RFORK, ADLOCK_CLR, 0, 0, 0);
474 bail_err:
475   return ret;
476 }
477 #endif /* AD_VERSION == AD_VERSION2 */
478
479 /* --------------------------- */
480 #ifdef ATACC
481 mode_t ad_hf_mode (mode_t mode)
482 {
483     /* we always need RW mode for file owner */
484 #if 0
485     mode |= S_IRUSR;
486 #endif    
487     /* fnctl lock need write access */
488     if ((mode & S_IRUSR))
489         mode |= S_IWUSR;
490     if ((mode & S_IRGRP))
491         mode |= S_IWGRP;
492     if ((mode & S_IROTH))
493         mode |= S_IWOTH;
494     /* if write mode set add read mode */
495     if ((mode & S_IWUSR))
496         mode |= S_IRUSR;
497     if ((mode & S_IWGRP))
498         mode |= S_IRGRP;
499     if ((mode & S_IWOTH))
500         mode |= S_IROTH;
501
502     return mode;
503 }
504
505 #endif
506
507 /* ------------------------------------- 
508   read in the entries 
509 */
510 static void parse_entries(struct adouble *ad, char *buf,
511                                     u_int16_t nentries)
512 {
513     u_int32_t   eid, len, off;
514     int         warning = 0;
515
516     /* now, read in the entry bits */
517     for (; nentries > 0; nentries-- ) {
518         memcpy(&eid, buf, sizeof( eid ));
519         eid = DISK_EID(ad, ntohl( eid ));
520         buf += sizeof( eid );
521         memcpy(&off, buf, sizeof( off ));
522         off = ntohl( off );
523         buf += sizeof( off );
524         memcpy(&len, buf, sizeof( len ));
525         len = ntohl( len );
526         buf += sizeof( len );
527
528         if (eid && eid < ADEID_MAX && off < sizeof(ad->ad_data) && 
529                  (off +len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
530             ad->ad_eid[ eid ].ade_off = off;
531             ad->ad_eid[ eid ].ade_len = len;
532         } else if (!warning) {
533             warning = 1;
534             LOG(log_debug, logtype_default, "ad_refresh: nentries %hd  eid %d",
535                     nentries, eid );
536         }
537     }
538 }
539
540
541 /* this reads enough of the header so that we can figure out all of
542  * the entry lengths and offsets. once that's done, we just read/mmap
543  * the rest of the header in.
544  *
545  * NOTE: we're assuming that the resource fork is kept at the end of
546  *       the file. also, mmapping won't work for the hfs fs until it
547  *       understands how to mmap header files. */
548 static int ad_header_read(struct adouble *ad, struct stat *hst)
549 {
550     char                *buf = ad->ad_data;
551     u_int16_t           nentries;
552     int                 len;
553     ssize_t             header_len;
554     static int          warning = 0;
555     struct stat         st;
556
557     /* read the header */
558     if ((header_len = adf_pread( &ad->ad_hf, buf, sizeof(ad->ad_data), 0)) < 0) {
559         return -1;
560     }
561     if (header_len < AD_HEADER_LEN) {
562         errno = EIO;
563         return -1;
564     }
565
566     memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
567     memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
568
569     /* tag broken v1 headers. just assume they're all right. 
570      * we detect two cases: null magic/version
571      *                      byte swapped magic/version
572      * XXX: in the future, you'll need the v1compat flag. 
573      * (ad->ad_flags & ADFLAGS_V1COMPAT) */
574     if (!ad->ad_magic && !ad->ad_version) {
575       if (!warning) {
576         LOG(log_debug, logtype_default, "notice: fixing up null v1 magic/version.");
577         warning++;
578       }
579       ad->ad_magic = AD_MAGIC;
580       ad->ad_version = AD_VERSION1;
581
582     } else if (ad->ad_magic == AD_MAGIC && ad->ad_version == AD_VERSION1) {
583       if (!warning) {
584         LOG(log_debug, logtype_default, "notice: fixing up byte-swapped v1 magic/version.");
585         warning++;
586       }
587
588     } else {
589       ad->ad_magic = ntohl( ad->ad_magic );
590       ad->ad_version = ntohl( ad->ad_version );
591     }
592
593     if ((ad->ad_magic != AD_MAGIC) || ((ad->ad_version != AD_VERSION1)
594 #if AD_VERSION == AD_VERSION2
595                                        && (ad->ad_version != AD_VERSION2)
596 #endif /* AD_VERSION == AD_VERSION2 */
597                                        )) {
598       errno = EIO;
599       LOG(log_debug, logtype_default, "ad_open: can't parse AppleDouble header.");
600       return -1;
601     }
602
603     memcpy(ad->ad_filler, buf + ADEDOFF_FILLER, sizeof( ad->ad_filler ));
604     memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
605     nentries = ntohs( nentries );
606
607     /* read in all the entry headers. if we have more than the 
608      * maximum, just hope that the rfork is specified early on. */
609     len = nentries*AD_ENTRY_LEN;
610
611     if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
612       len = sizeof(ad->ad_data) - AD_HEADER_LEN;
613
614     buf += AD_HEADER_LEN;
615     if (len > header_len - AD_HEADER_LEN) {
616         errno = EIO;
617         LOG(log_debug, logtype_default, "ad_header_read: can't read entry info.");
618         return -1;
619     }
620
621     /* figure out all of the entry offsets and lengths. if we aren't
622      * able to read a resource fork entry, bail. */
623     nentries = len / AD_ENTRY_LEN;
624     parse_entries(ad, buf, nentries);
625     if (!ad_getentryoff(ad, ADEID_RFORK)
626         || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
627         ) {
628         errno = EIO;
629         LOG(log_debug, logtype_default, "ad_header_read: problem with rfork entry offset."); 
630         return -1;
631     }
632
633     if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
634         errno = EIO;
635         LOG(log_debug, logtype_default, "ad_header_read: can't read in entries.");
636         return -1;
637     }
638     
639     if (hst == NULL) {
640         hst = &st;
641         if (fstat(ad->ad_hf.adf_fd, &st) < 0) {
642             return 1; /* fail silently */
643          }
644     }
645     ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
646
647     /* fix up broken dates */
648     if (ad->ad_version == AD_VERSION1) {
649       u_int32_t aint;
650       
651       /* check to see if the ad date is wrong. just see if we have
652       * a modification date in the future. */
653       if (((ad_getdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, &aint)) == 0) &&
654           (aint > TIMEWARP_DELTA + hst->st_mtime)) {
655         ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, aint - AD_DATE_DELTA);
656         ad_getdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, &aint);
657         ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, aint - AD_DATE_DELTA);
658         ad_getdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, &aint);
659         ad_setdate(ad, AD_DATE_BACKUP | AD_DATE_UNIX, aint - AD_DATE_DELTA);
660       }
661     }
662
663     return 0;
664 }
665
666 /* ---------------------------------------
667  * Put the .AppleDouble where it needs to be:
668  *
669  *          /   a/.AppleDouble/b
670  *      a/b
671  *          \   b/.AppleDouble/.Parent
672  *
673  * FIXME: should do something for pathname > MAXPATHLEN
674  */
675 char *
676 ad_path( path, adflags )
677     const char  *path;
678     int         adflags;
679 {
680     static char pathbuf[ MAXPATHLEN + 1];
681     char        c, *slash, buf[MAXPATHLEN + 1];
682     size_t      l;
683
684     l = strlcpy(buf, path, MAXPATHLEN +1);
685     if ( adflags & ADFLAGS_DIR ) {
686         strcpy( pathbuf, buf);
687         if ( *buf != '\0' && l < MAXPATHLEN) {
688             pathbuf[l++] = '/';
689             pathbuf[l] = 0;
690         }
691         slash = ".Parent";
692     } else {
693         if (NULL != ( slash = strrchr( buf, '/' )) ) {
694             c = *++slash;
695             *slash = '\0';
696             strcpy( pathbuf, buf);
697             *slash = c;
698         } else {
699             pathbuf[ 0 ] = '\0';
700             slash = buf;
701         }
702     }
703     strlcat( pathbuf, ".AppleDouble/", MAXPATHLEN +1);
704     strlcat( pathbuf, slash, MAXPATHLEN +1);
705
706     return( pathbuf );
707 }
708
709 /* -------------------- */
710 static int ad_mkrf(char *path)
711 {
712     char *slash;
713     /*
714      * Probably .AppleDouble doesn't exist, try to mkdir it.
715      */
716      if (NULL == ( slash = strrchr( path, '/' )) ) {
717          return -1;
718      }
719      *slash = '\0';
720      errno = 0;
721      if ( ad_mkdir( path, 0777 ) < 0 ) {
722           return -1;
723      }
724      *slash = '/';
725      return 0;
726 }
727
728 /* ---------------------------------------
729  * Put the resource fork where it needs to be:
730  * ._name
731  */
732 char *
733 ad_path_osx(const char *path, int adflags _U_)
734 {
735     static char pathbuf[ MAXPATHLEN + 1];
736     char        c, *slash, buf[MAXPATHLEN + 1];
737     
738     if (!strcmp(path,".")) {
739             /* fixme */
740         getcwd(buf, MAXPATHLEN);
741     }
742     else {
743         strlcpy(buf, path, MAXPATHLEN +1);
744     }
745     if (NULL != ( slash = strrchr( buf, '/' )) ) {
746         c = *++slash;
747         *slash = '\0';
748         strlcpy( pathbuf, buf, MAXPATHLEN +1);
749         *slash = c;
750     } else {
751         pathbuf[ 0 ] = '\0';
752         slash = buf;
753     }
754     strlcat( pathbuf, "._", MAXPATHLEN  +1);  
755     strlcat( pathbuf, slash, MAXPATHLEN +1);
756     return pathbuf;
757 }
758 /* -------------------- */
759 static int ad_mkrf_osx(char *path _U_)
760 {
761     return 0;
762 }
763
764 /* ---------------------------------------
765  * Put the .AppleDouble where it needs to be:
766  *
767  *          /   a/.AppleDouble/b/Afp_AfpInfo
768  *      a/b     
769  *          \   b/.AppleDouble/.Parent/Afp_AfpInfo
770  *
771  */
772 char *
773 ad_path_ads( path, adflags )
774     const char  *path;
775     int         adflags;
776 {
777     static char pathbuf[ MAXPATHLEN + 1];
778     char        c, *slash, buf[MAXPATHLEN + 1];
779     size_t      l;
780
781     l = strlcpy(buf, path, MAXPATHLEN +1);
782     if ( adflags & ADFLAGS_DIR ) {
783         strcpy( pathbuf, buf);
784         if ( *buf != '\0' && l < MAXPATHLEN) {
785             pathbuf[l++] = '/';
786             pathbuf[l] = 0;
787         }
788         slash = ".Parent";
789     } else {
790         if (NULL != ( slash = strrchr( buf, '/' )) ) {
791             c = *++slash;
792             *slash = '\0';
793             strcpy( pathbuf, buf);
794             *slash = c;
795         } else {
796             pathbuf[ 0 ] = '\0';
797             slash = buf;
798         }
799     }
800     strlcat( pathbuf, ".AppleDouble/", MAXPATHLEN +1);
801     strlcat( pathbuf, slash, MAXPATHLEN +1);
802
803     strlcat( pathbuf, "/Afp_AfpInfo", MAXPATHLEN +1);
804
805 #if 0
806     if ((adflags & ADFLAGS_HF)) {
807         strlcat( pathbuf, "Afp_AfpInfo", MAXPATHLEN +1);
808     else {
809         strlcat( pathbuf, "Afp_Resource", MAXPATHLEN +1);
810     }
811 #endif      
812     return( pathbuf );
813 }
814
815 /* -------------------- */
816 static int ad_mkrf_ads(char *path)
817 {
818     char *slash;
819     /*
820      * Probably .AppleDouble doesn't exist, try to mkdir it.
821      */
822      if (NULL == ( slash = strrchr( path, '/' )) ) {
823          return -1;
824      }
825      *slash = 0;
826      errno = 0;
827      if ( ad_mkdir( path, 0777 ) < 0 ) {
828          if ( errno == ENOENT ) {
829              char *slash1;
830              
831              if (NULL == ( slash1 = strrchr( path, '/' )) ) 
832                  return -1;
833              errno = 0;
834              *slash1 = 0;
835              if ( ad_mkdir( path, 0777 ) < 0 ) 
836                   return -1;
837              *slash1 = '/';
838              if ( ad_mkdir( path, 0777 ) < 0 )
839                  return -1;
840          }
841          else
842             return -1;
843      }     
844      *slash = '/';
845      return 0;
846 }
847
848 /* -------------------------
849  * Support inherited protection modes for AppleDouble files.  The supplied
850  * mode is ANDed with the parent directory's mask value in lieu of "umask",
851  * and that value is returned.
852  */
853
854 #define DEFMASK 07700   /* be conservative */
855
856 char 
857 *ad_dir(path)
858     const char          *path;
859 {
860     static char         modebuf[ MAXPATHLEN + 1];
861     char                *slash;
862     size_t              len;
863
864     if ( (len = strlen( path )) >= MAXPATHLEN ) {
865         errno = ENAMETOOLONG;
866         return NULL;  /* can't do it */
867     }
868
869     /*
870      * For a path with directories in it, remove the final component
871      * (path or subdirectory name) to get the name we want to stat.
872      * For a path which is just a filename, use "." instead.
873      */
874     strcpy( modebuf, path );
875     slash = strrchr( modebuf, '/' );
876     /* is last char a '/' */
877     if (slash && slash[1] == 0) {
878         while (modebuf < slash && slash[-1] == '/') {
879             --slash;
880         }
881         if (modebuf < slash) {
882             *slash = '\0';              /* remove pathname component */
883             slash = strrchr( modebuf, '/' );
884         }
885     }
886     if (slash) {
887         *slash = '\0';          /* remove pathname component */
888     } else {
889         modebuf[0] = '.';       /* use current directory */
890         modebuf[1] = '\0';
891     }
892     return modebuf;
893 }
894
895 /* ---------------- */
896 static uid_t default_uid = -1;
897
898 int ad_setfuid(const uid_t id)
899 {
900     default_uid = id;
901     return 0;
902 }
903
904 /* ---------------- */
905 uid_t ad_getfuid(void) 
906 {
907     return default_uid;
908 }
909
910 /* ---------------- 
911    return inode of path parent directory
912 */
913 int ad_stat(const char *path, struct stat *stbuf)
914 {
915     char                *p;
916
917     p = ad_dir(path);
918     if (!p) {
919         return -1;
920     }
921
922     return stat( p, stbuf );
923 }
924
925 /* ---------------- 
926    if we are root change path user/ group
927    It can be a native function for BSD cf. FAQ.Q10
928    path:  pathname to chown 
929    stbuf: parent directory inode
930    
931    use fstat and fchown or lchown with linux?
932 */
933 #define EMULATE_SUIDDIR
934  
935 static int ad_chown(const char *path, struct stat *stbuf)
936 {
937 int ret = 0;
938 #ifdef EMULATE_SUIDDIR
939 uid_t id;
940
941     if (default_uid != (uid_t)-1) {  
942         /* we are root (admin) */
943         id = (default_uid)?default_uid:stbuf->st_uid;
944         ret = chown( path, id, stbuf->st_gid );
945     }
946 #endif    
947     return ret;
948 }
949
950 /* ---------------- 
951    return access right and inode of path parent directory
952 */
953 static int ad_mode_st(const char *path, int *mode, struct stat *stbuf)
954 {
955     if (*mode == 0) {
956        return -1;
957     }
958     if (ad_stat(path, stbuf) != 0) {
959         *mode &= DEFMASK;
960         return -1;
961     }
962     *mode &= stbuf->st_mode;
963     return 0;    
964 }
965
966 /* ---------------- 
967    return access right of path parent directory
968 */
969 int
970 ad_mode( path, mode )
971     const char          *path;
972     int                 mode;
973 {
974     struct stat         stbuf;
975     ad_mode_st(path, &mode, &stbuf);
976     return mode;
977 }
978
979 /*
980  * Use mkdir() with mode bits taken from ad_mode().
981  */
982 int
983 ad_mkdir( path, mode )
984     const char          *path;
985     int                 mode;
986 {
987 int ret;
988 int st_invalid;
989 struct stat stbuf;
990
991 #ifdef DEBUG
992     LOG(log_info, logtype_default, "ad_mkdir: Creating directory with mode %d", mode);
993 #endif /* DEBUG */
994
995     st_invalid = ad_mode_st(path, &mode, &stbuf);
996     ret = mkdir( path, mode );
997     if (ret || st_invalid)
998         return ret;
999     ad_chown(path, &stbuf);
1000
1001     return ret;    
1002 }
1003
1004 /* ----------------- */
1005 static int ad_error(struct adouble *ad, int adflags)
1006 {
1007 int err = errno;
1008     if ((adflags & ADFLAGS_NOHF)) {
1009         /* FIXME double check : set header offset ?*/
1010         return 0;
1011     }
1012     if ((adflags & ADFLAGS_DF)) {
1013         ad_close( ad, ADFLAGS_DF );
1014         err = errno;
1015     }
1016     return -1 ;
1017 }
1018
1019 static int new_rfork(const char *path, struct adouble *ad, int adflags);
1020
1021 #ifdef  HAVE_PREAD
1022 #define AD_SET(a) 
1023 #else 
1024 #define AD_SET(a) a = 0
1025 #endif
1026
1027 void ad_init(struct adouble *ad, int flags, int options)
1028 {
1029     memset( ad, 0, sizeof( struct adouble ) );
1030     ad->ad_flags = flags;
1031     if (flags == AD_VERSION2_OSX) {
1032         ad->ad_path = ad_path_osx;
1033         ad->ad_mkrf = ad_mkrf_osx;
1034     }
1035     else if (flags == AD_VERSION1_ADS) {
1036         ad->ad_path = ad_path_ads;
1037         ad->ad_mkrf = ad_mkrf_ads;
1038     }
1039     else {
1040         ad->ad_path = ad_path;
1041         ad->ad_mkrf = ad_mkrf;
1042     }
1043     ad->ad_options = options;
1044 }
1045
1046 /* -------------------
1047  * It's not possible to open the header file O_RDONLY -- the read
1048  * will fail and return an error. this refcounts things now. 
1049  */
1050 int ad_open( path, adflags, oflags, mode, ad )
1051     const char          *path;
1052     int                 adflags, oflags, mode;
1053     struct adouble      *ad;
1054 {
1055     struct stat         st;
1056     char                *ad_p;
1057     int                 hoflags, admode;
1058     int                 st_invalid;
1059     int                 open_df = 0;
1060     
1061     if (ad->ad_inited != AD_INITED) {
1062         ad_dfileno(ad) = -1;
1063         ad_hfileno(ad) = -1;
1064         adf_lock_init(&ad->ad_df);
1065         adf_lock_init(&ad->ad_hf);
1066         ad->ad_inited = AD_INITED;
1067         ad->ad_refcount = 1;
1068     }
1069
1070     if ((adflags & ADFLAGS_DF)) { 
1071         if (ad_dfileno(ad) == -1) {
1072           hoflags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
1073           admode = mode;
1074           st_invalid = ad_mode_st(path, &admode, &st);
1075           ad->ad_df.adf_fd =open( path, hoflags, admode );
1076           if (ad->ad_df.adf_fd < 0 ) {
1077              if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
1078                 hoflags = oflags;
1079                 ad->ad_df.adf_fd =open( path, hoflags, admode );
1080              }
1081           }
1082           if ( ad->ad_df.adf_fd < 0)
1083                 return -1;
1084
1085           AD_SET(ad->ad_df.adf_off);
1086           ad->ad_df.adf_flags = hoflags;
1087           if ((oflags & O_CREAT) && !st_invalid) {
1088               /* just created, set owner if admin (root) */
1089               ad_chown(path, &st);
1090           }
1091         } 
1092         else {
1093             /* the file is already open... but */
1094             if ((oflags & ( O_RDWR | O_WRONLY)) &&             /* we want write access */
1095                 !(ad->ad_df.adf_flags & ( O_RDWR | O_WRONLY))) /* and it was denied the first time */
1096             {
1097                  errno = EACCES;
1098                  return -1;
1099             }
1100             /* FIXME 
1101              * for now ad_open is never called with O_TRUNC or O_EXCL if the file is
1102              * already open. Should we check for it? ie
1103              * O_EXCL --> error 
1104              * O_TRUNC --> truncate the fork.
1105              * idem for ressource fork.
1106              */
1107         }
1108         open_df = ADFLAGS_DF;
1109         ad->ad_df.adf_refcount++;
1110     }
1111
1112     if (!(adflags & ADFLAGS_HF)) 
1113         return 0;
1114         
1115     if (ad_hfileno(ad) != -1) { /* the file is already open */
1116         if ((oflags & ( O_RDWR | O_WRONLY)) &&             
1117                 !(ad->ad_hf.adf_flags & ( O_RDWR | O_WRONLY))) {
1118             if (open_df) {
1119                 /* don't call with ADFLAGS_HF because we didn't open ressource fork */
1120                 ad_close( ad, open_df );
1121             }
1122             errno = EACCES;
1123             return -1;
1124         }
1125         ad_refresh(ad);
1126         ad->ad_hf.adf_refcount++;
1127         return 0;
1128     }
1129
1130     ad_p = ad->ad_path( path, adflags );
1131
1132     hoflags = oflags & ~O_CREAT;
1133     if (!(adflags & ADFLAGS_RDONLY)) {
1134         hoflags = (hoflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
1135     }
1136     ad->ad_hf.adf_fd = open( ad_p, hoflags, 0 );
1137     if (ad->ad_hf.adf_fd < 0 ) {
1138         if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
1139             hoflags = oflags & ~O_CREAT;
1140             ad->ad_hf.adf_fd = open( ad_p, hoflags, 0 );
1141         }    
1142     }
1143
1144     if ( ad->ad_hf.adf_fd < 0 ) { 
1145         if (errno == ENOENT && (oflags & O_CREAT) ) {
1146             /*
1147              * We're expecting to create a new adouble header file,
1148              * here.
1149              * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
1150              */
1151             admode = mode;
1152             errno = 0;
1153             st_invalid = ad_mode_st(ad_p, &admode, &st);
1154             admode = ad_hf_mode(admode); 
1155             if ( errno == ENOENT && !(adflags & ADFLAGS_NOADOUBLE) && ad->ad_flags != AD_VERSION2_OSX) {
1156                 if (ad->ad_mkrf( ad_p) < 0) {
1157                     return ad_error(ad, adflags);
1158                 }
1159                 admode = mode;
1160                 st_invalid = ad_mode_st(ad_p, &admode, &st);
1161                 admode = ad_hf_mode(admode); 
1162             }
1163             /* retry with O_CREAT */
1164             ad->ad_hf.adf_fd = open( ad_p, oflags,admode );
1165             if ( ad->ad_hf.adf_fd < 0 ) {
1166                 return ad_error(ad, adflags);
1167             }
1168             ad->ad_hf.adf_flags = oflags;
1169             /* just created, set owner if admin owner (root) */
1170             if (!st_invalid) {
1171                 ad_chown(ad_p, &st);
1172             }
1173         }
1174         else {
1175             return ad_error(ad, adflags);
1176         }
1177     } else if (fstat(ad->ad_hf.adf_fd, &st) == 0 && st.st_size == 0) {
1178         /* for 0 length files, treat them as new. */
1179         ad->ad_hf.adf_flags = hoflags| O_TRUNC;
1180     } else {
1181         ad->ad_hf.adf_flags = hoflags;
1182     }
1183     AD_SET(ad->ad_hf.adf_off);
1184           
1185     memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
1186     ad->ad_hf.adf_refcount++;
1187     if ((ad->ad_hf.adf_flags & ( O_TRUNC | O_CREAT ))) {
1188         /*
1189          * This is a new adouble header file. Initialize the structure,
1190          * instead of reading it.
1191         */
1192         if (new_rfork(path, ad, adflags) < 0) {
1193             int err = errno;
1194             /* the file is already deleted, perm, whatever, so return an error*/
1195             ad_close(ad, adflags);
1196             errno = err;
1197             return -1;
1198         }
1199     } else {
1200             /* Read the adouble header in and parse it.*/
1201         if ((ad_header_read( ad , &st) < 0)
1202 #if AD_VERSION == AD_VERSION2
1203                 || (ad_convert(ad, ad_p) < 0) || (ad_update(ad, ad_p) < 0)
1204 #endif /* AD_VERSION == AD_VERSION2 */
1205         ) {
1206             int err = errno;
1207             
1208             ad_close( ad, adflags );
1209             errno = err;
1210             return -1;
1211         }
1212     }
1213     return 0 ;
1214 }
1215
1216 /* ----------------------------------- 
1217  * return only metadata but try very hard
1218 */
1219 int ad_metadata(const char *name, int flags, struct adouble *adp)
1220 {
1221     uid_t uid;
1222     int   ret, err;
1223
1224     if ((ret = ad_open(name, ADFLAGS_HF | flags, O_RDONLY, 0, adp)) < 0 && errno == EACCES) {
1225         uid = geteuid();
1226         if (seteuid(0)) {
1227             LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1228             errno = EACCES;
1229             return -1;
1230         }
1231         /* we are root open read only */
1232         ret = ad_open(name, ADFLAGS_HF|ADFLAGS_RDONLY|flags, O_RDONLY, 0, adp);
1233         err = errno;
1234         if ( seteuid(uid) < 0) {
1235             LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1236             exit(EXITERR_SYS);
1237         }
1238         errno = err;
1239     }
1240     return ret;
1241 }
1242
1243 /* ----------------------------------- */
1244 static int new_rfork(const char *path, struct adouble *ad, int adflags)
1245 {
1246     const struct entry  *eid;
1247     u_int16_t           ashort;
1248     struct stat         st;
1249
1250     ad->ad_magic = AD_MAGIC;
1251     ad->ad_version = ad->ad_flags & 0x0f0000;
1252     if (!ad->ad_version) {
1253         ad->ad_version = AD_VERSION;
1254     }
1255
1256     memset(ad->ad_filler, 0, sizeof( ad->ad_filler ));
1257     memset(ad->ad_data, 0, sizeof(ad->ad_data));
1258
1259 #if AD_VERSION == AD_VERSION2
1260     if (ad->ad_flags == AD_VERSION2)
1261        eid = entry_order2;
1262     else if (ad->ad_flags == AD_VERSION2_OSX)
1263        eid = entry_order_osx;
1264     else
1265 #endif
1266        eid = entry_order1;
1267
1268     while (eid->id) {
1269         ad->ad_eid[eid->id].ade_off = eid->offset;
1270         ad->ad_eid[eid->id].ade_len = eid->len;
1271         eid++;
1272     }
1273             
1274     /* put something sane in the directory finderinfo */
1275     if ((adflags & ADFLAGS_DIR)) {
1276         /* set default view */
1277         ashort = htons(FINDERINFO_CLOSEDVIEW);
1278         memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF, 
1279                      &ashort, sizeof(ashort));
1280     } else {
1281         /* set default creator/type fields */
1282         memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
1283         memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
1284     }
1285
1286     /* make things invisible */
1287     if ((*path == '.') && strcmp(path, ".") && strcmp(path, "..")) {
1288         ashort = htons(ATTRBIT_INVISIBLE);
1289         ad_setattr(ad, ashort);
1290         ashort = htons(FINDERINFO_INVISIBLE);
1291         memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF,
1292                      &ashort, sizeof(ashort));
1293     }
1294
1295     if (stat(path, &st) < 0) {
1296         return -1;
1297     }
1298             
1299     /* put something sane in the date fields */
1300     ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
1301     ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
1302     ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
1303     ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
1304     return 0;
1305 }
1306
1307 /* to do this with mmap, we need the hfs fs to understand how to mmap
1308    header files. */
1309 int ad_refresh(struct adouble *ad)
1310 {
1311
1312   if (ad->ad_hf.adf_fd < 0)
1313     return -1;
1314
1315   return ad_header_read(ad, NULL);
1316 }