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>
46 #include <atalk/logger.h>
47 #include <atalk/adouble.h>
48 #include <atalk/util.h>
50 #include <atalk/bstrlib.h>
51 #include <atalk/bstradd.h>
56 #define MAX(a, b) ((a) < (b) ? (b) : (a))
62 # define AD_SET(a) a = 0
65 #define ADEDOFF_MAGIC (0)
66 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
67 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
68 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
70 /* initial lengths of some of the fields */
71 #define ADEDLEN_INIT 0
73 /* i stick things in a slightly different order than their eid order in
74 * case i ever want to separate RootInfo behaviour from the rest of the
78 #define ADEDOFF_NAME_V2 (AD_HEADER_LEN + ADEID_NUM_V2*AD_ENTRY_LEN)
79 #define ADEDOFF_COMMENT_V2 (ADEDOFF_NAME_V2 + ADEDLEN_NAME)
80 #define ADEDOFF_FILEDATESI (ADEDOFF_COMMENT_V2 + ADEDLEN_COMMENT)
81 #define ADEDOFF_FINDERI_V2 (ADEDOFF_FILEDATESI + ADEDLEN_FILEDATESI)
82 #define ADEDOFF_DID (ADEDOFF_FINDERI_V2 + ADEDLEN_FINDERI)
83 #define ADEDOFF_AFPFILEI (ADEDOFF_DID + ADEDLEN_DID)
84 #define ADEDOFF_SHORTNAME (ADEDOFF_AFPFILEI + ADEDLEN_AFPFILEI)
85 #define ADEDOFF_PRODOSFILEI (ADEDOFF_SHORTNAME + ADEDLEN_SHORTNAME)
86 #define ADEDOFF_PRIVDEV (ADEDOFF_PRODOSFILEI + ADEDLEN_PRODOSFILEI)
87 #define ADEDOFF_PRIVINO (ADEDOFF_PRIVDEV + ADEDLEN_PRIVDEV)
88 #define ADEDOFF_PRIVSYN (ADEDOFF_PRIVINO + ADEDLEN_PRIVINO)
89 #define ADEDOFF_PRIVID (ADEDOFF_PRIVSYN + ADEDLEN_PRIVSYN)
90 #define ADEDOFF_RFORK_V2 (ADEDOFF_PRIVID + ADEDLEN_PRIVID)
93 #define ADEDOFF_FINDERI_EA (AD_HEADER_LEN + ADEID_NUM_EA * AD_ENTRY_LEN)
94 #define ADEDOFF_COMMENT_EA (ADEDOFF_FINDERI_EA + ADEDLEN_FINDERI)
95 #define ADEDOFF_FILEDATESI_EA (ADEDOFF_COMMENT_EA + ADEDLEN_COMMENT)
96 #define ADEDOFF_AFPFILEI_EA (ADEDOFF_FILEDATESI_EA + ADEDLEN_FILEDATESI)
98 /* this is to prevent changing timezones from causing problems with
99 localtime volumes. the screw-up is 30 years. we use a delta of 5 years */
100 #define TIMEWARP_DELTA 157680000
103 uint32_t id, offset, len;
106 /* --------------------------- */
107 static uid_t default_uid = -1;
109 /* Forward declarations */
110 static int ad_mkrf(const char *path);
111 static int ad_header_read(struct adouble *ad, struct stat *hst);
112 static int ad_header_upgrade(struct adouble *ad, const char *name);
114 static int ad_mkrf_ea(const char *path);
115 static int ad_header_read_ea(struct adouble *ad, struct stat *hst);
116 static int ad_header_upgrade_ea(struct adouble *ad, const char *name);
118 static struct adouble_fops ad_adouble = {
121 &ad_rebuild_adouble_header,
126 static struct adouble_fops ad_adouble_ea = {
129 &ad_rebuild_adouble_header,
131 &ad_header_upgrade_ea,
134 static const struct entry entry_order2[ADEID_NUM_V2 + 1] = {
135 {ADEID_NAME, ADEDOFF_NAME_V2, ADEDLEN_INIT},
136 {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
137 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
138 {ADEID_FINDERI, ADEDOFF_FINDERI_V2, ADEDLEN_FINDERI},
139 {ADEID_DID, ADEDOFF_DID, ADEDLEN_DID},
140 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
141 {ADEID_SHORTNAME, ADEDOFF_SHORTNAME, ADEDLEN_INIT},
142 {ADEID_PRODOSFILEI, ADEDOFF_PRODOSFILEI, ADEDLEN_PRODOSFILEI},
143 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV, ADEDLEN_INIT},
144 {ADEID_PRIVINO, ADEDOFF_PRIVINO, ADEDLEN_INIT},
145 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN, ADEDLEN_INIT},
146 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
147 {ADEID_RFORK, ADEDOFF_RFORK_V2, ADEDLEN_INIT},
151 /* Using Extended Attributes */
152 static const struct entry entry_order_ea[ADEID_NUM_EA + 1] = {
153 {ADEID_FINDERI, ADEDOFF_FINDERI_EA, ADEDLEN_FINDERI},
154 {ADEID_COMMENT, ADEDOFF_COMMENT_EA, ADEDLEN_INIT},
155 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_EA, ADEDLEN_FILEDATESI},
156 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_EA, ADEDLEN_AFPFILEI},
160 static uint32_t get_eid(uint32_t eid)
165 return ADEID_PRIVDEV;
167 return ADEID_PRIVINO;
169 return ADEID_PRIVSYN;
176 /* ----------------------------------- */
177 static int new_ad_header(const char *path, struct adouble *ad, int adflags)
179 const struct entry *eid;
183 ad->ad_magic = AD_MAGIC;
184 ad->ad_version = ad->ad_flags & 0x0f0000;
185 if (!ad->ad_version) {
186 ad->ad_version = AD_VERSION;
190 memset(ad->ad_data, 0, sizeof(ad->ad_data));
192 if (ad->ad_flags == AD_VERSION2)
194 else if (ad->ad_flags == AD_VERSION_EA)
195 eid = entry_order_ea;
201 ad->ad_eid[eid->id].ade_off = eid->offset;
202 ad->ad_eid[eid->id].ade_len = eid->len;
206 /* put something sane in the directory finderinfo */
207 if ((adflags & ADFLAGS_DIR)) {
208 /* set default view */
209 ashort = htons(FINDERINFO_CLOSEDVIEW);
210 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
212 /* set default creator/type fields */
213 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
214 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
217 /* make things invisible */
218 if ((ad->ad_options & ADVOL_INVDOTS) && (*path == '.')) {
219 ashort = htons(ATTRBIT_INVISIBLE);
220 ad_setattr(ad, ashort);
221 ashort = htons(FINDERINFO_INVISIBLE);
222 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
225 if (lstat(path, &st) < 0)
228 /* put something sane in the date fields */
229 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
230 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
231 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
232 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
236 /* -------------------------------------
239 static void parse_entries(struct adouble *ad, char *buf, uint16_t nentries)
241 uint32_t eid, len, off;
244 /* now, read in the entry bits */
245 for (; nentries > 0; nentries-- ) {
246 memcpy(&eid, buf, sizeof( eid ));
247 eid = get_eid(ntohl(eid));
248 buf += sizeof( eid );
249 memcpy(&off, buf, sizeof( off ));
251 buf += sizeof( off );
252 memcpy(&len, buf, sizeof( len ));
254 buf += sizeof( len );
256 if (eid && eid < ADEID_MAX && off < sizeof(ad->ad_data) &&
257 (off + len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
258 ad->ad_eid[ eid ].ade_off = off;
259 ad->ad_eid[ eid ].ade_len = len;
260 } else if (!warning) {
262 LOG(log_warning, logtype_default, "parse_entries: bogus eid: %d", eid);
267 /* this reads enough of the header so that we can figure out all of
268 * the entry lengths and offsets. once that's done, we just read/mmap
269 * the rest of the header in.
271 * NOTE: we're assuming that the resource fork is kept at the end of
272 * the file. also, mmapping won't work for the hfs fs until it
273 * understands how to mmap header files. */
274 static int ad_header_read(struct adouble *ad, struct stat *hst)
276 char *buf = ad->ad_data;
282 /* read the header */
283 if ((header_len = adf_pread( ad->ad_md, buf, sizeof(ad->ad_data), 0)) < 0) {
286 if (header_len < AD_HEADER_LEN) {
291 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
292 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
293 ad->ad_magic = ntohl( ad->ad_magic );
294 ad->ad_version = ntohl( ad->ad_version );
296 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
297 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
302 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
303 nentries = ntohs( nentries );
305 /* read in all the entry headers. if we have more than the
306 * maximum, just hope that the rfork is specified early on. */
307 len = nentries*AD_ENTRY_LEN;
309 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
310 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
312 buf += AD_HEADER_LEN;
313 if (len > header_len - AD_HEADER_LEN) {
314 LOG(log_error, logtype_default, "ad_header_read: can't read entry info.");
319 /* figure out all of the entry offsets and lengths. if we aren't
320 * able to read a resource fork entry, bail. */
321 nentries = len / AD_ENTRY_LEN;
322 parse_entries(ad, buf, nentries);
323 if (!ad_getentryoff(ad, ADEID_RFORK)
324 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
326 LOG(log_error, logtype_default, "ad_header_read: problem with rfork entry offset.");
331 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
332 LOG(log_error, logtype_default, "ad_header_read: can't read in entries.");
339 if (fstat(ad->ad_md->adf_fd, &st) < 0) {
340 return 1; /* fail silently */
344 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
349 static int ad_header_read_ea(struct adouble *ad, struct stat *hst _U_)
354 char *buf = ad->ad_data;
356 /* read the header */
357 if ((header_len = sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_META, ad->ad_data, AD_DATASZ_EA)) < 1) {
358 LOG(log_debug, logtype_default, "ad_header_read_ea: %s (%u)", strerror(errno), errno);
362 if (header_len < AD_HEADER_LEN) {
363 LOG(log_error, logtype_default, "ad_header_read_ea: bogus AppleDouble header.");
368 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
369 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
371 ad->ad_magic = ntohl( ad->ad_magic );
372 ad->ad_version = ntohl( ad->ad_version );
374 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
375 LOG(log_error, logtype_default, "ad_header_read_ea: wrong magic or version");
380 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
381 nentries = ntohs( nentries );
383 /* Protect against bogus nentries */
384 len = nentries * AD_ENTRY_LEN;
385 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
386 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
387 if (len > header_len - AD_HEADER_LEN) {
388 LOG(log_error, logtype_default, "ad_header_read_ea: can't read entry info.");
392 nentries = len / AD_ENTRY_LEN;
394 /* Now parse entries */
395 parse_entries(ad, buf + AD_HEADER_LEN, nentries);
399 static int ad_mkrf(const char *path)
403 * Probably .AppleDouble doesn't exist, try to mkdir it.
405 if (NULL == ( slash = strrchr( path, '/' )) ) {
410 if ( ad_mkdir( path, 0777 ) < 0 ) {
417 static int ad_mkrf_ea(const char *path _U_)
419 AFP_PANIC("ad_mkrf_ea: dont use");
424 if we are root change path user/ group
425 It can be a native function for BSD cf. FAQ.Q10
426 path: pathname to chown
427 stbuf: parent directory inode
429 use fstat and fchown or lchown with linux?
431 #define EMULATE_SUIDDIR
433 static int ad_chown(const char *path, struct stat *stbuf)
436 #ifdef EMULATE_SUIDDIR
439 if (default_uid != (uid_t)-1) {
440 /* we are root (admin) */
441 id = (default_uid)?default_uid:stbuf->st_uid;
442 ret = lchown( path, id, stbuf->st_gid );
448 #define DEFMASK 07700 /* be conservative */
451 return access right and inode of path parent directory
453 static int ad_mode_st(const char *path, int *mode, struct stat *stbuf)
458 if (ad_stat(path, stbuf) != 0) {
462 *mode &= stbuf->st_mode;
466 /* --------------------------- */
467 static int ad_header_upgrade(struct adouble *ad _U_, const char *name _U_)
472 static int ad_header_upgrade_ea(struct adouble *ad _U_, const char *name _U_)
474 AFP_PANIC("ad_header_upgrade_ea: dont use");
478 static int ad_open_df(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
484 LOG(log_maxdebug, logtype_default, "ad_open_df(\"%s/%s\", adf: 0x%04x, of: 0x%04x)",
485 getcwdpath(), path, adflags, oflags);
487 if (ad_data_fileno(ad) == -1) {
488 hoflags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
490 if ((oflags & O_CREAT)) {
491 st_invalid = ad_mode_st(path, &admode, &st_dir);
492 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
497 ad->ad_data_fork.adf_fd = open(path, hoflags | O_NOFOLLOW, admode);
499 if (ad->ad_data_fork.adf_fd == -1) {
500 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
502 ad->ad_data_fork.adf_fd = open( path, hoflags | O_NOFOLLOW, admode );
504 if (ad->ad_data_fork.adf_fd == -1 && errno == OPEN_NOFOLLOW_ERRNO) {
507 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
508 lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN);
510 free(ad->ad_data_fork.adf_syml);
513 ad->ad_data_fork.adf_syml[lsz] = 0;
514 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
518 if ( ad->ad_data_fork.adf_fd == -1 )
521 AD_SET(ad->ad_data_fork.adf_off);
522 ad->ad_data_fork.adf_flags = hoflags;
524 /* just created, set owner if admin (root) */
525 ad_chown(path, &st_dir);
528 /* the file is already open... but */
529 if ((oflags & ( O_RDWR | O_WRONLY))
530 /* we want write access */
531 && !(ad->ad_data_fork.adf_flags & ( O_RDWR | O_WRONLY))) {
532 /* and it was denied the first time */
537 * for now ad_open is never called with O_TRUNC or O_EXCL if the file is
538 * already open. Should we check for it? ie
540 * O_TRUNC --> truncate the fork.
541 * idem for ressource fork.
548 static int ad_open_hf_v2(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
552 struct stat *pst = NULL;
557 ad_p = ad->ad_ops->ad_path( path, adflags );
559 hoflags = (oflags & ~(O_CREAT | O_EXCL)) | O_NOFOLLOW;
561 ad->ad_md->adf_fd = open(ad_p, hoflags, 0);
563 if ( ad->ad_md->adf_fd < 0 ) {
564 if (errno == ENOENT && (oflags & O_CREAT) ) {
566 * We're expecting to create a new adouble header file here
567 * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
569 LOG(log_debug, logtype_default, "ad_open(\"%s\"): creating adouble file",
573 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
574 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
577 admode = ad_hf_mode(admode);
578 if ((errno == ENOENT)) {
579 if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
583 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
584 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
587 admode = ad_hf_mode(admode);
589 /* retry with O_CREAT */
590 ad->ad_md->adf_fd = open(ad_p, oflags, admode);
591 if ( ad->ad_md->adf_fd < 0 ) {
594 ad->ad_md->adf_flags = oflags;
595 /* just created, set owner if admin owner (root) */
597 ad_chown(ad_p, &st_dir);
603 ad->ad_md->adf_flags = hoflags;
604 if (fstat(ad->ad_md->adf_fd, &st_meta) == 0 && st_meta.st_size == 0) {
605 /* for 0 length files, treat them as new. */
606 ad->ad_md->adf_flags |= O_TRUNC;
608 /* we have valid data in st_meta stat structure, reused it in ad_header_read */
612 AD_SET(ad->ad_md->adf_off);
614 if ((ad->ad_md->adf_flags & ( O_TRUNC | O_CREAT ))) {
615 /* This is a new adouble header file, create it */
616 if (new_ad_header(path, ad, adflags) < 0) {
618 /* the file is already deleted, perm, whatever, so return an error */
624 /* Read the adouble header in and parse it.*/
625 if (ad->ad_ops->ad_header_read( ad , pst) < 0
626 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0) {
636 static int ad_open_hf_ea(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
641 LOG(log_maxdebug, logtype_default, "ad_open_hf_ea(\"%s\", adf: 0x%04x, of: 0x%04x)",
642 abspath(path), adflags, oflags);
644 if ((ad->ad_md->adf_fd = open(path, O_RDONLY | O_NOFOLLOW)) == -1)
647 /* Read the adouble header in and parse it.*/
648 if (ad->ad_ops->ad_header_read(ad, NULL) != 0) {
649 if (!(oflags & O_CREAT))
652 /* It doesnt exist, EPERM or another error */
653 if (errno != ENOATTR && errno != ENOENT) {
654 LOG(log_error, logtype_default, "ad_open_hf_ea: unexpected: %s", strerror(errno));
660 if (new_ad_header(path, ad, adflags) < 0) {
661 LOG(log_error, logtype_default, "ad_open_hf_ea: can't create new header: %s",
670 ad->ad_md->adf_flags = O_RDWR; /* Pretend its rw, in fact for the EA API it is */
672 if ((rforklen = sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, NULL, 0)) > 0)
673 ad->ad_rlen = rforklen;
681 static int ad_open_hf(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
685 LOG(log_maxdebug, logtype_default, "ad_open_hf(\"%s/%s\", adf: 0x%04x, of: 0x%04x)",
686 getcwdpath(), path, adflags, oflags);
688 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
691 switch (ad->ad_flags) {
693 ret = ad_open_hf_v2(path, adflags, oflags, mode, ad);
696 ret = ad_open_hf_ea(path, adflags, oflags, mode, ad);
707 * Open EA with resfork, only for AD_VERSION_EA, a nullop otherwise
709 static int ad_open_rf(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
713 if (ad->ad_flags != AD_VERSION_EA)
716 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\", adf: 0x%04x, of: 0x%04x)",
717 abspath(path), adflags, oflags);
719 if ((ad->ad_rlen = sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, NULL, 0)) <= 0) {
725 LOG(log_warning, logtype_default, "ad_open_rf(\"%s\"): %s",
726 abspath(path), strerror(errno));
732 /* Round up and allocate buffer */
733 size_t roundup = ((ad->ad_rlen / RFORK_EA_ALLOCSIZE) + 1) * RFORK_EA_ALLOCSIZE;
734 if ((ad->ad_resforkbuf = malloc(roundup)) == NULL) {
739 ad->ad_resforkbufsize = roundup;
741 /* Read the EA into the buffer */
742 if (ad->ad_rlen > 0) {
743 if (sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, ad->ad_resforkbuf, ad->ad_rlen) == -1) {
751 free(ad->ad_resforkbuf);
752 ad->ad_resforkbuf = NULL;
754 ad->ad_resforkbufsize = 0;
760 /***********************************************************************************
762 ********************************************************************************* */
764 const char *ad_path_ea( const char *path, int adflags _U_)
770 * Put the .AppleDouble where it needs to be:
774 * \ b/.AppleDouble/.Parent
776 * FIXME: should do something for pathname > MAXPATHLEN
778 const char *ad_path( const char *path, int adflags)
780 static char pathbuf[ MAXPATHLEN + 1];
784 if ( adflags & ADFLAGS_DIR ) {
785 l = strlcpy( pathbuf, path, sizeof(pathbuf));
787 if ( l && l < MAXPATHLEN) {
790 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
792 if (NULL != ( slash = strrchr( path, '/' )) ) {
795 /* XXX we must return NULL here and test in the caller */
798 memcpy( pathbuf, path, l);
803 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
804 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
810 /* -------------------------
811 * Support inherited protection modes for AppleDouble files. The supplied
812 * mode is ANDed with the parent directory's mask value in lieu of "umask",
813 * and that value is returned.
815 char *ad_dir(const char *path)
817 static char modebuf[ MAXPATHLEN + 1];
820 * For a path with directories in it, remove the final component
821 * (path or subdirectory name) to get the name we want to stat.
822 * For a path which is just a filename, use "." instead.
824 slash = strrchr( path, '/' );
829 if (len >= MAXPATHLEN) {
830 errno = ENAMETOOLONG;
831 return NULL; /* can't do it */
833 memcpy( modebuf, path, len );
835 /* is last char a '/' ? */
837 slash = modebuf+ len;
839 while (modebuf < slash && slash[-1] == '/') {
842 if (modebuf == slash) {
846 while (modebuf < slash && *slash != '/') {
849 if (modebuf == slash) {
852 *slash = '\0'; /* remove pathname component */
857 modebuf[0] = '.'; /* use current directory */
862 int ad_setfuid(const uid_t id)
868 /* ---------------- */
869 uid_t ad_getfuid(void)
875 stat path parent directory
877 int ad_stat(const char *path, struct stat *stbuf)
884 return lstat( p, stbuf );
888 return access right of path parent directory
890 int ad_mode( const char *path, int mode)
893 ad_mode_st(path, &mode, &stbuf);
898 * Use mkdir() with mode bits taken from ad_mode().
900 int ad_mkdir( const char *path, int mode)
906 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
907 path, mode, getcwdpath());
909 st_invalid = ad_mode_st(path, &mode, &stbuf);
910 ret = mkdir( path, mode );
911 if (ret || st_invalid)
913 ad_chown(path, &stbuf);
918 void ad_init(struct adouble *ad, int flags, int options)
920 memset(ad, 0, sizeof(struct adouble));
922 if (flags == AD_VERSION2) {
923 ad->ad_ops = &ad_adouble;
924 ad->ad_md = &ad->ad_resource_fork;
926 else if (flags == AD_VERSION_EA) {
927 ad->ad_ops = &ad_adouble_ea;
928 ad->ad_md = &ad->ad_metadata_fork;
930 LOG(log_error, logtype_default, "ad_init: unknown AD version");
935 ad->ad_flags = flags;
936 ad->ad_options = options;
937 ad_data_fileno(ad) = -1;
938 ad_reso_fileno(ad) = -1;
939 ad_meta_fileno(ad) = -1;
940 ad->ad_inited = AD_INITED;
943 const char *adflags2logstr(int adflags)
950 if (adflags & ADFLAGS_DF) {
951 strlcat(buf, "DF", 64);
954 if (adflags & ADFLAGS_RF) {
956 strlcat(buf, "|", 64);
957 strlcat(buf, "RF", 64);
960 if (adflags & ADFLAGS_HF) {
962 strlcat(buf, "|", 64);
963 strlcat(buf, "HF", 64);
966 if (adflags & ADFLAGS_NOHF) {
968 strlcat(buf, "|", 64);
969 strlcat(buf, "NOHF", 64);
972 if (adflags & ADFLAGS_DIR) {
974 strlcat(buf, "|", 64);
975 strlcat(buf, "DIR", 64);
978 if (adflags & ADFLAGS_OPENFORKS) {
980 strlcat(buf, "|", 64);
981 strlcat(buf, "OF", 64);
987 const char *oflags2logstr(int oflags)
994 if (oflags == O_RDONLY) {
995 strlcat(buf, "O_RDONLY", 64);
998 if (oflags & O_RDWR) {
1000 strlcat(buf, "|", 64);
1001 strlcat(buf, "O_RDWR", 64);
1004 if (oflags & O_CREAT) {
1006 strlcat(buf, "|", 64);
1007 strlcat(buf, "O_CREAT", 64);
1010 if (oflags & O_EXCL) {
1012 strlcat(buf, "|", 64);
1013 strlcat(buf, "O_EXCL", 64);
1020 * Open data-, metadata(header)- or ressource fork
1022 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
1025 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1028 * Open a files data fork, metadata fork or ressource fork.
1029 * For each fork to be opened specify the open flags and mode in case you want to create it
1030 * (O_CREAT in open flags). The order in which forks are opened is:
1034 * The variable arguments must be passed according to this order.
1036 * @param ad (rw) pointer to struct adouble
1037 * @param path (r) Path to file or directory
1038 * @param adflags (r) ADFLAGS_DF: open data fork \n
1039 * ADFLAGS_RF: open ressource fork \n
1040 * ADFLAGS_HF: open header (metadata) file \n
1041 * ADFLAGS_NOHF: it's not an error if header file couldn't be created \n
1042 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags \n
1043 * ADFLAGS_RDONLY: dont upgrade mode from r to rw with adouble:v2 headerfile \n
1044 * ADFLAGS_OPENFORKS: check for open forks from other processes
1046 * @returns 0 on success, any other value indicates an error
1048 static int vad_open(struct adouble *ad, const char *path, int adflags, va_list args)
1054 LOG(log_debug, logtype_default, "ad_open(\"%s\", %s)",
1055 abspath(path), adflags2logstr(adflags));
1057 if (ad->ad_inited != AD_INITED)
1058 AFP_PANIC("ad_open: not initialized");
1060 if (ad->ad_fullpath == NULL) {
1061 if ((ad->ad_fullpath = bfromcstr(abspath(path))) == NULL) {
1067 if ((adflags & ADFLAGS_DF) && !(ad->ad_adflags & ADFLAGS_DF)) {
1068 oflags = va_arg(args, int);
1069 if (oflags & O_CREAT)
1070 mode = va_arg(args, int);
1071 if (ad_open_df(path, adflags, oflags, mode, ad) != 0) {
1075 ad->ad_adflags |= ADFLAGS_DF;
1078 if ((adflags & ADFLAGS_HF) && !(ad->ad_adflags & ADFLAGS_HF)) {
1079 oflags = va_arg(args, int);
1080 if (oflags & O_CREAT)
1081 mode = va_arg(args, int);
1082 if (ad_open_hf(path, adflags, oflags, mode, ad) != 0) {
1086 ad->ad_adflags |= ADFLAGS_HF;
1089 if ((adflags & ADFLAGS_RF) && !(ad->ad_adflags & ADFLAGS_RF)) {
1090 oflags = va_arg(args, int);
1091 if (oflags & O_CREAT)
1092 mode = va_arg(args, int);
1093 if (ad_open_rf(path, adflags, oflags, mode, ad) != 0) {
1097 ad->ad_adflags |= ADFLAGS_RF;
1102 /* FIXME: ad_close stuff we opened before hitting an error */
1103 /* Dont forget ADFLAGS_NOHF !!! */
1105 if (ad->ad_fullpath) {
1106 bdestroy(ad->ad_fullpath);
1107 ad->ad_fullpath = NULL;
1114 int ad_open(struct adouble *ad, const char *path, int adflags, ...)
1119 va_start(args, adflags);
1120 ret = vad_open(ad, path, adflags, args);
1126 * @brief open metadata, possibly as root
1128 * Return only metadata but try very hard ie at first try as user, then try as root.
1130 * @param name name of file/dir
1131 * @param flags ADFLAGS_DIR: name is a directory \n
1132 * ADFLAGS_OPENFORKS: test if name is open by another afpd process
1134 * @param adp pointer to struct adouble
1136 int ad_metadata(const char *name, int flags, struct adouble *adp)
1141 dir = flags & ADFLAGS_DIR;
1143 if ((ret = ad_open(adp, name, ADFLAGS_HF | dir, O_RDONLY)) < 0 && errno == EACCES) {
1146 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1150 /* we are root open read only */
1151 ret = ad_open(adp, name, ADFLAGS_HF | dir, O_RDONLY);
1153 if ( seteuid(uid) < 0) {
1154 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1160 if (!ret && (ADFLAGS_OPENFORKS & flags)) {
1162 we need to check if the file is open by another process.
1163 it's slow so we only do it if we have to:
1165 - we don't already have the answer!
1167 adp->ad_open_forks |= ad_openforks(adp, adp->ad_open_forks);
1173 * @brief openat like wrapper for ad_metadata
1175 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1181 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1187 if (ad_metadata(name, flags, adp) < 0) {
1193 if (fchdir(cwdfd) != 0) {
1194 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1207 int ad_refresh(struct adouble *ad)
1210 if (ad_meta_fileno(ad) == -1)
1213 return ad->ad_ops->ad_header_read(ad, NULL);
1216 int ad_openat(struct adouble *ad,
1217 int dirfd, /* dir fd openat like */
1226 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1232 va_start(args, adflags);
1234 if (vad_open(ad, path, adflags, args) < 0) {
1242 if (fchdir(cwdfd) != 0) {
1243 AFP_PANIC("ad_openat: cant chdir back");