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>
57 #define ADEDOFF_MAGIC (0)
58 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
59 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
60 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
62 /* initial lengths of some of the fields */
63 #define ADEDLEN_INIT 0
65 /* i stick things in a slightly different order than their eid order in
66 * case i ever want to separate RootInfo behaviour from the rest of the
70 #define ADEDOFF_NAME_V2 (AD_HEADER_LEN + ADEID_NUM_V2*AD_ENTRY_LEN)
71 #define ADEDOFF_COMMENT_V2 (ADEDOFF_NAME_V2 + ADEDLEN_NAME)
72 #define ADEDOFF_FILEDATESI (ADEDOFF_COMMENT_V2 + ADEDLEN_COMMENT)
73 #define ADEDOFF_FINDERI_V2 (ADEDOFF_FILEDATESI + ADEDLEN_FILEDATESI)
74 #define ADEDOFF_DID (ADEDOFF_FINDERI_V2 + ADEDLEN_FINDERI)
75 #define ADEDOFF_AFPFILEI (ADEDOFF_DID + ADEDLEN_DID)
76 #define ADEDOFF_SHORTNAME (ADEDOFF_AFPFILEI + ADEDLEN_AFPFILEI)
77 #define ADEDOFF_PRODOSFILEI (ADEDOFF_SHORTNAME + ADEDLEN_SHORTNAME)
78 #define ADEDOFF_PRIVDEV (ADEDOFF_PRODOSFILEI + ADEDLEN_PRODOSFILEI)
79 #define ADEDOFF_PRIVINO (ADEDOFF_PRIVDEV + ADEDLEN_PRIVDEV)
80 #define ADEDOFF_PRIVSYN (ADEDOFF_PRIVINO + ADEDLEN_PRIVINO)
81 #define ADEDOFF_PRIVID (ADEDOFF_PRIVSYN + ADEDLEN_PRIVSYN)
82 #define ADEDOFF_RFORK_V2 (ADEDOFF_PRIVID + ADEDLEN_PRIVID)
85 #define ADEDOFF_FINDERI_EA (AD_HEADER_LEN + ADEID_NUM_EA * AD_ENTRY_LEN)
86 #define ADEDOFF_COMMENT_EA (ADEDOFF_FINDERI_EA + ADEDLEN_FINDERI)
87 #define ADEDOFF_FILEDATESI_EA (ADEDOFF_COMMENT_EA + ADEDLEN_COMMENT)
88 #define ADEDOFF_AFPFILEI_EA (ADEDOFF_FILEDATESI_EA + ADEDLEN_FILEDATESI)
90 /* this is to prevent changing timezones from causing problems with
91 localtime volumes. the screw-up is 30 years. we use a delta of 5 years */
92 #define TIMEWARP_DELTA 157680000
95 uint32_t id, offset, len;
98 /* --------------------------- */
99 static uid_t default_uid = -1;
101 /* Forward declarations */
102 static int ad_mkrf(const char *path);
103 static int ad_header_read(const char *path, struct adouble *ad, struct stat *hst);
104 static int ad_header_upgrade(struct adouble *ad, const char *name);
106 static int ad_mkrf_ea(const char *path);
107 static int ad_header_read_ea(const char *path, struct adouble *ad, struct stat *hst);
108 static int ad_header_upgrade_ea(struct adouble *ad, const char *name);
109 static int ad_reso_size(const char *path, int adflags, struct adouble *ad);
110 static const char *ad_path_osx(const char *path, int adflags);
111 static int ad_mkrf_osx(const char *path);
114 static struct adouble_fops ad_adouble = {
117 &ad_rebuild_adouble_header,
122 static struct adouble_fops ad_adouble_ea = {
130 &ad_rebuild_adouble_header,
132 &ad_header_upgrade_ea,
135 static struct adouble_fops ad_osx = {
138 &ad_rebuild_adouble_header,
143 static const struct entry entry_order2[ADEID_NUM_V2 + 1] = {
144 {ADEID_NAME, ADEDOFF_NAME_V2, ADEDLEN_INIT},
145 {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
146 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
147 {ADEID_FINDERI, ADEDOFF_FINDERI_V2, ADEDLEN_FINDERI},
148 {ADEID_DID, ADEDOFF_DID, ADEDLEN_DID},
149 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
150 {ADEID_SHORTNAME, ADEDOFF_SHORTNAME, ADEDLEN_INIT},
151 {ADEID_PRODOSFILEI, ADEDOFF_PRODOSFILEI, ADEDLEN_PRODOSFILEI},
152 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV, ADEDLEN_INIT},
153 {ADEID_PRIVINO, ADEDOFF_PRIVINO, ADEDLEN_INIT},
154 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN, ADEDLEN_INIT},
155 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
156 {ADEID_RFORK, ADEDOFF_RFORK_V2, ADEDLEN_INIT},
160 /* Using Extended Attributes */
161 static const struct entry entry_order_ea[ADEID_NUM_EA + 1] = {
162 {ADEID_FINDERI, ADEDOFF_FINDERI_EA, ADEDLEN_FINDERI},
163 {ADEID_COMMENT, ADEDOFF_COMMENT_EA, ADEDLEN_INIT},
164 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_EA, ADEDLEN_FILEDATESI},
165 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_EA, ADEDLEN_AFPFILEI},
166 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
170 /* fallback for EAs */
171 static const struct entry entry_order_osx[ADEID_NUM_OSX +1] = {
172 {ADEID_FINDERI, ADEDOFF_FINDERI_OSX, ADEDLEN_FINDERI},
173 {ADEID_RFORK, ADEDOFF_RFORK_OSX, ADEDLEN_INIT},
177 #define ADFLAGS2LOGSTRBUFSIZ 128
178 const char *adflags2logstr(int adflags)
181 static char buf[ADFLAGS2LOGSTRBUFSIZ];
185 if (adflags & ADFLAGS_DF) {
186 strlcat(buf, "DF", ADFLAGS2LOGSTRBUFSIZ);
189 if (adflags & ADFLAGS_RF) {
191 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
192 strlcat(buf, "RF", ADFLAGS2LOGSTRBUFSIZ);
195 if (adflags & ADFLAGS_HF) {
197 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
198 strlcat(buf, "HF", ADFLAGS2LOGSTRBUFSIZ);
201 if (adflags & ADFLAGS_NOHF) {
203 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
204 strlcat(buf, "NOHF", ADFLAGS2LOGSTRBUFSIZ);
207 if (adflags & ADFLAGS_DIR) {
209 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
210 strlcat(buf, "DIR", ADFLAGS2LOGSTRBUFSIZ);
213 if (adflags & ADFLAGS_CHECK_OF) {
215 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
216 strlcat(buf, "OF", ADFLAGS2LOGSTRBUFSIZ);
219 if (adflags & ADFLAGS_RDWR) {
221 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
222 strlcat(buf, "O_RDWR", ADFLAGS2LOGSTRBUFSIZ);
225 if (adflags & ADFLAGS_RDONLY) {
227 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
228 strlcat(buf, "O_RDONLY", ADFLAGS2LOGSTRBUFSIZ);
231 if (adflags & ADFLAGS_CREATE) {
233 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
234 strlcat(buf, "O_CREAT", ADFLAGS2LOGSTRBUFSIZ);
237 if (adflags & ADFLAGS_EXCL) {
239 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
240 strlcat(buf, "O_EXCL", ADFLAGS2LOGSTRBUFSIZ);
243 if (adflags & ADFLAGS_TRUNC) {
245 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
246 strlcat(buf, "O_TRUNC", ADFLAGS2LOGSTRBUFSIZ);
253 static uint32_t get_eid(uint32_t eid)
258 return ADEID_PRIVDEV;
260 return ADEID_PRIVINO;
262 return ADEID_PRIVSYN;
269 /* ----------------------------------- */
270 static int new_ad_header(struct adouble *ad, const char *path, struct stat *stp, int adflags)
272 const struct entry *eid;
276 LOG(log_debug, logtype_default, "new_ad_header(\"%s\")", path);
280 if (lstat(path, &st) != 0)
284 ad->ad_magic = AD_MAGIC;
285 ad->ad_version = ad->ad_vers & 0x0f0000;
286 if (!ad->ad_version) {
287 ad->ad_version = AD_VERSION;
291 memset(ad->ad_data, 0, sizeof(ad->ad_data));
293 if (ad->ad_vers == AD_VERSION2)
295 else if (ad->ad_vers == AD_VERSION_EA)
296 eid = entry_order_ea;
297 else if (ad->ad_vers == AD_VERSION2_OSX)
298 eid = entry_order_osx;
304 ad->ad_eid[eid->id].ade_off = eid->offset;
305 ad->ad_eid[eid->id].ade_len = eid->len;
309 /* put something sane in the directory finderinfo */
310 if (ad->ad_vers != AD_VERSION2_OSX) {
311 if ((adflags & ADFLAGS_DIR)) {
312 /* set default view */
313 ashort = htons(FINDERINFO_CLOSEDVIEW);
314 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
316 /* set default creator/type fields */
317 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
318 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
321 /* make things invisible */
322 if ((ad->ad_options & ADVOL_INVDOTS) && (*path == '.')) {
323 ashort = htons(ATTRBIT_INVISIBLE);
324 ad_setattr(ad, ashort);
325 ashort = htons(FINDERINFO_INVISIBLE);
326 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
329 /* put something sane in the date fields */
330 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, stp->st_mtime);
331 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, stp->st_mtime);
332 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, stp->st_mtime);
333 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
338 /* -------------------------------------
341 static void parse_entries(struct adouble *ad, char *buf, uint16_t nentries)
343 uint32_t eid, len, off;
346 /* now, read in the entry bits */
347 for (; nentries > 0; nentries-- ) {
348 memcpy(&eid, buf, sizeof( eid ));
349 eid = get_eid(ntohl(eid));
350 buf += sizeof( eid );
351 memcpy(&off, buf, sizeof( off ));
353 buf += sizeof( off );
354 memcpy(&len, buf, sizeof( len ));
356 buf += sizeof( len );
360 && off < sizeof(ad->ad_data)
361 && (off + len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
362 ad->ad_eid[ eid ].ade_off = off;
363 ad->ad_eid[ eid ].ade_len = len;
364 } else if (!warning) {
366 LOG(log_warning, logtype_default, "parse_entries: bogus eid: %d", eid);
371 /* this reads enough of the header so that we can figure out all of
372 * the entry lengths and offsets. once that's done, we just read/mmap
373 * the rest of the header in.
375 * NOTE: we're assuming that the resource fork is kept at the end of
376 * the file. also, mmapping won't work for the hfs fs until it
377 * understands how to mmap header files. */
378 static int ad_header_read(const char *path _U_, struct adouble *ad, struct stat *hst)
380 char *buf = ad->ad_data;
386 /* read the header */
387 if ((header_len = adf_pread( ad->ad_mdp, buf, AD_DATASZ, 0)) < 0) {
390 if (header_len < AD_HEADER_LEN) {
395 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
396 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
397 ad->ad_magic = ntohl( ad->ad_magic );
398 ad->ad_version = ntohl( ad->ad_version );
400 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
401 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
406 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
407 nentries = ntohs( nentries );
409 /* read in all the entry headers. if we have more than the
410 * maximum, just hope that the rfork is specified early on. */
411 len = nentries*AD_ENTRY_LEN;
413 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
414 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
416 buf += AD_HEADER_LEN;
417 if (len > header_len - AD_HEADER_LEN) {
418 LOG(log_error, logtype_default, "ad_header_read: can't read entry info.");
423 /* figure out all of the entry offsets and lengths. if we aren't
424 * able to read a resource fork entry, bail. */
425 nentries = len / AD_ENTRY_LEN;
426 parse_entries(ad, buf, nentries);
427 if (!ad_getentryoff(ad, ADEID_RFORK)
428 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
430 LOG(log_error, logtype_default, "ad_header_read: problem with rfork entry offset.");
435 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
436 LOG(log_error, logtype_default, "ad_header_read: can't read in entries.");
443 if (fstat(ad->ad_mdp->adf_fd, &st) < 0) {
444 return 1; /* fail silently */
448 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
453 /* Read an ._ file, only uses the resofork, finderinfo is taken from EA */
454 static int ad_header_read_osx(const char *path _U_, struct adouble *ad, struct stat *hst)
457 struct adouble adosx;
458 char *buf = &adosx.ad_data[0];
464 memset(buf, 0, sizeof(adosx.ad_data));
466 /* read the header */
467 EC_NEG1( header_len = adf_pread(ad->ad_rfp, buf, AD_DATASZ_OSX, 0) );
469 if (header_len < AD_HEADER_LEN) {
474 memcpy(&adosx.ad_magic, buf, sizeof(adosx.ad_magic));
475 memcpy(&adosx.ad_version, buf + ADEDOFF_VERSION, sizeof(adosx.ad_version));
476 adosx.ad_magic = ntohl(adosx.ad_magic);
477 adosx.ad_version = ntohl(adosx.ad_version);
479 if ((adosx.ad_magic != AD_MAGIC) || (adosx.ad_version != AD_VERSION2)) {
480 LOG(log_error, logtype_afpd, "ad_header_read_osx: can't parse AppleDouble header");
485 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
486 nentries = ntohs(nentries);
487 len = nentries * AD_ENTRY_LEN;
489 if (len + AD_HEADER_LEN > sizeof(adosx.ad_data))
490 len = sizeof(adosx.ad_data) - AD_HEADER_LEN;
492 buf += AD_HEADER_LEN;
493 if (len > header_len - AD_HEADER_LEN) {
494 LOG(log_error, logtype_afpd, "ad_header_read_osx: can't read entry info.");
499 nentries = len / AD_ENTRY_LEN;
500 parse_entries(&adosx, buf, nentries);
502 if (ad_getentryoff(&adosx, ADEID_RFORK) == 0
503 || ad_getentryoff(&adosx, ADEID_RFORK) > sizeof(ad->ad_data)
504 || ad_getentryoff(&adosx, ADEID_RFORK) > header_len
506 LOG(log_error, logtype_afpd, "ad_header_read_osx: problem with rfork entry offset.");
513 EC_NEG1( fstat(ad_reso_fileno(ad), &st) );
516 ad_setentryoff(ad, ADEID_RFORK, ad_getentryoff(&adosx, ADEID_RFORK));
517 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
523 static int ad_header_read_ea(const char *path, struct adouble *ad, struct stat *hst _U_)
528 char *buf = ad->ad_data;
530 /* read the header */
531 if ((ad_meta_fileno(ad) == -1)
532 && (ad->ad_adflags & ADFLAGS_RDWR)) {
533 LOG(log_error, logtype_default, "ad_header_read_ea: need filedescriptor for rw access");
537 if (ad_meta_fileno(ad) != -1)
538 header_len = sys_fgetxattr(ad_meta_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA);
540 header_len = sys_lgetxattr(path, AD_EA_META, ad->ad_data, AD_DATASZ_EA);
541 if (header_len < 1) {
542 LOG(log_debug, logtype_default, "ad_header_read_ea: %s", strerror(errno));
546 if (header_len < AD_HEADER_LEN) {
547 LOG(log_error, logtype_default, "ad_header_read_ea: bogus AppleDouble header.");
552 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
553 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
555 ad->ad_magic = ntohl( ad->ad_magic );
556 ad->ad_version = ntohl( ad->ad_version );
558 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
559 LOG(log_error, logtype_default, "ad_header_read_ea: wrong magic or version");
564 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
565 nentries = ntohs( nentries );
567 /* Protect against bogus nentries */
568 len = nentries * AD_ENTRY_LEN;
569 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
570 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
571 if (len > header_len - AD_HEADER_LEN) {
572 LOG(log_error, logtype_default, "ad_header_read_ea: can't read entry info.");
576 nentries = len / AD_ENTRY_LEN;
578 /* Now parse entries */
579 parse_entries(ad, buf + AD_HEADER_LEN, nentries);
585 * Takes a path to an AppleDouble file and creates the parrent .AppleDouble directory
588 * path: "/path/.AppleDouble/file"
589 * => mkdir("/path/.AppleDouble/") (in ad_mkdir())
591 static int ad_mkrf(const char *path)
595 * Probably .AppleDouble doesn't exist, try to mkdir it.
597 if (NULL == ( slash = strrchr( path, '/' )) ) {
602 if ( ad_mkdir( path, 0777 ) < 0 ) {
609 static int ad_mkrf_ea(const char *path _U_)
611 AFP_PANIC("ad_mkrf_ea: dont use");
615 static int ad_mkrf_osx(const char *path _U_)
621 if we are root change path user/ group
622 It can be a native function for BSD cf. FAQ.Q10
623 path: pathname to chown
624 stbuf: parent directory inode
626 use fstat and fchown or lchown with linux?
628 #define EMULATE_SUIDDIR
630 static int ad_chown(const char *path, struct stat *stbuf)
633 #ifdef EMULATE_SUIDDIR
636 if (default_uid != (uid_t)-1) {
637 /* we are root (admin) */
638 id = (default_uid)?default_uid:stbuf->st_uid;
639 ret = lchown( path, id, stbuf->st_gid );
645 #define DEFMASK 07700 /* be conservative */
648 return access right and inode of path parent directory
650 static int ad_mode_st(const char *path, mode_t *mode, struct stat *stbuf)
655 if (ad_stat(path, stbuf) != 0) {
659 *mode &= stbuf->st_mode;
663 /* --------------------------- */
664 static int ad_header_upgrade(struct adouble *ad _U_, const char *name _U_)
669 static int ad_header_upgrade_ea(struct adouble *ad _U_, const char *name _U_)
671 AFP_PANIC("ad_header_upgrade_ea: dont use");
676 * Error handling for adouble header(=metadata) file open error
678 * We're called because opening ADFLAGS_HF caused an error.
679 * 1. In case ad_open is called with ADFLAGS_NOHF the error is suppressed.
680 * 2. If ad_open was called with ADFLAGS_DF we may have opened the datafork and thus
681 * ought to close it before returning with an error condition.
683 static int ad_error(struct adouble *ad, int adflags)
686 if (adflags & ADFLAGS_NOHF) { /* 1 */
687 ad->ad_adflags &= ~ADFLAGS_HF;
690 if (adflags & ADFLAGS_DF) { /* 2 */
691 ad_close( ad, ADFLAGS_DF );
697 /* Map ADFLAGS to open() flags */
698 static int ad2openflags(int adflags)
702 if (adflags & ADFLAGS_RDWR)
704 if (adflags & ADFLAGS_RDONLY) {
705 if (adflags & ADFLAGS_SETSHRMD)
710 if (adflags & ADFLAGS_CREATE)
712 if (adflags & ADFLAGS_EXCL)
714 if (adflags & ADFLAGS_TRUNC)
720 static int ad_open_df(const char *path, int adflags, mode_t mode, struct adouble *ad)
728 LOG(log_debug, logtype_default, "ad_open_df(\"%s\", %04o)",
729 fullpathname(path), mode);
731 if (ad_data_fileno(ad) != -1) {
732 /* the file is already open, but we want write access: */
733 if ((adflags & ADFLAGS_RDWR)
734 /* and it was denied the first time: */
735 && (ad->ad_data_fork.adf_flags & O_RDONLY)) {
739 /* it's not new anymore */
740 ad->ad_data_fork.adf_flags &= ~( O_TRUNC | O_CREAT );
741 ad->ad_data_fork.adf_refcount++;
745 oflags = O_NOFOLLOW | ad2openflags(adflags);
748 if ((adflags & ADFLAGS_CREATE)) {
749 st_invalid = ad_mode_st(path, &admode, &st_dir);
750 if ((ad->ad_options & ADVOL_UNIXPRIV))
754 ad->ad_data_fork.adf_fd = open(path, oflags, admode);
756 if (ad->ad_data_fork.adf_fd == -1) {
761 if ((adflags & ADFLAGS_SETSHRMD) && (adflags & ADFLAGS_RDONLY)) {
764 if ((ad->ad_data_fork.adf_fd = open(path, oflags, admode)) == -1)
769 case OPEN_NOFOLLOW_ERRNO:
770 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
771 if ((lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN)) <= 0) {
772 free(ad->ad_data_fork.adf_syml);
775 ad->ad_data_fork.adf_syml[lsz] = 0;
776 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
784 ad_chown(path, &st_dir); /* just created, set owner if admin (root) */
786 ad->ad_data_fork.adf_flags = oflags;
787 adf_lock_init(&ad->ad_data_fork);
788 ad->ad_data_fork.adf_refcount++;
793 /* TODO: error handling */
794 static int ad_open_hf_v2(const char *path, int adflags, mode_t mode, struct adouble *ad)
798 struct stat *pst = NULL;
800 int oflags, nocreatflags;
804 if (ad_meta_fileno(ad) != -1) {
805 /* the file is already open, but we want write access: */
806 if ((adflags & ADFLAGS_RDWR) &&
807 /* and it was already denied: */
808 (ad->ad_mdp->adf_flags & O_RDONLY)) {
812 ad_refresh(path, ad);
813 /* it's not new anymore */
814 ad->ad_mdp->adf_flags &= ~( O_TRUNC | O_CREAT );
815 ad->ad_mdp->adf_refcount++;
819 ad_p = ad->ad_ops->ad_path(path, adflags);
820 oflags = O_NOFOLLOW | ad2openflags(adflags);
821 nocreatflags = oflags & ~(O_CREAT | O_EXCL);
823 ad->ad_mdp->adf_fd = open(ad_p, nocreatflags);
825 if (ad->ad_mdp->adf_fd != -1) {
826 ad->ad_mdp->adf_flags = nocreatflags;
832 if ((adflags & ADFLAGS_RDONLY) && (adflags & ADFLAGS_SETSHRMD)) {
833 nocreatflags &= ~O_RDWR;
834 nocreatflags |= O_RDONLY;
835 if ((ad->ad_mdp->adf_fd = open(ad_p, nocreatflags)) == -1)
837 ad->ad_mdp->adf_flags = nocreatflags;
842 if (!(oflags & O_CREAT))
843 return ad_error(ad, adflags);
845 * We're expecting to create a new adouble header file here
847 LOG(log_debug, logtype_default, "ad_open(\"%s\"): creating adouble file",
851 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
852 if ((ad->ad_options & ADVOL_UNIXPRIV))
854 admode = ad_hf_mode(admode);
855 if ((errno == ENOENT) && (ad->ad_vers != AD_VERSION2_OSX)) {
856 if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
857 return ad_error(ad, adflags);
860 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
861 if ((ad->ad_options & ADVOL_UNIXPRIV))
863 admode = ad_hf_mode(admode);
866 /* retry with O_CREAT */
867 ad->ad_mdp->adf_fd = open(ad_p, oflags, admode);
868 if ( ad->ad_mdp->adf_fd < 0 )
869 return ad_error(ad, adflags);
871 ad->ad_mdp->adf_flags = oflags;
872 /* just created, set owner if admin owner (root) */
874 ad_chown(ad_p, &st_dir);
881 if (!(ad->ad_mdp->adf_flags & O_CREAT)) {
882 /* check for 0 length files, treat them as new. */
883 if (fstat(ad->ad_mdp->adf_fd, &st_meta) == 0) {
884 if (st_meta.st_size == 0)
885 ad->ad_mdp->adf_flags |= O_TRUNC;
887 /* we have valid data in st_meta stat structure, reused it in ad_header_read */
892 adf_lock_init(ad->ad_mdp);
893 ad->ad_mdp->adf_refcount = 1;
895 if ((ad->ad_mdp->adf_flags & ( O_TRUNC | O_CREAT ))) {
896 /* This is a new adouble header file, create it */
897 if (new_ad_header(ad, path, pst, adflags) < 0) {
899 /* the file is already deleted, perm, whatever, so return an error */
900 ad_close(ad, adflags);
906 /* Read the adouble header in and parse it.*/
907 if (ad->ad_ops->ad_header_read(path, ad, pst) < 0
908 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0) {
910 ad_close(ad, adflags);
919 /* TODO: error handling */
920 static int ad_open_hf_ea(const char *path, int adflags, int mode, struct adouble *ad)
925 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\", %s, %04o)",
926 path, adflags2logstr(adflags), mode);
928 oflags = O_NOFOLLOW | (ad2openflags(adflags) & ~(O_CREAT | O_TRUNC));
930 if (ad_meta_fileno(ad) != -1) {
931 /* the file is already open, but we want write access: */
932 if ((adflags & ADFLAGS_RDWR) &&
933 /* and it was already denied: */
934 (ad->ad_mdp->adf_flags & O_RDONLY)) {
935 LOG(log_error, logtype_default, "ad_open_hf_ea(%s): rw request for ro file: %s",
936 fullpathname(path), strerror(errno));
940 /* it's not new anymore */
941 ad->ad_mdp->adf_flags &= ~( O_TRUNC | O_CREAT );
942 ad->ad_mdp->adf_refcount++;
944 if (adflags & ADFLAGS_RDWR) {
945 /* Fo a RDONLY adouble we just use sys_lgetxattr instead if sys_fgetxattr */
946 if (adflags & ADFLAGS_DIR)
947 /* For directories we open the directory RDONYL so we can later fchdir() */
948 oflags = (oflags & ~O_RDWR) | O_RDONLY;
949 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): opening base file for meta adouble EA", path);
950 if ((ad_meta_fileno(ad) = open(path, oflags)) == -1)
952 ad->ad_mdp->adf_flags = oflags;
954 ad->ad_mdp->adf_refcount = 1;
957 /* Read the adouble header in and parse it.*/
958 if (ad->ad_ops->ad_header_read(path, ad, NULL) != 0) {
959 LOG(log_error, logtype_default, "ad_open_hf_ea: no EA adouble");
961 if (!(adflags & ADFLAGS_CREATE))
964 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): creating metadata EA", path);
966 /* It doesnt exist, EPERM or another error */
967 if (!(errno == ENOATTR || errno == ENOENT)) {
968 LOG(log_error, logtype_default, "ad_open_hf_ea: unexpected: %s", strerror(errno));
973 if (new_ad_header(ad, path, NULL, adflags) < 0) {
974 LOG(log_error, logtype_default, "ad_open_hf_ea: can't create new header: %s",
978 ad->ad_mdp->adf_flags |= O_CREAT; /* mark as just created */
980 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): created metadata EA", path);
983 (void)ad_reso_size(path, adflags, ad);
988 if (ad_meta_fileno(ad) != -1) {
989 close(ad_meta_fileno(ad));
990 ad_meta_fileno(ad) = -1;
991 ad->ad_mdp->adf_refcount = 0;
993 return ad_error(ad, adflags);
996 static int ad_open_hf(const char *path, int adflags, int mode, struct adouble *ad)
1000 LOG(log_debug, logtype_default, "ad_open_hf(\"%s\", %04o)", path, mode);
1002 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
1005 switch (ad->ad_vers) {
1007 ret = ad_open_hf_v2(path, adflags, mode, ad);
1010 ret = ad_open_hf_ea(path, adflags, mode, ad);
1018 return ad_error(ad, adflags);
1024 * Get resofork length for adouble:ea
1026 static int ad_reso_size(const char *path, int adflags, struct adouble *ad)
1031 LOG(log_debug, logtype_default, "ad_reso_size(\"%s\")", path);
1035 int eafd = ad_reso_fileno(ad);
1037 EC_NEG1( eafd = sys_getxattrfd(path, O_RDONLY) );
1040 EC_NEG1( rlen = fstat(eafd, &st) );
1041 ad->ad_rlen = st.st_size;
1044 EC_NULL_LOG( rfpath = ad->ad_ops->ad_path(path, adflags));
1045 EC_ZERO( lstat(rfpath, &st));
1046 if (st.st_size > ADEDOFF_RFORK_OSX)
1047 ad->ad_rlen = st.st_size - ADEDOFF_RFORK_OSX;
1052 LOG(log_debug, logtype_default, "ad_reso_size(\"%s\"): size: %zd", path, ad->ad_rlen);
1065 * Open ressource fork
1067 * Only for adouble:ea, a nullop otherwise because adouble:v2 has the ressource fork as part
1068 * of the adouble file which is openend by ADFLAGS_HF.
1070 static int ad_open_rf(const char *path, int adflags, int mode, struct adouble *ad)
1074 int closeflags = adflags & (ADFLAGS_DF | ADFLAGS_HF);
1081 if (ad->ad_vers != AD_VERSION_EA)
1084 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\", %04o)", path, mode);
1086 oflags = O_NOFOLLOW | (ad2openflags(adflags) & ~O_CREAT);
1088 if (ad_reso_fileno(ad) != -1) {
1089 /* the file is already open, but we want write access: */
1090 if ((adflags & ADFLAGS_RDWR)
1091 /* and it was already denied: */
1092 && (ad->ad_rfp->adf_flags & O_RDONLY)) {
1096 ad->ad_rfp->adf_flags &= ~( O_TRUNC | O_CREAT );
1097 ad->ad_rfp->adf_refcount++;
1098 EC_NEG1_LOG( ad_reso_size(path, adflags, ad));
1102 if ((ad_reso_fileno(ad) = sys_getxattrfd(path, oflags)) == -1) {
1103 if (!(adflags & ADFLAGS_CREATE))
1106 EC_NEG1_LOG( ad_reso_fileno(ad) = sys_getxattrfd(path, oflags, 0666) );
1109 EC_NULL_LOG( rfpath = ad->ad_ops->ad_path(path, adflags) );
1110 if ((ad_reso_fileno(ad) = open(rfpath, oflags)) == -1) {
1111 if (!(adflags & ADFLAGS_CREATE))
1114 EC_NEG1_LOG( ad_reso_fileno(ad) = open(rfpath, oflags, mode) );
1115 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): created adouble rfork: \"%s\"",
1120 ad->ad_rfp->adf_refcount = 1;
1121 ad->ad_rfp->adf_flags = oflags;
1124 EC_ZERO_LOG( fstat(ad_reso_fileno(ad), &st) );
1125 if (ad->ad_rfp->adf_flags & O_CREAT) {
1126 /* This is a new adouble header file, create it */
1127 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): created adouble rfork, initializing: \"%s\"",
1129 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): created adouble rfork, flushing: \"%s\"",
1133 /* Read the adouble header */
1134 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): reading adouble rfork: \"%s\"",
1136 EC_NEG1_LOG( ad_header_read_osx(NULL, ad, &st) );
1140 EC_NEG1_LOG( ad_reso_size(path, adflags, ad));
1144 if (ad_reso_fileno(ad) != -1) {
1145 close(ad_reso_fileno(ad));
1146 ad_reso_fileno(ad) = -1;
1147 ad->ad_rfp->adf_refcount = 0;
1150 (void)ad_close(ad, closeflags);
1155 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): END", path);
1160 /***********************************************************************************
1162 ********************************************************************************* */
1164 const char *ad_path_ea( const char *path, int adflags _U_)
1169 const char *ad_path_osx(const char *path, int adflags _U_)
1171 static char pathbuf[ MAXPATHLEN + 1];
1172 char c, *slash, buf[MAXPATHLEN + 1];
1174 if (!strcmp(path,".")) {
1176 getcwd(buf, MAXPATHLEN);
1179 strlcpy(buf, path, MAXPATHLEN +1);
1181 if (NULL != ( slash = strrchr( buf, '/' )) ) {
1184 strlcpy( pathbuf, buf, MAXPATHLEN +1);
1187 pathbuf[ 0 ] = '\0';
1190 strlcat( pathbuf, "._", MAXPATHLEN +1);
1191 strlcat( pathbuf, slash, MAXPATHLEN +1);
1196 * Put the .AppleDouble where it needs to be:
1198 * / a/.AppleDouble/b
1200 * \ b/.AppleDouble/.Parent
1202 * FIXME: should do something for pathname > MAXPATHLEN
1204 const char *ad_path( const char *path, int adflags)
1206 static char pathbuf[ MAXPATHLEN + 1];
1210 if ( adflags & ADFLAGS_DIR ) {
1211 l = strlcpy( pathbuf, path, sizeof(pathbuf));
1213 if ( l && l < MAXPATHLEN) {
1216 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
1218 if (NULL != ( slash = strrchr( path, '/' )) ) {
1221 /* XXX we must return NULL here and test in the caller */
1224 memcpy( pathbuf, path, l);
1229 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
1230 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
1236 /* -------------------------
1237 * Support inherited protection modes for AppleDouble files. The supplied
1238 * mode is ANDed with the parent directory's mask value in lieu of "umask",
1239 * and that value is returned.
1241 char *ad_dir(const char *path)
1243 static char modebuf[ MAXPATHLEN + 1];
1246 * For a path with directories in it, remove the final component
1247 * (path or subdirectory name) to get the name we want to stat.
1248 * For a path which is just a filename, use "." instead.
1250 slash = strrchr( path, '/' );
1255 if (len >= MAXPATHLEN) {
1256 errno = ENAMETOOLONG;
1257 return NULL; /* can't do it */
1259 memcpy( modebuf, path, len );
1260 modebuf[len] = '\0';
1261 /* is last char a '/' ? */
1262 if (slash[1] == 0) {
1263 slash = modebuf+ len;
1265 while (modebuf < slash && slash[-1] == '/') {
1268 if (modebuf == slash) {
1272 while (modebuf < slash && *slash != '/') {
1275 if (modebuf == slash) {
1278 *slash = '\0'; /* remove pathname component */
1283 modebuf[0] = '.'; /* use current directory */
1288 int ad_setfuid(const uid_t id)
1294 /* ---------------- */
1295 uid_t ad_getfuid(void)
1301 stat path parent directory
1303 int ad_stat(const char *path, struct stat *stbuf)
1310 return lstat( p, stbuf );
1314 return access right of path parent directory
1316 int ad_mode( const char *path, mode_t mode)
1319 ad_mode_st(path, &mode, &stbuf);
1324 * Use mkdir() with mode bits taken from ad_mode().
1326 int ad_mkdir( const char *path, mode_t mode)
1332 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
1333 path, mode, getcwdpath());
1335 st_invalid = ad_mode_st(path, &mode, &stbuf);
1336 ret = mkdir( path, mode );
1337 if (ret || st_invalid)
1339 ad_chown(path, &stbuf);
1344 static void ad_init_func(struct adouble *ad)
1346 switch (ad->ad_vers) {
1348 ad->ad_ops = &ad_adouble;
1349 ad->ad_rfp = &ad->ad_resource_fork;
1350 ad->ad_mdp = &ad->ad_resource_fork;
1353 ad->ad_ops = &ad_adouble_ea;
1354 ad->ad_rfp = &ad->ad_resource_fork;
1355 ad->ad_mdp = &ad->ad_data_fork;
1358 AFP_PANIC("ad_init: unknown AD version");
1362 ad_data_fileno(ad) = -1;
1363 ad_reso_fileno(ad) = -1;
1364 ad_meta_fileno(ad) = -1;
1365 ad->ad_refcount = 1;
1369 void ad_init_old(struct adouble *ad, int flags, int options)
1371 memset(ad, 0, sizeof(struct adouble));
1372 ad->ad_vers = flags;
1373 ad->ad_options = options;
1377 void ad_init(struct adouble *ad, const struct vol * restrict vol)
1379 memset(ad, 0, sizeof(struct adouble));
1380 ad->ad_vers = vol->v_adouble;
1381 ad->ad_options = vol->v_ad_options;
1386 * Open data-, metadata(header)- or ressource fork
1388 * ad_open(struct adouble *ad, const char *path, int adflags, int flags)
1389 * ad_open(struct adouble *ad, const char *path, int adflags, int flags, mode_t mode)
1391 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
1394 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1397 * Open a files data fork, metadata fork or ressource fork.
1399 * @param ad (rw) pointer to struct adouble
1400 * @param path (r) Path to file or directory
1401 * @param adflags (r) Flags specifying which fork to open, can be or'd:
1402 * ADFLAGS_DF: open data fork
1403 * ADFLAGS_RF: open ressource fork
1404 * ADFLAGS_HF: open header (metadata) file
1405 * ADFLAGS_NOHF: it's not an error if header file couldn't be created
1406 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags
1408 * Access mode for the forks:
1409 * ADFLAGS_RDONLY: open read only
1410 * ADFLAGS_RDWR: open read write
1413 * ADFLAGS_CREATE: create if not existing
1414 * ADFLAGS_TRUNC: truncate
1417 * ADFLAGS_CHECK_OF: check for open forks from us and other afpd's
1418 * ADFLAGS_SETSHRMD: this adouble struct will be used to set sharemode locks.
1419 * This basically results in the files being opened RW instead of RDONLY.
1420 * @param mode (r) mode used with O_CREATE
1422 * The open mode flags (rw vs ro) have to take into account all the following requirements:
1423 * - we remember open fds for files because me must avoid a single close releasing fcntl locks for other
1424 * fds of the same file
1426 * @returns 0 on success, any other value indicates an error
1428 int ad_open(struct adouble *ad, const char *path, int adflags, ...)
1434 LOG(log_debug, logtype_default, "ad_open(\"%s\", %s)",
1435 fullpathname(path), adflags2logstr(adflags));
1437 if (adflags & ADFLAGS_CHECK_OF)
1438 /* Checking for open forks requires sharemode lock support (ie RDWR instead of RDONLY) */
1439 adflags |= ADFLAGS_SETSHRMD;
1441 if ((ad->ad_vers == AD_VERSION_EA) && (adflags & ADFLAGS_SETSHRMD))
1442 /* adouble:ea sets sharemode locks on the datafork */
1443 adflags |= ADFLAGS_DF;
1445 if (ad->ad_inited != AD_INITED) {
1446 ad->ad_adflags = adflags;
1447 ad->ad_inited = AD_INITED;
1449 ad->ad_open_forks = ((ad->ad_data_fork.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
1450 if (ad->ad_resource_fork.adf_refcount > 0)
1451 ad->ad_open_forks |= ATTRBIT_ROPEN;
1454 va_start(args, adflags);
1455 if (adflags & ADFLAGS_CREATE)
1456 mode = va_arg(args, mode_t);
1459 if (adflags & ADFLAGS_DF) {
1460 EC_ZERO( ad_open_df(path, adflags, mode, ad) );
1461 ad->ad_adflags |= ADFLAGS_DF;
1464 if (adflags & ADFLAGS_HF) {
1465 EC_ZERO( ad_open_hf(path, adflags, mode, ad) );
1466 ad->ad_adflags |= ADFLAGS_HF;
1469 if (adflags & ADFLAGS_RF) {
1470 EC_ZERO( ad_open_rf(path, adflags, mode, ad) );
1471 ad->ad_adflags |= ADFLAGS_RF;
1474 if (adflags & ADFLAGS_CHECK_OF) {
1475 ad->ad_open_forks |= ad_openforks(ad, ad->ad_open_forks);
1483 * @brief open metadata, possibly as root
1485 * Return only metadata but try very hard ie at first try as user, then try as root.
1487 * @param name name of file/dir
1488 * @param flags ADFLAGS_DIR: name is a directory \n
1489 * ADFLAGS_CHECK_OF: test if name is open by us or another afpd process
1491 * @param adp pointer to struct adouble
1493 int ad_metadata(const char *name, int flags, struct adouble *adp)
1496 int ret, err, oflags;
1498 /* Sanitize flags */
1499 oflags = (flags & (ADFLAGS_CHECK_OF | ADFLAGS_DIR)) | ADFLAGS_HF | ADFLAGS_RDONLY;
1501 if ((ret = ad_open(adp, name, oflags)) < 0 && errno == EACCES) {
1504 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1508 /* we are root open read only */
1509 ret = ad_open(adp, name, oflags);
1511 if ( seteuid(uid) < 0) {
1512 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1522 * @brief openat like wrapper for ad_metadata
1524 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1530 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1536 if (ad_metadata(name, flags, adp) < 0) {
1542 if (fchdir(cwdfd) != 0) {
1543 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1556 int ad_refresh(const char *path, struct adouble *ad)
1558 switch (ad->ad_vers) {
1560 if (ad_meta_fileno(ad) == -1)
1562 return ad->ad_ops->ad_header_read(NULL, ad, NULL);
1566 if (AD_META_OPEN(ad)) {
1567 if (ad_data_fileno(ad) == -1)
1569 // TODO: read meta EA
1572 if (AD_RSRC_OPEN(ad)) {
1573 if (ad_reso_fileno(ad) == -1)
1576 if ((len = fstat(ad_reso_fileno(ad))) == -1)
1581 if (AD_META_OPEN(ad)) {
1582 if (ad_data_fileno(ad) == -1)
1584 // TODO: read meta EA
1586 if (AD_RSRC_OPEN(ad)) {
1587 if (ad_reso_fileno(ad) == -1)
1589 if (ad_header_read_osx(path, ad, NULL) < 0)
1593 return ad->ad_ops->ad_header_read(path, ad, NULL);
1602 int ad_openat(struct adouble *ad,
1603 int dirfd, /* dir fd openat like */
1613 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0))
1617 va_start(args, adflags);
1618 if (adflags & ADFLAGS_CREATE)
1619 mode = va_arg(args, mode_t);
1622 EC_NEG1( ad_open(ad, path, adflags, mode) );
1625 if (fchdir(cwdfd) != 0) {
1626 AFP_PANIC("ad_openat: cant chdir back");