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>
53 #include <atalk/volume.h>
58 #define MAX(a, b) ((a) < (b) ? (b) : (a))
61 #define ADEDOFF_MAGIC (0)
62 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
63 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
64 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
66 /* initial lengths of some of the fields */
67 #define ADEDLEN_INIT 0
69 /* i stick things in a slightly different order than their eid order in
70 * case i ever want to separate RootInfo behaviour from the rest of the
74 #define ADEDOFF_NAME_V2 (AD_HEADER_LEN + ADEID_NUM_V2*AD_ENTRY_LEN)
75 #define ADEDOFF_COMMENT_V2 (ADEDOFF_NAME_V2 + ADEDLEN_NAME)
76 #define ADEDOFF_FILEDATESI (ADEDOFF_COMMENT_V2 + ADEDLEN_COMMENT)
77 #define ADEDOFF_FINDERI_V2 (ADEDOFF_FILEDATESI + ADEDLEN_FILEDATESI)
78 #define ADEDOFF_DID (ADEDOFF_FINDERI_V2 + ADEDLEN_FINDERI)
79 #define ADEDOFF_AFPFILEI (ADEDOFF_DID + ADEDLEN_DID)
80 #define ADEDOFF_SHORTNAME (ADEDOFF_AFPFILEI + ADEDLEN_AFPFILEI)
81 #define ADEDOFF_PRODOSFILEI (ADEDOFF_SHORTNAME + ADEDLEN_SHORTNAME)
82 #define ADEDOFF_PRIVDEV (ADEDOFF_PRODOSFILEI + ADEDLEN_PRODOSFILEI)
83 #define ADEDOFF_PRIVINO (ADEDOFF_PRIVDEV + ADEDLEN_PRIVDEV)
84 #define ADEDOFF_PRIVSYN (ADEDOFF_PRIVINO + ADEDLEN_PRIVINO)
85 #define ADEDOFF_PRIVID (ADEDOFF_PRIVSYN + ADEDLEN_PRIVSYN)
86 #define ADEDOFF_RFORK_V2 (ADEDOFF_PRIVID + ADEDLEN_PRIVID)
89 #define ADEDOFF_FINDERI_EA (AD_HEADER_LEN + ADEID_NUM_EA * AD_ENTRY_LEN)
90 #define ADEDOFF_COMMENT_EA (ADEDOFF_FINDERI_EA + ADEDLEN_FINDERI)
91 #define ADEDOFF_FILEDATESI_EA (ADEDOFF_COMMENT_EA + ADEDLEN_COMMENT)
92 #define ADEDOFF_AFPFILEI_EA (ADEDOFF_FILEDATESI_EA + ADEDLEN_FILEDATESI)
94 /* this is to prevent changing timezones from causing problems with
95 localtime volumes. the screw-up is 30 years. we use a delta of 5 years */
96 #define TIMEWARP_DELTA 157680000
99 uint32_t id, offset, len;
102 /* --------------------------- */
103 static uid_t default_uid = -1;
105 /* Forward declarations */
106 static int ad_mkrf(const char *path);
107 static int ad_header_read(struct adouble *ad, struct stat *hst);
108 static int ad_header_upgrade(struct adouble *ad, const char *name);
110 static int ad_mkrf_ea(const char *path);
111 static int ad_header_read_ea(struct adouble *ad, struct stat *hst);
112 static int ad_header_upgrade_ea(struct adouble *ad, const char *name);
114 static struct adouble_fops ad_adouble = {
117 &ad_rebuild_adouble_header,
122 static struct adouble_fops ad_adouble_ea = {
125 &ad_rebuild_adouble_header,
127 &ad_header_upgrade_ea,
130 static const struct entry entry_order2[ADEID_NUM_V2 + 1] = {
131 {ADEID_NAME, ADEDOFF_NAME_V2, ADEDLEN_INIT},
132 {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
133 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
134 {ADEID_FINDERI, ADEDOFF_FINDERI_V2, ADEDLEN_FINDERI},
135 {ADEID_DID, ADEDOFF_DID, ADEDLEN_DID},
136 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
137 {ADEID_SHORTNAME, ADEDOFF_SHORTNAME, ADEDLEN_INIT},
138 {ADEID_PRODOSFILEI, ADEDOFF_PRODOSFILEI, ADEDLEN_PRODOSFILEI},
139 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV, ADEDLEN_INIT},
140 {ADEID_PRIVINO, ADEDOFF_PRIVINO, ADEDLEN_INIT},
141 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN, ADEDLEN_INIT},
142 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
143 {ADEID_RFORK, ADEDOFF_RFORK_V2, ADEDLEN_INIT},
147 /* Using Extended Attributes */
148 static const struct entry entry_order_ea[ADEID_NUM_EA + 1] = {
149 {ADEID_FINDERI, ADEDOFF_FINDERI_EA, ADEDLEN_FINDERI},
150 {ADEID_COMMENT, ADEDOFF_COMMENT_EA, ADEDLEN_INIT},
151 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_EA, ADEDLEN_FILEDATESI},
152 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_EA, ADEDLEN_AFPFILEI},
156 #define ADFLAGS2LOGSTRBUFSIZ 128
157 const char *adflags2logstr(int adflags)
160 static char buf[ADFLAGS2LOGSTRBUFSIZ];
164 if (adflags & ADFLAGS_DF) {
165 strlcat(buf, "DF", ADFLAGS2LOGSTRBUFSIZ);
168 if (adflags & ADFLAGS_RF) {
170 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
171 strlcat(buf, "RF", ADFLAGS2LOGSTRBUFSIZ);
174 if (adflags & ADFLAGS_HF) {
176 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
177 strlcat(buf, "HF", ADFLAGS2LOGSTRBUFSIZ);
180 if (adflags & ADFLAGS_NOHF) {
182 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
183 strlcat(buf, "NOHF", ADFLAGS2LOGSTRBUFSIZ);
186 if (adflags & ADFLAGS_DIR) {
188 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
189 strlcat(buf, "DIR", ADFLAGS2LOGSTRBUFSIZ);
192 if (adflags & ADFLAGS_CHECK_OF) {
194 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
195 strlcat(buf, "OF", ADFLAGS2LOGSTRBUFSIZ);
198 if (adflags & ADFLAGS_RDWR) {
200 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
201 strlcat(buf, "O_RDWR", ADFLAGS2LOGSTRBUFSIZ);
204 if (adflags & ADFLAGS_RDONLY) {
206 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
207 strlcat(buf, "O_RDONLY", ADFLAGS2LOGSTRBUFSIZ);
210 if (adflags & ADFLAGS_CREATE) {
212 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
213 strlcat(buf, "O_CREAT", ADFLAGS2LOGSTRBUFSIZ);
216 if (adflags & ADFLAGS_EXCL) {
218 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
219 strlcat(buf, "O_EXCL", ADFLAGS2LOGSTRBUFSIZ);
222 if (adflags & ADFLAGS_TRUNC) {
224 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
225 strlcat(buf, "O_TRUNC", ADFLAGS2LOGSTRBUFSIZ);
232 static uint32_t get_eid(uint32_t eid)
237 return ADEID_PRIVDEV;
239 return ADEID_PRIVINO;
241 return ADEID_PRIVSYN;
248 /* ----------------------------------- */
249 static int new_ad_header(const char *path, struct adouble *ad, int adflags)
251 const struct entry *eid;
255 ad->ad_magic = AD_MAGIC;
256 ad->ad_version = ad->ad_vers & 0x0f0000;
257 if (!ad->ad_version) {
258 ad->ad_version = AD_VERSION;
262 memset(ad->ad_data, 0, sizeof(ad->ad_data));
264 if (ad->ad_vers == AD_VERSION2)
266 else if (ad->ad_vers == AD_VERSION_EA)
267 eid = entry_order_ea;
273 ad->ad_eid[eid->id].ade_off = eid->offset;
274 ad->ad_eid[eid->id].ade_len = eid->len;
278 /* put something sane in the directory finderinfo */
279 if ((adflags & ADFLAGS_DIR)) {
280 /* set default view */
281 ashort = htons(FINDERINFO_CLOSEDVIEW);
282 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
284 /* set default creator/type fields */
285 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
286 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
289 /* make things invisible */
290 if ((ad->ad_options & ADVOL_INVDOTS) && (*path == '.')) {
291 ashort = htons(ATTRBIT_INVISIBLE);
292 ad_setattr(ad, ashort);
293 ashort = htons(FINDERINFO_INVISIBLE);
294 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
297 if (lstat(path, &st) < 0)
300 /* put something sane in the date fields */
301 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
302 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
303 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
304 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
308 /* -------------------------------------
311 static void parse_entries(struct adouble *ad, char *buf, uint16_t nentries)
313 uint32_t eid, len, off;
316 /* now, read in the entry bits */
317 for (; nentries > 0; nentries-- ) {
318 memcpy(&eid, buf, sizeof( eid ));
319 eid = get_eid(ntohl(eid));
320 buf += sizeof( eid );
321 memcpy(&off, buf, sizeof( off ));
323 buf += sizeof( off );
324 memcpy(&len, buf, sizeof( len ));
326 buf += sizeof( len );
328 if (eid && eid < ADEID_MAX && off < sizeof(ad->ad_data) &&
329 (off + len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
330 ad->ad_eid[ eid ].ade_off = off;
331 ad->ad_eid[ eid ].ade_len = len;
332 } else if (!warning) {
334 LOG(log_warning, logtype_default, "parse_entries: bogus eid: %d", eid);
339 /* this reads enough of the header so that we can figure out all of
340 * the entry lengths and offsets. once that's done, we just read/mmap
341 * the rest of the header in.
343 * NOTE: we're assuming that the resource fork is kept at the end of
344 * the file. also, mmapping won't work for the hfs fs until it
345 * understands how to mmap header files. */
346 static int ad_header_read(struct adouble *ad, struct stat *hst)
348 char *buf = ad->ad_data;
354 /* read the header */
355 if ((header_len = adf_pread( ad->ad_mdp, buf, sizeof(ad->ad_data), 0)) < 0) {
358 if (header_len < AD_HEADER_LEN) {
363 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
364 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
365 ad->ad_magic = ntohl( ad->ad_magic );
366 ad->ad_version = ntohl( ad->ad_version );
368 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
369 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
374 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
375 nentries = ntohs( nentries );
377 /* read in all the entry headers. if we have more than the
378 * maximum, just hope that the rfork is specified early on. */
379 len = nentries*AD_ENTRY_LEN;
381 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
382 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
384 buf += AD_HEADER_LEN;
385 if (len > header_len - AD_HEADER_LEN) {
386 LOG(log_error, logtype_default, "ad_header_read: can't read entry info.");
391 /* figure out all of the entry offsets and lengths. if we aren't
392 * able to read a resource fork entry, bail. */
393 nentries = len / AD_ENTRY_LEN;
394 parse_entries(ad, buf, nentries);
395 if (!ad_getentryoff(ad, ADEID_RFORK)
396 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
398 LOG(log_error, logtype_default, "ad_header_read: problem with rfork entry offset.");
403 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
404 LOG(log_error, logtype_default, "ad_header_read: can't read in entries.");
411 if (fstat(ad->ad_mdp->adf_fd, &st) < 0) {
412 return 1; /* fail silently */
416 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
421 static int ad_header_read_ea(struct adouble *ad, struct stat *hst _U_)
426 char *buf = ad->ad_data;
428 /* read the header */
429 if ((header_len = sys_fgetxattr(ad_data_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA)) < 1) {
430 LOG(log_debug, logtype_default, "ad_header_read_ea: %s (%u)", strerror(errno), errno);
434 if (header_len < AD_HEADER_LEN) {
435 LOG(log_error, logtype_default, "ad_header_read_ea: bogus AppleDouble header.");
440 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
441 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
443 ad->ad_magic = ntohl( ad->ad_magic );
444 ad->ad_version = ntohl( ad->ad_version );
446 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
447 LOG(log_error, logtype_default, "ad_header_read_ea: wrong magic or version");
452 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
453 nentries = ntohs( nentries );
455 /* Protect against bogus nentries */
456 len = nentries * AD_ENTRY_LEN;
457 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
458 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
459 if (len > header_len - AD_HEADER_LEN) {
460 LOG(log_error, logtype_default, "ad_header_read_ea: can't read entry info.");
464 nentries = len / AD_ENTRY_LEN;
466 /* Now parse entries */
467 parse_entries(ad, buf + AD_HEADER_LEN, nentries);
472 static int ad_rsrc_len_ea(struct adouble *ad)
476 if ((rlen = sys_fgetxattr(ad_data_fileno(ad), AD_EA_RESO, NULL, 0)) <= 0) {
483 LOG(log_error, logtype_default, "ad_refresh_rsrc_len_ea: %s", strerror(errno));
492 static int ad_mkrf(const char *path)
496 * Probably .AppleDouble doesn't exist, try to mkdir it.
498 if (NULL == ( slash = strrchr( path, '/' )) ) {
503 if ( ad_mkdir( path, 0777 ) < 0 ) {
510 static int ad_mkrf_ea(const char *path _U_)
512 AFP_PANIC("ad_mkrf_ea: dont use");
517 if we are root change path user/ group
518 It can be a native function for BSD cf. FAQ.Q10
519 path: pathname to chown
520 stbuf: parent directory inode
522 use fstat and fchown or lchown with linux?
524 #define EMULATE_SUIDDIR
526 static int ad_chown(const char *path, struct stat *stbuf)
529 #ifdef EMULATE_SUIDDIR
532 if (default_uid != (uid_t)-1) {
533 /* we are root (admin) */
534 id = (default_uid)?default_uid:stbuf->st_uid;
535 ret = lchown( path, id, stbuf->st_gid );
541 #define DEFMASK 07700 /* be conservative */
544 return access right and inode of path parent directory
546 static int ad_mode_st(const char *path, mode_t *mode, struct stat *stbuf)
551 if (ad_stat(path, stbuf) != 0) {
555 *mode &= stbuf->st_mode;
559 /* --------------------------- */
560 static int ad_header_upgrade(struct adouble *ad _U_, const char *name _U_)
565 static int ad_header_upgrade_ea(struct adouble *ad _U_, const char *name _U_)
567 AFP_PANIC("ad_header_upgrade_ea: dont use");
572 * Error handling for adouble header(=metadata) file open error
574 * We're called because opening ADFLAGS_HF caused an error.
575 * 1. In case ad_open is called with ADFLAGS_NOHF the error is suppressed.
576 * 2. If ad_open was called with ADFLAGS_DF we may have opened the datafork and thus
577 * ought to close it before returning with an error condition.
579 static int ad_error(struct adouble *ad, int adflags)
582 if (adflags & ADFLAGS_NOHF) { /* 1 */
583 ad->ad_adflags &= ~ADFLAGS_HF;
586 if (adflags & ADFLAGS_DF) { /* 2 */
587 ad_close( ad, ADFLAGS_DF );
593 /* Map ADFLAGS to open() flags */
594 static int ad2openflags(int adflags)
598 if (adflags & ADFLAGS_RDWR)
600 if ((adflags & ADFLAGS_RDONLY) && (adflags & ADFLAGS_SETSHRMD))
604 if (adflags & ADFLAGS_CREATE)
606 if (adflags & ADFLAGS_EXCL)
608 if (adflags & ADFLAGS_TRUNC)
614 static int ad_open_df(const char *path, int adflags, mode_t mode, struct adouble *ad)
622 LOG(log_debug, logtype_default, "ad_open_df(\"%s\", %04o)",
623 fullpathname(path), mode);
625 if (ad_data_fileno(ad) != -1) {
626 /* the file is already open, but we want write access: */
627 if ((adflags & ADFLAGS_RDWR)
628 /* and it was denied the first time: */
629 && (ad->ad_data_fork.adf_flags & O_RDONLY)) {
633 /* it's not new anymore */
634 ad->ad_mdp->adf_flags &= ~( O_TRUNC | O_CREAT );
635 ad->ad_data_fork.adf_refcount++;
639 oflags = O_NOFOLLOW | ad2openflags(adflags);
642 if ((adflags & ADFLAGS_CREATE)) {
643 st_invalid = ad_mode_st(path, &admode, &st_dir);
644 if ((ad->ad_options & ADVOL_UNIXPRIV))
648 ad->ad_data_fork.adf_fd = open(path, oflags, admode);
650 if (ad->ad_data_fork.adf_fd == -1) {
655 if ((adflags & ADFLAGS_SETSHRMD) && (adflags & ADFLAGS_RDONLY)) {
658 if ((ad->ad_data_fork.adf_fd = open(path, oflags, admode)) == -1)
663 case OPEN_NOFOLLOW_ERRNO:
664 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
665 if ((lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN)) <= 0) {
666 free(ad->ad_data_fork.adf_syml);
669 ad->ad_data_fork.adf_syml[lsz] = 0;
670 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
678 ad_chown(path, &st_dir); /* just created, set owner if admin (root) */
680 ad->ad_data_fork.adf_flags = oflags;
681 adf_lock_init(&ad->ad_data_fork);
682 ad->ad_data_fork.adf_refcount++;
687 static int ad_open_hf_v2(const char *path, int adflags, mode_t mode, struct adouble *ad)
691 struct stat *pst = NULL;
693 int oflags, nocreatflags;
697 if (ad_meta_fileno(ad) != -1) {
698 /* the file is already open, but we want write access: */
699 if ((adflags & ADFLAGS_RDWR) &&
700 /* and it was already denied: */
701 (ad->ad_mdp->adf_flags & O_RDONLY)) {
706 /* it's not new anymore */
707 ad->ad_mdp->adf_flags &= ~( O_TRUNC | O_CREAT );
708 ad->ad_mdp->adf_refcount++;
712 ad_p = ad->ad_ops->ad_path(path, adflags);
713 oflags = O_NOFOLLOW | ad2openflags(adflags);
714 nocreatflags = oflags & ~(O_CREAT | O_EXCL);
716 ad->ad_mdp->adf_fd = open(ad_p, nocreatflags);
718 if (ad->ad_mdp->adf_fd != -1) {
719 ad->ad_mdp->adf_flags = nocreatflags;
725 if ((adflags & ADFLAGS_RDONLY) && (adflags & ADFLAGS_SETSHRMD)) {
726 nocreatflags &= ~O_RDWR;
727 nocreatflags |= O_RDONLY;
728 if ((ad->ad_mdp->adf_fd = open(ad_p, nocreatflags)) == -1)
730 ad->ad_mdp->adf_flags = nocreatflags;
735 if (!(oflags & O_CREAT))
736 return ad_error(ad, adflags);
738 * We're expecting to create a new adouble header file here
739 * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
741 LOG(log_debug, logtype_default, "ad_open(\"%s\"): creating adouble file",
745 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
746 if ((ad->ad_options & ADVOL_UNIXPRIV))
748 admode = ad_hf_mode(admode);
749 if (errno == ENOENT) {
750 if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
751 return ad_error(ad, adflags);
754 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
755 if ((ad->ad_options & ADVOL_UNIXPRIV))
757 admode = ad_hf_mode(admode);
760 /* retry with O_CREAT */
761 ad->ad_mdp->adf_fd = open(ad_p, oflags, admode);
762 if ( ad->ad_mdp->adf_fd < 0 )
763 return ad_error(ad, adflags);
765 ad->ad_mdp->adf_flags = oflags;
766 /* just created, set owner if admin owner (root) */
768 ad_chown(ad_p, &st_dir);
775 if (!(ad->ad_mdp->adf_flags & O_CREAT)) {
776 /* check for 0 length files, treat them as new. */
777 if (fstat(ad->ad_mdp->adf_fd, &st_meta) == 0) {
778 if (st_meta.st_size == 0)
779 ad->ad_mdp->adf_flags |= O_TRUNC;
781 /* we have valid data in st_meta stat structure, reused it in ad_header_read */
786 ad->ad_mdp->adf_refcount = 1;
787 adf_lock_init(ad->ad_mdp);
789 if ((ad->ad_mdp->adf_flags & ( O_TRUNC | O_CREAT ))) {
790 /* This is a new adouble header file, create it */
791 if (new_ad_header(path, ad, adflags) < 0) {
793 /* the file is already deleted, perm, whatever, so return an error */
794 ad_close(ad, adflags);
800 /* Read the adouble header in and parse it.*/
801 if (ad->ad_ops->ad_header_read( ad , pst) < 0
802 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0) {
804 ad_close(ad, adflags);
813 static int ad_open_hf_ea(const char *path, int adflags, int mode, struct adouble *ad)
816 int oflags = O_NOFOLLOW;
818 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\", %04o)", path, mode);
820 oflags |= ad2openflags(adflags) & ~(O_CREAT | O_TRUNC);
822 if (ad_data_fileno(ad) != -1) {
823 /* the file is already open, but we want write access: */
824 if ((adflags & ADFLAGS_RDWR) &&
825 /* and it was already denied: */
826 (ad->ad_data_fork.adf_flags & O_RDONLY)) {
827 LOG(log_error, logtype_default, "ad_open_hf_ea(%s): rw request for ro file: %s",
828 fullpathname(path), strerror(errno));
833 /* it's not new anymore */
834 ad->ad_mdp->adf_flags &= ~( O_TRUNC | O_CREAT );
836 if ((ad_data_fileno(ad) = open(path, oflags)) == -1)
838 ad->ad_data_fork.adf_flags = oflags;
839 adf_lock_init(&ad->ad_data_fork);
842 /* Read the adouble header in and parse it.*/
843 if (ad->ad_ops->ad_header_read(ad, NULL) != 0) {
844 if (!(adflags & ADFLAGS_CREATE))
847 /* It doesnt exist, EPERM or another error */
848 if (!(errno == ENOATTR || errno == ENOENT)) {
849 LOG(log_error, logtype_default, "ad_open_hf_ea: unexpected: %s", strerror(errno));
854 if (new_ad_header(path, ad, adflags) < 0) {
855 LOG(log_error, logtype_default, "ad_open_hf_ea: can't create new header: %s",
859 ad->ad_mdp->adf_flags |= O_CREAT; /* mark as just created */
861 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): created metadata EA", path);
864 ad->ad_data_fork.adf_refcount++;
866 if ((rforklen = sys_fgetxattr(ad_data_fileno(ad), AD_EA_RESO, NULL, 0)) > 0)
867 ad->ad_rlen = rforklen;
872 if (ad_data_fileno(ad) != -1) {
873 close(ad_data_fileno(ad));
874 ad_data_fileno(ad) = -1;
876 return ad_error(ad, adflags);
879 static int ad_open_hf(const char *path, int adflags, int mode, struct adouble *ad)
883 LOG(log_debug, logtype_default, "ad_open_hf(\"%s\", %04o)", path, mode);
885 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
888 switch (ad->ad_vers) {
890 ret = ad_open_hf_v2(path, adflags, mode, ad);
893 ret = ad_open_hf_ea(path, adflags, mode, ad);
905 * Open ressource fork
907 * Only for adouble:ea, a nullop otherwise because adouble:v2 has the ressource fork as part
908 * of the adouble file which is openend by ADFLAGS_HF.
910 static int ad_open_rf(const char *path, int adflags, int mode, struct adouble *ad)
916 if (ad->ad_vers != AD_VERSION_EA)
919 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\", %04o)", path, mode);
921 oflags |= ad2openflags(adflags) & ~(O_CREAT | O_TRUNC);
923 if (ad_data_fileno(ad) != -1) {
924 /* the file is already open, but we want write access: */
925 if ((adflags & ADFLAGS_RDWR) &&
926 /* and it was already denied: */
927 (ad->ad_data_fork.adf_flags & O_RDONLY)) {
928 LOG(log_error, logtype_default, "ad_open_rf(%s): rw request for ro file: %s",
929 fullpathname(path), strerror(errno));
934 if ((ad_data_fileno(ad) = open(path, oflags)) == -1)
936 ad->ad_data_fork.adf_flags = oflags;
937 adf_lock_init(&ad->ad_data_fork);
940 if ((ret = ad_rsrc_len_ea(ad)) != 0)
943 ad->ad_data_fork.adf_refcount++;
949 /***********************************************************************************
951 ********************************************************************************* */
953 const char *ad_path_ea( const char *path, int adflags _U_)
959 * Put the .AppleDouble where it needs to be:
963 * \ b/.AppleDouble/.Parent
965 * FIXME: should do something for pathname > MAXPATHLEN
967 const char *ad_path( const char *path, int adflags)
969 static char pathbuf[ MAXPATHLEN + 1];
973 if ( adflags & ADFLAGS_DIR ) {
974 l = strlcpy( pathbuf, path, sizeof(pathbuf));
976 if ( l && l < MAXPATHLEN) {
979 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
981 if (NULL != ( slash = strrchr( path, '/' )) ) {
984 /* XXX we must return NULL here and test in the caller */
987 memcpy( pathbuf, path, l);
992 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
993 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
999 /* -------------------------
1000 * Support inherited protection modes for AppleDouble files. The supplied
1001 * mode is ANDed with the parent directory's mask value in lieu of "umask",
1002 * and that value is returned.
1004 char *ad_dir(const char *path)
1006 static char modebuf[ MAXPATHLEN + 1];
1009 * For a path with directories in it, remove the final component
1010 * (path or subdirectory name) to get the name we want to stat.
1011 * For a path which is just a filename, use "." instead.
1013 slash = strrchr( path, '/' );
1018 if (len >= MAXPATHLEN) {
1019 errno = ENAMETOOLONG;
1020 return NULL; /* can't do it */
1022 memcpy( modebuf, path, len );
1023 modebuf[len] = '\0';
1024 /* is last char a '/' ? */
1025 if (slash[1] == 0) {
1026 slash = modebuf+ len;
1028 while (modebuf < slash && slash[-1] == '/') {
1031 if (modebuf == slash) {
1035 while (modebuf < slash && *slash != '/') {
1038 if (modebuf == slash) {
1041 *slash = '\0'; /* remove pathname component */
1046 modebuf[0] = '.'; /* use current directory */
1051 int ad_setfuid(const uid_t id)
1057 /* ---------------- */
1058 uid_t ad_getfuid(void)
1064 stat path parent directory
1066 int ad_stat(const char *path, struct stat *stbuf)
1073 return lstat( p, stbuf );
1077 return access right of path parent directory
1079 int ad_mode( const char *path, mode_t mode)
1082 ad_mode_st(path, &mode, &stbuf);
1087 * Use mkdir() with mode bits taken from ad_mode().
1089 int ad_mkdir( const char *path, mode_t mode)
1095 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
1096 path, mode, getcwdpath());
1098 st_invalid = ad_mode_st(path, &mode, &stbuf);
1099 ret = mkdir( path, mode );
1100 if (ret || st_invalid)
1102 ad_chown(path, &stbuf);
1107 static void ad_init_func(struct adouble *ad)
1109 switch (ad->ad_vers) {
1111 ad->ad_ops = &ad_adouble;
1112 ad->ad_rfp = &ad->ad_resource_fork;
1113 ad->ad_mdp = &ad->ad_resource_fork;
1116 ad->ad_ops = &ad_adouble_ea;
1117 ad->ad_rfp = &ad->ad_resource_fork;
1118 ad->ad_mdp = &ad->ad_resource_fork;
1121 AFP_PANIC("ad_init: unknown AD version");
1124 ad_data_fileno(ad) = -1;
1125 ad_reso_fileno(ad) = -1;
1126 ad_meta_fileno(ad) = -1;
1127 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
1129 ad->ad_refcount = 1;
1130 ad->ad_open_forks = 0;
1131 ad->ad_resource_fork.adf_refcount = 0;
1132 ad->ad_resforkbuf = NULL;
1133 ad->ad_data_fork.adf_refcount = 0;
1134 ad->ad_data_fork.adf_syml = 0;
1139 void ad_init_old(struct adouble *ad, int flags, int options)
1141 ad->ad_vers = flags;
1142 ad->ad_options = options;
1146 void ad_init(struct adouble *ad, const struct vol * restrict vol)
1148 ad->ad_vers = vol->v_adouble;
1149 ad->ad_options = vol->v_ad_options;
1150 // ad->ad_maxeafssize = 3500; /* FIXME: option from vol */
1151 ad->ad_maxeafssize = 0; /* no limit */
1156 * Open data-, metadata(header)- or ressource fork
1158 * ad_open(struct adouble *ad, const char *path, int adflags, int flags)
1159 * ad_open(struct adouble *ad, const char *path, int adflags, int flags, mode_t mode)
1161 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
1164 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1167 * Open a files data fork, metadata fork or ressource fork.
1169 * @param ad (rw) pointer to struct adouble
1170 * @param path (r) Path to file or directory
1171 * @param adflags (r) ADFLAGS_DF: open data fork \n
1172 * ADFLAGS_RF: open ressource fork \n
1173 * ADFLAGS_HF: open header (metadata) file \n
1174 * ADFLAGS_NOHF: it's not an error if header file couldn't be created \n
1175 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags \n
1176 * The open mode flags (rw vs ro) have to take into account all the following requirements:
1177 * - we remember open fds for files because me must avoid a single close releasing fcntl locks for other
1178 * fds of the same file
1179 * - a file may be opened first ro, then rw and theres no way to upgrade this -> fork.c always opens rw
1180 * ADFLAGS_CHECK_OF: check for open forks from us and other afpd's
1181 * @param mode (r) mode used with O_CREATE
1183 * @returns 0 on success, any other value indicates an error
1185 int ad_open(struct adouble *ad, const char *path, int adflags, ...)
1191 LOG(log_debug, logtype_default, "ad_open(\"%s\", %s)",
1192 fullpathname(path), adflags2logstr(adflags));
1194 if (ad->ad_inited != AD_INITED) {
1195 ad->ad_adflags = adflags;
1196 ad->ad_inited = AD_INITED;
1198 ad->ad_open_forks = ((ad->ad_data_fork.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
1199 if (ad->ad_resource_fork.adf_refcount > 0)
1200 ad->ad_open_forks |= ATTRBIT_ROPEN;
1203 va_start(args, adflags);
1204 if (adflags & ADFLAGS_CREATE)
1205 mode = va_arg(args, mode_t);
1208 if (adflags & ADFLAGS_DF) {
1209 EC_ZERO( ad_open_df(path, adflags, mode, ad) );
1212 if (adflags & ADFLAGS_HF) {
1213 EC_ZERO( ad_open_hf(path, adflags, mode, ad) );
1216 if (adflags & ADFLAGS_RF) {
1217 EC_ZERO( ad_open_rf(path, adflags, mode, ad) );
1225 * @brief open metadata, possibly as root
1227 * Return only metadata but try very hard ie at first try as user, then try as root.
1229 * @param name name of file/dir
1230 * @param flags ADFLAGS_DIR: name is a directory \n
1231 * ADFLAGS_CHECK_OF: test if name is open by us or another afpd process
1233 * @param adp pointer to struct adouble
1235 int ad_metadata(const char *name, int flags, struct adouble *adp)
1238 int ret, err, oflags;
1240 oflags = (flags & ADFLAGS_DIR) | ADFLAGS_HF | ADFLAGS_RDONLY;
1242 if ((ret = ad_open(adp, name, oflags | ADFLAGS_SETSHRMD)) < 0 && errno == EACCES) {
1245 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1249 /* we are root open read only */
1250 ret = ad_open(adp, name, oflags);
1252 if ( seteuid(uid) < 0) {
1253 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1259 if ((ret == 0) && (ADFLAGS_CHECK_OF & flags)) {
1261 we need to check if the file is open by another process.
1262 it's slow so we only do it if we have to:
1264 - we don't already have the answer!
1266 adp->ad_open_forks |= ad_openforks(adp, adp->ad_open_forks);
1272 * @brief openat like wrapper for ad_metadata
1274 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1280 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1286 if (ad_metadata(name, flags, adp) < 0) {
1292 if (fchdir(cwdfd) != 0) {
1293 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1306 int ad_refresh(struct adouble *ad)
1308 switch (ad->ad_vers) {
1310 if (ad_meta_fileno(ad) == -1)
1312 return ad->ad_ops->ad_header_read(ad, NULL);
1315 if (ad_data_fileno(ad) == -1)
1317 if (ad_rsrc_len_ea(ad) != 0)
1319 return ad->ad_ops->ad_header_read(ad, NULL);
1328 int ad_openat(struct adouble *ad,
1329 int dirfd, /* dir fd openat like */
1339 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0))
1343 va_start(args, adflags);
1344 if (adflags & ADFLAGS_CREATE)
1345 mode = va_arg(args, mode_t);
1348 EC_NEG1( ad_open(ad, path, adflags, mode) );
1351 if (fchdir(cwdfd) != 0) {
1352 AFP_PANIC("ad_openat: cant chdir back");