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 * @sa include/atalk/adouble.h
36 #endif /* HAVE_CONFIG_H */
39 #include <sys/param.h>
43 #include <arpa/inet.h>
45 #include <atalk/logger.h>
46 #include <atalk/adouble.h>
47 #include <atalk/util.h>
49 #include <atalk/bstrlib.h>
50 #include <atalk/bstradd.h>
51 #include <atalk/compat.h>
52 #include <atalk/errchk.h>
57 #define MAX(a, b) ((a) < (b) ? (b) : (a))
60 #define ADEDOFF_MAGIC (0)
61 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
62 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
63 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
65 /* initial lengths of some of the fields */
66 #define ADEDLEN_INIT 0
68 /* i stick things in a slightly different order than their eid order in
69 * case i ever want to separate RootInfo behaviour from the rest of the
73 #define ADEDOFF_NAME_V2 (AD_HEADER_LEN + ADEID_NUM_V2*AD_ENTRY_LEN)
74 #define ADEDOFF_COMMENT_V2 (ADEDOFF_NAME_V2 + ADEDLEN_NAME)
75 #define ADEDOFF_FILEDATESI (ADEDOFF_COMMENT_V2 + ADEDLEN_COMMENT)
76 #define ADEDOFF_FINDERI_V2 (ADEDOFF_FILEDATESI + ADEDLEN_FILEDATESI)
77 #define ADEDOFF_DID (ADEDOFF_FINDERI_V2 + ADEDLEN_FINDERI)
78 #define ADEDOFF_AFPFILEI (ADEDOFF_DID + ADEDLEN_DID)
79 #define ADEDOFF_SHORTNAME (ADEDOFF_AFPFILEI + ADEDLEN_AFPFILEI)
80 #define ADEDOFF_PRODOSFILEI (ADEDOFF_SHORTNAME + ADEDLEN_SHORTNAME)
81 #define ADEDOFF_PRIVDEV (ADEDOFF_PRODOSFILEI + ADEDLEN_PRODOSFILEI)
82 #define ADEDOFF_PRIVINO (ADEDOFF_PRIVDEV + ADEDLEN_PRIVDEV)
83 #define ADEDOFF_PRIVSYN (ADEDOFF_PRIVINO + ADEDLEN_PRIVINO)
84 #define ADEDOFF_PRIVID (ADEDOFF_PRIVSYN + ADEDLEN_PRIVSYN)
85 #define ADEDOFF_RFORK_V2 (ADEDOFF_PRIVID + ADEDLEN_PRIVID)
88 #define ADEDOFF_FINDERI_EA (AD_HEADER_LEN + ADEID_NUM_EA * AD_ENTRY_LEN)
89 #define ADEDOFF_COMMENT_EA (ADEDOFF_FINDERI_EA + ADEDLEN_FINDERI)
90 #define ADEDOFF_FILEDATESI_EA (ADEDOFF_COMMENT_EA + ADEDLEN_COMMENT)
91 #define ADEDOFF_AFPFILEI_EA (ADEDOFF_FILEDATESI_EA + ADEDLEN_FILEDATESI)
93 /* this is to prevent changing timezones from causing problems with
94 localtime volumes. the screw-up is 30 years. we use a delta of 5 years */
95 #define TIMEWARP_DELTA 157680000
98 uint32_t id, offset, len;
101 /* --------------------------- */
102 static uid_t default_uid = -1;
104 /* Forward declarations */
105 static int ad_mkrf(const char *path);
106 static int ad_header_read(struct adouble *ad, struct stat *hst);
107 static int ad_header_upgrade(struct adouble *ad, const char *name);
109 static int ad_mkrf_ea(const char *path);
110 static int ad_header_read_ea(struct adouble *ad, struct stat *hst);
111 static int ad_header_upgrade_ea(struct adouble *ad, const char *name);
113 static struct adouble_fops ad_adouble = {
116 &ad_rebuild_adouble_header,
121 static struct adouble_fops ad_adouble_ea = {
124 &ad_rebuild_adouble_header,
126 &ad_header_upgrade_ea,
129 static const struct entry entry_order2[ADEID_NUM_V2 + 1] = {
130 {ADEID_NAME, ADEDOFF_NAME_V2, ADEDLEN_INIT},
131 {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
132 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
133 {ADEID_FINDERI, ADEDOFF_FINDERI_V2, ADEDLEN_FINDERI},
134 {ADEID_DID, ADEDOFF_DID, ADEDLEN_DID},
135 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
136 {ADEID_SHORTNAME, ADEDOFF_SHORTNAME, ADEDLEN_INIT},
137 {ADEID_PRODOSFILEI, ADEDOFF_PRODOSFILEI, ADEDLEN_PRODOSFILEI},
138 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV, ADEDLEN_INIT},
139 {ADEID_PRIVINO, ADEDOFF_PRIVINO, ADEDLEN_INIT},
140 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN, ADEDLEN_INIT},
141 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
142 {ADEID_RFORK, ADEDOFF_RFORK_V2, ADEDLEN_INIT},
146 /* Using Extended Attributes */
147 static const struct entry entry_order_ea[ADEID_NUM_EA + 1] = {
148 {ADEID_FINDERI, ADEDOFF_FINDERI_EA, ADEDLEN_FINDERI},
149 {ADEID_COMMENT, ADEDOFF_COMMENT_EA, ADEDLEN_INIT},
150 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_EA, ADEDLEN_FILEDATESI},
151 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_EA, ADEDLEN_AFPFILEI},
155 #define ADFLAGS2LOGSTRBUFSIZ 128
156 const char *adflags2logstr(int adflags)
159 static char buf[ADFLAGS2LOGSTRBUFSIZ];
163 if (adflags & ADFLAGS_DF) {
164 strlcat(buf, "DF", ADFLAGS2LOGSTRBUFSIZ);
167 if (adflags & ADFLAGS_RF) {
169 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
170 strlcat(buf, "RF", ADFLAGS2LOGSTRBUFSIZ);
173 if (adflags & ADFLAGS_HF) {
175 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
176 strlcat(buf, "HF", ADFLAGS2LOGSTRBUFSIZ);
179 if (adflags & ADFLAGS_NOHF) {
181 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
182 strlcat(buf, "NOHF", ADFLAGS2LOGSTRBUFSIZ);
185 if (adflags & ADFLAGS_DIR) {
187 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
188 strlcat(buf, "DIR", ADFLAGS2LOGSTRBUFSIZ);
191 if (adflags & ADFLAGS_CHECK_OF) {
193 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
194 strlcat(buf, "OF", ADFLAGS2LOGSTRBUFSIZ);
197 if (adflags & ADFLAGS_RDWR) {
199 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
200 strlcat(buf, "O_RDWR", ADFLAGS2LOGSTRBUFSIZ);
203 if (adflags & ADFLAGS_RDONLY) {
205 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
206 strlcat(buf, "O_RDONLY", ADFLAGS2LOGSTRBUFSIZ);
209 if (adflags & ADFLAGS_CREATE) {
211 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
212 strlcat(buf, "O_CREAT", ADFLAGS2LOGSTRBUFSIZ);
215 if (adflags & ADFLAGS_EXCL) {
217 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
218 strlcat(buf, "O_EXCL", ADFLAGS2LOGSTRBUFSIZ);
221 if (adflags & ADFLAGS_TRUNC) {
223 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
224 strlcat(buf, "O_TRUNC", ADFLAGS2LOGSTRBUFSIZ);
231 static uint32_t get_eid(uint32_t eid)
236 return ADEID_PRIVDEV;
238 return ADEID_PRIVINO;
240 return ADEID_PRIVSYN;
247 /* ----------------------------------- */
248 static int new_ad_header(const char *path, struct adouble *ad, int adflags)
250 const struct entry *eid;
254 ad->ad_magic = AD_MAGIC;
255 ad->ad_version = ad->ad_flags & 0x0f0000;
256 if (!ad->ad_version) {
257 ad->ad_version = AD_VERSION;
261 memset(ad->ad_data, 0, sizeof(ad->ad_data));
263 if (ad->ad_flags == AD_VERSION2)
265 else if (ad->ad_flags == AD_VERSION_EA)
266 eid = entry_order_ea;
272 ad->ad_eid[eid->id].ade_off = eid->offset;
273 ad->ad_eid[eid->id].ade_len = eid->len;
277 /* put something sane in the directory finderinfo */
278 if ((adflags & ADFLAGS_DIR)) {
279 /* set default view */
280 ashort = htons(FINDERINFO_CLOSEDVIEW);
281 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
283 /* set default creator/type fields */
284 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
285 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
288 /* make things invisible */
289 if ((ad->ad_options & ADVOL_INVDOTS) && (*path == '.')) {
290 ashort = htons(ATTRBIT_INVISIBLE);
291 ad_setattr(ad, ashort);
292 ashort = htons(FINDERINFO_INVISIBLE);
293 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
296 if (lstat(path, &st) < 0)
299 /* put something sane in the date fields */
300 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
301 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
302 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
303 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
307 /* -------------------------------------
310 static void parse_entries(struct adouble *ad, char *buf, uint16_t nentries)
312 uint32_t eid, len, off;
315 /* now, read in the entry bits */
316 for (; nentries > 0; nentries-- ) {
317 memcpy(&eid, buf, sizeof( eid ));
318 eid = get_eid(ntohl(eid));
319 buf += sizeof( eid );
320 memcpy(&off, buf, sizeof( off ));
322 buf += sizeof( off );
323 memcpy(&len, buf, sizeof( len ));
325 buf += sizeof( len );
327 if (eid && eid < ADEID_MAX && off < sizeof(ad->ad_data) &&
328 (off + len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
329 ad->ad_eid[ eid ].ade_off = off;
330 ad->ad_eid[ eid ].ade_len = len;
331 } else if (!warning) {
333 LOG(log_warning, logtype_default, "parse_entries: bogus eid: %d", eid);
338 /* this reads enough of the header so that we can figure out all of
339 * the entry lengths and offsets. once that's done, we just read/mmap
340 * the rest of the header in.
342 * NOTE: we're assuming that the resource fork is kept at the end of
343 * the file. also, mmapping won't work for the hfs fs until it
344 * understands how to mmap header files. */
345 static int ad_header_read(struct adouble *ad, struct stat *hst)
347 char *buf = ad->ad_data;
353 /* read the header */
354 if ((header_len = adf_pread( ad->ad_mdp, buf, sizeof(ad->ad_data), 0)) < 0) {
357 if (header_len < AD_HEADER_LEN) {
362 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
363 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
364 ad->ad_magic = ntohl( ad->ad_magic );
365 ad->ad_version = ntohl( ad->ad_version );
367 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
368 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
373 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
374 nentries = ntohs( nentries );
376 /* read in all the entry headers. if we have more than the
377 * maximum, just hope that the rfork is specified early on. */
378 len = nentries*AD_ENTRY_LEN;
380 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
381 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
383 buf += AD_HEADER_LEN;
384 if (len > header_len - AD_HEADER_LEN) {
385 LOG(log_error, logtype_default, "ad_header_read: can't read entry info.");
390 /* figure out all of the entry offsets and lengths. if we aren't
391 * able to read a resource fork entry, bail. */
392 nentries = len / AD_ENTRY_LEN;
393 parse_entries(ad, buf, nentries);
394 if (!ad_getentryoff(ad, ADEID_RFORK)
395 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
397 LOG(log_error, logtype_default, "ad_header_read: problem with rfork entry offset.");
402 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
403 LOG(log_error, logtype_default, "ad_header_read: can't read in entries.");
410 if (fstat(ad->ad_mdp->adf_fd, &st) < 0) {
411 return 1; /* fail silently */
415 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
420 static int ad_header_read_ea(struct adouble *ad, struct stat *hst _U_)
425 char *buf = ad->ad_data;
427 /* read the header */
428 if ((header_len = sys_fgetxattr(ad_data_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA)) < 1) {
429 LOG(log_debug, logtype_default, "ad_header_read_ea: %s (%u)", strerror(errno), errno);
433 if (header_len < AD_HEADER_LEN) {
434 LOG(log_error, logtype_default, "ad_header_read_ea: bogus AppleDouble header.");
439 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
440 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
442 ad->ad_magic = ntohl( ad->ad_magic );
443 ad->ad_version = ntohl( ad->ad_version );
445 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
446 LOG(log_error, logtype_default, "ad_header_read_ea: wrong magic or version");
451 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
452 nentries = ntohs( nentries );
454 /* Protect against bogus nentries */
455 len = nentries * AD_ENTRY_LEN;
456 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
457 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
458 if (len > header_len - AD_HEADER_LEN) {
459 LOG(log_error, logtype_default, "ad_header_read_ea: can't read entry info.");
463 nentries = len / AD_ENTRY_LEN;
465 /* Now parse entries */
466 parse_entries(ad, buf + AD_HEADER_LEN, nentries);
470 static int ad_mkrf(const char *path)
474 * Probably .AppleDouble doesn't exist, try to mkdir it.
476 if (NULL == ( slash = strrchr( path, '/' )) ) {
481 if ( ad_mkdir( path, 0777 ) < 0 ) {
488 static int ad_mkrf_ea(const char *path _U_)
490 AFP_PANIC("ad_mkrf_ea: dont use");
495 if we are root change path user/ group
496 It can be a native function for BSD cf. FAQ.Q10
497 path: pathname to chown
498 stbuf: parent directory inode
500 use fstat and fchown or lchown with linux?
502 #define EMULATE_SUIDDIR
504 static int ad_chown(const char *path, struct stat *stbuf)
507 #ifdef EMULATE_SUIDDIR
510 if (default_uid != (uid_t)-1) {
511 /* we are root (admin) */
512 id = (default_uid)?default_uid:stbuf->st_uid;
513 ret = lchown( path, id, stbuf->st_gid );
519 #define DEFMASK 07700 /* be conservative */
522 return access right and inode of path parent directory
524 static int ad_mode_st(const char *path, int *mode, struct stat *stbuf)
529 if (ad_stat(path, stbuf) != 0) {
533 *mode &= stbuf->st_mode;
537 /* --------------------------- */
538 static int ad_header_upgrade(struct adouble *ad _U_, const char *name _U_)
543 static int ad_header_upgrade_ea(struct adouble *ad _U_, const char *name _U_)
545 AFP_PANIC("ad_header_upgrade_ea: dont use");
550 * Error handling for adouble header(=metadata) file open error
552 * We're called because opening ADFLAGS_HF caused an error.
553 * 1. In case ad_open is called with ADFLAGS_NOHF the error is suppressed.
554 * 2. If ad_open was called with ADFLAGS_DF we may have opened the datafork and thus
555 * ought to close it before returning with an error condition.
557 static int ad_error(struct adouble *ad, int adflags)
560 if ((adflags & ADFLAGS_NOHF)) { /* 1 */
561 /* FIXME double check : set header offset ?*/
564 if ((adflags & ADFLAGS_DF)) { /* 2 */
565 ad_close( ad, ADFLAGS_DF );
571 /* Map ADFLAGS to open() flags */
572 static int ad2openflags(int adflags)
576 if (adflags & ADFLAGS_RDWR)
578 if ((adflags & ADFLAGS_RDONLY) && (adflags & ADFLAGS_SETSHRMD))
582 if (adflags & ADFLAGS_CREATE)
584 if (adflags & ADFLAGS_EXCL)
586 if (adflags & ADFLAGS_TRUNC)
592 static int ad_open_df(const char *path, int adflags, mode_t mode, struct adouble *ad)
600 LOG(log_debug, logtype_default, "ad_open_df(\"%s\", %04o)",
601 fullpathname(path), mode);
603 if (ad_data_fileno(ad) != -1) {
604 /* the file is already open, but we want write access: */
605 if ((adflags & ADFLAGS_RDWR)
606 /* and it was denied the first time: */
607 && (ad->ad_data_fork.adf_flags & O_RDONLY)) {
611 /* it's not new anymore */
612 ad->ad_mdp->adf_flags &= ~( O_TRUNC | O_CREAT );
613 ad->ad_data_fork.adf_refcount++;
617 oflags = O_NOFOLLOW | ad2openflags(adflags);
620 if ((adflags & ADFLAGS_CREATE)) {
621 st_invalid = ad_mode_st(path, &admode, &st_dir);
622 if ((ad->ad_options & ADVOL_UNIXPRIV))
626 ad->ad_data_fork.adf_fd = open(path, oflags, admode);
628 if (ad->ad_data_fork.adf_fd == -1) {
633 if ((adflags & ADFLAGS_SETSHRMD) && (adflags & ADFLAGS_RDONLY)) {
636 if ((ad->ad_data_fork.adf_fd = open(path, oflags, admode)) == -1)
641 case OPEN_NOFOLLOW_ERRNO:
642 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
643 if ((lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN)) <= 0) {
644 free(ad->ad_data_fork.adf_syml);
647 ad->ad_data_fork.adf_syml[lsz] = 0;
648 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
656 ad_chown(path, &st_dir); /* just created, set owner if admin (root) */
658 ad->ad_data_fork.adf_flags = oflags;
659 adf_lock_init(&ad->ad_data_fork);
660 ad->ad_data_fork.adf_refcount++;
665 static int ad_open_hf_v2(const char *path, int adflags, mode_t mode, struct adouble *ad)
669 struct stat *pst = NULL;
671 int oflags, nocreatflags;
675 if (ad_meta_fileno(ad) != -1) {
676 /* the file is already open, but we want write access: */
677 if (!(adflags & ADFLAGS_RDONLY) &&
678 /* and it was already denied: */
679 !(ad->ad_mdp->adf_flags & O_RDWR)) {
684 /* it's not new anymore */
685 ad->ad_mdp->adf_flags &= ~( O_TRUNC | O_CREAT );
686 ad->ad_mdp->adf_refcount++;
690 ad_p = ad->ad_ops->ad_path(path, adflags);
691 oflags = O_NOFOLLOW | ad2openflags(adflags);
692 nocreatflags = oflags & ~(O_CREAT | O_EXCL);
694 ad->ad_mdp->adf_fd = open(ad_p, nocreatflags);
696 if (ad->ad_mdp->adf_fd < 0) {
701 if ((adflags & ADFLAGS_RDONLY) && (adflags & ADFLAGS_SETSHRMD)) {
702 nocreatflags &= ~O_RDWR;
703 nocreatflags |= O_RDONLY;
704 if ((ad->ad_mdp->adf_fd = open(ad_p, nocreatflags)) == -1)
710 if (!(oflags & O_CREAT))
711 return ad_error(ad, adflags);
713 * We're expecting to create a new adouble header file here
714 * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
716 LOG(log_debug, logtype_default, "ad_open(\"%s\"): creating adouble file",
720 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
721 if ((ad->ad_options & ADVOL_UNIXPRIV))
723 admode = ad_hf_mode(admode);
724 if (errno == ENOENT) {
725 if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
726 return ad_error(ad, adflags);
729 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
730 if ((ad->ad_options & ADVOL_UNIXPRIV))
732 admode = ad_hf_mode(admode);
735 /* retry with O_CREAT */
736 ad->ad_mdp->adf_fd = open(ad_p, oflags, admode);
737 if ( ad->ad_mdp->adf_fd < 0 )
738 return ad_error(ad, adflags);
740 ad->ad_mdp->adf_flags = oflags;
741 /* just created, set owner if admin owner (root) */
743 ad_chown(ad_p, &st_dir);
747 ad->ad_mdp->adf_flags = nocreatflags;
748 if (fstat(ad->ad_mdp->adf_fd, &st_meta) == 0 && st_meta.st_size == 0) {
749 /* for 0 length files, treat them as new. */
750 ad->ad_mdp->adf_flags |= O_TRUNC;
752 /* we have valid data in st_meta stat structure, reused it in ad_header_read */
757 ad->ad_mdp->adf_refcount = 1;
758 adf_lock_init(ad->ad_mdp);
760 if ((ad->ad_mdp->adf_flags & ( O_TRUNC | O_CREAT ))) {
761 /* This is a new adouble header file, create it */
762 if (new_ad_header(path, ad, adflags) < 0) {
764 /* the file is already deleted, perm, whatever, so return an error */
765 ad_close(ad, adflags);
771 /* Read the adouble header in and parse it.*/
772 if (ad->ad_ops->ad_header_read( ad , pst) < 0
773 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0) {
775 ad_close(ad, adflags);
784 static int ad_open_hf_ea(const char *path, int adflags, int mode, struct adouble *ad)
787 int oflags = O_NOFOLLOW;
789 oflags = ad2openflags(adflags) & ~(O_CREAT | O_TRUNC);
791 if (ad_meta_fileno(ad) == -1) {
792 if ((ad_meta_fileno(ad) = open(path, oflags)) == -1)
794 ad->ad_mdp->adf_flags = oflags;
795 ad->ad_mdp->adf_refcount = 1;
796 adf_lock_init(ad->ad_mdp);
799 /* Read the adouble header in and parse it.*/
800 if (ad->ad_ops->ad_header_read(ad, NULL) != 0) {
801 if (!(adflags & ADFLAGS_CREATE))
804 /* It doesnt exist, EPERM or another error */
805 if (!(errno == ENOATTR || errno == ENOENT)) {
806 LOG(log_error, logtype_default, "ad_open_hf_ea: unexpected: %s", strerror(errno));
811 if (new_ad_header(path, ad, adflags) < 0) {
812 LOG(log_error, logtype_default, "ad_open_hf_ea: can't create new header: %s",
816 ad->ad_mdp->adf_flags |= O_CREAT; /* mark as just created */
818 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): created metadata EA", path);
821 ad->ad_mdp->adf_refcount++;
823 if ((rforklen = sys_fgetxattr(ad_meta_fileno(ad), AD_EA_RESO, NULL, 0)) > 0)
824 ad->ad_rlen = rforklen;
829 if (ad_meta_fileno(ad) != -1) {
830 close(ad_meta_fileno(ad));
831 ad_meta_fileno(ad) = -1;
833 return ad_error(ad, adflags);
836 static int ad_open_hf(const char *path, int adflags, int mode, struct adouble *ad)
840 LOG(log_debug, logtype_default, "ad_open_hf(\"%s\", %04o)", path, mode);
842 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
845 switch (ad->ad_flags) {
847 ret = ad_open_hf_v2(path, adflags, mode, ad);
850 ret = ad_open_hf_ea(path, adflags, mode, ad);
861 * Open ressource fork
863 * Only for adouble:ea, a nullop otherwise because adouble:v2 has the ressource fork as part
864 * of the adouble file which is openend by ADFLAGS_HF.
866 static int ad_open_rf(const char *path, int adflags, int mode, struct adouble *ad)
870 if (ad->ad_flags != AD_VERSION_EA)
873 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\", %04o)",
876 if ((ad->ad_rlen = sys_fgetxattr(ad_meta_fileno(ad), AD_EA_RESO, NULL, 0)) <= 0) {
882 LOG(log_warning, logtype_default, "ad_open_rf(\"%s\"): %s",
883 fullpathname(path), strerror(errno));
889 /* Round up and allocate buffer */
890 size_t roundup = ((ad->ad_rlen / RFORK_EA_ALLOCSIZE) + 1) * RFORK_EA_ALLOCSIZE;
891 if ((ad->ad_resforkbuf = malloc(roundup)) == NULL) {
896 ad->ad_resforkbufsize = roundup;
898 /* Read the EA into the buffer */
899 if (ad->ad_rlen > 0) {
900 if (sys_fgetxattr(ad_meta_fileno(ad), AD_EA_RESO, ad->ad_resforkbuf, ad->ad_rlen) == -1) {
908 free(ad->ad_resforkbuf);
909 ad->ad_resforkbuf = NULL;
911 ad->ad_resforkbufsize = 0;
917 /***********************************************************************************
919 ********************************************************************************* */
921 const char *ad_path_ea( const char *path, int adflags _U_)
927 * Put the .AppleDouble where it needs to be:
931 * \ b/.AppleDouble/.Parent
933 * FIXME: should do something for pathname > MAXPATHLEN
935 const char *ad_path( const char *path, int adflags)
937 static char pathbuf[ MAXPATHLEN + 1];
941 if ( adflags & ADFLAGS_DIR ) {
942 l = strlcpy( pathbuf, path, sizeof(pathbuf));
944 if ( l && l < MAXPATHLEN) {
947 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
949 if (NULL != ( slash = strrchr( path, '/' )) ) {
952 /* XXX we must return NULL here and test in the caller */
955 memcpy( pathbuf, path, l);
960 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
961 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
967 /* -------------------------
968 * Support inherited protection modes for AppleDouble files. The supplied
969 * mode is ANDed with the parent directory's mask value in lieu of "umask",
970 * and that value is returned.
972 char *ad_dir(const char *path)
974 static char modebuf[ MAXPATHLEN + 1];
977 * For a path with directories in it, remove the final component
978 * (path or subdirectory name) to get the name we want to stat.
979 * For a path which is just a filename, use "." instead.
981 slash = strrchr( path, '/' );
986 if (len >= MAXPATHLEN) {
987 errno = ENAMETOOLONG;
988 return NULL; /* can't do it */
990 memcpy( modebuf, path, len );
992 /* is last char a '/' ? */
994 slash = modebuf+ len;
996 while (modebuf < slash && slash[-1] == '/') {
999 if (modebuf == slash) {
1003 while (modebuf < slash && *slash != '/') {
1006 if (modebuf == slash) {
1009 *slash = '\0'; /* remove pathname component */
1014 modebuf[0] = '.'; /* use current directory */
1019 int ad_setfuid(const uid_t id)
1025 /* ---------------- */
1026 uid_t ad_getfuid(void)
1032 stat path parent directory
1034 int ad_stat(const char *path, struct stat *stbuf)
1041 return lstat( p, stbuf );
1045 return access right of path parent directory
1047 int ad_mode( const char *path, int mode)
1050 ad_mode_st(path, &mode, &stbuf);
1055 * Use mkdir() with mode bits taken from ad_mode().
1057 int ad_mkdir( const char *path, int mode)
1063 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
1064 path, mode, getcwdpath());
1066 st_invalid = ad_mode_st(path, &mode, &stbuf);
1067 ret = mkdir( path, mode );
1068 if (ret || st_invalid)
1070 ad_chown(path, &stbuf);
1075 void ad_init(struct adouble *ad, int flags, int options)
1079 ad->ad_ops = &ad_adouble;
1080 ad->ad_rfp = &ad->ad_resource_fork;
1081 ad->ad_mdp = &ad->ad_resource_fork;
1084 ad->ad_ops = &ad_adouble_ea;
1085 ad->ad_rfp = &ad->ad_data_fork;
1086 ad->ad_mdp = &ad->ad_data_fork;
1089 LOG(log_error, logtype_default, "ad_init: unknown AD version");
1094 ad->ad_flags = flags;
1095 ad->ad_options = options;
1096 ad_data_fileno(ad) = -1;
1097 ad_reso_fileno(ad) = -1;
1098 ad_meta_fileno(ad) = -1;
1099 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
1101 ad->ad_refcount = 1;
1102 ad->ad_open_forks = 0;
1103 ad->ad_resource_fork.adf_refcount = 0;
1104 ad->ad_resforkbuf = NULL;
1105 ad->ad_data_fork.adf_refcount = 0;
1106 ad->ad_data_fork.adf_syml=0;
1111 * Open data-, metadata(header)- or ressource fork
1113 * ad_open(struct adouble *ad, const char *path, int adflags, int flags)
1114 * ad_open(struct adouble *ad, const char *path, int adflags, int flags, mode_t mode)
1116 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
1119 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1122 * Open a files data fork, metadata fork or ressource fork.
1124 * @param ad (rw) pointer to struct adouble
1125 * @param path (r) Path to file or directory
1126 * @param adflags (r) ADFLAGS_DF: open data fork \n
1127 * ADFLAGS_RF: open ressource fork \n
1128 * ADFLAGS_HF: open header (metadata) file \n
1129 * ADFLAGS_NOHF: it's not an error if header file couldn't be created \n
1130 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags \n
1131 * The open mode flags (rw vs ro) have to take into account all the following requirements:
1132 * - we remember open fds for files because me must avoid a single close releasing fcntl locks for other
1133 * fds of the same file
1134 * - a file may be opened first ro, then rw and theres no way to upgrade this -> fork.c always opens rw
1135 * ADFLAGS_CHECK_OF: check for open forks from us and other afpd's
1136 * @param mode (r) mode used with O_CREATE
1138 * @returns 0 on success, any other value indicates an error
1140 int ad_open(struct adouble *ad, const char *path, int adflags, ...)
1146 LOG(log_debug, logtype_default, "ad_open(\"%s\", %s)",
1147 fullpathname(path), adflags2logstr(adflags));
1149 if (ad->ad_inited != AD_INITED) {
1150 ad->ad_adflags = adflags;
1151 ad->ad_inited = AD_INITED;
1153 ad->ad_open_forks = ((ad->ad_data_fork.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
1154 if (ad->ad_resource_fork.adf_refcount > 0)
1155 ad->ad_open_forks |= ATTRBIT_ROPEN;
1158 va_start(args, adflags);
1159 if (adflags & ADFLAGS_CREATE)
1160 mode = va_arg(args, mode_t);
1163 if (adflags & ADFLAGS_DF) {
1164 EC_ZERO( ad_open_df(path, adflags, mode, ad) );
1167 if (adflags & ADFLAGS_HF) {
1168 EC_ZERO( ad_open_hf(path, adflags, mode, ad) );
1171 if (adflags & ADFLAGS_RF) {
1172 EC_ZERO( ad_open_rf(path, adflags, mode, ad) );
1180 * @brief open metadata, possibly as root
1182 * Return only metadata but try very hard ie at first try as user, then try as root.
1184 * @param name name of file/dir
1185 * @param flags ADFLAGS_DIR: name is a directory \n
1186 * ADFLAGS_CHECK_OF: test if name is open by us or another afpd process
1188 * @param adp pointer to struct adouble
1190 int ad_metadata(const char *name, int flags, struct adouble *adp)
1193 int ret, err, oflags;
1195 oflags = (flags & ADFLAGS_DIR) | ADFLAGS_HF | ADFLAGS_RDONLY;
1197 if ((ret = ad_open(adp, name, oflags | ADFLAGS_SETSHRMD)) < 0 && errno == EACCES) {
1200 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1204 /* we are root open read only */
1205 ret = ad_open(adp, name, oflags);
1207 if ( seteuid(uid) < 0) {
1208 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1214 if ((ret == 0) && (ADFLAGS_CHECK_OF & flags)) {
1216 we need to check if the file is open by another process.
1217 it's slow so we only do it if we have to:
1219 - we don't already have the answer!
1221 adp->ad_open_forks |= ad_openforks(adp, adp->ad_open_forks);
1227 * @brief openat like wrapper for ad_metadata
1229 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1235 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1241 if (ad_metadata(name, flags, adp) < 0) {
1247 if (fchdir(cwdfd) != 0) {
1248 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1261 int ad_refresh(struct adouble *ad)
1264 if (ad_meta_fileno(ad) == -1)
1267 return ad->ad_ops->ad_header_read(ad, NULL);
1270 int ad_openat(struct adouble *ad,
1271 int dirfd, /* dir fd openat like */
1281 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0))
1285 va_start(args, adflags);
1286 if (adflags & ADFLAGS_CREATE)
1287 mode = va_arg(args, mode_t);
1290 EC_NEG1( ad_open(ad, path, adflags, mode) );
1293 if (fchdir(cwdfd) != 0) {
1294 AFP_PANIC("ad_openat: cant chdir back");