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 <arpa/inet.h>
47 #include <atalk/logger.h>
48 #include <atalk/adouble.h>
49 #include <atalk/util.h>
51 #include <atalk/bstrlib.h>
52 #include <atalk/bstradd.h>
53 #include <atalk/compat.h>
58 #define MAX(a, b) ((a) < (b) ? (b) : (a))
64 # define AD_SET(a) a = 0
67 #define ADEDOFF_MAGIC (0)
68 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
69 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
70 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
72 /* initial lengths of some of the fields */
73 #define ADEDLEN_INIT 0
75 /* i stick things in a slightly different order than their eid order in
76 * case i ever want to separate RootInfo behaviour from the rest of the
80 #define ADEDOFF_NAME_V2 (AD_HEADER_LEN + ADEID_NUM_V2*AD_ENTRY_LEN)
81 #define ADEDOFF_COMMENT_V2 (ADEDOFF_NAME_V2 + ADEDLEN_NAME)
82 #define ADEDOFF_FILEDATESI (ADEDOFF_COMMENT_V2 + ADEDLEN_COMMENT)
83 #define ADEDOFF_FINDERI_V2 (ADEDOFF_FILEDATESI + ADEDLEN_FILEDATESI)
84 #define ADEDOFF_DID (ADEDOFF_FINDERI_V2 + ADEDLEN_FINDERI)
85 #define ADEDOFF_AFPFILEI (ADEDOFF_DID + ADEDLEN_DID)
86 #define ADEDOFF_SHORTNAME (ADEDOFF_AFPFILEI + ADEDLEN_AFPFILEI)
87 #define ADEDOFF_PRODOSFILEI (ADEDOFF_SHORTNAME + ADEDLEN_SHORTNAME)
88 #define ADEDOFF_PRIVDEV (ADEDOFF_PRODOSFILEI + ADEDLEN_PRODOSFILEI)
89 #define ADEDOFF_PRIVINO (ADEDOFF_PRIVDEV + ADEDLEN_PRIVDEV)
90 #define ADEDOFF_PRIVSYN (ADEDOFF_PRIVINO + ADEDLEN_PRIVINO)
91 #define ADEDOFF_PRIVID (ADEDOFF_PRIVSYN + ADEDLEN_PRIVSYN)
92 #define ADEDOFF_RFORK_V2 (ADEDOFF_PRIVID + ADEDLEN_PRIVID)
95 #define ADEDOFF_FINDERI_EA (AD_HEADER_LEN + ADEID_NUM_EA * AD_ENTRY_LEN)
96 #define ADEDOFF_COMMENT_EA (ADEDOFF_FINDERI_EA + ADEDLEN_FINDERI)
97 #define ADEDOFF_FILEDATESI_EA (ADEDOFF_COMMENT_EA + ADEDLEN_COMMENT)
98 #define ADEDOFF_AFPFILEI_EA (ADEDOFF_FILEDATESI_EA + ADEDLEN_FILEDATESI)
100 /* this is to prevent changing timezones from causing problems with
101 localtime volumes. the screw-up is 30 years. we use a delta of 5 years */
102 #define TIMEWARP_DELTA 157680000
105 uint32_t id, offset, len;
108 /* --------------------------- */
109 static uid_t default_uid = -1;
111 /* Forward declarations */
112 static int ad_mkrf(const char *path);
113 static int ad_header_read(struct adouble *ad, struct stat *hst);
114 static int ad_header_upgrade(struct adouble *ad, const char *name);
116 static int ad_mkrf_ea(const char *path);
117 static int ad_header_read_ea(struct adouble *ad, struct stat *hst);
118 static int ad_header_upgrade_ea(struct adouble *ad, const char *name);
120 static struct adouble_fops ad_adouble = {
123 &ad_rebuild_adouble_header,
128 static struct adouble_fops ad_adouble_ea = {
131 &ad_rebuild_adouble_header,
133 &ad_header_upgrade_ea,
136 static const struct entry entry_order2[ADEID_NUM_V2 + 1] = {
137 {ADEID_NAME, ADEDOFF_NAME_V2, ADEDLEN_INIT},
138 {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
139 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
140 {ADEID_FINDERI, ADEDOFF_FINDERI_V2, ADEDLEN_FINDERI},
141 {ADEID_DID, ADEDOFF_DID, ADEDLEN_DID},
142 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
143 {ADEID_SHORTNAME, ADEDOFF_SHORTNAME, ADEDLEN_INIT},
144 {ADEID_PRODOSFILEI, ADEDOFF_PRODOSFILEI, ADEDLEN_PRODOSFILEI},
145 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV, ADEDLEN_INIT},
146 {ADEID_PRIVINO, ADEDOFF_PRIVINO, ADEDLEN_INIT},
147 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN, ADEDLEN_INIT},
148 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
149 {ADEID_RFORK, ADEDOFF_RFORK_V2, ADEDLEN_INIT},
153 /* Using Extended Attributes */
154 static const struct entry entry_order_ea[ADEID_NUM_EA + 1] = {
155 {ADEID_FINDERI, ADEDOFF_FINDERI_EA, ADEDLEN_FINDERI},
156 {ADEID_COMMENT, ADEDOFF_COMMENT_EA, ADEDLEN_INIT},
157 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_EA, ADEDLEN_FILEDATESI},
158 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_EA, ADEDLEN_AFPFILEI},
162 static uint32_t get_eid(uint32_t eid)
167 return ADEID_PRIVDEV;
169 return ADEID_PRIVINO;
171 return ADEID_PRIVSYN;
178 /* ----------------------------------- */
179 static int new_ad_header(const char *path, struct adouble *ad, int adflags)
181 const struct entry *eid;
185 ad->ad_magic = AD_MAGIC;
186 ad->ad_version = ad->ad_flags & 0x0f0000;
187 if (!ad->ad_version) {
188 ad->ad_version = AD_VERSION;
192 memset(ad->ad_data, 0, sizeof(ad->ad_data));
194 if (ad->ad_flags == AD_VERSION2)
196 else if (ad->ad_flags == AD_VERSION_EA)
197 eid = entry_order_ea;
203 ad->ad_eid[eid->id].ade_off = eid->offset;
204 ad->ad_eid[eid->id].ade_len = eid->len;
208 /* put something sane in the directory finderinfo */
209 if ((adflags & ADFLAGS_DIR)) {
210 /* set default view */
211 ashort = htons(FINDERINFO_CLOSEDVIEW);
212 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
214 /* set default creator/type fields */
215 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
216 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
219 /* make things invisible */
220 if ((ad->ad_options & ADVOL_INVDOTS) && (*path == '.')) {
221 ashort = htons(ATTRBIT_INVISIBLE);
222 ad_setattr(ad, ashort);
223 ashort = htons(FINDERINFO_INVISIBLE);
224 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
227 if (lstat(path, &st) < 0)
230 /* put something sane in the date fields */
231 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
232 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
233 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
234 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
238 /* -------------------------------------
241 static void parse_entries(struct adouble *ad, char *buf, uint16_t nentries)
243 uint32_t eid, len, off;
246 /* now, read in the entry bits */
247 for (; nentries > 0; nentries-- ) {
248 memcpy(&eid, buf, sizeof( eid ));
249 eid = get_eid(ntohl(eid));
250 buf += sizeof( eid );
251 memcpy(&off, buf, sizeof( off ));
253 buf += sizeof( off );
254 memcpy(&len, buf, sizeof( len ));
256 buf += sizeof( len );
258 if (eid && eid < ADEID_MAX && off < sizeof(ad->ad_data) &&
259 (off + len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
260 ad->ad_eid[ eid ].ade_off = off;
261 ad->ad_eid[ eid ].ade_len = len;
262 } else if (!warning) {
264 LOG(log_warning, logtype_default, "parse_entries: bogus eid: %d", eid);
269 /* this reads enough of the header so that we can figure out all of
270 * the entry lengths and offsets. once that's done, we just read/mmap
271 * the rest of the header in.
273 * NOTE: we're assuming that the resource fork is kept at the end of
274 * the file. also, mmapping won't work for the hfs fs until it
275 * understands how to mmap header files. */
276 static int ad_header_read(struct adouble *ad, struct stat *hst)
278 char *buf = ad->ad_data;
284 /* read the header */
285 if ((header_len = adf_pread( ad->ad_md, buf, sizeof(ad->ad_data), 0)) < 0) {
288 if (header_len < AD_HEADER_LEN) {
293 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
294 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
295 ad->ad_magic = ntohl( ad->ad_magic );
296 ad->ad_version = ntohl( ad->ad_version );
298 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
299 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
304 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
305 nentries = ntohs( nentries );
307 /* read in all the entry headers. if we have more than the
308 * maximum, just hope that the rfork is specified early on. */
309 len = nentries*AD_ENTRY_LEN;
311 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
312 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
314 buf += AD_HEADER_LEN;
315 if (len > header_len - AD_HEADER_LEN) {
316 LOG(log_error, logtype_default, "ad_header_read: can't read entry info.");
321 /* figure out all of the entry offsets and lengths. if we aren't
322 * able to read a resource fork entry, bail. */
323 nentries = len / AD_ENTRY_LEN;
324 parse_entries(ad, buf, nentries);
325 if (!ad_getentryoff(ad, ADEID_RFORK)
326 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
328 LOG(log_error, logtype_default, "ad_header_read: problem with rfork entry offset.");
333 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
334 LOG(log_error, logtype_default, "ad_header_read: can't read in entries.");
341 if (fstat(ad->ad_md->adf_fd, &st) < 0) {
342 return 1; /* fail silently */
346 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
351 static int ad_header_read_ea(struct adouble *ad, struct stat *hst _U_)
356 char *buf = ad->ad_data;
358 /* read the header */
359 if ((header_len = sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_META, ad->ad_data, AD_DATASZ_EA)) < 1) {
360 LOG(log_debug, logtype_default, "ad_header_read_ea: %s (%u)", strerror(errno), errno);
364 if (header_len < AD_HEADER_LEN) {
365 LOG(log_error, logtype_default, "ad_header_read_ea: bogus AppleDouble header.");
370 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
371 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
373 ad->ad_magic = ntohl( ad->ad_magic );
374 ad->ad_version = ntohl( ad->ad_version );
376 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
377 LOG(log_error, logtype_default, "ad_header_read_ea: wrong magic or version");
382 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
383 nentries = ntohs( nentries );
385 /* Protect against bogus nentries */
386 len = nentries * AD_ENTRY_LEN;
387 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
388 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
389 if (len > header_len - AD_HEADER_LEN) {
390 LOG(log_error, logtype_default, "ad_header_read_ea: can't read entry info.");
394 nentries = len / AD_ENTRY_LEN;
396 /* Now parse entries */
397 parse_entries(ad, buf + AD_HEADER_LEN, nentries);
401 static int ad_mkrf(const char *path)
405 * Probably .AppleDouble doesn't exist, try to mkdir it.
407 if (NULL == ( slash = strrchr( path, '/' )) ) {
412 if ( ad_mkdir( path, 0777 ) < 0 ) {
419 static int ad_mkrf_ea(const char *path _U_)
421 AFP_PANIC("ad_mkrf_ea: dont use");
426 if we are root change path user/ group
427 It can be a native function for BSD cf. FAQ.Q10
428 path: pathname to chown
429 stbuf: parent directory inode
431 use fstat and fchown or lchown with linux?
433 #define EMULATE_SUIDDIR
435 static int ad_chown(const char *path, struct stat *stbuf)
438 #ifdef EMULATE_SUIDDIR
441 if (default_uid != (uid_t)-1) {
442 /* we are root (admin) */
443 id = (default_uid)?default_uid:stbuf->st_uid;
444 ret = lchown( path, id, stbuf->st_gid );
450 #define DEFMASK 07700 /* be conservative */
453 return access right and inode of path parent directory
455 static int ad_mode_st(const char *path, int *mode, struct stat *stbuf)
460 if (ad_stat(path, stbuf) != 0) {
464 *mode &= stbuf->st_mode;
468 /* --------------------------- */
469 static int ad_header_upgrade(struct adouble *ad _U_, const char *name _U_)
474 static int ad_header_upgrade_ea(struct adouble *ad _U_, const char *name _U_)
476 AFP_PANIC("ad_header_upgrade_ea: dont use");
481 * Error handling for adouble header(=metadata) file open error
483 * We're called because opening ADFLAGS_HF caused an error.
484 * 1. In case ad_open is called with ADFLAGS_NOHF the error is suppressed.
485 * 2. If ad_open was called with ADFLAGS_DF we may have opened the datafork and thus
486 * ought to close it before returning with an error condition.
488 static int ad_error(struct adouble *ad, int adflags)
491 if ((adflags & ADFLAGS_NOHF)) { /* 1 */
492 /* FIXME double check : set header offset ?*/
495 if ((adflags & ADFLAGS_DF)) { /* 2 */
496 ad_close( ad, ADFLAGS_DF );
502 static int ad_open_df(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
508 LOG(log_debug, logtype_default, "ad_open_df(\"%s\", %s, %04o)",
509 abspath(path), oflags2logstr(oflags), mode);
511 if (ad_data_fileno(ad) == -1) {
512 hoflags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
514 if ((oflags & O_CREAT)) {
515 st_invalid = ad_mode_st(path, &admode, &st_dir);
516 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
521 ad->ad_data_fork.adf_fd = open(path, hoflags | O_NOFOLLOW, admode);
523 if (ad->ad_data_fork.adf_fd == -1) {
524 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
526 ad->ad_data_fork.adf_fd = open( path, hoflags | O_NOFOLLOW, admode );
528 if (ad->ad_data_fork.adf_fd == -1 && errno == OPEN_NOFOLLOW_ERRNO) {
531 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
532 lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN);
534 free(ad->ad_data_fork.adf_syml);
537 ad->ad_data_fork.adf_syml[lsz] = 0;
538 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
542 if ( ad->ad_data_fork.adf_fd == -1 )
545 AD_SET(ad->ad_data_fork.adf_off);
546 ad->ad_data_fork.adf_flags = hoflags;
548 /* just created, set owner if admin (root) */
549 ad_chown(path, &st_dir);
552 /* the file is already open... but */
553 if ((oflags & ( O_RDWR | O_WRONLY))
554 /* we want write access */
555 && !(ad->ad_data_fork.adf_flags & ( O_RDWR | O_WRONLY))) {
556 /* and it was denied the first time */
561 * for now ad_open is never called with O_TRUNC or O_EXCL if the file is
562 * already open. Should we check for it? ie
564 * O_TRUNC --> truncate the fork.
565 * idem for ressource fork.
572 static int ad_open_hf_v2(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
576 struct stat *pst = NULL;
581 ad_p = ad->ad_ops->ad_path( path, adflags );
583 hoflags = (oflags & ~(O_CREAT | O_EXCL)) | O_NOFOLLOW;
585 ad->ad_md->adf_fd = open(ad_p, hoflags, 0);
587 if ( ad->ad_md->adf_fd < 0 ) {
588 if (errno == ENOENT && (oflags & O_CREAT) ) {
590 * We're expecting to create a new adouble header file here
591 * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
593 LOG(log_debug, logtype_default, "ad_open(\"%s\"): creating adouble file",
597 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
598 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
601 admode = ad_hf_mode(admode);
602 if ((errno == ENOENT)) {
603 if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
604 return ad_error(ad, adflags);
607 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
608 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
611 admode = ad_hf_mode(admode);
613 /* retry with O_CREAT */
614 ad->ad_md->adf_fd = open(ad_p, oflags, admode);
615 if ( ad->ad_md->adf_fd < 0 ) {
616 return ad_error(ad, adflags);
618 ad->ad_md->adf_flags = oflags;
619 /* just created, set owner if admin owner (root) */
621 ad_chown(ad_p, &st_dir);
624 return ad_error(ad, adflags);
627 ad->ad_md->adf_flags = hoflags;
628 if (fstat(ad->ad_md->adf_fd, &st_meta) == 0 && st_meta.st_size == 0) {
629 /* for 0 length files, treat them as new. */
630 ad->ad_md->adf_flags |= O_TRUNC;
632 /* we have valid data in st_meta stat structure, reused it in ad_header_read */
637 AD_SET(ad->ad_md->adf_off);
639 if ((ad->ad_md->adf_flags & ( O_TRUNC | O_CREAT ))) {
640 /* This is a new adouble header file, create it */
641 if (new_ad_header(path, ad, adflags) < 0) {
643 /* the file is already deleted, perm, whatever, so return an error */
649 /* Read the adouble header in and parse it.*/
650 if (ad->ad_ops->ad_header_read( ad , pst) < 0
651 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0) {
661 static int ad_open_hf_ea(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
665 /* we dont use this fd, but open it anyway, maybe we need it sometimes */
666 if ((ad->ad_md->adf_fd = open(path, O_RDONLY | O_NOFOLLOW)) == -1)
669 /* Read the adouble header in and parse it.*/
670 if (ad->ad_ops->ad_header_read(ad, NULL) != 0) {
671 if (!(oflags & O_CREAT))
674 /* It doesnt exist, EPERM or another error */
675 if (errno != ENOATTR && errno != ENOENT) {
676 LOG(log_error, logtype_default, "ad_open_hf_ea: unexpected: %s", strerror(errno));
681 if (new_ad_header(path, ad, adflags) < 0) {
682 LOG(log_error, logtype_default, "ad_open_hf_ea: can't create new header: %s",
686 ad->ad_md->adf_flags |= O_CREAT; /* mark as just created */
688 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): created metadata EA",
689 cfrombstr(ad->ad_fullpath));
692 ad->ad_md->adf_flags |= oflags & (O_RDONLY|O_WRONLY|O_RDWR); /* store current flags */
694 if ((rforklen = sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, NULL, 0)) > 0)
695 ad->ad_rlen = rforklen;
700 if (ad->ad_md->adf_fd != -1) {
701 close(ad->ad_md->adf_fd);
702 ad->ad_md->adf_fd = -1;
704 return ad_error(ad, adflags);
707 static int ad_open_hf(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
711 LOG(log_debug, logtype_default, "ad_open_hf(\"%s\", %s, %04o)",
712 cfrombstr(ad->ad_fullpath), oflags2logstr(oflags), mode);
714 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
717 switch (ad->ad_flags) {
719 ret = ad_open_hf_v2(path, adflags, oflags, mode, ad);
722 ret = ad_open_hf_ea(path, adflags, oflags, mode, ad);
733 * Open ressource fork
735 * Only for adouble:ea, a nullop otherwise because adouble:v2 has the ressource fork as part
736 * of the adouble file which is openend by ADFLAGS_HF.
738 static int ad_open_rf(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
742 if (ad->ad_flags != AD_VERSION_EA)
745 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\", %s, %04o)",
746 cfrombstr(ad->ad_fullpath), oflags2logstr(oflags), mode);
748 if ((ad->ad_rlen = sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, NULL, 0)) <= 0) {
754 LOG(log_warning, logtype_default, "ad_open_rf(\"%s\"): %s",
755 abspath(path), strerror(errno));
761 /* Round up and allocate buffer */
762 size_t roundup = ((ad->ad_rlen / RFORK_EA_ALLOCSIZE) + 1) * RFORK_EA_ALLOCSIZE;
763 if ((ad->ad_resforkbuf = malloc(roundup)) == NULL) {
768 ad->ad_resforkbufsize = roundup;
770 /* Read the EA into the buffer */
771 if (ad->ad_rlen > 0) {
772 if (sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, ad->ad_resforkbuf, ad->ad_rlen) == -1) {
780 free(ad->ad_resforkbuf);
781 ad->ad_resforkbuf = NULL;
783 ad->ad_resforkbufsize = 0;
789 /***********************************************************************************
791 ********************************************************************************* */
793 const char *ad_path_ea( const char *path, int adflags _U_)
799 * Put the .AppleDouble where it needs to be:
803 * \ b/.AppleDouble/.Parent
805 * FIXME: should do something for pathname > MAXPATHLEN
807 const char *ad_path( const char *path, int adflags)
809 static char pathbuf[ MAXPATHLEN + 1];
813 if ( adflags & ADFLAGS_DIR ) {
814 l = strlcpy( pathbuf, path, sizeof(pathbuf));
816 if ( l && l < MAXPATHLEN) {
819 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
821 if (NULL != ( slash = strrchr( path, '/' )) ) {
824 /* XXX we must return NULL here and test in the caller */
827 memcpy( pathbuf, path, l);
832 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
833 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
839 /* -------------------------
840 * Support inherited protection modes for AppleDouble files. The supplied
841 * mode is ANDed with the parent directory's mask value in lieu of "umask",
842 * and that value is returned.
844 char *ad_dir(const char *path)
846 static char modebuf[ MAXPATHLEN + 1];
849 * For a path with directories in it, remove the final component
850 * (path or subdirectory name) to get the name we want to stat.
851 * For a path which is just a filename, use "." instead.
853 slash = strrchr( path, '/' );
858 if (len >= MAXPATHLEN) {
859 errno = ENAMETOOLONG;
860 return NULL; /* can't do it */
862 memcpy( modebuf, path, len );
864 /* is last char a '/' ? */
866 slash = modebuf+ len;
868 while (modebuf < slash && slash[-1] == '/') {
871 if (modebuf == slash) {
875 while (modebuf < slash && *slash != '/') {
878 if (modebuf == slash) {
881 *slash = '\0'; /* remove pathname component */
886 modebuf[0] = '.'; /* use current directory */
891 int ad_setfuid(const uid_t id)
897 /* ---------------- */
898 uid_t ad_getfuid(void)
904 stat path parent directory
906 int ad_stat(const char *path, struct stat *stbuf)
913 return lstat( p, stbuf );
917 return access right of path parent directory
919 int ad_mode( const char *path, int mode)
922 ad_mode_st(path, &mode, &stbuf);
927 * Use mkdir() with mode bits taken from ad_mode().
929 int ad_mkdir( const char *path, int mode)
935 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
936 path, mode, getcwdpath());
938 st_invalid = ad_mode_st(path, &mode, &stbuf);
939 ret = mkdir( path, mode );
940 if (ret || st_invalid)
942 ad_chown(path, &stbuf);
947 void ad_init(struct adouble *ad, int flags, int options)
949 memset(ad, 0, sizeof(struct adouble));
951 if (flags == AD_VERSION2) {
952 ad->ad_ops = &ad_adouble;
953 ad->ad_md = &ad->ad_resource_fork;
955 else if (flags == AD_VERSION_EA) {
956 ad->ad_ops = &ad_adouble_ea;
957 ad->ad_md = &ad->ad_metadata_fork;
959 LOG(log_error, logtype_default, "ad_init: unknown AD version");
964 ad->ad_flags = flags;
965 ad->ad_options = options;
966 ad_data_fileno(ad) = -1;
967 ad_reso_fileno(ad) = -1;
968 ad_meta_fileno(ad) = -1;
969 ad->ad_inited = AD_INITED;
972 const char *adflags2logstr(int adflags)
979 if (adflags & ADFLAGS_DF) {
980 strlcat(buf, "DF", 64);
983 if (adflags & ADFLAGS_RF) {
985 strlcat(buf, "|", 64);
986 strlcat(buf, "RF", 64);
989 if (adflags & ADFLAGS_HF) {
991 strlcat(buf, "|", 64);
992 strlcat(buf, "HF", 64);
995 if (adflags & ADFLAGS_NOHF) {
997 strlcat(buf, "|", 64);
998 strlcat(buf, "NOHF", 64);
1001 if (adflags & ADFLAGS_DIR) {
1003 strlcat(buf, "|", 64);
1004 strlcat(buf, "DIR", 64);
1007 if (adflags & ADFLAGS_CHECK_OF) {
1009 strlcat(buf, "|", 64);
1010 strlcat(buf, "OF", 64);
1016 const char *oflags2logstr(int oflags)
1019 static char buf[64];
1023 if (oflags == O_RDONLY) {
1024 strlcat(buf, "O_RDONLY", 64);
1027 if (oflags & O_RDWR) {
1029 strlcat(buf, "|", 64);
1030 strlcat(buf, "O_RDWR", 64);
1033 if (oflags & O_CREAT) {
1035 strlcat(buf, "|", 64);
1036 strlcat(buf, "O_CREAT", 64);
1039 if (oflags & O_EXCL) {
1041 strlcat(buf, "|", 64);
1042 strlcat(buf, "O_EXCL", 64);
1049 * Open data-, metadata(header)- or ressource fork
1051 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
1054 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1057 * Open a files data fork, metadata fork or ressource fork.
1059 * For each fork to be opened specify the open flags and mode in case you want to create it
1060 * (O_CREAT in open flags). The order in which forks are opened is:
1061 * 1) ADFLAGS_DF, 2) ADFLAGS_HF, 3) ADFLAGS_RF.
1062 * It is possible to call ad_open subsequently opening ADFLAGS_DF first and ADFLAGS_HF in
1063 * another ad_open call. Already openend forks are uneffected in subsequent calls (3).
1064 * The variable arguments must be passed according to this order. Then:
1065 * 1. Ensure the adouble struct has been initialized.
1066 * 2. Store the full path to the object the first time ad_open is called for it.
1067 * 3. Check if the fork has already been opened.
1069 * ad_close accompanies ad_open and closes a struct adouble. In order to tigh together the
1070 * semantics of struct adouble/ad_open/ad_close and versus forks, which use and keep a ref to
1071 * the according struct adouble for the fork, open forks refcount their struct adouble
1072 * in struct adouble.ad_refcount. An ad_close is ignored when ad_refcount != 0, because
1073 * ad_refcount != 0 means an open fork is using this very struct adouble.
1075 * @param ad (rw) pointer to struct adouble
1076 * @param path (r) Path to file or directory
1077 * @param adflags (r) ADFLAGS_DF: open data fork \n
1078 * ADFLAGS_RF: open ressource fork \n
1079 * ADFLAGS_HF: open header (metadata) file \n
1080 * ADFLAGS_NOHF: it's not an error if header file couldn't be created \n
1081 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags \n
1082 * ADFLAGS_RDONLY: dont upgrade mode from r to rw with adouble:v2 headerfile \n
1083 * ADFLAGS_CHECK_OF: check for open forks from us and other afpd's
1085 * @returns 0 on success, any other value indicates an error
1087 static int vad_open(struct adouble *ad, const char *path, int adflags, va_list args)
1093 LOG(log_debug, logtype_default, "ad_open(\"%s\", %s)",
1094 abspath(path), adflags2logstr(adflags));
1096 if (ad->ad_inited != AD_INITED) /* 1 */
1097 AFP_PANIC("ad_open: not initialized");
1099 if (ad->ad_fullpath == NULL) { /* 2 */
1100 if ((ad->ad_fullpath = bfromcstr(abspath(path))) == NULL) {
1106 if ((adflags & ADFLAGS_DF) && !(ad->ad_adflags & ADFLAGS_DF)) { /* 3 */
1107 oflags = va_arg(args, int);
1108 if (oflags & O_CREAT)
1109 mode = va_arg(args, int);
1110 if (ad_open_df(path, adflags, oflags, mode, ad) != 0) {
1114 ad->ad_adflags |= ADFLAGS_DF;
1117 if ((adflags & ADFLAGS_HF) && !(ad->ad_adflags & ADFLAGS_HF)) { /* 3 */
1118 oflags = va_arg(args, int);
1119 if (oflags & O_CREAT)
1120 mode = va_arg(args, int);
1121 if (ad_open_hf(path, adflags, oflags, mode, ad) != 0) {
1125 ad->ad_adflags |= ADFLAGS_HF;
1128 if ((adflags & ADFLAGS_RF) && !(ad->ad_adflags & ADFLAGS_RF)) { /* 3 */
1129 oflags = va_arg(args, int);
1130 if (oflags & O_CREAT)
1131 mode = va_arg(args, int);
1132 if (ad_open_rf(path, adflags, oflags, mode, ad) != 0) {
1136 ad->ad_adflags |= ADFLAGS_RF;
1141 /* FIXME: ad_close stuff we opened before hitting an error */
1142 /* Dont forget ADFLAGS_NOHF !!! */
1144 if (ad->ad_fullpath) {
1145 bdestroy(ad->ad_fullpath);
1146 ad->ad_fullpath = NULL;
1153 int ad_open(struct adouble *ad, const char *path, int adflags, ...)
1158 va_start(args, adflags);
1159 ret = vad_open(ad, path, adflags, args);
1165 * @brief open metadata, possibly as root
1167 * Return only metadata but try very hard ie at first try as user, then try as root.
1169 * @param name name of file/dir
1170 * @param flags ADFLAGS_DIR: name is a directory \n
1171 * ADFLAGS_CHECK_OF: test if name is open by us or another afpd process
1173 * @param adp pointer to struct adouble
1175 int ad_metadata(const char *name, int flags, struct adouble *adp)
1180 dir = flags & ADFLAGS_DIR;
1182 if ((ret = ad_open(adp, name, ADFLAGS_HF | dir, O_RDONLY)) < 0 && errno == EACCES) {
1185 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1189 /* we are root open read only */
1190 ret = ad_open(adp, name, ADFLAGS_HF | dir, O_RDONLY);
1192 if ( seteuid(uid) < 0) {
1193 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1199 if (!ret && (ADFLAGS_CHECK_OF & flags)) {
1201 we need to check if the file is open by another process.
1202 it's slow so we only do it if we have to:
1204 - we don't already have the answer!
1206 adp->ad_open_forks |= ad_openforks(adp, adp->ad_open_forks);
1212 * @brief openat like wrapper for ad_metadata
1214 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1220 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1226 if (ad_metadata(name, flags, adp) < 0) {
1232 if (fchdir(cwdfd) != 0) {
1233 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1246 int ad_refresh(struct adouble *ad)
1249 if (ad_meta_fileno(ad) == -1)
1252 return ad->ad_ops->ad_header_read(ad, NULL);
1255 int ad_openat(struct adouble *ad,
1256 int dirfd, /* dir fd openat like */
1265 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1271 va_start(args, adflags);
1273 if (vad_open(ad, path, adflags, args) < 0) {
1281 if (fchdir(cwdfd) != 0) {
1282 AFP_PANIC("ad_openat: cant chdir back");