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)
89 #define ADEDOFF_PRIVID_EA (ADEDOFF_AFPFILEI_EA + ADEDLEN_AFPFILEI)
91 /* this is to prevent changing timezones from causing problems with
92 localtime volumes. the screw-up is 30 years. we use a delta of 5 years */
93 #define TIMEWARP_DELTA 157680000
96 uint32_t id, offset, len;
99 /* --------------------------- */
100 static uid_t default_uid = -1;
102 /* Forward declarations */
103 static int ad_mkrf(const char *path);
104 static int ad_header_read(const char *path, struct adouble *ad, struct stat *hst);
105 static int ad_header_upgrade(struct adouble *ad, const char *name);
107 static int ad_mkrf_ea(const char *path);
108 static int ad_header_read_ea(const char *path, struct adouble *ad, struct stat *hst);
109 static int ad_header_upgrade_ea(struct adouble *ad, const char *name);
110 static int ad_reso_size(const char *path, int adflags, struct adouble *ad);
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_EA, 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_NORF) {
197 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
198 strlcat(buf, "NORF", ADFLAGS2LOGSTRBUFSIZ);
201 if (adflags & ADFLAGS_HF) {
203 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
204 strlcat(buf, "HF", ADFLAGS2LOGSTRBUFSIZ);
207 if (adflags & ADFLAGS_NOHF) {
209 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
210 strlcat(buf, "NOHF", ADFLAGS2LOGSTRBUFSIZ);
213 if (adflags & ADFLAGS_DIR) {
215 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
216 strlcat(buf, "DIR", ADFLAGS2LOGSTRBUFSIZ);
219 if (adflags & ADFLAGS_CHECK_OF) {
221 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
222 strlcat(buf, "OF", ADFLAGS2LOGSTRBUFSIZ);
225 if (adflags & ADFLAGS_SETSHRMD) {
227 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
228 strlcat(buf, "SHRMD", ADFLAGS2LOGSTRBUFSIZ);
231 if (adflags & ADFLAGS_RDWR) {
233 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
234 strlcat(buf, "O_RDWR", ADFLAGS2LOGSTRBUFSIZ);
237 if (adflags & ADFLAGS_RDONLY) {
239 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
240 strlcat(buf, "O_RDONLY", ADFLAGS2LOGSTRBUFSIZ);
243 if (adflags & ADFLAGS_CREATE) {
245 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
246 strlcat(buf, "O_CREAT", ADFLAGS2LOGSTRBUFSIZ);
249 if (adflags & ADFLAGS_EXCL) {
251 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
252 strlcat(buf, "O_EXCL", ADFLAGS2LOGSTRBUFSIZ);
255 if (adflags & ADFLAGS_TRUNC) {
257 strlcat(buf, "|", ADFLAGS2LOGSTRBUFSIZ);
258 strlcat(buf, "O_TRUNC", ADFLAGS2LOGSTRBUFSIZ);
265 static uint32_t get_eid(uint32_t eid)
270 return ADEID_PRIVDEV;
272 return ADEID_PRIVINO;
274 return ADEID_PRIVSYN;
281 /* ----------------------------------- */
282 static int new_ad_header(struct adouble *ad, const char *path, struct stat *stp, int adflags)
284 const struct entry *eid;
288 LOG(log_debug, logtype_default, "new_ad_header(\"%s\")", path);
292 if (lstat(path, &st) != 0)
296 ad->ad_magic = AD_MAGIC;
297 ad->ad_version = ad->ad_vers & 0x0f0000;
298 if (!ad->ad_version) {
299 ad->ad_version = AD_VERSION;
302 memset(ad->ad_data, 0, sizeof(ad->ad_data));
304 if (ad->ad_vers == AD_VERSION2)
306 else if (ad->ad_vers == AD_VERSION_EA)
307 eid = entry_order_ea;
308 else if (ad->ad_vers == AD_VERSION2_OSX)
309 eid = entry_order_osx;
315 ad->ad_eid[eid->id].ade_off = eid->offset;
316 ad->ad_eid[eid->id].ade_len = eid->len;
320 /* put something sane in the directory finderinfo */
321 if (ad->ad_vers != AD_VERSION2_OSX) {
322 if ((adflags & ADFLAGS_DIR)) {
323 /* set default view */
324 ashort = htons(FINDERINFO_CLOSEDVIEW);
325 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
327 /* set default creator/type fields */
328 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
329 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
332 /* make things invisible */
333 if ((ad->ad_options & ADVOL_INVDOTS)
335 && !((adflags & ADFLAGS_DIR) && (path[1] == 0))
337 ashort = htons(ATTRBIT_INVISIBLE);
338 ad_setattr(ad, ashort);
339 ashort = htons(FINDERINFO_INVISIBLE);
340 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
343 /* put something sane in the date fields */
344 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, stp->st_mtime);
345 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, stp->st_mtime);
346 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, stp->st_mtime);
347 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
352 /* -------------------------------------
355 static void parse_entries(struct adouble *ad, char *buf, uint16_t nentries)
357 uint32_t eid, len, off;
360 /* now, read in the entry bits */
361 for (; nentries > 0; nentries-- ) {
362 memcpy(&eid, buf, sizeof( eid ));
363 eid = get_eid(ntohl(eid));
364 buf += sizeof( eid );
365 memcpy(&off, buf, sizeof( off ));
367 buf += sizeof( off );
368 memcpy(&len, buf, sizeof( len ));
370 buf += sizeof( len );
374 && off < sizeof(ad->ad_data)
375 && (off + len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
376 ad->ad_eid[ eid ].ade_off = off;
377 ad->ad_eid[ eid ].ade_len = len;
378 } else if (!warning) {
380 LOG(log_warning, logtype_default, "parse_entries: bogus eid: %d", eid);
385 /* this reads enough of the header so that we can figure out all of
386 * the entry lengths and offsets. once that's done, we just read/mmap
387 * the rest of the header in.
389 * NOTE: we're assuming that the resource fork is kept at the end of
390 * the file. also, mmapping won't work for the hfs fs until it
391 * understands how to mmap header files. */
392 static int ad_header_read(const char *path _U_, struct adouble *ad, struct stat *hst)
394 char *buf = ad->ad_data;
400 /* read the header */
401 if ((header_len = adf_pread( ad->ad_mdp, buf, AD_DATASZ, 0)) < 0) {
404 if (header_len < AD_HEADER_LEN) {
409 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
410 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
411 ad->ad_magic = ntohl( ad->ad_magic );
412 ad->ad_version = ntohl( ad->ad_version );
414 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
415 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
420 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
421 nentries = ntohs( nentries );
423 /* read in all the entry headers. if we have more than the
424 * maximum, just hope that the rfork is specified early on. */
425 len = nentries*AD_ENTRY_LEN;
427 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
428 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
430 buf += AD_HEADER_LEN;
431 if (len > header_len - AD_HEADER_LEN) {
432 LOG(log_error, logtype_default, "ad_header_read: can't read entry info.");
437 /* figure out all of the entry offsets and lengths. if we aren't
438 * able to read a resource fork entry, bail. */
439 nentries = len / AD_ENTRY_LEN;
440 parse_entries(ad, buf, nentries);
441 if (!ad_getentryoff(ad, ADEID_RFORK)
442 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
444 LOG(log_error, logtype_default, "ad_header_read: problem with rfork entry offset.");
449 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
450 LOG(log_error, logtype_default, "ad_header_read: can't read in entries.");
457 if (fstat(ad->ad_mdp->adf_fd, &st) < 0) {
458 return 1; /* fail silently */
462 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
467 /* Read an ._ file, only uses the resofork, finderinfo is taken from EA */
468 static int ad_header_read_osx(const char *path _U_, struct adouble *ad, struct stat *hst)
471 struct adouble adosx;
472 char *buf = &adosx.ad_data[0];
478 memset(buf, 0, sizeof(adosx.ad_data));
480 /* read the header */
481 EC_NEG1( header_len = adf_pread(ad->ad_rfp, buf, AD_DATASZ_OSX, 0) );
483 if (header_len < AD_HEADER_LEN) {
488 memcpy(&adosx.ad_magic, buf, sizeof(adosx.ad_magic));
489 memcpy(&adosx.ad_version, buf + ADEDOFF_VERSION, sizeof(adosx.ad_version));
490 adosx.ad_magic = ntohl(adosx.ad_magic);
491 adosx.ad_version = ntohl(adosx.ad_version);
493 if ((adosx.ad_magic != AD_MAGIC) || (adosx.ad_version != AD_VERSION2)) {
494 LOG(log_error, logtype_afpd, "ad_header_read_osx: can't parse AppleDouble header");
499 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
500 nentries = ntohs(nentries);
501 len = nentries * AD_ENTRY_LEN;
503 if (len + AD_HEADER_LEN > sizeof(adosx.ad_data))
504 len = sizeof(adosx.ad_data) - AD_HEADER_LEN;
506 buf += AD_HEADER_LEN;
507 if (len > header_len - AD_HEADER_LEN) {
508 LOG(log_error, logtype_afpd, "ad_header_read_osx: can't read entry info.");
513 nentries = len / AD_ENTRY_LEN;
514 parse_entries(&adosx, buf, nentries);
516 if (ad_getentryoff(&adosx, ADEID_RFORK) == 0
517 || ad_getentryoff(&adosx, ADEID_RFORK) > sizeof(ad->ad_data)
518 || ad_getentryoff(&adosx, ADEID_RFORK) > header_len
520 LOG(log_error, logtype_afpd, "ad_header_read_osx: problem with rfork entry offset.");
527 EC_NEG1( fstat(ad_reso_fileno(ad), &st) );
530 ad_setentryoff(ad, ADEID_RFORK, ad_getentryoff(&adosx, ADEID_RFORK));
531 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
537 static int ad_header_read_ea(const char *path, struct adouble *ad, struct stat *hst _U_)
542 char *buf = ad->ad_data;
544 if (ad_meta_fileno(ad) != -1)
545 header_len = sys_fgetxattr(ad_meta_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA);
547 header_len = sys_lgetxattr(path, AD_EA_META, ad->ad_data, AD_DATASZ_EA);
548 if (header_len < 1) {
549 LOG(log_debug, logtype_default, "ad_header_read_ea: %s", strerror(errno));
553 if (header_len < AD_HEADER_LEN) {
554 LOG(log_error, logtype_default, "ad_header_read_ea: bogus AppleDouble header.");
559 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
560 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
562 ad->ad_magic = ntohl( ad->ad_magic );
563 ad->ad_version = ntohl( ad->ad_version );
565 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
566 LOG(log_error, logtype_default, "ad_header_read_ea: wrong magic or version");
571 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
572 nentries = ntohs( nentries );
574 /* Protect against bogus nentries */
575 len = nentries * AD_ENTRY_LEN;
576 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
577 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
578 if (len > header_len - AD_HEADER_LEN) {
579 LOG(log_error, logtype_default, "ad_header_read_ea: can't read entry info.");
583 nentries = len / AD_ENTRY_LEN;
585 /* Now parse entries */
586 parse_entries(ad, buf + AD_HEADER_LEN, nentries);
592 * Takes a path to an AppleDouble file and creates the parrent .AppleDouble directory
595 * path: "/path/.AppleDouble/file"
596 * => mkdir("/path/.AppleDouble/") (in ad_mkdir())
598 static int ad_mkrf(const char *path)
602 * Probably .AppleDouble doesn't exist, try to mkdir it.
604 if (NULL == ( slash = strrchr( path, '/' )) ) {
609 if ( ad_mkdir( path, 0777 ) < 0 ) {
616 static int ad_mkrf_ea(const char *path _U_)
618 AFP_PANIC("ad_mkrf_ea: dont use");
622 static int ad_mkrf_osx(const char *path _U_)
628 if we are root change path user/ group
629 It can be a native function for BSD cf. FAQ.Q10
630 path: pathname to chown
631 stbuf: parent directory inode
633 use fstat and fchown or lchown with linux?
635 #define EMULATE_SUIDDIR
637 static int ad_chown(const char *path, struct stat *stbuf)
640 #ifdef EMULATE_SUIDDIR
643 if (default_uid != (uid_t)-1) {
644 /* we are root (admin) */
645 id = (default_uid)?default_uid:stbuf->st_uid;
646 ret = lchown( path, id, stbuf->st_gid );
652 #define DEFMASK 07700 /* be conservative */
655 return access right and inode of path parent directory
657 static int ad_mode_st(const char *path, mode_t *mode, struct stat *stbuf)
662 if (ad_stat(path, stbuf) != 0) {
666 *mode &= stbuf->st_mode;
670 /* --------------------------- */
671 static int ad_header_upgrade(struct adouble *ad _U_, const char *name _U_)
676 static int ad_header_upgrade_ea(struct adouble *ad _U_, const char *name _U_)
678 AFP_PANIC("ad_header_upgrade_ea: dont use");
683 * Error handling for adouble header(=metadata) file open error
685 * We're called because opening ADFLAGS_HF caused an error.
686 * 1. In case ad_open is called with ADFLAGS_NOHF the error is suppressed.
687 * 2. If ad_open was called with ADFLAGS_DF we may have opened the datafork and thus
688 * ought to close it before returning with an error condition.
690 static int ad_error(struct adouble *ad, int adflags)
693 if (adflags & ADFLAGS_NOHF) { /* 1 */
696 if (adflags & ADFLAGS_DF) { /* 2 */
697 ad_close( ad, ADFLAGS_DF );
703 /* Map ADFLAGS to open() flags */
704 static int ad2openflags(int adflags)
708 if (adflags & ADFLAGS_RDWR)
710 if (adflags & ADFLAGS_RDONLY) {
711 if (adflags & ADFLAGS_SETSHRMD)
716 if (adflags & ADFLAGS_CREATE)
718 if (adflags & ADFLAGS_EXCL)
720 if (adflags & ADFLAGS_TRUNC)
726 static int ad_open_df(const char *path, int adflags, mode_t mode, struct adouble *ad)
734 LOG(log_debug, logtype_default, "ad_open_df(\"%s\", %04o)",
735 fullpathname(path), mode);
737 if (ad_data_fileno(ad) != -1) {
738 /* the file is already open, but we want write access: */
739 if ((adflags & ADFLAGS_RDWR)
740 /* and it was denied the first time: */
741 && (ad->ad_data_fork.adf_flags & O_RDONLY)) {
745 /* it's not new anymore */
746 ad->ad_data_fork.adf_flags &= ~( O_TRUNC | O_CREAT );
747 ad->ad_data_fork.adf_refcount++;
751 oflags = O_NOFOLLOW | ad2openflags(adflags);
754 if ((adflags & ADFLAGS_CREATE)) {
755 st_invalid = ad_mode_st(path, &admode, &st_dir);
756 if ((ad->ad_options & ADVOL_UNIXPRIV))
760 ad->ad_data_fork.adf_fd = open(path, oflags, admode);
762 if (ad->ad_data_fork.adf_fd == -1) {
767 if ((adflags & ADFLAGS_SETSHRMD) && (adflags & ADFLAGS_RDONLY)) {
770 if ((ad->ad_data_fork.adf_fd = open(path, oflags, admode)) == -1)
775 case OPEN_NOFOLLOW_ERRNO:
776 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
777 if ((lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN)) <= 0) {
778 free(ad->ad_data_fork.adf_syml);
781 ad->ad_data_fork.adf_syml[lsz] = 0;
782 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
790 ad_chown(path, &st_dir); /* just created, set owner if admin (root) */
792 ad->ad_data_fork.adf_flags = oflags;
793 adf_lock_init(&ad->ad_data_fork);
794 ad->ad_data_fork.adf_refcount++;
799 /* TODO: error handling */
800 static int ad_open_hf_v2(const char *path, int adflags, mode_t mode, struct adouble *ad)
804 struct stat *pst = NULL;
806 int oflags, nocreatflags;
810 if (ad_meta_fileno(ad) != -1) {
811 /* the file is already open, but we want write access: */
812 if ((adflags & ADFLAGS_RDWR) &&
813 /* and it was already denied: */
814 (ad->ad_mdp->adf_flags & O_RDONLY)) {
818 ad_refresh(path, ad);
819 /* it's not new anymore */
820 ad->ad_mdp->adf_flags &= ~( O_TRUNC | O_CREAT );
821 ad->ad_mdp->adf_refcount++;
825 ad_p = ad->ad_ops->ad_path(path, adflags);
826 oflags = O_NOFOLLOW | ad2openflags(adflags);
827 nocreatflags = oflags & ~(O_CREAT | O_EXCL);
829 ad->ad_mdp->adf_fd = open(ad_p, nocreatflags);
831 if (ad->ad_mdp->adf_fd != -1) {
832 ad->ad_mdp->adf_flags = nocreatflags;
838 if ((adflags & ADFLAGS_RDONLY) && (adflags & ADFLAGS_SETSHRMD)) {
839 nocreatflags &= ~O_RDWR;
840 nocreatflags |= O_RDONLY;
841 if ((ad->ad_mdp->adf_fd = open(ad_p, nocreatflags)) == -1)
843 ad->ad_mdp->adf_flags = nocreatflags;
848 if (!(oflags & O_CREAT))
849 return ad_error(ad, adflags);
851 * We're expecting to create a new adouble header file here
853 LOG(log_debug, logtype_default, "ad_open(\"%s\"): creating adouble file",
857 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
858 if ((ad->ad_options & ADVOL_UNIXPRIV))
860 admode = ad_hf_mode(admode);
861 if (errno == ENOENT) {
862 if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
863 return ad_error(ad, adflags);
866 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
867 if ((ad->ad_options & ADVOL_UNIXPRIV))
869 admode = ad_hf_mode(admode);
872 /* retry with O_CREAT */
873 ad->ad_mdp->adf_fd = open(ad_p, oflags, admode);
874 if ( ad->ad_mdp->adf_fd < 0 )
875 return ad_error(ad, adflags);
877 ad->ad_mdp->adf_flags = oflags;
878 /* just created, set owner if admin owner (root) */
880 ad_chown(ad_p, &st_dir);
887 if (!(ad->ad_mdp->adf_flags & O_CREAT)) {
888 /* check for 0 length files, treat them as new. */
889 if (fstat(ad->ad_mdp->adf_fd, &st_meta) == 0) {
890 if (st_meta.st_size == 0)
891 ad->ad_mdp->adf_flags |= O_TRUNC;
893 /* we have valid data in st_meta stat structure, reused it in ad_header_read */
898 adf_lock_init(ad->ad_mdp);
899 ad->ad_mdp->adf_refcount = 1;
901 if ((ad->ad_mdp->adf_flags & ( O_TRUNC | O_CREAT ))) {
902 /* This is a new adouble header file, create it */
903 if (new_ad_header(ad, path, pst, adflags) < 0) {
905 /* the file is already deleted, perm, whatever, so return an error */
906 ad_close(ad, adflags);
912 /* Read the adouble header in and parse it.*/
913 if (ad->ad_ops->ad_header_read(path, ad, pst) < 0
914 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0) {
916 ad_close(ad, adflags);
925 static int ad_open_hf_ea(const char *path, int adflags, int mode, struct adouble *ad)
932 LOG(log_debug, logtype_default,
933 "ad_open_hf_ea(\"%s\", %s): BEGIN [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
934 fullpathname(path), adflags2logstr(adflags),
935 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
936 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
937 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
939 oflags = O_NOFOLLOW | (ad2openflags(adflags) & ~(O_CREAT | O_TRUNC));
941 if (ad_meta_fileno(ad) == -2)
945 if (ad_meta_fileno(ad) != -1) {
946 /* the file is already open, but we want write access: */
947 if ((oflags & O_RDWR) &&
948 /* and it was already denied: */
949 (ad->ad_mdp->adf_flags & O_RDONLY)) {
950 LOG(log_error, logtype_default, "ad_open_hf_ea(%s): rw request for ro file: %s",
951 fullpathname(path), strerror(errno));
955 /* it's not new anymore */
956 ad->ad_mdp->adf_flags &= ~( O_TRUNC | O_CREAT );
958 if (oflags & O_RDWR) {
959 /* Fo a RDONLY adouble we just use sys_lgetxattr instead if sys_fgetxattr */
960 if (adflags & ADFLAGS_DIR)
961 /* For directories we open the directory RDONYL so we can later fchdir() */
962 oflags = (oflags & ~O_RDWR) | O_RDONLY;
963 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): opening base file for meta adouble EA", path);
964 EC_NEG1_LOG(ad_meta_fileno(ad) = open(path, oflags));
966 ad->ad_mdp->adf_flags = oflags;
970 /* Read the adouble header in and parse it.*/
971 if (ad->ad_ops->ad_header_read(path, ad, NULL) != 0) {
972 if (!(adflags & ADFLAGS_CREATE)) {
973 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): can't read metadata EA", path);
978 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): creating metadata EA", path);
980 /* It doesnt exist, EPERM or another error */
981 if (!(errno == ENOATTR || errno == ENOENT)) {
982 LOG(log_error, logtype_default, "ad_open_hf_ea: unexpected: %s", strerror(errno));
987 EC_NEG1_LOG(new_ad_header(ad, path, NULL, adflags));
988 ad->ad_mdp->adf_flags |= O_CREAT; /* mark as just created */
990 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): created metadata EA", path);
993 ad->ad_mdp->adf_refcount++;
994 (void)ad_reso_size(path, adflags, ad);
997 if (ret != 0 && opened && ad_meta_fileno(ad) != -1) {
998 close(ad_meta_fileno(ad));
999 ad_meta_fileno(ad) = -1;
1000 ad->ad_mdp->adf_refcount = 0;
1002 LOG(log_debug, logtype_default,
1003 "ad_open_hf_ea(\"%s\", %s): END: %d [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
1004 fullpathname(path), adflags2logstr(adflags), ret,
1005 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
1006 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
1007 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
1012 static int ad_open_hf(const char *path, int adflags, int mode, struct adouble *ad)
1016 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
1019 switch (ad->ad_vers) {
1021 ret = ad_open_hf_v2(path, adflags, mode, ad);
1024 ret = ad_open_hf_ea(path, adflags, mode, ad);
1032 ret = ad_error(ad, adflags);
1038 * Get resofork length for adouble:ea
1040 static int ad_reso_size(const char *path, int adflags, struct adouble *ad)
1045 LOG(log_debug, logtype_default, "ad_reso_size(\"%s\")", path);
1050 if (ad_reso_fileno(ad) != -1) {
1051 EC_NEG1( fstat(ad_reso_fileno(ad), &st) );
1052 ad->ad_rlen = st.st_size;
1053 } else if (ad_meta_fileno(ad) != -1) {
1054 EC_NEG1( easz = sys_fgetxattr(ad_meta_fileno(ad), AD_EA_RESO, NULL, 0) );
1062 EC_NULL_LOG( rfpath = ad->ad_ops->ad_path(path, adflags));
1063 EC_ZERO( lstat(rfpath, &st));
1064 if (st.st_size > ADEDOFF_RFORK_OSX)
1065 ad->ad_rlen = st.st_size - ADEDOFF_RFORK_OSX;
1070 LOG(log_debug, logtype_default, "ad_reso_size(\"%s\"): size: %zd", path, ad->ad_rlen);
1079 * Open ressource fork
1081 * Only for adouble:ea, a nullop otherwise because adouble:v2 has the ressource fork as part
1082 * of the adouble file which is openend by ADFLAGS_HF.
1084 static int ad_open_rf(const char *path, int adflags, int mode, struct adouble *ad)
1089 int closeflags = adflags & (ADFLAGS_DF | ADFLAGS_HF);
1096 if (ad->ad_vers != AD_VERSION_EA)
1099 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): BEGIN", fullpathname(path));
1101 oflags = O_NOFOLLOW | (ad2openflags(adflags) & ~O_CREAT);
1103 if (ad_reso_fileno(ad) != -1) {
1104 /* the file is already open, but we want write access: */
1105 if ((oflags & O_RDWR)
1106 /* and it was already denied: */
1107 && (ad->ad_rfp->adf_flags & O_RDONLY)) {
1111 ad->ad_rfp->adf_flags &= ~( O_TRUNC | O_CREAT );
1112 ad->ad_rfp->adf_refcount++;
1113 EC_NEG1_LOG( ad_reso_size(path, adflags, ad));
1117 if (ad_meta_fileno(ad) == -1)
1119 if ((ad_reso_fileno(ad) = sys_getxattrfd(ad_meta_fileno(ad), AD_EA_RESO, oflags)) == -1) {
1120 if (!(adflags & ADFLAGS_CREATE)) {
1125 EC_NEG1_LOG( ad_reso_fileno(ad) = sys_getxattrfd(ad_meta_fileno(ad),
1126 AD_EA_RESO, oflags, 0666) );
1129 EC_NULL_LOG( rfpath = ad->ad_ops->ad_path(path, adflags) );
1130 if ((ad_reso_fileno(ad) = open(rfpath, oflags)) == -1) {
1131 if (!(adflags & ADFLAGS_CREATE))
1134 EC_NEG1_LOG( ad_reso_fileno(ad) = open(rfpath, oflags, mode) );
1135 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): created adouble rfork: \"%s\"",
1140 ad->ad_rfp->adf_refcount = 1;
1141 ad->ad_rfp->adf_flags = oflags;
1144 EC_ZERO_LOG( fstat(ad_reso_fileno(ad), &st) );
1145 if (ad->ad_rfp->adf_flags & O_CREAT) {
1146 /* This is a new adouble header file, create it */
1147 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): created adouble rfork, initializing: \"%s\"",
1149 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): created adouble rfork, flushing: \"%s\"",
1153 /* Read the adouble header */
1154 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): reading adouble rfork: \"%s\"",
1156 EC_NEG1_LOG( ad_header_read_osx(NULL, ad, &st) );
1160 (void)ad_reso_size(path, adflags, ad);
1164 if (opened && (ad_reso_fileno(ad) != -1)) {
1165 close(ad_reso_fileno(ad));
1166 ad_reso_fileno(ad) = -1;
1167 ad->ad_rfp->adf_refcount = 0;
1169 if (adflags & ADFLAGS_NORF) {
1173 (void)ad_close(ad, closeflags);
1179 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): END: %d", fullpathname(path), ret);
1184 /***********************************************************************************
1186 ********************************************************************************* */
1188 const char *ad_path_ea( const char *path, int adflags _U_)
1193 const char *ad_path_osx(const char *path, int adflags _U_)
1195 static char pathbuf[ MAXPATHLEN + 1];
1196 char c, *slash, buf[MAXPATHLEN + 1];
1198 if (!strcmp(path,".")) {
1200 getcwd(buf, MAXPATHLEN);
1203 strlcpy(buf, path, MAXPATHLEN +1);
1205 if (NULL != ( slash = strrchr( buf, '/' )) ) {
1208 strlcpy( pathbuf, buf, MAXPATHLEN +1);
1211 pathbuf[ 0 ] = '\0';
1214 strlcat( pathbuf, "._", MAXPATHLEN +1);
1215 strlcat( pathbuf, slash, MAXPATHLEN +1);
1220 * Put the .AppleDouble where it needs to be:
1222 * / a/.AppleDouble/b
1224 * \ b/.AppleDouble/.Parent
1226 * FIXME: should do something for pathname > MAXPATHLEN
1228 const char *ad_path( const char *path, int adflags)
1230 static char pathbuf[ MAXPATHLEN + 1];
1234 if ( adflags & ADFLAGS_DIR ) {
1235 l = strlcpy( pathbuf, path, sizeof(pathbuf));
1237 if ( l && l < MAXPATHLEN) {
1240 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
1242 if (NULL != ( slash = strrchr( path, '/' )) ) {
1245 /* XXX we must return NULL here and test in the caller */
1248 memcpy( pathbuf, path, l);
1253 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
1254 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
1260 /* -------------------------
1261 * Support inherited protection modes for AppleDouble files. The supplied
1262 * mode is ANDed with the parent directory's mask value in lieu of "umask",
1263 * and that value is returned.
1265 char *ad_dir(const char *path)
1267 static char modebuf[ MAXPATHLEN + 1];
1270 * For a path with directories in it, remove the final component
1271 * (path or subdirectory name) to get the name we want to stat.
1272 * For a path which is just a filename, use "." instead.
1274 slash = strrchr( path, '/' );
1279 if (len >= MAXPATHLEN) {
1280 errno = ENAMETOOLONG;
1281 return NULL; /* can't do it */
1283 memcpy( modebuf, path, len );
1284 modebuf[len] = '\0';
1285 /* is last char a '/' ? */
1286 if (slash[1] == 0) {
1287 slash = modebuf+ len;
1289 while (modebuf < slash && slash[-1] == '/') {
1292 if (modebuf == slash) {
1296 while (modebuf < slash && *slash != '/') {
1299 if (modebuf == slash) {
1302 *slash = '\0'; /* remove pathname component */
1307 modebuf[0] = '.'; /* use current directory */
1312 int ad_setfuid(const uid_t id)
1318 /* ---------------- */
1319 uid_t ad_getfuid(void)
1325 stat path parent directory
1327 int ad_stat(const char *path, struct stat *stbuf)
1334 return lstat( p, stbuf );
1338 return access right of path parent directory
1340 int ad_mode( const char *path, mode_t mode)
1343 ad_mode_st(path, &mode, &stbuf);
1348 * Use mkdir() with mode bits taken from ad_mode().
1350 int ad_mkdir( const char *path, mode_t mode)
1356 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
1357 path, mode, getcwdpath());
1359 st_invalid = ad_mode_st(path, &mode, &stbuf);
1360 ret = mkdir( path, mode );
1361 if (ret || st_invalid)
1363 ad_chown(path, &stbuf);
1368 static void ad_init_func(struct adouble *ad)
1370 switch (ad->ad_vers) {
1372 ad->ad_ops = &ad_adouble;
1373 ad->ad_rfp = &ad->ad_resource_fork;
1374 ad->ad_mdp = &ad->ad_resource_fork;
1377 ad->ad_ops = &ad_adouble_ea;
1378 ad->ad_rfp = &ad->ad_resource_fork;
1379 ad->ad_mdp = &ad->ad_data_fork;
1382 AFP_PANIC("ad_init: unknown AD version");
1386 ad_data_fileno(ad) = -1;
1387 ad_reso_fileno(ad) = -1;
1388 ad_meta_fileno(ad) = -1;
1389 ad->ad_refcount = 1;
1393 void ad_init_old(struct adouble *ad, int flags, int options)
1395 memset(ad, 0, sizeof(struct adouble));
1396 ad->ad_vers = flags;
1397 ad->ad_options = options;
1401 void ad_init(struct adouble *ad, const struct vol * restrict vol)
1403 memset(ad, 0, sizeof(struct adouble));
1404 ad->ad_vers = vol->v_adouble;
1405 ad->ad_options = vol->v_ad_options;
1410 * Open data-, metadata(header)- or ressource fork
1412 * ad_open(struct adouble *ad, const char *path, int adflags, int flags)
1413 * ad_open(struct adouble *ad, const char *path, int adflags, int flags, mode_t mode)
1415 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
1418 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1421 * Open a files data fork, metadata fork or ressource fork.
1423 * @param ad (rw) pointer to struct adouble
1424 * @param path (r) Path to file or directory
1425 * @param adflags (r) Flags specifying which fork to open, can be or'd:
1426 * ADFLAGS_DF: open data fork
1427 * ADFLAGS_RF: open ressource fork
1428 * ADFLAGS_HF: open header (metadata) file
1429 * ADFLAGS_NOHF: it's not an error if header file couldn't be opened
1430 * ADFLAGS_NORF: it's not an error if reso fork couldn't be opened
1431 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags
1433 * Access mode for the forks:
1434 * ADFLAGS_RDONLY: open read only
1435 * ADFLAGS_RDWR: open read write
1438 * ADFLAGS_CREATE: create if not existing
1439 * ADFLAGS_TRUNC: truncate
1442 * ADFLAGS_CHECK_OF: check for open forks from us and other afpd's
1443 * ADFLAGS_SETSHRMD: this adouble struct will be used to set sharemode locks.
1444 * This basically results in the files being opened RW instead of RDONLY.
1445 * @param mode (r) mode used with O_CREATE
1447 * The open mode flags (rw vs ro) have to take into account all the following requirements:
1448 * - we remember open fds for files because me must avoid a single close releasing fcntl locks for other
1449 * fds of the same file
1451 * @returns 0 on success, any other value indicates an error
1453 int ad_open(struct adouble *ad, const char *path, int adflags, ...)
1459 LOG(log_debug, logtype_default,
1460 "ad_open(\"%s\", %s): BEGIN [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
1461 fullpathname(path), adflags2logstr(adflags),
1462 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
1463 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
1464 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
1466 if (adflags & ADFLAGS_CHECK_OF)
1467 /* Checking for open forks requires sharemode lock support (ie RDWR instead of RDONLY) */
1468 adflags |= ADFLAGS_SETSHRMD;
1470 if ((ad->ad_vers == AD_VERSION2) && (adflags & ADFLAGS_RF)) {
1471 adflags |= ADFLAGS_HF;
1472 if (adflags & ADFLAGS_NORF)
1473 adflags |= ADFLAGS_NOHF;
1476 if (ad->ad_inited != AD_INITED) {
1477 ad->ad_adflags = adflags;
1478 ad->ad_inited = AD_INITED;
1480 ad->ad_open_forks = ((ad->ad_data_fork.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
1481 if (ad->ad_resource_fork.adf_refcount > 0)
1482 ad->ad_open_forks |= ATTRBIT_ROPEN;
1485 va_start(args, adflags);
1486 if (adflags & ADFLAGS_CREATE)
1487 mode = va_arg(args, mode_t);
1490 if (adflags & ADFLAGS_DF) {
1491 EC_ZERO( ad_open_df(path, adflags, mode, ad) );
1494 if (adflags & ADFLAGS_HF) {
1495 EC_ZERO( ad_open_hf(path, adflags, mode, ad) );
1498 if (adflags & ADFLAGS_RF) {
1499 EC_ZERO( ad_open_rf(path, adflags, mode, ad) );
1502 if (adflags & ADFLAGS_CHECK_OF) {
1503 ad->ad_open_forks |= ad_openforks(ad, ad->ad_open_forks);
1507 LOG(log_debug, logtype_default,
1508 "ad_open(\"%s\"): END: %d [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
1509 fullpathname(path), ret,
1510 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
1511 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
1512 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
1518 * @brief open metadata, possibly as root
1520 * Return only metadata but try very hard ie at first try as user, then try as root.
1522 * @param name name of file/dir
1523 * @param flags ADFLAGS_DIR: name is a directory \n
1524 * ADFLAGS_CHECK_OF: test if name is open by us or another afpd process
1526 * @param adp pointer to struct adouble
1528 int ad_metadata(const char *name, int flags, struct adouble *adp)
1531 int ret, err, oflags;
1533 /* Sanitize flags */
1534 oflags = (flags & (ADFLAGS_CHECK_OF | ADFLAGS_DIR)) | ADFLAGS_HF | ADFLAGS_RDONLY;
1536 if ((ret = ad_open(adp, name, oflags)) < 0 && errno == EACCES) {
1539 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1543 /* we are root open read only */
1544 ret = ad_open(adp, name, oflags);
1546 if ( seteuid(uid) < 0) {
1547 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1557 * @brief openat like wrapper for ad_metadata
1559 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1565 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1571 if (ad_metadata(name, flags, adp) < 0) {
1578 if (fchdir(cwdfd) != 0) {
1579 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1592 int ad_refresh(const char *path, struct adouble *ad)
1594 switch (ad->ad_vers) {
1596 if (ad_meta_fileno(ad) == -1)
1598 return ad->ad_ops->ad_header_read(NULL, ad, NULL);
1602 if (AD_META_OPEN(ad)) {
1603 if (ad_data_fileno(ad) == -1)
1605 // TODO: read meta EA
1608 if (AD_RSRC_OPEN(ad)) {
1609 if (ad_reso_fileno(ad) == -1)
1612 if (fstat(ad_reso_fileno(ad), &st) == -1)
1614 ad->ad_rlen = st.st_size;
1617 if (AD_META_OPEN(ad)) {
1618 if (ad_data_fileno(ad) == -1)
1620 // TODO: read meta EA
1622 if (AD_RSRC_OPEN(ad)) {
1623 if (ad_reso_fileno(ad) == -1)
1625 if (ad_header_read_osx(path, ad, NULL) < 0)
1629 return ad->ad_ops->ad_header_read(path, ad, NULL);
1638 int ad_openat(struct adouble *ad,
1639 int dirfd, /* dir fd openat like */
1649 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0))
1653 va_start(args, adflags);
1654 if (adflags & ADFLAGS_CREATE)
1655 mode = va_arg(args, mode_t);
1658 EC_NEG1( ad_open(ad, path, adflags, mode) );
1661 if (fchdir(cwdfd) != 0) {
1662 AFP_PANIC("ad_openat: cant chdir back");