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 int ad_mkrf_osx(const char *path);
113 static struct adouble_fops ad_adouble = {
116 &ad_rebuild_adouble_header,
121 static struct adouble_fops ad_adouble_ea = {
129 &ad_rebuild_adouble_header,
131 &ad_header_upgrade_ea,
134 static struct adouble_fops ad_osx = {
137 &ad_rebuild_adouble_header,
142 static const struct entry entry_order2[ADEID_NUM_V2 + 1] = {
143 {ADEID_NAME, ADEDOFF_NAME_V2, ADEDLEN_INIT},
144 {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
145 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
146 {ADEID_FINDERI, ADEDOFF_FINDERI_V2, ADEDLEN_FINDERI},
147 {ADEID_DID, ADEDOFF_DID, ADEDLEN_DID},
148 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
149 {ADEID_SHORTNAME, ADEDOFF_SHORTNAME, ADEDLEN_INIT},
150 {ADEID_PRODOSFILEI, ADEDOFF_PRODOSFILEI, ADEDLEN_PRODOSFILEI},
151 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV, ADEDLEN_INIT},
152 {ADEID_PRIVINO, ADEDOFF_PRIVINO, ADEDLEN_INIT},
153 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN, ADEDLEN_INIT},
154 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
155 {ADEID_RFORK, ADEDOFF_RFORK_V2, ADEDLEN_INIT},
159 /* Using Extended Attributes */
160 static const struct entry entry_order_ea[ADEID_NUM_EA + 1] = {
161 {ADEID_FINDERI, ADEDOFF_FINDERI_EA, ADEDLEN_FINDERI},
162 {ADEID_COMMENT, ADEDOFF_COMMENT_EA, ADEDLEN_INIT},
163 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_EA, ADEDLEN_FILEDATESI},
164 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_EA, ADEDLEN_AFPFILEI},
165 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
169 /* fallback for EAs */
170 static const struct entry entry_order_osx[ADEID_NUM_OSX +1] = {
171 {ADEID_FINDERI, ADEDOFF_FINDERI_OSX, ADEDLEN_FINDERI},
172 {ADEID_RFORK, ADEDOFF_RFORK_OSX, ADEDLEN_INIT},
176 #define ADFLAGS2LOGSTRBUFSIZ 128
177 const char *adflags2logstr(int adflags)
180 static char buf[ADFLAGS2LOGSTRBUFSIZ];
184 if (adflags & ADFLAGS_DF) {
185 strlcat(buf, "DF", ADFLAGS2LOGSTRBUFSIZ);
188 if (adflags & ADFLAGS_RF) {
190 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
191 strlcat(buf, "RF", ADFLAGS2LOGSTRBUFSIZ);
194 if (adflags & ADFLAGS_NORF) {
196 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
197 strlcat(buf, "NORF", ADFLAGS2LOGSTRBUFSIZ);
200 if (adflags & ADFLAGS_HF) {
202 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
203 strlcat(buf, "HF", ADFLAGS2LOGSTRBUFSIZ);
206 if (adflags & ADFLAGS_NOHF) {
208 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
209 strlcat(buf, "NOHF", ADFLAGS2LOGSTRBUFSIZ);
212 if (adflags & ADFLAGS_DIR) {
214 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
215 strlcat(buf, "DIR", ADFLAGS2LOGSTRBUFSIZ);
218 if (adflags & ADFLAGS_CHECK_OF) {
220 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
221 strlcat(buf, "OF", ADFLAGS2LOGSTRBUFSIZ);
224 if (adflags & ADFLAGS_SETSHRMD) {
226 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
227 strlcat(buf, "SHRMD", ADFLAGS2LOGSTRBUFSIZ);
230 if (adflags & ADFLAGS_RDWR) {
232 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
233 strlcat(buf, "O_RDWR", ADFLAGS2LOGSTRBUFSIZ);
236 if (adflags & ADFLAGS_RDONLY) {
238 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
239 strlcat(buf, "O_RDONLY", ADFLAGS2LOGSTRBUFSIZ);
242 if (adflags & ADFLAGS_CREATE) {
244 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
245 strlcat(buf, "O_CREAT", ADFLAGS2LOGSTRBUFSIZ);
248 if (adflags & ADFLAGS_EXCL) {
250 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
251 strlcat(buf, "O_EXCL", ADFLAGS2LOGSTRBUFSIZ);
254 if (adflags & ADFLAGS_TRUNC) {
256 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
257 strlcat(buf, "O_TRUNC", ADFLAGS2LOGSTRBUFSIZ);
264 static uint32_t get_eid(uint32_t eid)
269 return ADEID_PRIVDEV;
271 return ADEID_PRIVINO;
273 return ADEID_PRIVSYN;
280 /* ----------------------------------- */
281 static int new_ad_header(struct adouble *ad, const char *path, struct stat *stp, int adflags)
283 const struct entry *eid;
287 LOG(log_debug, logtype_default, "new_ad_header(\"%s\")", path);
291 if (lstat(path, &st) != 0)
295 ad->ad_magic = AD_MAGIC;
296 ad->ad_version = ad->ad_vers & 0x0f0000;
297 if (!ad->ad_version) {
298 ad->ad_version = AD_VERSION;
301 memset(ad->ad_data, 0, sizeof(ad->ad_data));
303 if (ad->ad_vers == AD_VERSION2)
305 else if (ad->ad_vers == AD_VERSION_EA)
306 eid = entry_order_ea;
307 else if (ad->ad_vers == AD_VERSION2_OSX)
308 eid = entry_order_osx;
314 ad->ad_eid[eid->id].ade_off = eid->offset;
315 ad->ad_eid[eid->id].ade_len = eid->len;
319 /* put something sane in the directory finderinfo */
320 if (ad->ad_vers != AD_VERSION2_OSX) {
321 if ((adflags & ADFLAGS_DIR)) {
322 /* set default view */
323 ashort = htons(FINDERINFO_CLOSEDVIEW);
324 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
326 /* set default creator/type fields */
327 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
328 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
331 /* make things invisible */
332 if ((ad->ad_options & ADVOL_INVDOTS)
334 && !((adflags & ADFLAGS_DIR) && (path[1] == 0))
336 ashort = htons(ATTRBIT_INVISIBLE);
337 ad_setattr(ad, ashort);
338 ashort = htons(FINDERINFO_INVISIBLE);
339 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
342 /* put something sane in the date fields */
343 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, stp->st_mtime);
344 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, stp->st_mtime);
345 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, stp->st_mtime);
346 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
351 /* -------------------------------------
354 static void parse_entries(struct adouble *ad, char *buf, uint16_t nentries)
356 uint32_t eid, len, off;
359 /* now, read in the entry bits */
360 for (; nentries > 0; nentries-- ) {
361 memcpy(&eid, buf, sizeof( eid ));
362 eid = get_eid(ntohl(eid));
363 buf += sizeof( eid );
364 memcpy(&off, buf, sizeof( off ));
366 buf += sizeof( off );
367 memcpy(&len, buf, sizeof( len ));
369 buf += sizeof( len );
373 && off < sizeof(ad->ad_data)
374 && (off + len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
375 ad->ad_eid[ eid ].ade_off = off;
376 ad->ad_eid[ eid ].ade_len = len;
377 } else if (!warning) {
379 LOG(log_warning, logtype_default, "parse_entries: bogus eid: %d", eid);
384 /* this reads enough of the header so that we can figure out all of
385 * the entry lengths and offsets. once that's done, we just read/mmap
386 * the rest of the header in.
388 * NOTE: we're assuming that the resource fork is kept at the end of
389 * the file. also, mmapping won't work for the hfs fs until it
390 * understands how to mmap header files. */
391 static int ad_header_read(const char *path _U_, struct adouble *ad, struct stat *hst)
393 char *buf = ad->ad_data;
399 /* read the header */
400 if ((header_len = adf_pread( ad->ad_mdp, buf, AD_DATASZ, 0)) < 0) {
403 if (header_len < AD_HEADER_LEN) {
408 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
409 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
410 ad->ad_magic = ntohl( ad->ad_magic );
411 ad->ad_version = ntohl( ad->ad_version );
413 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
414 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
419 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
420 nentries = ntohs( nentries );
422 /* read in all the entry headers. if we have more than the
423 * maximum, just hope that the rfork is specified early on. */
424 len = nentries*AD_ENTRY_LEN;
426 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
427 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
429 buf += AD_HEADER_LEN;
430 if (len > header_len - AD_HEADER_LEN) {
431 LOG(log_error, logtype_default, "ad_header_read: can't read entry info.");
436 /* figure out all of the entry offsets and lengths. if we aren't
437 * able to read a resource fork entry, bail. */
438 nentries = len / AD_ENTRY_LEN;
439 parse_entries(ad, buf, nentries);
440 if (!ad_getentryoff(ad, ADEID_RFORK)
441 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
443 LOG(log_error, logtype_default, "ad_header_read: problem with rfork entry offset.");
448 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
449 LOG(log_error, logtype_default, "ad_header_read: can't read in entries.");
456 if (fstat(ad->ad_mdp->adf_fd, &st) < 0) {
457 return 1; /* fail silently */
461 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
466 /* Read an ._ file, only uses the resofork, finderinfo is taken from EA */
467 static int ad_header_read_osx(const char *path _U_, struct adouble *ad, struct stat *hst)
470 struct adouble adosx;
471 char *buf = &adosx.ad_data[0];
477 memset(buf, 0, sizeof(adosx.ad_data));
479 /* read the header */
480 EC_NEG1( header_len = adf_pread(ad->ad_rfp, buf, AD_DATASZ_OSX, 0) );
482 if (header_len < AD_HEADER_LEN) {
487 memcpy(&adosx.ad_magic, buf, sizeof(adosx.ad_magic));
488 memcpy(&adosx.ad_version, buf + ADEDOFF_VERSION, sizeof(adosx.ad_version));
489 adosx.ad_magic = ntohl(adosx.ad_magic);
490 adosx.ad_version = ntohl(adosx.ad_version);
492 if ((adosx.ad_magic != AD_MAGIC) || (adosx.ad_version != AD_VERSION2)) {
493 LOG(log_error, logtype_afpd, "ad_header_read_osx: can't parse AppleDouble header");
498 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
499 nentries = ntohs(nentries);
500 len = nentries * AD_ENTRY_LEN;
502 if (len + AD_HEADER_LEN > sizeof(adosx.ad_data))
503 len = sizeof(adosx.ad_data) - AD_HEADER_LEN;
505 buf += AD_HEADER_LEN;
506 if (len > header_len - AD_HEADER_LEN) {
507 LOG(log_error, logtype_afpd, "ad_header_read_osx: can't read entry info.");
512 nentries = len / AD_ENTRY_LEN;
513 parse_entries(&adosx, buf, nentries);
515 if (ad_getentryoff(&adosx, ADEID_RFORK) == 0
516 || ad_getentryoff(&adosx, ADEID_RFORK) > sizeof(ad->ad_data)
517 || ad_getentryoff(&adosx, ADEID_RFORK) > header_len
519 LOG(log_error, logtype_afpd, "ad_header_read_osx: problem with rfork entry offset.");
526 EC_NEG1( fstat(ad_reso_fileno(ad), &st) );
529 ad_setentryoff(ad, ADEID_RFORK, ad_getentryoff(&adosx, ADEID_RFORK));
530 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
536 static int ad_header_read_ea(const char *path, struct adouble *ad, struct stat *hst _U_)
541 char *buf = ad->ad_data;
543 if (ad_meta_fileno(ad) != -1)
544 header_len = sys_fgetxattr(ad_meta_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA);
546 header_len = sys_lgetxattr(path, AD_EA_META, ad->ad_data, AD_DATASZ_EA);
547 if (header_len < 1) {
548 LOG(log_debug, logtype_default, "ad_header_read_ea: %s", strerror(errno));
552 if (header_len < AD_HEADER_LEN) {
553 LOG(log_error, logtype_default, "ad_header_read_ea: bogus AppleDouble header.");
558 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
559 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
561 ad->ad_magic = ntohl( ad->ad_magic );
562 ad->ad_version = ntohl( ad->ad_version );
564 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
565 LOG(log_error, logtype_default, "ad_header_read_ea: wrong magic or version");
570 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
571 nentries = ntohs( nentries );
573 /* Protect against bogus nentries */
574 len = nentries * AD_ENTRY_LEN;
575 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
576 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
577 if (len > header_len - AD_HEADER_LEN) {
578 LOG(log_error, logtype_default, "ad_header_read_ea: can't read entry info.");
582 nentries = len / AD_ENTRY_LEN;
584 /* Now parse entries */
585 parse_entries(ad, buf + AD_HEADER_LEN, nentries);
591 * Takes a path to an AppleDouble file and creates the parrent .AppleDouble directory
594 * path: "/path/.AppleDouble/file"
595 * => mkdir("/path/.AppleDouble/") (in ad_mkdir())
597 static int ad_mkrf(const char *path)
601 * Probably .AppleDouble doesn't exist, try to mkdir it.
603 if (NULL == ( slash = strrchr( path, '/' )) ) {
608 if ( ad_mkdir( path, 0777 ) < 0 ) {
615 static int ad_mkrf_ea(const char *path _U_)
617 AFP_PANIC("ad_mkrf_ea: dont use");
621 static int ad_mkrf_osx(const char *path _U_)
627 if we are root change path user/ group
628 It can be a native function for BSD cf. FAQ.Q10
629 path: pathname to chown
630 stbuf: parent directory inode
632 use fstat and fchown or lchown with linux?
634 #define EMULATE_SUIDDIR
636 static int ad_chown(const char *path, struct stat *stbuf)
639 #ifdef EMULATE_SUIDDIR
642 if (default_uid != (uid_t)-1) {
643 /* we are root (admin) */
644 id = (default_uid)?default_uid:stbuf->st_uid;
645 ret = lchown( path, id, stbuf->st_gid );
651 #define DEFMASK 07700 /* be conservative */
654 return access right and inode of path parent directory
656 static int ad_mode_st(const char *path, mode_t *mode, struct stat *stbuf)
661 if (ad_stat(path, stbuf) != 0) {
665 *mode &= stbuf->st_mode;
669 /* --------------------------- */
670 static int ad_header_upgrade(struct adouble *ad _U_, const char *name _U_)
675 static int ad_header_upgrade_ea(struct adouble *ad _U_, const char *name _U_)
677 AFP_PANIC("ad_header_upgrade_ea: dont use");
682 * Error handling for adouble header(=metadata) file open error
684 * We're called because opening ADFLAGS_HF caused an error.
685 * 1. In case ad_open is called with ADFLAGS_NOHF the error is suppressed.
686 * 2. If ad_open was called with ADFLAGS_DF we may have opened the datafork and thus
687 * ought to close it before returning with an error condition.
689 static int ad_error(struct adouble *ad, int adflags)
692 if (adflags & ADFLAGS_NOHF) { /* 1 */
695 if (adflags & ADFLAGS_DF) { /* 2 */
696 ad_close( ad, ADFLAGS_DF );
702 /* Map ADFLAGS to open() flags */
703 static int ad2openflags(int adflags)
707 if (adflags & ADFLAGS_RDWR)
709 if (adflags & ADFLAGS_RDONLY) {
710 if (adflags & ADFLAGS_SETSHRMD)
715 if (adflags & ADFLAGS_CREATE)
717 if (adflags & ADFLAGS_EXCL)
719 if (adflags & ADFLAGS_TRUNC)
725 static int ad_open_df(const char *path, int adflags, mode_t mode, struct adouble *ad)
733 LOG(log_debug, logtype_default, "ad_open_df(\"%s\", %04o)",
734 fullpathname(path), mode);
736 if (ad_data_fileno(ad) != -1) {
737 /* the file is already open, but we want write access: */
738 if ((adflags & ADFLAGS_RDWR)
739 /* and it was denied the first time: */
740 && (ad->ad_data_fork.adf_flags & O_RDONLY)) {
744 /* it's not new anymore */
745 ad->ad_data_fork.adf_flags &= ~( O_TRUNC | O_CREAT );
746 ad->ad_data_fork.adf_refcount++;
750 oflags = O_NOFOLLOW | ad2openflags(adflags);
753 if ((adflags & ADFLAGS_CREATE)) {
754 st_invalid = ad_mode_st(path, &admode, &st_dir);
755 if ((ad->ad_options & ADVOL_UNIXPRIV))
759 ad->ad_data_fork.adf_fd = open(path, oflags, admode);
761 if (ad->ad_data_fork.adf_fd == -1) {
766 if ((adflags & ADFLAGS_SETSHRMD) && (adflags & ADFLAGS_RDONLY)) {
769 if ((ad->ad_data_fork.adf_fd = open(path, oflags, admode)) == -1)
774 case OPEN_NOFOLLOW_ERRNO:
775 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
776 if ((lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN)) <= 0) {
777 free(ad->ad_data_fork.adf_syml);
780 ad->ad_data_fork.adf_syml[lsz] = 0;
781 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
789 ad_chown(path, &st_dir); /* just created, set owner if admin (root) */
791 ad->ad_data_fork.adf_flags = oflags;
792 adf_lock_init(&ad->ad_data_fork);
793 ad->ad_data_fork.adf_refcount++;
798 /* TODO: error handling */
799 static int ad_open_hf_v2(const char *path, int adflags, mode_t mode, struct adouble *ad)
803 struct stat *pst = NULL;
805 int oflags, nocreatflags;
809 if (ad_meta_fileno(ad) != -1) {
810 /* the file is already open, but we want write access: */
811 if ((adflags & ADFLAGS_RDWR) &&
812 /* and it was already denied: */
813 (ad->ad_mdp->adf_flags & O_RDONLY)) {
817 ad_refresh(path, ad);
818 /* it's not new anymore */
819 ad->ad_mdp->adf_flags &= ~( O_TRUNC | O_CREAT );
820 ad->ad_mdp->adf_refcount++;
824 ad_p = ad->ad_ops->ad_path(path, adflags);
825 oflags = O_NOFOLLOW | ad2openflags(adflags);
826 nocreatflags = oflags & ~(O_CREAT | O_EXCL);
828 ad->ad_mdp->adf_fd = open(ad_p, nocreatflags);
830 if (ad->ad_mdp->adf_fd != -1) {
831 ad->ad_mdp->adf_flags = nocreatflags;
837 if ((adflags & ADFLAGS_RDONLY) && (adflags & ADFLAGS_SETSHRMD)) {
838 nocreatflags &= ~O_RDWR;
839 nocreatflags |= O_RDONLY;
840 if ((ad->ad_mdp->adf_fd = open(ad_p, nocreatflags)) == -1)
842 ad->ad_mdp->adf_flags = nocreatflags;
847 if (!(oflags & O_CREAT))
848 return ad_error(ad, adflags);
850 * We're expecting to create a new adouble header file here
852 LOG(log_debug, logtype_default, "ad_open(\"%s\"): creating adouble file",
856 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
857 if ((ad->ad_options & ADVOL_UNIXPRIV))
859 admode = ad_hf_mode(admode);
860 if (errno == ENOENT) {
861 if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
862 return ad_error(ad, adflags);
865 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
866 if ((ad->ad_options & ADVOL_UNIXPRIV))
868 admode = ad_hf_mode(admode);
871 /* retry with O_CREAT */
872 ad->ad_mdp->adf_fd = open(ad_p, oflags, admode);
873 if ( ad->ad_mdp->adf_fd < 0 )
874 return ad_error(ad, adflags);
876 ad->ad_mdp->adf_flags = oflags;
877 /* just created, set owner if admin owner (root) */
879 ad_chown(ad_p, &st_dir);
886 if (!(ad->ad_mdp->adf_flags & O_CREAT)) {
887 /* check for 0 length files, treat them as new. */
888 if (fstat(ad->ad_mdp->adf_fd, &st_meta) == 0) {
889 if (st_meta.st_size == 0)
890 ad->ad_mdp->adf_flags |= O_TRUNC;
892 /* we have valid data in st_meta stat structure, reused it in ad_header_read */
897 adf_lock_init(ad->ad_mdp);
898 ad->ad_mdp->adf_refcount = 1;
900 if ((ad->ad_mdp->adf_flags & ( O_TRUNC | O_CREAT ))) {
901 /* This is a new adouble header file, create it */
902 if (new_ad_header(ad, path, pst, adflags) < 0) {
904 /* the file is already deleted, perm, whatever, so return an error */
905 ad_close(ad, adflags);
911 /* Read the adouble header in and parse it.*/
912 if (ad->ad_ops->ad_header_read(path, ad, pst) < 0
913 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0) {
915 ad_close(ad, adflags);
924 static int ad_open_hf_ea(const char *path, int adflags, int mode, struct adouble *ad)
931 LOG(log_debug, logtype_default,
932 "ad_open_hf_ea(\"%s\", %s): BEGIN [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
933 fullpathname(path), adflags2logstr(adflags),
934 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
935 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
936 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
938 oflags = O_NOFOLLOW | (ad2openflags(adflags) & ~(O_CREAT | O_TRUNC));
940 if (ad_meta_fileno(ad) != -1) {
941 /* the file is already open, but we want write access: */
942 if ((oflags & O_RDWR) &&
943 /* and it was already denied: */
944 (ad->ad_mdp->adf_flags & O_RDONLY)) {
945 LOG(log_error, logtype_default, "ad_open_hf_ea(%s): rw request for ro file: %s",
946 fullpathname(path), strerror(errno));
950 /* it's not new anymore */
951 ad->ad_mdp->adf_flags &= ~( O_TRUNC | O_CREAT );
953 if (oflags & O_RDWR) {
954 /* Fo a RDONLY adouble we just use sys_lgetxattr instead if sys_fgetxattr */
955 if (adflags & ADFLAGS_DIR)
956 /* For directories we open the directory RDONYL so we can later fchdir() */
957 oflags = (oflags & ~O_RDWR) | O_RDONLY;
958 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): opening base file for meta adouble EA", path);
959 EC_NEG1(ad_meta_fileno(ad) = open(path, oflags));
961 ad->ad_mdp->adf_flags = oflags;
965 /* Read the adouble header in and parse it.*/
966 if (ad->ad_ops->ad_header_read(path, ad, NULL) != 0) {
967 if (!(adflags & ADFLAGS_CREATE)) {
972 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): creating metadata EA", path);
974 /* It doesnt exist, EPERM or another error */
975 if (!(errno == ENOATTR || errno == ENOENT)) {
976 LOG(log_error, logtype_default, "ad_open_hf_ea: unexpected: %s", strerror(errno));
981 EC_NEG1_LOG(new_ad_header(ad, path, NULL, adflags));
982 ad->ad_mdp->adf_flags |= O_CREAT; /* mark as just created */
984 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): created metadata EA", path);
987 ad->ad_mdp->adf_refcount++;
988 (void)ad_reso_size(path, adflags, ad);
991 if (ret != 0 && opened && ad_meta_fileno(ad) != -1) {
992 close(ad_meta_fileno(ad));
993 ad_meta_fileno(ad) = -1;
994 ad->ad_mdp->adf_refcount = 0;
996 LOG(log_debug, logtype_default,
997 "ad_open_hf_ea(\"%s\", %s): END: %d [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
998 fullpathname(path), adflags2logstr(adflags), ret,
999 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
1000 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
1001 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
1006 static int ad_open_hf(const char *path, int adflags, int mode, struct adouble *ad)
1010 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
1013 switch (ad->ad_vers) {
1015 ret = ad_open_hf_v2(path, adflags, mode, ad);
1018 ret = ad_open_hf_ea(path, adflags, mode, ad);
1026 ret = ad_error(ad, adflags);
1032 * Get resofork length for adouble:ea
1034 static int ad_reso_size(const char *path, int adflags, struct adouble *ad)
1039 LOG(log_debug, logtype_default, "ad_reso_size(\"%s\")", path);
1043 int eafd = ad_reso_fileno(ad);
1045 EC_NEG1( eafd = sys_getxattrfd(path, AD_EA_RESO, O_RDONLY) );
1048 EC_NEG1( rlen = fstat(eafd, &st) );
1049 ad->ad_rlen = st.st_size;
1052 EC_NULL_LOG( rfpath = ad->ad_ops->ad_path(path, adflags));
1053 EC_ZERO( lstat(rfpath, &st));
1054 if (st.st_size > ADEDOFF_RFORK_OSX)
1055 ad->ad_rlen = st.st_size - ADEDOFF_RFORK_OSX;
1060 LOG(log_debug, logtype_default, "ad_reso_size(\"%s\"): size: %zd", path, ad->ad_rlen);
1073 * Open ressource fork
1075 * Only for adouble:ea, a nullop otherwise because adouble:v2 has the ressource fork as part
1076 * of the adouble file which is openend by ADFLAGS_HF.
1078 static int ad_open_rf(const char *path, int adflags, int mode, struct adouble *ad)
1083 int closeflags = adflags & (ADFLAGS_DF | ADFLAGS_HF);
1090 if (ad->ad_vers != AD_VERSION_EA)
1093 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): BEGIN", fullpathname(path));
1095 oflags = O_NOFOLLOW | (ad2openflags(adflags) & ~O_CREAT);
1097 if (ad_reso_fileno(ad) != -1) {
1098 /* the file is already open, but we want write access: */
1099 if ((oflags & O_RDWR)
1100 /* and it was already denied: */
1101 && (ad->ad_rfp->adf_flags & O_RDONLY)) {
1105 ad->ad_rfp->adf_flags &= ~( O_TRUNC | O_CREAT );
1106 ad->ad_rfp->adf_refcount++;
1107 EC_NEG1_LOG( ad_reso_size(path, adflags, ad));
1111 if ((ad_reso_fileno(ad) = sys_getxattrfd(path, oflags)) == -1) {
1112 if (!(adflags & ADFLAGS_CREATE))
1115 EC_NEG1_LOG( ad_reso_fileno(ad) = sys_getxattrfd(path, oflags, 0666) );
1118 EC_NULL_LOG( rfpath = ad->ad_ops->ad_path(path, adflags) );
1119 if ((ad_reso_fileno(ad) = open(rfpath, oflags)) == -1) {
1120 if (!(adflags & ADFLAGS_CREATE))
1123 EC_NEG1_LOG( ad_reso_fileno(ad) = open(rfpath, oflags, mode) );
1124 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): created adouble rfork: \"%s\"",
1129 ad->ad_rfp->adf_refcount = 1;
1130 ad->ad_rfp->adf_flags = oflags;
1133 EC_ZERO_LOG( fstat(ad_reso_fileno(ad), &st) );
1134 if (ad->ad_rfp->adf_flags & O_CREAT) {
1135 /* This is a new adouble header file, create it */
1136 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): created adouble rfork, initializing: \"%s\"",
1138 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): created adouble rfork, flushing: \"%s\"",
1142 /* Read the adouble header */
1143 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): reading adouble rfork: \"%s\"",
1145 EC_NEG1_LOG( ad_header_read_osx(NULL, ad, &st) );
1149 (void)ad_reso_size(path, adflags, ad);
1153 if (opened && (ad_reso_fileno(ad) != -1)) {
1154 close(ad_reso_fileno(ad));
1155 ad_reso_fileno(ad) = -1;
1156 ad->ad_rfp->adf_refcount = 0;
1158 if (adflags & ADFLAGS_NORF) {
1162 (void)ad_close(ad, closeflags);
1168 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): END: %d", fullpathname(path), ret);
1173 /***********************************************************************************
1175 ********************************************************************************* */
1177 const char *ad_path_ea( const char *path, int adflags _U_)
1182 const char *ad_path_osx(const char *path, int adflags _U_)
1184 static char pathbuf[ MAXPATHLEN + 1];
1185 char c, *slash, buf[MAXPATHLEN + 1];
1187 if (!strcmp(path,".")) {
1189 getcwd(buf, MAXPATHLEN);
1192 strlcpy(buf, path, MAXPATHLEN +1);
1194 if (NULL != ( slash = strrchr( buf, '/' )) ) {
1197 strlcpy( pathbuf, buf, MAXPATHLEN +1);
1200 pathbuf[ 0 ] = '\0';
1203 strlcat( pathbuf, "._", MAXPATHLEN +1);
1204 strlcat( pathbuf, slash, MAXPATHLEN +1);
1209 * Put the .AppleDouble where it needs to be:
1211 * / a/.AppleDouble/b
1213 * \ b/.AppleDouble/.Parent
1215 * FIXME: should do something for pathname > MAXPATHLEN
1217 const char *ad_path( const char *path, int adflags)
1219 static char pathbuf[ MAXPATHLEN + 1];
1223 if ( adflags & ADFLAGS_DIR ) {
1224 l = strlcpy( pathbuf, path, sizeof(pathbuf));
1226 if ( l && l < MAXPATHLEN) {
1229 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
1231 if (NULL != ( slash = strrchr( path, '/' )) ) {
1234 /* XXX we must return NULL here and test in the caller */
1237 memcpy( pathbuf, path, l);
1242 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
1243 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
1249 /* -------------------------
1250 * Support inherited protection modes for AppleDouble files. The supplied
1251 * mode is ANDed with the parent directory's mask value in lieu of "umask",
1252 * and that value is returned.
1254 char *ad_dir(const char *path)
1256 static char modebuf[ MAXPATHLEN + 1];
1259 * For a path with directories in it, remove the final component
1260 * (path or subdirectory name) to get the name we want to stat.
1261 * For a path which is just a filename, use "." instead.
1263 slash = strrchr( path, '/' );
1268 if (len >= MAXPATHLEN) {
1269 errno = ENAMETOOLONG;
1270 return NULL; /* can't do it */
1272 memcpy( modebuf, path, len );
1273 modebuf[len] = '\0';
1274 /* is last char a '/' ? */
1275 if (slash[1] == 0) {
1276 slash = modebuf+ len;
1278 while (modebuf < slash && slash[-1] == '/') {
1281 if (modebuf == slash) {
1285 while (modebuf < slash && *slash != '/') {
1288 if (modebuf == slash) {
1291 *slash = '\0'; /* remove pathname component */
1296 modebuf[0] = '.'; /* use current directory */
1301 int ad_setfuid(const uid_t id)
1307 /* ---------------- */
1308 uid_t ad_getfuid(void)
1314 stat path parent directory
1316 int ad_stat(const char *path, struct stat *stbuf)
1323 return lstat( p, stbuf );
1327 return access right of path parent directory
1329 int ad_mode( const char *path, mode_t mode)
1332 ad_mode_st(path, &mode, &stbuf);
1337 * Use mkdir() with mode bits taken from ad_mode().
1339 int ad_mkdir( const char *path, mode_t mode)
1345 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
1346 path, mode, getcwdpath());
1348 st_invalid = ad_mode_st(path, &mode, &stbuf);
1349 ret = mkdir( path, mode );
1350 if (ret || st_invalid)
1352 ad_chown(path, &stbuf);
1357 static void ad_init_func(struct adouble *ad)
1359 switch (ad->ad_vers) {
1361 ad->ad_ops = &ad_adouble;
1362 ad->ad_rfp = &ad->ad_resource_fork;
1363 ad->ad_mdp = &ad->ad_resource_fork;
1366 ad->ad_ops = &ad_adouble_ea;
1367 ad->ad_rfp = &ad->ad_resource_fork;
1368 ad->ad_mdp = &ad->ad_data_fork;
1371 AFP_PANIC("ad_init: unknown AD version");
1375 ad_data_fileno(ad) = -1;
1376 ad_reso_fileno(ad) = -1;
1377 ad_meta_fileno(ad) = -1;
1378 ad->ad_refcount = 1;
1382 void ad_init_old(struct adouble *ad, int flags, int options)
1384 memset(ad, 0, sizeof(struct adouble));
1385 ad->ad_vers = flags;
1386 ad->ad_options = options;
1390 void ad_init(struct adouble *ad, const struct vol * restrict vol)
1392 memset(ad, 0, sizeof(struct adouble));
1393 ad->ad_vers = vol->v_adouble;
1394 ad->ad_options = vol->v_ad_options;
1399 * Open data-, metadata(header)- or ressource fork
1401 * ad_open(struct adouble *ad, const char *path, int adflags, int flags)
1402 * ad_open(struct adouble *ad, const char *path, int adflags, int flags, mode_t mode)
1404 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
1407 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1410 * Open a files data fork, metadata fork or ressource fork.
1412 * @param ad (rw) pointer to struct adouble
1413 * @param path (r) Path to file or directory
1414 * @param adflags (r) Flags specifying which fork to open, can be or'd:
1415 * ADFLAGS_DF: open data fork
1416 * ADFLAGS_RF: open ressource fork
1417 * ADFLAGS_HF: open header (metadata) file
1418 * ADFLAGS_NOHF: it's not an error if header file couldn't be opened
1419 * ADFLAGS_NORF: it's not an error if reso fork couldn't be opened
1420 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags
1422 * Access mode for the forks:
1423 * ADFLAGS_RDONLY: open read only
1424 * ADFLAGS_RDWR: open read write
1427 * ADFLAGS_CREATE: create if not existing
1428 * ADFLAGS_TRUNC: truncate
1431 * ADFLAGS_CHECK_OF: check for open forks from us and other afpd's
1432 * ADFLAGS_SETSHRMD: this adouble struct will be used to set sharemode locks.
1433 * This basically results in the files being opened RW instead of RDONLY.
1434 * @param mode (r) mode used with O_CREATE
1436 * The open mode flags (rw vs ro) have to take into account all the following requirements:
1437 * - we remember open fds for files because me must avoid a single close releasing fcntl locks for other
1438 * fds of the same file
1440 * @returns 0 on success, any other value indicates an error
1442 int ad_open(struct adouble *ad, const char *path, int adflags, ...)
1448 LOG(log_debug, logtype_default,
1449 "ad_open(\"%s\", %s): BEGIN [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
1450 fullpathname(path), adflags2logstr(adflags),
1451 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
1452 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
1453 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
1455 if (adflags & ADFLAGS_CHECK_OF)
1456 /* Checking for open forks requires sharemode lock support (ie RDWR instead of RDONLY) */
1457 adflags |= ADFLAGS_SETSHRMD;
1459 if ((ad->ad_vers == AD_VERSION2) && (adflags & ADFLAGS_RF)) {
1460 adflags |= ADFLAGS_HF;
1461 if (adflags & ADFLAGS_NORF)
1462 adflags |= ADFLAGS_NOHF;
1465 if (ad->ad_inited != AD_INITED) {
1466 ad->ad_adflags = adflags;
1467 ad->ad_inited = AD_INITED;
1469 ad->ad_open_forks = ((ad->ad_data_fork.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
1470 if (ad->ad_resource_fork.adf_refcount > 0)
1471 ad->ad_open_forks |= ATTRBIT_ROPEN;
1474 va_start(args, adflags);
1475 if (adflags & ADFLAGS_CREATE)
1476 mode = va_arg(args, mode_t);
1479 if (adflags & ADFLAGS_DF) {
1480 EC_ZERO( ad_open_df(path, adflags, mode, ad) );
1483 if (adflags & ADFLAGS_HF) {
1484 EC_ZERO( ad_open_hf(path, adflags, mode, ad) );
1487 if (adflags & ADFLAGS_RF) {
1488 EC_ZERO( ad_open_rf(path, adflags, mode, ad) );
1491 if (adflags & ADFLAGS_CHECK_OF) {
1492 ad->ad_open_forks |= ad_openforks(ad, ad->ad_open_forks);
1496 LOG(log_debug, logtype_default,
1497 "ad_open(\"%s\"): END: %d [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
1498 fullpathname(path), ret,
1499 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
1500 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
1501 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
1507 * @brief open metadata, possibly as root
1509 * Return only metadata but try very hard ie at first try as user, then try as root.
1511 * @param name name of file/dir
1512 * @param flags ADFLAGS_DIR: name is a directory \n
1513 * ADFLAGS_CHECK_OF: test if name is open by us or another afpd process
1515 * @param adp pointer to struct adouble
1517 int ad_metadata(const char *name, int flags, struct adouble *adp)
1520 int ret, err, oflags;
1522 /* Sanitize flags */
1523 oflags = (flags & (ADFLAGS_CHECK_OF | ADFLAGS_DIR)) | ADFLAGS_HF | ADFLAGS_RDONLY;
1525 if ((ret = ad_open(adp, name, oflags)) < 0 && errno == EACCES) {
1528 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1532 /* we are root open read only */
1533 ret = ad_open(adp, name, oflags);
1535 if ( seteuid(uid) < 0) {
1536 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1546 * @brief openat like wrapper for ad_metadata
1548 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1554 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1560 if (ad_metadata(name, flags, adp) < 0) {
1567 if (fchdir(cwdfd) != 0) {
1568 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1581 int ad_refresh(const char *path, struct adouble *ad)
1583 switch (ad->ad_vers) {
1585 if (ad_meta_fileno(ad) == -1)
1587 return ad->ad_ops->ad_header_read(NULL, ad, NULL);
1591 if (AD_META_OPEN(ad)) {
1592 if (ad_data_fileno(ad) == -1)
1594 // TODO: read meta EA
1597 if (AD_RSRC_OPEN(ad)) {
1598 if (ad_reso_fileno(ad) == -1)
1601 if ((len = fstat(ad_reso_fileno(ad))) == -1)
1606 if (AD_META_OPEN(ad)) {
1607 if (ad_data_fileno(ad) == -1)
1609 // TODO: read meta EA
1611 if (AD_RSRC_OPEN(ad)) {
1612 if (ad_reso_fileno(ad) == -1)
1614 if (ad_header_read_osx(path, ad, NULL) < 0)
1618 return ad->ad_ops->ad_header_read(path, ad, NULL);
1627 int ad_openat(struct adouble *ad,
1628 int dirfd, /* dir fd openat like */
1638 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0))
1642 va_start(args, adflags);
1643 if (adflags & ADFLAGS_CREATE)
1644 mode = va_arg(args, mode_t);
1647 EC_NEG1( ad_open(ad, path, adflags, mode) );
1650 if (fchdir(cwdfd) != 0) {
1651 AFP_PANIC("ad_openat: cant chdir back");