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 #define OPENFLAGS2LOGSTRBUFSIZ 128
266 const char *openflags2logstr(int oflags)
269 static char buf[OPENFLAGS2LOGSTRBUFSIZ];
273 if ((oflags & O_RDONLY) || (oflags == O_RDONLY)) {
274 strlcat(buf, "O_RDONLY", OPENFLAGS2LOGSTRBUFSIZ);
277 if (oflags & O_RDWR) {
279 strlcat(buf, "|", OPENFLAGS2LOGSTRBUFSIZ);
280 strlcat(buf, "O_RDWR", OPENFLAGS2LOGSTRBUFSIZ);
283 if (oflags & O_CREAT) {
285 strlcat(buf, "|", OPENFLAGS2LOGSTRBUFSIZ);
286 strlcat(buf, "O_CREAT", OPENFLAGS2LOGSTRBUFSIZ);
289 if (oflags & O_TRUNC) {
291 strlcat(buf, "|", OPENFLAGS2LOGSTRBUFSIZ);
292 strlcat(buf, "O_TRUNC", OPENFLAGS2LOGSTRBUFSIZ);
295 if (oflags & O_EXCL) {
297 strlcat(buf, "|", OPENFLAGS2LOGSTRBUFSIZ);
298 strlcat(buf, "O_EXCL", OPENFLAGS2LOGSTRBUFSIZ);
304 static uint32_t get_eid(uint32_t eid)
309 return ADEID_PRIVDEV;
311 return ADEID_PRIVINO;
313 return ADEID_PRIVSYN;
320 /* ----------------------------------- */
321 static int new_ad_header(struct adouble *ad, const char *path, struct stat *stp, int adflags)
323 const struct entry *eid;
327 LOG(log_debug, logtype_default, "new_ad_header(\"%s\")", path);
331 if (lstat(path, &st) != 0)
335 ad->ad_magic = AD_MAGIC;
336 ad->ad_version = ad->ad_vers & 0x0f0000;
337 if (!ad->ad_version) {
338 ad->ad_version = AD_VERSION;
341 memset(ad->ad_data, 0, sizeof(ad->ad_data));
343 if (ad->ad_vers == AD_VERSION2)
345 else if (ad->ad_vers == AD_VERSION_EA)
346 eid = entry_order_ea;
347 else if (ad->ad_vers == AD_VERSION2_OSX)
348 eid = entry_order_osx;
354 ad->ad_eid[eid->id].ade_off = eid->offset;
355 ad->ad_eid[eid->id].ade_len = eid->len;
359 /* put something sane in the directory finderinfo */
360 if (ad->ad_vers != AD_VERSION2_OSX) {
361 if ((adflags & ADFLAGS_DIR)) {
362 /* set default view */
363 ashort = htons(FINDERINFO_CLOSEDVIEW);
364 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
366 /* set default creator/type fields */
367 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
368 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
371 /* make things invisible */
372 if ((ad->ad_options & ADVOL_INVDOTS)
374 && !((adflags & ADFLAGS_DIR) && (path[1] == 0))
376 ashort = htons(ATTRBIT_INVISIBLE);
377 ad_setattr(ad, ashort);
378 ashort = htons(FINDERINFO_INVISIBLE);
379 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
382 /* put something sane in the date fields */
383 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, stp->st_mtime);
384 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, stp->st_mtime);
385 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, stp->st_mtime);
386 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
391 /* -------------------------------------
394 static void parse_entries(struct adouble *ad, char *buf, uint16_t nentries)
396 uint32_t eid, len, off;
399 /* now, read in the entry bits */
400 for (; nentries > 0; nentries-- ) {
401 memcpy(&eid, buf, sizeof( eid ));
402 eid = get_eid(ntohl(eid));
403 buf += sizeof( eid );
404 memcpy(&off, buf, sizeof( off ));
406 buf += sizeof( off );
407 memcpy(&len, buf, sizeof( len ));
409 buf += sizeof( len );
413 && off < sizeof(ad->ad_data)
414 && (off + len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
415 ad->ad_eid[ eid ].ade_off = off;
416 ad->ad_eid[ eid ].ade_len = len;
417 } else if (!warning) {
419 LOG(log_warning, logtype_default, "parse_entries: bogus eid: %d", eid);
424 /* this reads enough of the header so that we can figure out all of
425 * the entry lengths and offsets. once that's done, we just read/mmap
426 * the rest of the header in.
428 * NOTE: we're assuming that the resource fork is kept at the end of
429 * the file. also, mmapping won't work for the hfs fs until it
430 * understands how to mmap header files. */
431 static int ad_header_read(const char *path _U_, struct adouble *ad, struct stat *hst)
433 char *buf = ad->ad_data;
439 /* read the header */
440 if ((header_len = adf_pread( ad->ad_mdp, buf, AD_DATASZ, 0)) < 0) {
443 if (header_len < AD_HEADER_LEN) {
448 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
449 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
450 ad->ad_magic = ntohl( ad->ad_magic );
451 ad->ad_version = ntohl( ad->ad_version );
453 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
454 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
459 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
460 nentries = ntohs( nentries );
462 /* read in all the entry headers. if we have more than the
463 * maximum, just hope that the rfork is specified early on. */
464 len = nentries*AD_ENTRY_LEN;
466 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
467 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
469 buf += AD_HEADER_LEN;
470 if (len > header_len - AD_HEADER_LEN) {
471 LOG(log_error, logtype_default, "ad_header_read: can't read entry info.");
476 /* figure out all of the entry offsets and lengths. if we aren't
477 * able to read a resource fork entry, bail. */
478 nentries = len / AD_ENTRY_LEN;
479 parse_entries(ad, buf, nentries);
480 if (!ad_getentryoff(ad, ADEID_RFORK)
481 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
483 LOG(log_error, logtype_default, "ad_header_read: problem with rfork entry offset.");
488 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
489 LOG(log_error, logtype_default, "ad_header_read: can't read in entries.");
496 if (fstat(ad->ad_mdp->adf_fd, &st) < 0) {
497 return 1; /* fail silently */
501 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
506 /* Read an ._ file, only uses the resofork, finderinfo is taken from EA */
507 static int ad_header_read_osx(const char *path _U_, struct adouble *ad, struct stat *hst)
510 struct adouble adosx;
511 char *buf = &adosx.ad_data[0];
517 memset(buf, 0, sizeof(adosx.ad_data));
519 /* read the header */
520 EC_NEG1( header_len = adf_pread(ad->ad_rfp, buf, AD_DATASZ_OSX, 0) );
522 if (header_len < AD_HEADER_LEN) {
527 memcpy(&adosx.ad_magic, buf, sizeof(adosx.ad_magic));
528 memcpy(&adosx.ad_version, buf + ADEDOFF_VERSION, sizeof(adosx.ad_version));
529 adosx.ad_magic = ntohl(adosx.ad_magic);
530 adosx.ad_version = ntohl(adosx.ad_version);
532 if ((adosx.ad_magic != AD_MAGIC) || (adosx.ad_version != AD_VERSION2)) {
533 LOG(log_error, logtype_afpd, "ad_header_read_osx: can't parse AppleDouble header");
538 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
539 nentries = ntohs(nentries);
540 len = nentries * AD_ENTRY_LEN;
542 if (len + AD_HEADER_LEN > sizeof(adosx.ad_data))
543 len = sizeof(adosx.ad_data) - AD_HEADER_LEN;
545 buf += AD_HEADER_LEN;
546 if (len > header_len - AD_HEADER_LEN) {
547 LOG(log_error, logtype_afpd, "ad_header_read_osx: can't read entry info.");
552 nentries = len / AD_ENTRY_LEN;
553 parse_entries(&adosx, buf, nentries);
555 if (ad_getentryoff(&adosx, ADEID_RFORK) == 0
556 || ad_getentryoff(&adosx, ADEID_RFORK) > sizeof(ad->ad_data)
557 || ad_getentryoff(&adosx, ADEID_RFORK) > header_len
559 LOG(log_error, logtype_afpd, "ad_header_read_osx: problem with rfork entry offset.");
566 EC_NEG1( fstat(ad_reso_fileno(ad), &st) );
569 ad_setentryoff(ad, ADEID_RFORK, ad_getentryoff(&adosx, ADEID_RFORK));
570 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
576 static int ad_header_read_ea(const char *path, struct adouble *ad, struct stat *hst _U_)
581 char *buf = ad->ad_data;
583 if (ad_meta_fileno(ad) != -1)
584 header_len = sys_fgetxattr(ad_meta_fileno(ad), AD_EA_META, ad->ad_data, AD_DATASZ_EA);
586 header_len = sys_lgetxattr(path, AD_EA_META, ad->ad_data, AD_DATASZ_EA);
587 if (header_len < 1) {
588 LOG(log_debug, logtype_default, "ad_header_read_ea: %s", strerror(errno));
592 if (header_len < AD_HEADER_LEN) {
593 LOG(log_error, logtype_default, "ad_header_read_ea: bogus AppleDouble header.");
598 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
599 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
601 ad->ad_magic = ntohl( ad->ad_magic );
602 ad->ad_version = ntohl( ad->ad_version );
604 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
605 LOG(log_error, logtype_default, "ad_header_read_ea: wrong magic or version");
610 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
611 nentries = ntohs( nentries );
613 /* Protect against bogus nentries */
614 len = nentries * AD_ENTRY_LEN;
615 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
616 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
617 if (len > header_len - AD_HEADER_LEN) {
618 LOG(log_error, logtype_default, "ad_header_read_ea: can't read entry info.");
622 nentries = len / AD_ENTRY_LEN;
624 /* Now parse entries */
625 parse_entries(ad, buf + AD_HEADER_LEN, nentries);
631 * Takes a path to an AppleDouble file and creates the parrent .AppleDouble directory
634 * path: "/path/.AppleDouble/file"
635 * => mkdir("/path/.AppleDouble/") (in ad_mkdir())
637 static int ad_mkrf(const char *path)
641 * Probably .AppleDouble doesn't exist, try to mkdir it.
643 if (NULL == ( slash = strrchr( path, '/' )) ) {
648 if ( ad_mkdir( path, 0777 ) < 0 ) {
655 static int ad_mkrf_ea(const char *path _U_)
657 AFP_PANIC("ad_mkrf_ea: dont use");
661 static int ad_mkrf_osx(const char *path _U_)
667 if we are root change path user/ group
668 It can be a native function for BSD cf. FAQ.Q10
669 path: pathname to chown
670 stbuf: parent directory inode
672 use fstat and fchown or lchown with linux?
674 #define EMULATE_SUIDDIR
676 static int ad_chown(const char *path, struct stat *stbuf)
679 #ifdef EMULATE_SUIDDIR
682 if (default_uid != (uid_t)-1) {
683 /* we are root (admin) */
684 id = (default_uid)?default_uid:stbuf->st_uid;
685 ret = lchown( path, id, stbuf->st_gid );
691 #define DEFMASK 07700 /* be conservative */
694 return access right and inode of path parent directory
696 static int ad_mode_st(const char *path, mode_t *mode, struct stat *stbuf)
701 if (ad_stat(path, stbuf) != 0) {
705 *mode &= stbuf->st_mode;
709 /* --------------------------- */
710 static int ad_header_upgrade(struct adouble *ad _U_, const char *name _U_)
715 static int ad_header_upgrade_ea(struct adouble *ad _U_, const char *name _U_)
717 AFP_PANIC("ad_header_upgrade_ea: dont use");
722 * Error handling for adouble header(=metadata) file open error
724 * We're called because opening ADFLAGS_HF caused an error.
725 * 1. In case ad_open is called with ADFLAGS_NOHF the error is suppressed.
726 * 2. If ad_open was called with ADFLAGS_DF we may have opened the datafork and thus
727 * ought to close it before returning with an error condition.
729 static int ad_error(struct adouble *ad, int adflags)
732 if (adflags & ADFLAGS_NOHF) { /* 1 */
735 if (adflags & (ADFLAGS_DF | ADFLAGS_SETSHRMD | ADFLAGS_CHECK_OF)) { /* 2 */
736 ad_close( ad, ADFLAGS_DF );
743 * Map ADFLAGS to open() flags
745 * @param adfile (r) the file you really want to open: ADFLAGS_DF or ADFLAGS_HF
746 * @param adflags (r) flags from ad_open(..., adflags, ...)
747 * @returns mapped flags suitable for calling open()
749 static int ad2openflags(const struct adouble *ad, int adfile, int adflags)
753 if (adflags & ADFLAGS_RDWR)
755 if (adflags & ADFLAGS_RDONLY) {
756 if (((adfile == ADFLAGS_DF || adfile == ADFLAGS_RF) && (adflags & ADFLAGS_SETSHRMD))
757 /* need rw access for locks */
758 || ((adfile == ADFLAGS_HF) && (ad->ad_vers == AD_VERSION2)))
759 /* need rw access for adouble file for the case:
760 1) openfork(data+meta:O_RDONLY), 2) openfork(reso(=meta):O_RDWR) */
765 if (adflags & ADFLAGS_CREATE)
767 if (adflags & ADFLAGS_EXCL)
769 if (adflags & ADFLAGS_TRUNC)
775 static int ad_open_df(const char *path, int adflags, mode_t mode, struct adouble *ad)
784 LOG(log_debug, logtype_default,
785 "ad_open_df(\"%s\", %s): BEGIN [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
786 fullpathname(path), adflags2logstr(adflags),
787 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
788 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
789 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
791 if (ad_data_fileno(ad) != -1) {
792 /* the file is already open, but we want write access: */
793 if ((adflags & ADFLAGS_RDWR)
794 /* and it was denied the first time: */
795 && (ad->ad_data_fork.adf_flags & O_RDONLY)) {
799 /* it's not new anymore */
800 ad->ad_data_fork.adf_flags &= ~( O_TRUNC | O_CREAT );
801 ad->ad_data_fork.adf_refcount++;
805 oflags = O_NOFOLLOW | ad2openflags(ad, ADFLAGS_DF, adflags);
808 if ((adflags & ADFLAGS_CREATE)) {
809 st_invalid = ad_mode_st(path, &admode, &st_dir);
810 if ((ad->ad_options & ADVOL_UNIXPRIV))
814 ad->ad_data_fork.adf_fd = open(path, oflags, admode);
816 if (ad->ad_data_fork.adf_fd == -1) {
821 if ((adflags & ADFLAGS_SETSHRMD) && (adflags & ADFLAGS_RDONLY)) {
824 EC_NEG1( ad->ad_data_fork.adf_fd = open(path, oflags, admode) );
828 case OPEN_NOFOLLOW_ERRNO:
829 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
830 if ((lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN)) <= 0) {
831 free(ad->ad_data_fork.adf_syml);
834 ad->ad_data_fork.adf_syml[lsz] = 0;
835 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
843 ad_chown(path, &st_dir); /* just created, set owner if admin (root) */
845 ad->ad_data_fork.adf_flags = oflags;
846 adf_lock_init(&ad->ad_data_fork);
847 ad->ad_data_fork.adf_refcount++;
850 LOG(log_debug, logtype_default,
851 "ad_open_df(\"%s\", %s): END: %d [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
852 fullpathname(path), adflags2logstr(adflags), ret,
853 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
854 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
855 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
859 static int ad_open_hf_v2(const char *path, int adflags, mode_t mode, struct adouble *ad)
864 struct stat *pst = NULL;
866 int oflags, nocreatflags, opened = 0;
870 LOG(log_debug, logtype_default,
871 "ad_open_hf_v2(\"%s\", %s): BEGIN [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
872 fullpathname(path), adflags2logstr(adflags),
873 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
874 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
875 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
877 if (ad_meta_fileno(ad) != -1) {
878 /* the file is already open, but we want write access: */
879 if ((adflags & ADFLAGS_RDWR) &&
880 /* and it was already denied: */
881 (ad->ad_mdp->adf_flags & O_RDONLY)) {
885 ad_refresh(path, ad);
886 /* it's not new anymore */
887 ad->ad_mdp->adf_flags &= ~( O_TRUNC | O_CREAT );
888 ad->ad_mdp->adf_refcount++;
892 ad_p = ad->ad_ops->ad_path(path, adflags);
893 oflags = O_NOFOLLOW | ad2openflags(ad, ADFLAGS_HF, adflags);
894 LOG(log_debug, logtype_default,"ad_open_hf_v2(\"%s\"): open flags: %s",
895 fullpathname(path), openflags2logstr(oflags));
896 nocreatflags = oflags & ~(O_CREAT | O_EXCL);
898 ad_meta_fileno(ad) = open(ad_p, nocreatflags);
900 if (ad_meta_fileno(ad) != -1) {
901 ad->ad_mdp->adf_flags = nocreatflags;
907 if ((adflags & ADFLAGS_RDONLY) && (adflags & ADFLAGS_SETSHRMD)) {
908 nocreatflags &= ~O_RDWR;
909 nocreatflags |= O_RDONLY;
910 EC_NEG1( ad_meta_fileno(ad) = open(ad_p, nocreatflags) );
911 ad->ad_mdp->adf_flags = nocreatflags;
916 if (!(oflags & O_CREAT))
919 * We're expecting to create a new adouble header file here
921 LOG(log_debug, logtype_default, "ad_open(\"%s\"): creating adouble file",
925 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
926 if ((ad->ad_options & ADVOL_UNIXPRIV))
928 admode = ad_hf_mode(admode);
929 if (errno == ENOENT) {
930 EC_NEG1_LOG( ad->ad_ops->ad_mkrf(ad_p) );
932 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
933 if ((ad->ad_options & ADVOL_UNIXPRIV))
935 admode = ad_hf_mode(admode);
938 /* retry with O_CREAT */
939 EC_NEG1( ad_meta_fileno(ad) = open(ad_p, oflags, admode) );
940 ad->ad_mdp->adf_flags = oflags;
941 /* just created, set owner if admin owner (root) */
943 ad_chown(ad_p, &st_dir);
950 /* Now we've got a new opened fd, we need to check that in the error case */
953 if (!(ad->ad_mdp->adf_flags & O_CREAT)) {
954 /* check for 0 length files, treat them as new. */
955 if (fstat(ad->ad_mdp->adf_fd, &st_meta) == 0) {
956 if (st_meta.st_size == 0)
957 ad->ad_mdp->adf_flags |= O_TRUNC;
959 /* we have valid data in st_meta stat structure, reused it in ad_header_read */
964 adf_lock_init(ad->ad_mdp);
965 ad->ad_mdp->adf_refcount = 1;
967 if ((ad->ad_mdp->adf_flags & ( O_TRUNC | O_CREAT ))) {
968 /* This is a new adouble header file, create it */
969 EC_NEG1_LOG( new_ad_header(ad, path, pst, adflags) );
972 /* Read the adouble header in and parse it.*/
973 EC_NEG1_LOG( ad->ad_ops->ad_header_read(path, ad, pst) );
977 if (ret != 0 && opened && ad_meta_fileno(ad) != -1) {
978 close(ad_meta_fileno(ad));
979 ad_meta_fileno(ad) = -1;
980 ad->ad_mdp->adf_refcount = 0;
982 LOG(log_debug, logtype_default,
983 "ad_open_hf_v2(\"%s\", %s): END: %d [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
984 fullpathname(path), adflags2logstr(adflags), ret,
985 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
986 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
987 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
991 static int ad_open_hf_ea(const char *path, int adflags, int mode, struct adouble *ad)
998 LOG(log_debug, logtype_default,
999 "ad_open_hf_ea(\"%s\", %s): BEGIN [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
1000 fullpathname(path), adflags2logstr(adflags),
1001 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
1002 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
1003 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
1005 oflags = O_NOFOLLOW | (ad2openflags(ad, ADFLAGS_DF, adflags) & ~(O_CREAT | O_TRUNC));
1007 if (ad_meta_fileno(ad) == -2)
1011 if (ad_meta_fileno(ad) != -1) {
1012 /* the file is already open, but we want write access: */
1013 if ((oflags & O_RDWR) &&
1014 /* and it was already denied: */
1015 (ad->ad_mdp->adf_flags & O_RDONLY)) {
1016 LOG(log_error, logtype_default, "ad_open_hf_ea(%s): rw request for ro file: %s",
1017 fullpathname(path), strerror(errno));
1021 /* it's not new anymore */
1022 ad->ad_mdp->adf_flags &= ~( O_TRUNC | O_CREAT );
1024 if (oflags & O_RDWR) {
1025 /* Fo a RDONLY adouble we just use sys_lgetxattr instead if sys_fgetxattr */
1026 if (adflags & ADFLAGS_DIR)
1027 /* For directories we open the directory RDONYL so we can later fchdir() */
1028 oflags = (oflags & ~O_RDWR) | O_RDONLY;
1029 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): opening base file for meta adouble EA", path);
1030 EC_NEG1(ad_meta_fileno(ad) = open(path, oflags));
1032 ad->ad_mdp->adf_flags = oflags;
1036 /* Read the adouble header in and parse it.*/
1037 if (ad->ad_ops->ad_header_read(path, ad, NULL) != 0) {
1038 if (!(adflags & ADFLAGS_CREATE)) {
1039 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): can't read metadata EA", path);
1044 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): creating metadata EA", path);
1046 /* It doesnt exist, EPERM or another error */
1047 if (!(errno == ENOATTR || errno == ENOENT)) {
1048 LOG(log_error, logtype_default, "ad_open_hf_ea: unexpected: %s", strerror(errno));
1053 EC_NEG1_LOG(new_ad_header(ad, path, NULL, adflags));
1054 ad->ad_mdp->adf_flags |= O_CREAT; /* mark as just created */
1056 LOG(log_debug, logtype_default, "ad_open_hf_ea(\"%s\"): created metadata EA", path);
1059 ad->ad_mdp->adf_refcount++;
1060 (void)ad_reso_size(path, adflags, ad);
1063 if (ret != 0 && opened && ad_meta_fileno(ad) != -1) {
1064 close(ad_meta_fileno(ad));
1065 ad_meta_fileno(ad) = -1;
1066 ad->ad_mdp->adf_refcount = 0;
1068 LOG(log_debug, logtype_default,
1069 "ad_open_hf_ea(\"%s\", %s): END: %d [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
1070 fullpathname(path), adflags2logstr(adflags), ret,
1071 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
1072 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
1073 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
1078 static int ad_open_hf(const char *path, int adflags, int mode, struct adouble *ad)
1082 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
1085 switch (ad->ad_vers) {
1087 ret = ad_open_hf_v2(path, adflags, mode, ad);
1090 ret = ad_open_hf_ea(path, adflags, mode, ad);
1098 ret = ad_error(ad, adflags);
1104 * Get resofork length for adouble:ea
1106 static int ad_reso_size(const char *path, int adflags, struct adouble *ad)
1111 LOG(log_debug, logtype_default, "ad_reso_size(\"%s\")", path);
1116 if (ad_reso_fileno(ad) != -1) {
1117 EC_NEG1( fstat(ad_reso_fileno(ad), &st) );
1118 ad->ad_rlen = st.st_size;
1119 } else if (ad_meta_fileno(ad) != -1) {
1120 EC_NEG1( easz = sys_fgetxattr(ad_meta_fileno(ad), AD_EA_RESO, NULL, 0) );
1128 EC_NULL_LOG( rfpath = ad->ad_ops->ad_path(path, adflags));
1129 EC_ZERO( lstat(rfpath, &st));
1130 if (st.st_size > ADEDOFF_RFORK_OSX)
1131 ad->ad_rlen = st.st_size - ADEDOFF_RFORK_OSX;
1136 LOG(log_debug, logtype_default, "ad_reso_size(\"%s\"): size: %zd", path, ad->ad_rlen);
1145 * Open ressource fork
1147 * Only for adouble:ea, a nullop otherwise because adouble:v2 has the ressource fork as part
1148 * of the adouble file which is openend by ADFLAGS_HF.
1150 static int ad_open_rf(const char *path, int adflags, int mode, struct adouble *ad)
1155 int closeflags = adflags & (ADFLAGS_DF | ADFLAGS_HF);
1162 if (ad->ad_vers != AD_VERSION_EA)
1165 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): BEGIN", fullpathname(path));
1167 oflags = O_NOFOLLOW | (ad2openflags(ad, ADFLAGS_RF, adflags) & ~O_CREAT);
1169 if (ad_reso_fileno(ad) != -1) {
1170 /* the file is already open, but we want write access: */
1171 if ((oflags & O_RDWR)
1172 /* and it was already denied: */
1173 && (ad->ad_rfp->adf_flags & O_RDONLY)) {
1177 ad->ad_rfp->adf_flags &= ~( O_TRUNC | O_CREAT );
1178 ad->ad_rfp->adf_refcount++;
1179 EC_NEG1_LOG( ad_reso_size(path, adflags, ad));
1183 if (ad_meta_fileno(ad) == -1)
1185 if ((ad_reso_fileno(ad) = sys_getxattrfd(ad_meta_fileno(ad), AD_EA_RESO, oflags)) == -1) {
1186 if (!(adflags & ADFLAGS_CREATE)) {
1191 EC_NEG1_LOG( ad_reso_fileno(ad) = sys_getxattrfd(ad_meta_fileno(ad),
1192 AD_EA_RESO, oflags, 0666) );
1195 EC_NULL_LOG( rfpath = ad->ad_ops->ad_path(path, adflags) );
1196 if ((ad_reso_fileno(ad) = open(rfpath, oflags)) == -1) {
1197 if (!(adflags & ADFLAGS_CREATE))
1200 EC_NEG1_LOG( ad_reso_fileno(ad) = open(rfpath, oflags, mode) );
1201 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): created adouble rfork: \"%s\"",
1206 ad->ad_rfp->adf_refcount = 1;
1207 ad->ad_rfp->adf_flags = oflags;
1210 EC_ZERO_LOG( fstat(ad_reso_fileno(ad), &st) );
1211 if (ad->ad_rfp->adf_flags & O_CREAT) {
1212 /* This is a new adouble header file, create it */
1213 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): created adouble rfork, initializing: \"%s\"",
1215 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): created adouble rfork, flushing: \"%s\"",
1219 /* Read the adouble header */
1220 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): reading adouble rfork: \"%s\"",
1222 EC_NEG1_LOG( ad_header_read_osx(NULL, ad, &st) );
1226 (void)ad_reso_size(path, adflags, ad);
1230 if (opened && (ad_reso_fileno(ad) != -1)) {
1231 close(ad_reso_fileno(ad));
1232 ad_reso_fileno(ad) = -1;
1233 ad->ad_rfp->adf_refcount = 0;
1235 if (adflags & ADFLAGS_NORF) {
1239 (void)ad_close(ad, closeflags);
1245 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\"): END: %d", fullpathname(path), ret);
1250 /***********************************************************************************
1252 ********************************************************************************* */
1254 off_t ad_getentryoff(const struct adouble *ad, int eid)
1256 if (ad->ad_vers == AD_VERSION2)
1257 return ad->ad_eid[eid].ade_off;
1266 return ADEDOFF_RFORK_OSX;
1269 return ad->ad_eid[eid].ade_off;
1272 AFP_PANIC("What am I doing here?");
1275 const char *ad_path_ea( const char *path, int adflags _U_)
1280 const char *ad_path_osx(const char *path, int adflags _U_)
1282 static char pathbuf[ MAXPATHLEN + 1];
1283 char c, *slash, buf[MAXPATHLEN + 1];
1285 if (!strcmp(path,".")) {
1287 getcwd(buf, MAXPATHLEN);
1290 strlcpy(buf, path, MAXPATHLEN +1);
1292 if (NULL != ( slash = strrchr( buf, '/' )) ) {
1295 strlcpy( pathbuf, buf, MAXPATHLEN +1);
1298 pathbuf[ 0 ] = '\0';
1301 strlcat( pathbuf, "._", MAXPATHLEN +1);
1302 strlcat( pathbuf, slash, MAXPATHLEN +1);
1307 * Put the .AppleDouble where it needs to be:
1309 * / a/.AppleDouble/b
1311 * \ b/.AppleDouble/.Parent
1313 * FIXME: should do something for pathname > MAXPATHLEN
1315 const char *ad_path( const char *path, int adflags)
1317 static char pathbuf[ MAXPATHLEN + 1];
1321 if ( adflags & ADFLAGS_DIR ) {
1322 l = strlcpy( pathbuf, path, sizeof(pathbuf));
1324 if ( l && l < MAXPATHLEN) {
1327 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
1329 if (NULL != ( slash = strrchr( path, '/' )) ) {
1332 /* XXX we must return NULL here and test in the caller */
1335 memcpy( pathbuf, path, l);
1340 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
1341 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
1347 /* -------------------------
1348 * Support inherited protection modes for AppleDouble files. The supplied
1349 * mode is ANDed with the parent directory's mask value in lieu of "umask",
1350 * and that value is returned.
1352 char *ad_dir(const char *path)
1354 static char modebuf[ MAXPATHLEN + 1];
1357 * For a path with directories in it, remove the final component
1358 * (path or subdirectory name) to get the name we want to stat.
1359 * For a path which is just a filename, use "." instead.
1361 slash = strrchr( path, '/' );
1366 if (len >= MAXPATHLEN) {
1367 errno = ENAMETOOLONG;
1368 return NULL; /* can't do it */
1370 memcpy( modebuf, path, len );
1371 modebuf[len] = '\0';
1372 /* is last char a '/' ? */
1373 if (slash[1] == 0) {
1374 slash = modebuf+ len;
1376 while (modebuf < slash && slash[-1] == '/') {
1379 if (modebuf == slash) {
1383 while (modebuf < slash && *slash != '/') {
1386 if (modebuf == slash) {
1389 *slash = '\0'; /* remove pathname component */
1394 modebuf[0] = '.'; /* use current directory */
1399 int ad_setfuid(const uid_t id)
1405 /* ---------------- */
1406 uid_t ad_getfuid(void)
1412 stat path parent directory
1414 int ad_stat(const char *path, struct stat *stbuf)
1421 return lstat( p, stbuf );
1425 return access right of path parent directory
1427 int ad_mode( const char *path, mode_t mode)
1430 ad_mode_st(path, &mode, &stbuf);
1435 * Use mkdir() with mode bits taken from ad_mode().
1437 int ad_mkdir( const char *path, mode_t mode)
1443 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
1444 path, mode, getcwdpath());
1446 st_invalid = ad_mode_st(path, &mode, &stbuf);
1447 ret = mkdir( path, mode );
1448 if (ret || st_invalid)
1450 ad_chown(path, &stbuf);
1455 static void ad_init_func(struct adouble *ad)
1457 switch (ad->ad_vers) {
1459 ad->ad_ops = &ad_adouble;
1460 ad->ad_rfp = &ad->ad_resource_fork;
1461 ad->ad_mdp = &ad->ad_resource_fork;
1464 ad->ad_ops = &ad_adouble_ea;
1465 ad->ad_rfp = &ad->ad_resource_fork;
1466 ad->ad_mdp = &ad->ad_data_fork;
1469 AFP_PANIC("ad_init: unknown AD version");
1473 ad_data_fileno(ad) = -1;
1474 ad_reso_fileno(ad) = -1;
1475 ad_meta_fileno(ad) = -1;
1476 ad->ad_refcount = 1;
1480 void ad_init_old(struct adouble *ad, int flags, int options)
1482 memset(ad, 0, sizeof(struct adouble));
1483 ad->ad_vers = flags;
1484 ad->ad_options = options;
1488 void ad_init(struct adouble *ad, const struct vol * restrict vol)
1490 memset(ad, 0, sizeof(struct adouble));
1491 ad->ad_vers = vol->v_adouble;
1492 ad->ad_options = vol->v_ad_options;
1497 * Open data-, metadata(header)- or ressource fork
1499 * ad_open(struct adouble *ad, const char *path, int adflags, int flags)
1500 * ad_open(struct adouble *ad, const char *path, int adflags, int flags, mode_t mode)
1502 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
1505 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1508 * Open a files data fork, metadata fork or ressource fork.
1510 * @param ad (rw) pointer to struct adouble
1511 * @param path (r) Path to file or directory
1512 * @param adflags (r) Flags specifying which fork to open, can be or'd:
1513 * ADFLAGS_DF: open data fork
1514 * ADFLAGS_RF: open ressource fork
1515 * ADFLAGS_HF: open header (metadata) file
1516 * ADFLAGS_NOHF: it's not an error if header file couldn't be opened
1517 * ADFLAGS_NORF: it's not an error if reso fork couldn't be opened
1518 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags
1520 * Access mode for the forks:
1521 * ADFLAGS_RDONLY: open read only
1522 * ADFLAGS_RDWR: open read write
1525 * ADFLAGS_CREATE: create if not existing
1526 * ADFLAGS_TRUNC: truncate
1529 * ADFLAGS_CHECK_OF: check for open forks from us and other afpd's
1530 * ADFLAGS_SETSHRMD: this adouble struct will be used to set sharemode locks.
1531 * This basically results in the files being opened RW instead of RDONLY.
1532 * @param mode (r) mode used with O_CREATE
1534 * The open mode flags (rw vs ro) have to take into account all the following requirements:
1535 * - we remember open fds for files because me must avoid a single close releasing fcntl locks for other
1536 * fds of the same file
1538 * @returns 0 on success, any other value indicates an error
1540 int ad_open(struct adouble *ad, const char *path, int adflags, ...)
1546 LOG(log_debug, logtype_default,
1547 "ad_open(\"%s\", %s): BEGIN [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
1548 fullpathname(path), adflags2logstr(adflags),
1549 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
1550 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
1551 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
1553 if (adflags & ADFLAGS_CHECK_OF)
1554 /* Checking for open forks requires sharemode lock support (ie RDWR instead of RDONLY) */
1555 adflags |= ADFLAGS_SETSHRMD;
1557 if (ad->ad_vers == AD_VERSION2) {
1558 if (adflags & ADFLAGS_SETSHRMD)
1559 /* sharemode locks are stored in the data fork, adouble:v2 needs this extra handling here */
1560 adflags |= ADFLAGS_DF;
1561 if (adflags & ADFLAGS_RF)
1562 adflags |= ADFLAGS_HF;
1563 if (adflags & ADFLAGS_NORF)
1564 adflags |= ADFLAGS_NOHF;
1567 if (ad->ad_inited != AD_INITED) {
1568 ad->ad_adflags = adflags;
1569 ad->ad_inited = AD_INITED;
1571 ad->ad_open_forks = ((ad->ad_data_fork.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
1572 if (ad->ad_resource_fork.adf_refcount > 0)
1573 ad->ad_open_forks |= ATTRBIT_ROPEN;
1576 va_start(args, adflags);
1577 if (adflags & ADFLAGS_CREATE)
1578 mode = va_arg(args, mode_t);
1581 if (adflags & ADFLAGS_DF) {
1582 EC_ZERO( ad_open_df(path, adflags, mode, ad) );
1585 if (adflags & ADFLAGS_HF) {
1586 EC_ZERO( ad_open_hf(path, adflags, mode, ad) );
1589 if (adflags & ADFLAGS_RF) {
1590 EC_ZERO( ad_open_rf(path, adflags, mode, ad) );
1593 if (adflags & ADFLAGS_CHECK_OF) {
1594 ad->ad_open_forks |= ad_openforks(ad, ad->ad_open_forks);
1598 LOG(log_debug, logtype_default,
1599 "ad_open(\"%s\"): END: %d [dfd: %d (ref: %d), mfd: %d (ref: %d), rfd: %d (ref: %d)]",
1600 fullpathname(path), ret,
1601 ad_data_fileno(ad), ad->ad_data_fork.adf_refcount,
1602 ad_meta_fileno(ad), ad->ad_mdp->adf_refcount,
1603 ad_reso_fileno(ad), ad->ad_rfp->adf_refcount);
1609 * @brief open metadata, possibly as root
1611 * Return only metadata but try very hard ie at first try as user, then try as root.
1613 * @param name name of file/dir
1614 * @param flags ADFLAGS_DIR: name is a directory \n
1615 * ADFLAGS_CHECK_OF: test if name is open by us or another afpd process
1617 * @param adp pointer to struct adouble
1619 int ad_metadata(const char *name, int flags, struct adouble *adp)
1622 int ret, err, oflags;
1624 /* Sanitize flags */
1625 oflags = (flags & (ADFLAGS_CHECK_OF | ADFLAGS_DIR)) | ADFLAGS_HF | ADFLAGS_RDONLY;
1627 if ((ret = ad_open(adp, name, oflags)) < 0 && errno == EACCES) {
1630 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1634 /* we are root open read only */
1635 ret = ad_open(adp, name, oflags);
1637 if ( seteuid(uid) < 0) {
1638 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1648 * @brief openat like wrapper for ad_metadata
1650 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1656 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1662 if (ad_metadata(name, flags, adp) < 0) {
1669 if (fchdir(cwdfd) != 0) {
1670 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1683 int ad_refresh(const char *path, struct adouble *ad)
1685 switch (ad->ad_vers) {
1687 if (ad_meta_fileno(ad) == -1)
1689 return ad->ad_ops->ad_header_read(NULL, ad, NULL);
1693 if (AD_META_OPEN(ad)) {
1694 if (ad_data_fileno(ad) == -1)
1696 // TODO: read meta EA
1699 if (AD_RSRC_OPEN(ad)) {
1700 if (ad_reso_fileno(ad) == -1)
1703 if (fstat(ad_reso_fileno(ad), &st) == -1)
1705 ad->ad_rlen = st.st_size;
1708 if (AD_META_OPEN(ad)) {
1709 if (ad_data_fileno(ad) == -1)
1711 // TODO: read meta EA
1713 if (AD_RSRC_OPEN(ad)) {
1714 if (ad_reso_fileno(ad) == -1)
1716 if (ad_header_read_osx(path, ad, NULL) < 0)
1720 return ad->ad_ops->ad_header_read(path, ad, NULL);
1729 int ad_openat(struct adouble *ad,
1730 int dirfd, /* dir fd openat like */
1740 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0))
1744 va_start(args, adflags);
1745 if (adflags & ADFLAGS_CREATE)
1746 mode = va_arg(args, mode_t);
1749 EC_NEG1( ad_open(ad, path, adflags, mode) );
1752 if (fchdir(cwdfd) != 0) {
1753 AFP_PANIC("ad_openat: cant chdir back");