2 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
3 * Copyright (c) 1990,1991 Regents of The University of Michigan.
4 * Copyright (c) 2010 Frank Lahm
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.
18 * Research Systems Unix Group
19 * The University of Michigan
21 * 535 W. William Street
24 * netatalk@itd.umich.edu
30 * Part of Netatalk's AppleDouble implementatation
31 * @note We don't use inlines because a good compiler should be
32 * able to optimize all the static funcs below.
33 * @sa include/atalk/adouble.h
38 #endif /* HAVE_CONFIG_H */
41 #include <sys/param.h>
45 #include <atalk/logger.h>
46 #include <atalk/adouble.h>
47 #include <atalk/util.h>
50 #include "ad_private.h"
53 #define MAX(a, b) ((a) < (b) ? (b) : (a))
59 # define AD_SET(a) a = 0
63 * AppleDouble entry default offsets.
64 * The layout looks like this:
66 * this is the v1 layout:
68 * | NAME | COMMENT | FILEI | FINDERI | RFORK |
70 * we need to change it to look like this:
73 * field length (in bytes)
76 * FILEDATESI 16 replaces FILEI
80 * SHORTNAME 12 8.3 new
83 * so, all we need to do is replace FILEI with FILEDATESI, move RFORK,
84 * and add in the new fields.
87 #define ADEDOFF_MAGIC (0)
88 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
89 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
90 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
92 /* initial lengths of some of the fields */
93 #define ADEDLEN_INIT 0
95 /* make sure we don't redefine ADEDOFF_FILEI */
98 #endif /* ADEDOFF_FILEI */
100 /* i stick things in a slightly different order than their eid order in
101 * case i ever want to separate RootInfo behaviour from the rest of the
105 #define ADEDOFF_NAME_V2 (AD_HEADER_LEN + ADEID_NUM_V2*AD_ENTRY_LEN)
106 #define ADEDOFF_COMMENT_V2 (ADEDOFF_NAME_V2 + ADEDLEN_NAME)
107 #define ADEDOFF_FILEDATESI (ADEDOFF_COMMENT_V2 + ADEDLEN_COMMENT)
108 #define ADEDOFF_FINDERI_V2 (ADEDOFF_FILEDATESI + ADEDLEN_FILEDATESI)
109 #define ADEDOFF_DID (ADEDOFF_FINDERI_V2 + ADEDLEN_FINDERI)
110 #define ADEDOFF_AFPFILEI (ADEDOFF_DID + ADEDLEN_DID)
111 #define ADEDOFF_SHORTNAME (ADEDOFF_AFPFILEI + ADEDLEN_AFPFILEI)
112 #define ADEDOFF_PRODOSFILEI (ADEDOFF_SHORTNAME + ADEDLEN_SHORTNAME)
113 #define ADEDOFF_PRIVDEV (ADEDOFF_PRODOSFILEI + ADEDLEN_PRODOSFILEI)
114 #define ADEDOFF_PRIVINO (ADEDOFF_PRIVDEV + ADEDLEN_PRIVDEV)
115 #define ADEDOFF_PRIVSYN (ADEDOFF_PRIVINO + ADEDLEN_PRIVINO)
116 #define ADEDOFF_PRIVID (ADEDOFF_PRIVSYN + ADEDLEN_PRIVSYN)
117 #define ADEDOFF_RFORK_V2 (ADEDOFF_PRIVID + ADEDLEN_PRIVID)
120 #define ADEDOFF_FINDERI_EA (AD_HEADER_LEN + ADEID_NUM_EA * AD_ENTRY_LEN)
121 #define ADEDOFF_COMMENT_EA (ADEDOFF_FINDERI_EA + ADEDLEN_FINDERI)
122 #define ADEDOFF_FILEDATESI_EA (ADEDOFF_COMMENT_EA + ADEDLEN_COMMENT)
123 #define ADEDOFF_AFPFILEI_EA (ADEDOFF_FILEDATESI_EA + ADEDLEN_FILEDATESI)
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 years */
127 #define TIMEWARP_DELTA 157680000
130 uint32_t id, offset, len;
133 /* --------------------------- */
134 static uid_t default_uid = -1;
136 /* Forward declarations */
137 static int ad_mkrf(char *path);
138 static int ad_header_read(struct adouble *ad, struct stat *hst);
139 static int ad_header_upgrade(struct adouble *ad, char *name);
141 static int ad_mkrf_ea(char *path);
142 static int ad_header_read_ea(struct adouble *ad, struct stat *hst);
143 static int ad_header_upgrade_ea(struct adouble *ad, char *name);
145 static struct adouble_fops ad_adouble = {
148 &ad_rebuild_adouble_header,
153 static struct adouble_fops ad_adouble_ea = {
156 &ad_rebuild_adouble_header,
158 &ad_header_upgrade_ea,
161 static const struct entry entry_order2[ADEID_NUM_V2 + 1] = {
162 {ADEID_NAME, ADEDOFF_NAME_V2, ADEDLEN_INIT},
163 {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
164 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
165 {ADEID_FINDERI, ADEDOFF_FINDERI_V2, ADEDLEN_FINDERI},
166 {ADEID_DID, ADEDOFF_DID, ADEDLEN_DID},
167 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
168 {ADEID_SHORTNAME, ADEDOFF_SHORTNAME, ADEDLEN_INIT},
169 {ADEID_PRODOSFILEI, ADEDOFF_PRODOSFILEI, ADEDLEN_PRODOSFILEI},
170 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV, ADEDLEN_INIT},
171 {ADEID_PRIVINO, ADEDOFF_PRIVINO, ADEDLEN_INIT},
172 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN, ADEDLEN_INIT},
173 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
174 {ADEID_RFORK, ADEDOFF_RFORK_V2, ADEDLEN_INIT},
178 /* Using Extended Attributes */
179 static const struct entry entry_order_ea[ADEID_NUM_EA + 1] = {
180 {ADEID_FINDERI, ADEDOFF_FINDERI_EA, ADEDLEN_FINDERI},
181 {ADEID_COMMENT, ADEDOFF_COMMENT_EA, ADEDLEN_INIT},
182 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_EA, ADEDLEN_FILEDATESI},
183 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_EA, ADEDLEN_AFPFILEI},
187 uint32_t adv2_disk_eid[] = {
188 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
189 AD_DEV, AD_INO, AD_SYN, AD_ID
192 /* ----------------------------------- */
193 static int new_ad_header(const char *path, struct adouble *ad, int adflags)
195 const struct entry *eid;
199 ad->ad_magic = AD_MAGIC;
200 ad->ad_version = ad->ad_flags & 0x0f0000;
201 if (!ad->ad_version) {
202 ad->ad_version = AD_VERSION;
206 memset(ad->ad_data, 0, sizeof(ad->ad_data));
208 if (ad->ad_flags == AD_VERSION2)
210 else if (ad->ad_flags == AD_VERSION_EA)
211 eid = entry_order_ea;
217 ad->ad_eid[eid->id].ade_off = eid->offset;
218 ad->ad_eid[eid->id].ade_len = eid->len;
222 /* put something sane in the directory finderinfo */
223 if ((adflags & ADFLAGS_DIR)) {
224 /* set default view */
225 ashort = htons(FINDERINFO_CLOSEDVIEW);
226 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
228 /* set default creator/type fields */
229 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
230 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
233 /* make things invisible */
234 if ((ad->ad_options & ADVOL_INVDOTS) && (*path == '.')) {
235 ashort = htons(ATTRBIT_INVISIBLE);
236 ad_setattr(ad, ashort);
237 ashort = htons(FINDERINFO_INVISIBLE);
238 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
241 if (lstat(path, &st) < 0)
244 /* put something sane in the date fields */
245 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
246 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
247 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
248 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
252 /* -------------------------------------
255 static void parse_entries(struct adouble *ad, char *buf, uint16_t nentries)
257 uint32_t eid, len, off;
260 /* now, read in the entry bits */
261 for (; nentries > 0; nentries-- ) {
262 memcpy(&eid, buf, sizeof( eid ));
263 eid = DISK_EID(ntohl( eid ));
264 buf += sizeof( eid );
265 memcpy(&off, buf, sizeof( off ));
267 buf += sizeof( off );
268 memcpy(&len, buf, sizeof( len ));
270 buf += sizeof( len );
272 if (eid && eid < ADEID_MAX && off < sizeof(ad->ad_data) &&
273 (off + len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
274 ad->ad_eid[ eid ].ade_off = off;
275 ad->ad_eid[ eid ].ade_len = len;
276 } else if (!warning) {
278 LOG(log_warning, logtype_default, "parse_entries: bogus eid: %d", eid);
283 /* this reads enough of the header so that we can figure out all of
284 * the entry lengths and offsets. once that's done, we just read/mmap
285 * the rest of the header in.
287 * NOTE: we're assuming that the resource fork is kept at the end of
288 * the file. also, mmapping won't work for the hfs fs until it
289 * understands how to mmap header files. */
290 static int ad_header_read(struct adouble *ad, struct stat *hst)
292 char *buf = ad->ad_data;
298 /* read the header */
299 if ((header_len = adf_pread( ad->ad_md, buf, sizeof(ad->ad_data), 0)) < 0) {
302 if (header_len < AD_HEADER_LEN) {
307 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
308 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
310 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
311 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
316 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
317 nentries = ntohs( nentries );
319 /* read in all the entry headers. if we have more than the
320 * maximum, just hope that the rfork is specified early on. */
321 len = nentries*AD_ENTRY_LEN;
323 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
324 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
326 buf += AD_HEADER_LEN;
327 if (len > header_len - AD_HEADER_LEN) {
328 LOG(log_error, logtype_default, "ad_header_read: can't read entry info.");
333 /* figure out all of the entry offsets and lengths. if we aren't
334 * able to read a resource fork entry, bail. */
335 nentries = len / AD_ENTRY_LEN;
336 parse_entries(ad, buf, nentries);
337 if (!ad_getentryoff(ad, ADEID_RFORK)
338 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
340 LOG(log_error, logtype_default, "ad_header_read: problem with rfork entry offset.");
345 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
346 LOG(log_error, logtype_default, "ad_header_read: can't read in entries.");
353 if (fstat(ad->ad_md->adf_fd, &st) < 0) {
354 return 1; /* fail silently */
357 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
362 static int ad_header_read_ea(struct adouble *ad, struct stat *hst _U_)
367 char *buf = ad->ad_data;
369 /* read the header */
370 if ((header_len = sys_fgetxattr(ad->ad_md->adf_fd, AD_EA_META, ad->ad_data, AD_DATASZ_EA)) < 1) {
371 LOG(log_debug, logtype_default, "ad_header_read_ea: %s (%u)", strerror(errno), errno);
375 if (header_len < AD_HEADER_LEN) {
376 LOG(log_error, logtype_default, "ad_header_read_ea: bogus AppleDouble header.");
381 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
382 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
384 ad->ad_magic = ntohl( ad->ad_magic );
385 ad->ad_version = ntohl( ad->ad_version );
387 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
388 LOG(log_error, logtype_default, "ad_header_read_ea: wrong magic or version");
393 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
394 nentries = ntohs( nentries );
396 /* Protect against bogus nentries */
397 len = nentries * AD_ENTRY_LEN;
398 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
399 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
400 if (len > header_len - AD_HEADER_LEN) {
401 LOG(log_error, logtype_default, "ad_header_read_ea: can't read entry info.");
405 nentries = len / AD_ENTRY_LEN;
407 /* Now parse entries */
408 parse_entries(ad, buf + AD_HEADER_LEN, nentries);
412 static int ad_mkrf(char *path)
416 * Probably .AppleDouble doesn't exist, try to mkdir it.
418 if (NULL == ( slash = strrchr( path, '/' )) ) {
423 if ( ad_mkdir( path, 0777 ) < 0 ) {
430 static int ad_mkrf_ea(char *path _U_)
432 AFP_PANIC("ad_mkrf_ea: dont use");
437 if we are root change path user/ group
438 It can be a native function for BSD cf. FAQ.Q10
439 path: pathname to chown
440 stbuf: parent directory inode
442 use fstat and fchown or lchown with linux?
444 #define EMULATE_SUIDDIR
446 static int ad_chown(const char *path, struct stat *stbuf)
449 #ifdef EMULATE_SUIDDIR
452 if (default_uid != (uid_t)-1) {
453 /* we are root (admin) */
454 id = (default_uid)?default_uid:stbuf->st_uid;
455 ret = lchown( path, id, stbuf->st_gid );
461 #define DEFMASK 07700 /* be conservative */
464 return access right and inode of path parent directory
466 static int ad_mode_st(const char *path, int *mode, struct stat *stbuf)
471 if (ad_stat(path, stbuf) != 0) {
475 *mode &= stbuf->st_mode;
479 /* ----------------- */
480 static int ad_error(struct adouble *ad, int adflags)
483 if ((adflags & ADFLAGS_NOHF)) {
484 /* FIXME double check : set header offset ?*/
487 if ((adflags & ADFLAGS_DF)) {
488 ad_close( ad, ADFLAGS_DF );
494 /* --------------------------- */
495 static int ad_header_upgrade(struct adouble *ad _U_, char *name _U_)
500 static int ad_header_upgrade_ea(struct adouble *ad _U_, char *name _U_)
502 AFP_PANIC("ad_header_upgrade_ea: dont use");
506 static int ad_open_df(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
512 LOG(log_maxdebug, logtype_default, "ad_open_df(\"%s/%s\", adf: 0x%04x, of: 0x%04x)",
513 getcwdpath(), path, adflags, oflags);
515 if (ad_data_fileno(ad) == -1) {
516 hoflags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
518 if ((oflags & O_CREAT)) {
519 st_invalid = ad_mode_st(path, &admode, &st_dir);
520 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
525 ad->ad_data_fork.adf_fd = open(path, hoflags | O_NOFOLLOW, admode);
527 if (ad->ad_data_fork.adf_fd == -1) {
528 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
530 ad->ad_data_fork.adf_fd = open( path, hoflags | O_NOFOLLOW, admode );
532 if (ad->ad_data_fork.adf_fd == -1 && errno == OPEN_NOFOLLOW_ERRNO) {
535 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
536 lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN);
538 free(ad->ad_data_fork.adf_syml);
541 ad->ad_data_fork.adf_syml[lsz] = 0;
542 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
546 if ( ad->ad_data_fork.adf_fd == -1 )
549 AD_SET(ad->ad_data_fork.adf_off);
550 ad->ad_data_fork.adf_flags = hoflags;
552 /* just created, set owner if admin (root) */
553 ad_chown(path, &st_dir);
555 adf_lock_init(&ad->ad_data_fork);
557 /* the file is already open... but */
558 if ((oflags & ( O_RDWR | O_WRONLY))
559 /* we want write access */
560 && !(ad->ad_data_fork.adf_flags & ( O_RDWR | O_WRONLY))) {
561 /* and it was denied the first time */
566 * for now ad_open is never called with O_TRUNC or O_EXCL if the file is
567 * already open. Should we check for it? ie
569 * O_TRUNC --> truncate the fork.
570 * idem for ressource fork.
573 ad->ad_data_fork.adf_refcount++;
578 static int ad_open_hf_v2(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
582 struct stat *pst = NULL;
587 ad_p = ad->ad_ops->ad_path( path, adflags );
589 hoflags = oflags & ~(O_CREAT | O_EXCL);
590 if (!(adflags & ADFLAGS_RDONLY)) {
591 hoflags = (hoflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
593 ad->ad_md->adf_fd = open( ad_p, hoflags | O_NOFOLLOW, 0 );
594 if (ad->ad_md->adf_fd < 0 ) {
595 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
596 hoflags = oflags & ~(O_CREAT | O_EXCL);
597 ad->ad_md->adf_fd = open( ad_p, hoflags | O_NOFOLLOW, 0 );
601 if ( ad->ad_md->adf_fd < 0 ) {
602 if (errno == ENOENT && (oflags & O_CREAT) ) {
604 * We're expecting to create a new adouble header file here
605 * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
607 LOG(log_debug, logtype_default, "ad_open(\"%s/%s\"): creating adouble file",
611 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
612 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
615 admode = ad_hf_mode(admode);
616 if ((errno == ENOENT)) {
617 if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
618 return ad_error(ad, adflags);
621 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
622 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
625 admode = ad_hf_mode(admode);
627 /* retry with O_CREAT */
628 ad->ad_md->adf_fd = open( ad_p, oflags,admode );
629 if ( ad->ad_md->adf_fd < 0 ) {
630 return ad_error(ad, adflags);
632 ad->ad_md->adf_flags = oflags;
633 /* just created, set owner if admin owner (root) */
635 ad_chown(ad_p, &st_dir);
638 return ad_error(ad, adflags);
641 ad->ad_md->adf_flags = hoflags;
642 if (fstat(ad->ad_md->adf_fd, &st_meta) == 0 && st_meta.st_size == 0) {
643 /* for 0 length files, treat them as new. */
644 ad->ad_md->adf_flags |= O_TRUNC;
646 /* we have valid data in st_meta stat structure, reused it in ad_header_read */
650 AD_SET(ad->ad_md->adf_off);
652 if ((ad->ad_md->adf_flags & ( O_TRUNC | O_CREAT ))) {
653 /* This is a new adouble header file, create it */
654 if (new_ad_header(path, ad, adflags) < 0) {
656 /* the file is already deleted, perm, whatever, so return an error */
657 ad_close(ad, adflags);
663 /* Read the adouble header in and parse it.*/
664 if (ad->ad_ops->ad_header_read( ad , pst) < 0
665 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0) {
667 ad_close( ad, adflags );
676 static int ad_open_hf_ea(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
682 LOG(log_maxdebug, logtype_default, "ad_open_hf_ea(\"%s/%s\", adf: 0x%04x, of: 0x%04x)",
683 getcwdpath(), path, adflags, oflags);
685 if ((ad->ad_md->adf_fd = open(path, O_RDONLY | O_NOFOLLOW)) == -1)
688 /* Read the adouble header in and parse it.*/
689 if (ad->ad_ops->ad_header_read(ad, NULL) != 0) {
690 if (!(oflags & O_CREAT)) {
692 ad_close(ad, adflags);
697 /* It doesnt exist, EPERM or another error */
698 if (errno != ENOATTR && errno != ENOENT) {
699 LOG(log_error, logtype_default, "ad_open_hf_ea: unexpected: %s", strerror(errno));
705 if (new_ad_header(path, ad, adflags) < 0) {
706 LOG(log_error, logtype_default, "ad_open_hf_ea: can't create new header: %s/%s",
715 ad->ad_md->adf_flags = O_RDWR; /* Pretend its rw, in fact for the EA API it is */
717 if ((rforklen = sys_fgetxattr(ad->ad_md->adf_fd, AD_EA_RESO, NULL, 0)) > 0)
718 ad->ad_rlen = rforklen;
724 ad_close(ad, adflags);
729 static int ad_open_hf(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
733 LOG(log_maxdebug, logtype_default, "ad_open_hf(\"%s/%s\", adf: 0x%04x, of: 0x%04x)",
734 getcwdpath(), path, adflags, oflags);
736 if (ad_meta_fileno(ad) != -1) { /* the file is already open */
737 LOG(log_maxdebug, logtype_default,"the file is already open");
738 if ((oflags & ( O_RDWR | O_WRONLY))
739 && !(ad->ad_md->adf_flags & ( O_RDWR | O_WRONLY))) {
740 if ((adflags & ADFLAGS_HF) && ad->ad_data_fork.adf_refcount)
741 /* We just have openend the df, so we have to close it now */
742 ad_close(ad, ADFLAGS_DF);
747 /* it's not new anymore */
748 ad->ad_md->adf_flags &= ~( O_TRUNC | O_CREAT );
749 ad->ad_md->adf_refcount++;
753 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
755 adf_lock_init(ad->ad_md);
757 switch (ad->ad_flags) {
759 ret = ad_open_hf_v2(path, adflags, oflags, mode, ad);
762 ret = ad_open_hf_ea(path, adflags, oflags, mode, ad);
769 ad->ad_md->adf_refcount = 1;
774 static int ad_open_rf(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
779 /***********************************************************************************
781 ********************************************************************************* */
783 char *ad_path_ea( const char *path, int adflags _U_)
789 * Put the .AppleDouble where it needs to be:
793 * \ b/.AppleDouble/.Parent
795 * FIXME: should do something for pathname > MAXPATHLEN
797 char *ad_path( const char *path, int adflags)
799 static char pathbuf[ MAXPATHLEN + 1];
803 if ( adflags & ADFLAGS_DIR ) {
804 l = strlcpy( pathbuf, path, sizeof(pathbuf));
806 if ( l && l < MAXPATHLEN) {
809 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
811 if (NULL != ( slash = strrchr( path, '/' )) ) {
814 /* XXX we must return NULL here and test in the caller */
817 memcpy( pathbuf, path, l);
822 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
823 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
829 /* -------------------------
830 * Support inherited protection modes for AppleDouble files. The supplied
831 * mode is ANDed with the parent directory's mask value in lieu of "umask",
832 * and that value is returned.
834 char *ad_dir(const char *path)
836 static char modebuf[ MAXPATHLEN + 1];
839 * For a path with directories in it, remove the final component
840 * (path or subdirectory name) to get the name we want to stat.
841 * For a path which is just a filename, use "." instead.
843 slash = strrchr( path, '/' );
848 if (len >= MAXPATHLEN) {
849 errno = ENAMETOOLONG;
850 return NULL; /* can't do it */
852 memcpy( modebuf, path, len );
854 /* is last char a '/' ? */
856 slash = modebuf+ len;
858 while (modebuf < slash && slash[-1] == '/') {
861 if (modebuf == slash) {
865 while (modebuf < slash && *slash != '/') {
868 if (modebuf == slash) {
871 *slash = '\0'; /* remove pathname component */
876 modebuf[0] = '.'; /* use current directory */
881 int ad_setfuid(const uid_t id)
887 /* ---------------- */
888 uid_t ad_getfuid(void)
894 stat path parent directory
896 int ad_stat(const char *path, struct stat *stbuf)
903 return lstat( p, stbuf );
907 return access right of path parent directory
909 int ad_mode( const char *path, int mode)
912 ad_mode_st(path, &mode, &stbuf);
917 * Use mkdir() with mode bits taken from ad_mode().
919 int ad_mkdir( const char *path, int mode)
925 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
926 path, mode, getcwdpath());
928 st_invalid = ad_mode_st(path, &mode, &stbuf);
929 ret = mkdir( path, mode );
930 if (ret || st_invalid)
932 ad_chown(path, &stbuf);
937 void ad_init(struct adouble *ad, int flags, int options)
940 ad->ad_flags = flags;
941 if (flags == AD_VERSION2) {
942 ad->ad_ops = &ad_adouble;
943 ad->ad_md = &ad->ad_resource_fork;
945 else if (flags == AD_VERSION_EA) {
946 ad->ad_ops = &ad_adouble_ea;
947 ad->ad_md = &ad->ad_metadata_fork;
949 LOG(log_error, logtype_default, "ad_init: unknown AD version");
954 ad->ad_options = options;
956 ad_data_fileno(ad) = -1;
957 ad_reso_fileno(ad) = -1;
958 ad_meta_fileno(ad) = -1;
960 /* following can be read even if there's no meda data. */
961 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
966 * Open data-, metadata(header)- or ressource fork
968 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
971 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
974 * @param path Path to file or directory
976 * @param adflags ADFLAGS_DF: open data fork \n
977 * ADFLAGS_RF: open ressource fork \n
978 * ADFLAGS_HF: open header (metadata) file \n
979 * ADFLAGS_NOHF: it's not an error if header file couldn't be created \n
980 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags \n
981 * ADFLAGS_NOADOUBLE: dont create adouble files if not necessary \n
982 * ADFLAGS_RDONLY: open read only \n
983 * ADFLAGS_OPENFORKS: check for open forks from other processes
985 * @param oflags flags passed through to open syscall: \n
986 * O_RDONLY: *** FIXME *** \n
987 * O_RDWR: *** FIXME *** \n
988 * O_CREAT: create fork \n
989 * O_EXCL: fail if exists with O_CREAT
991 * @param mode passed to open with O_CREAT
992 * @param ad pointer to struct adouble
994 * @returns 0 on success
996 * @note It's not possible to open the header file O_RDONLY -- the read
997 * will fail and return an error. this refcounts things now.\n
998 * metadata(ressource)-fork only gets created with O_CREAT.
1000 int ad_open(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
1002 if (ad->ad_inited != AD_INITED) {
1003 ad->ad_inited = AD_INITED;
1004 ad->ad_refcount = 1;
1005 ad->ad_open_forks = 0;
1006 ad->ad_adflags = adflags;
1007 ad->ad_resource_fork.adf_refcount = 0;
1008 ad->ad_data_fork.adf_refcount = 0;
1009 ad->ad_data_fork.adf_syml = 0;
1011 ad->ad_open_forks = ((ad->ad_data_fork.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
1012 /* XXX not true if we have a meta data fork ? */
1013 if ((ad->ad_resource_fork.adf_refcount > ad->ad_data_fork.adf_refcount))
1014 ad->ad_open_forks |= ATTRBIT_ROPEN;
1017 if ((adflags & ADFLAGS_DF)) {
1018 if (ad_open_df(path, adflags, oflags, mode, ad) != 0)
1022 if ((adflags & ADFLAGS_HF)) {
1023 if (ad_open_hf(path, adflags, oflags, mode, ad) != 0)
1028 if ((adflags & ADFLAGS_RF)) {
1029 if (ad_open_rf(path, adflags, oflags, mode, ad) != 0)
1038 * @brief open metadata, possibly as root
1040 * Return only metadata but try very hard ie at first try as user, then try as root.
1042 * @param name name of file/dir
1043 * @param flags ADFLAGS_DIR: name is a directory \n
1044 * ADFLAGS_CREATE: force creation of header file, but only as user, not as root\n
1045 * ADFLAGS_OPENFORKS: test if name is open by another afpd process
1047 * @param adp pointer to struct adouble
1049 * @note caller MUST pass ADFLAGS_DIR for directories. Whether ADFLAGS_CREATE really creates
1050 * a adouble file depends on various other volume options, eg. ADVOL_CACHE
1052 int ad_metadata(const char *name, int flags, struct adouble *adp)
1056 int create = O_RDONLY;
1058 dir = flags & ADFLAGS_DIR;
1060 /* Check if we shall call ad_open with O_CREAT */
1061 if ( (adp->ad_options & ADVOL_CACHE)
1062 && ! (adp->ad_options & ADVOL_NOADOUBLE)
1063 && (flags & ADFLAGS_CREATE) ) {
1064 create = O_CREAT | O_RDWR;
1066 if ((ret = ad_open(name, ADFLAGS_HF | dir, create, 0666, adp)) < 0 && errno == EACCES) {
1069 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1073 /* we are root open read only */
1074 ret = ad_open(name, ADFLAGS_HF|ADFLAGS_RDONLY| dir, O_RDONLY, 0, adp);
1076 if ( seteuid(uid) < 0) {
1077 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1083 if (!ret && (ADFLAGS_OPENFORKS & flags)) {
1085 we need to check if the file is open by another process.
1086 it's slow so we only do it if we have to:
1088 - we don't already have the answer!
1090 adp->ad_open_forks |= ad_openforks(adp, adp->ad_open_forks);
1096 * @brief openat like wrapper for ad_metadata
1098 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1104 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1110 if (ad_metadata(name, flags, adp) < 0) {
1116 if (fchdir(cwdfd) != 0) {
1117 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1130 int ad_refresh(struct adouble *ad)
1133 if (ad_meta_fileno(ad) == -1)
1136 return ad->ad_ops->ad_header_read(ad, NULL);
1139 int ad_openat(int dirfd, /* dir fd openat like */
1150 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1156 if (ad_open(path, adflags, oflags, mode, ad) < 0) {
1162 if (fchdir(cwdfd) != 0) {
1163 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");