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 * @note We don't use inlines because a good compiler should be
32 * able to optimize all the static funcs below.
33 * @sa include/atalk/adouble.h
38 #endif /* HAVE_CONFIG_H */
41 #include <sys/param.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>
55 #define MAX(a, b) ((a) < (b) ? (b) : (a))
61 # define AD_SET(a) a = 0
64 #define ADEDOFF_MAGIC (0)
65 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
66 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
67 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
69 /* initial lengths of some of the fields */
70 #define ADEDLEN_INIT 0
72 /* i stick things in a slightly different order than their eid order in
73 * case i ever want to separate RootInfo behaviour from the rest of the
77 #define ADEDOFF_NAME_V2 (AD_HEADER_LEN + ADEID_NUM_V2*AD_ENTRY_LEN)
78 #define ADEDOFF_COMMENT_V2 (ADEDOFF_NAME_V2 + ADEDLEN_NAME)
79 #define ADEDOFF_FILEDATESI (ADEDOFF_COMMENT_V2 + ADEDLEN_COMMENT)
80 #define ADEDOFF_FINDERI_V2 (ADEDOFF_FILEDATESI + ADEDLEN_FILEDATESI)
81 #define ADEDOFF_DID (ADEDOFF_FINDERI_V2 + ADEDLEN_FINDERI)
82 #define ADEDOFF_AFPFILEI (ADEDOFF_DID + ADEDLEN_DID)
83 #define ADEDOFF_SHORTNAME (ADEDOFF_AFPFILEI + ADEDLEN_AFPFILEI)
84 #define ADEDOFF_PRODOSFILEI (ADEDOFF_SHORTNAME + ADEDLEN_SHORTNAME)
85 #define ADEDOFF_PRIVDEV (ADEDOFF_PRODOSFILEI + ADEDLEN_PRODOSFILEI)
86 #define ADEDOFF_PRIVINO (ADEDOFF_PRIVDEV + ADEDLEN_PRIVDEV)
87 #define ADEDOFF_PRIVSYN (ADEDOFF_PRIVINO + ADEDLEN_PRIVINO)
88 #define ADEDOFF_PRIVID (ADEDOFF_PRIVSYN + ADEDLEN_PRIVSYN)
89 #define ADEDOFF_RFORK_V2 (ADEDOFF_PRIVID + ADEDLEN_PRIVID)
92 #define ADEDOFF_FINDERI_EA (AD_HEADER_LEN + ADEID_NUM_EA * AD_ENTRY_LEN)
93 #define ADEDOFF_COMMENT_EA (ADEDOFF_FINDERI_EA + ADEDLEN_FINDERI)
94 #define ADEDOFF_FILEDATESI_EA (ADEDOFF_COMMENT_EA + ADEDLEN_COMMENT)
95 #define ADEDOFF_AFPFILEI_EA (ADEDOFF_FILEDATESI_EA + ADEDLEN_FILEDATESI)
97 /* this is to prevent changing timezones from causing problems with
98 localtime volumes. the screw-up is 30 years. we use a delta of 5 years */
99 #define TIMEWARP_DELTA 157680000
102 uint32_t id, offset, len;
105 /* --------------------------- */
106 static uid_t default_uid = -1;
108 /* Forward declarations */
109 static int ad_mkrf(char *path);
110 static int ad_header_read(struct adouble *ad, struct stat *hst);
111 static int ad_header_upgrade(struct adouble *ad, char *name);
113 static int ad_mkrf_ea(char *path);
114 static int ad_header_read_ea(struct adouble *ad, struct stat *hst);
115 static int ad_header_upgrade_ea(struct adouble *ad, char *name);
117 static struct adouble_fops ad_adouble = {
120 &ad_rebuild_adouble_header,
125 static struct adouble_fops ad_adouble_ea = {
128 &ad_rebuild_adouble_header,
130 &ad_header_upgrade_ea,
133 static const struct entry entry_order2[ADEID_NUM_V2 + 1] = {
134 {ADEID_NAME, ADEDOFF_NAME_V2, ADEDLEN_INIT},
135 {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
136 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
137 {ADEID_FINDERI, ADEDOFF_FINDERI_V2, ADEDLEN_FINDERI},
138 {ADEID_DID, ADEDOFF_DID, ADEDLEN_DID},
139 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
140 {ADEID_SHORTNAME, ADEDOFF_SHORTNAME, ADEDLEN_INIT},
141 {ADEID_PRODOSFILEI, ADEDOFF_PRODOSFILEI, ADEDLEN_PRODOSFILEI},
142 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV, ADEDLEN_INIT},
143 {ADEID_PRIVINO, ADEDOFF_PRIVINO, ADEDLEN_INIT},
144 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN, ADEDLEN_INIT},
145 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
146 {ADEID_RFORK, ADEDOFF_RFORK_V2, ADEDLEN_INIT},
150 /* Using Extended Attributes */
151 static const struct entry entry_order_ea[ADEID_NUM_EA + 1] = {
152 {ADEID_FINDERI, ADEDOFF_FINDERI_EA, ADEDLEN_FINDERI},
153 {ADEID_COMMENT, ADEDOFF_COMMENT_EA, ADEDLEN_INIT},
154 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_EA, ADEDLEN_FILEDATESI},
155 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_EA, ADEDLEN_AFPFILEI},
159 static uint32_t get_eid(uint32_t eid)
164 return ADEID_PRIVDEV;
166 return ADEID_PRIVINO;
168 return ADEID_PRIVSYN;
175 /* ----------------------------------- */
176 static int new_ad_header(const char *path, struct adouble *ad, int adflags)
178 const struct entry *eid;
182 ad->ad_magic = AD_MAGIC;
183 ad->ad_version = ad->ad_flags & 0x0f0000;
184 if (!ad->ad_version) {
185 ad->ad_version = AD_VERSION;
189 memset(ad->ad_data, 0, sizeof(ad->ad_data));
191 if (ad->ad_flags == AD_VERSION2)
193 else if (ad->ad_flags == AD_VERSION_EA)
194 eid = entry_order_ea;
200 ad->ad_eid[eid->id].ade_off = eid->offset;
201 ad->ad_eid[eid->id].ade_len = eid->len;
205 /* put something sane in the directory finderinfo */
206 if ((adflags & ADFLAGS_DIR)) {
207 /* set default view */
208 ashort = htons(FINDERINFO_CLOSEDVIEW);
209 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
211 /* set default creator/type fields */
212 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
213 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
216 /* make things invisible */
217 if ((ad->ad_options & ADVOL_INVDOTS) && (*path == '.')) {
218 ashort = htons(ATTRBIT_INVISIBLE);
219 ad_setattr(ad, ashort);
220 ashort = htons(FINDERINFO_INVISIBLE);
221 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
224 if (lstat(path, &st) < 0)
227 /* put something sane in the date fields */
228 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
229 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
230 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
231 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
235 /* -------------------------------------
238 static void parse_entries(struct adouble *ad, char *buf, uint16_t nentries)
240 uint32_t eid, len, off;
243 /* now, read in the entry bits */
244 for (; nentries > 0; nentries-- ) {
245 memcpy(&eid, buf, sizeof( eid ));
246 eid = get_eid(ntohl(eid));
247 buf += sizeof( eid );
248 memcpy(&off, buf, sizeof( off ));
250 buf += sizeof( off );
251 memcpy(&len, buf, sizeof( len ));
253 buf += sizeof( len );
255 if (eid && eid < ADEID_MAX && off < sizeof(ad->ad_data) &&
256 (off + len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
257 ad->ad_eid[ eid ].ade_off = off;
258 ad->ad_eid[ eid ].ade_len = len;
259 } else if (!warning) {
261 LOG(log_warning, logtype_default, "parse_entries: bogus eid: %d", eid);
266 /* this reads enough of the header so that we can figure out all of
267 * the entry lengths and offsets. once that's done, we just read/mmap
268 * the rest of the header in.
270 * NOTE: we're assuming that the resource fork is kept at the end of
271 * the file. also, mmapping won't work for the hfs fs until it
272 * understands how to mmap header files. */
273 static int ad_header_read(struct adouble *ad, struct stat *hst)
275 char *buf = ad->ad_data;
281 /* read the header */
282 if ((header_len = adf_pread( ad->ad_md, buf, sizeof(ad->ad_data), 0)) < 0) {
285 if (header_len < AD_HEADER_LEN) {
290 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
291 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
292 ad->ad_magic = ntohl( ad->ad_magic );
293 ad->ad_version = ntohl( ad->ad_version );
295 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
296 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
301 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
302 nentries = ntohs( nentries );
304 /* read in all the entry headers. if we have more than the
305 * maximum, just hope that the rfork is specified early on. */
306 len = nentries*AD_ENTRY_LEN;
308 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
309 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
311 buf += AD_HEADER_LEN;
312 if (len > header_len - AD_HEADER_LEN) {
313 LOG(log_error, logtype_default, "ad_header_read: can't read entry info.");
318 /* figure out all of the entry offsets and lengths. if we aren't
319 * able to read a resource fork entry, bail. */
320 nentries = len / AD_ENTRY_LEN;
321 parse_entries(ad, buf, nentries);
322 if (!ad_getentryoff(ad, ADEID_RFORK)
323 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
325 LOG(log_error, logtype_default, "ad_header_read: problem with rfork entry offset.");
330 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
331 LOG(log_error, logtype_default, "ad_header_read: can't read in entries.");
338 if (fstat(ad->ad_md->adf_fd, &st) < 0) {
339 return 1; /* fail silently */
343 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
348 static int ad_header_read_ea(struct adouble *ad, struct stat *hst _U_)
353 char *buf = ad->ad_data;
355 /* read the header */
356 if ((header_len = sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_META, ad->ad_data, AD_DATASZ_EA)) < 1) {
357 LOG(log_debug, logtype_default, "ad_header_read_ea: %s (%u)", strerror(errno), errno);
361 if (header_len < AD_HEADER_LEN) {
362 LOG(log_error, logtype_default, "ad_header_read_ea: bogus AppleDouble header.");
367 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
368 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
370 ad->ad_magic = ntohl( ad->ad_magic );
371 ad->ad_version = ntohl( ad->ad_version );
373 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
374 LOG(log_error, logtype_default, "ad_header_read_ea: wrong magic or version");
379 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
380 nentries = ntohs( nentries );
382 /* Protect against bogus nentries */
383 len = nentries * AD_ENTRY_LEN;
384 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
385 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
386 if (len > header_len - AD_HEADER_LEN) {
387 LOG(log_error, logtype_default, "ad_header_read_ea: can't read entry info.");
391 nentries = len / AD_ENTRY_LEN;
393 /* Now parse entries */
394 parse_entries(ad, buf + AD_HEADER_LEN, nentries);
398 static int ad_mkrf(char *path)
402 * Probably .AppleDouble doesn't exist, try to mkdir it.
404 if (NULL == ( slash = strrchr( path, '/' )) ) {
409 if ( ad_mkdir( path, 0777 ) < 0 ) {
416 static int ad_mkrf_ea(char *path _U_)
418 AFP_PANIC("ad_mkrf_ea: dont use");
423 if we are root change path user/ group
424 It can be a native function for BSD cf. FAQ.Q10
425 path: pathname to chown
426 stbuf: parent directory inode
428 use fstat and fchown or lchown with linux?
430 #define EMULATE_SUIDDIR
432 static int ad_chown(const char *path, struct stat *stbuf)
435 #ifdef EMULATE_SUIDDIR
438 if (default_uid != (uid_t)-1) {
439 /* we are root (admin) */
440 id = (default_uid)?default_uid:stbuf->st_uid;
441 ret = lchown( path, id, stbuf->st_gid );
447 #define DEFMASK 07700 /* be conservative */
450 return access right and inode of path parent directory
452 static int ad_mode_st(const char *path, int *mode, struct stat *stbuf)
457 if (ad_stat(path, stbuf) != 0) {
461 *mode &= stbuf->st_mode;
465 /* --------------------------- */
466 static int ad_header_upgrade(struct adouble *ad _U_, char *name _U_)
471 static int ad_header_upgrade_ea(struct adouble *ad _U_, char *name _U_)
473 AFP_PANIC("ad_header_upgrade_ea: dont use");
477 static int ad_open_df(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
483 LOG(log_maxdebug, logtype_default, "ad_open_df(\"%s/%s\", adf: 0x%04x, of: 0x%04x)",
484 getcwdpath(), path, adflags, oflags);
486 if (ad_data_fileno(ad) == -1) {
487 hoflags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
489 if ((oflags & O_CREAT)) {
490 st_invalid = ad_mode_st(path, &admode, &st_dir);
491 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
496 ad->ad_data_fork.adf_fd = open(path, hoflags | O_NOFOLLOW, admode);
498 if (ad->ad_data_fork.adf_fd == -1) {
499 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
501 ad->ad_data_fork.adf_fd = open( path, hoflags | O_NOFOLLOW, admode );
503 if (ad->ad_data_fork.adf_fd == -1 && errno == OPEN_NOFOLLOW_ERRNO) {
506 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
507 lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN);
509 free(ad->ad_data_fork.adf_syml);
512 ad->ad_data_fork.adf_syml[lsz] = 0;
513 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
517 if ( ad->ad_data_fork.adf_fd == -1 )
520 AD_SET(ad->ad_data_fork.adf_off);
521 ad->ad_data_fork.adf_flags = hoflags;
523 /* just created, set owner if admin (root) */
524 ad_chown(path, &st_dir);
527 /* the file is already open... but */
528 if ((oflags & ( O_RDWR | O_WRONLY))
529 /* we want write access */
530 && !(ad->ad_data_fork.adf_flags & ( O_RDWR | O_WRONLY))) {
531 /* and it was denied the first time */
536 * for now ad_open is never called with O_TRUNC or O_EXCL if the file is
537 * already open. Should we check for it? ie
539 * O_TRUNC --> truncate the fork.
540 * idem for ressource fork.
547 static int ad_open_hf_v2(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
551 struct stat *pst = NULL;
556 ad_p = ad->ad_ops->ad_path( path, adflags );
558 hoflags = oflags & ~(O_CREAT | O_EXCL);
559 if (!(adflags & ADFLAGS_RDONLY)) {
560 hoflags = (hoflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
562 ad->ad_md->adf_fd = open( ad_p, hoflags | O_NOFOLLOW, 0 );
563 if (ad->ad_md->adf_fd < 0 ) {
564 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
565 hoflags = oflags & ~(O_CREAT | O_EXCL);
566 ad->ad_md->adf_fd = open( ad_p, hoflags | O_NOFOLLOW, 0 );
570 if ( ad->ad_md->adf_fd < 0 ) {
571 if (errno == ENOENT && (oflags & O_CREAT) ) {
573 * We're expecting to create a new adouble header file here
574 * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
576 LOG(log_debug, logtype_default, "ad_open(\"%s/%s\"): creating adouble file",
580 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
581 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
584 admode = ad_hf_mode(admode);
585 if ((errno == ENOENT)) {
586 if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
590 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
591 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
594 admode = ad_hf_mode(admode);
596 /* retry with O_CREAT */
597 ad->ad_md->adf_fd = open( ad_p, oflags,admode );
598 if ( ad->ad_md->adf_fd < 0 ) {
601 ad->ad_md->adf_flags = oflags;
602 /* just created, set owner if admin owner (root) */
604 ad_chown(ad_p, &st_dir);
610 ad->ad_md->adf_flags = hoflags;
611 if (fstat(ad->ad_md->adf_fd, &st_meta) == 0 && st_meta.st_size == 0) {
612 /* for 0 length files, treat them as new. */
613 ad->ad_md->adf_flags |= O_TRUNC;
615 /* we have valid data in st_meta stat structure, reused it in ad_header_read */
619 AD_SET(ad->ad_md->adf_off);
621 if ((ad->ad_md->adf_flags & ( O_TRUNC | O_CREAT ))) {
622 /* This is a new adouble header file, create it */
623 if (new_ad_header(path, ad, adflags) < 0) {
625 /* the file is already deleted, perm, whatever, so return an error */
631 /* Read the adouble header in and parse it.*/
632 if (ad->ad_ops->ad_header_read( ad , pst) < 0
633 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0) {
643 static int ad_open_hf_ea(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
648 LOG(log_maxdebug, logtype_default, "ad_open_hf_ea(\"%s\", adf: 0x%04x, of: 0x%04x)",
649 abspath(path), adflags, oflags);
651 if ((ad->ad_md->adf_fd = open(path, O_RDONLY | O_NOFOLLOW)) == -1)
654 /* Read the adouble header in and parse it.*/
655 if (ad->ad_ops->ad_header_read(ad, NULL) != 0) {
656 if (!(oflags & O_CREAT))
659 /* It doesnt exist, EPERM or another error */
660 if (errno != ENOATTR && errno != ENOENT) {
661 LOG(log_error, logtype_default, "ad_open_hf_ea: unexpected: %s", strerror(errno));
667 if (new_ad_header(path, ad, adflags) < 0) {
668 LOG(log_error, logtype_default, "ad_open_hf_ea: can't create new header: %s",
677 ad->ad_md->adf_flags = O_RDWR; /* Pretend its rw, in fact for the EA API it is */
679 if ((rforklen = sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, NULL, 0)) > 0)
680 ad->ad_rlen = rforklen;
688 static int ad_open_hf(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
692 LOG(log_maxdebug, logtype_default, "ad_open_hf(\"%s/%s\", adf: 0x%04x, of: 0x%04x)",
693 getcwdpath(), path, adflags, oflags);
695 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
698 switch (ad->ad_flags) {
700 ret = ad_open_hf_v2(path, adflags, oflags, mode, ad);
703 ret = ad_open_hf_ea(path, adflags, oflags, mode, ad);
713 #define RFORK_EA_ALLOCSIZE (128*1024) /* 128k */
715 * Open EA with resfork, only for AD_VERSION_EA, a nullop otherwise
717 static int ad_open_rf(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
721 if (ad->ad_flags != AD_VERSION_EA)
724 LOG(log_debug, logtype_default, "ad_open_rf(\"%s\", adf: 0x%04x, of: 0x%04x)",
725 abspath(path), adflags, oflags);
727 if ((ad->ad_rlen = sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, NULL, 0)) <= 0) {
733 LOG(log_warning, logtype_default, "ad_open_rf(\"%s\"): %s",
734 abspath(path), strerror(errno));
739 /* Round up and allocate buffer */
740 size_t roundup = ((ad->ad_rlen / RFORK_EA_ALLOCSIZE) + 1) * RFORK_EA_ALLOCSIZE;
741 if ((ad->ad_resforkbuf = malloc(roundup)) == NULL)
744 ad->ad_resforkbufsize = roundup;
746 /* Read the EA into the buffer */
747 if (sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_META, ad->ad_resforkbuf, ad->ad_rlen) == -1)
751 free(ad->ad_resforkbuf);
752 ad->ad_resforkbuf = NULL;
758 /***********************************************************************************
760 ********************************************************************************* */
762 char *ad_path_ea( const char *path, int adflags _U_)
768 * Put the .AppleDouble where it needs to be:
772 * \ b/.AppleDouble/.Parent
774 * FIXME: should do something for pathname > MAXPATHLEN
776 char *ad_path( const char *path, int adflags)
778 static char pathbuf[ MAXPATHLEN + 1];
782 if ( adflags & ADFLAGS_DIR ) {
783 l = strlcpy( pathbuf, path, sizeof(pathbuf));
785 if ( l && l < MAXPATHLEN) {
788 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
790 if (NULL != ( slash = strrchr( path, '/' )) ) {
793 /* XXX we must return NULL here and test in the caller */
796 memcpy( pathbuf, path, l);
801 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
802 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
808 /* -------------------------
809 * Support inherited protection modes for AppleDouble files. The supplied
810 * mode is ANDed with the parent directory's mask value in lieu of "umask",
811 * and that value is returned.
813 char *ad_dir(const char *path)
815 static char modebuf[ MAXPATHLEN + 1];
818 * For a path with directories in it, remove the final component
819 * (path or subdirectory name) to get the name we want to stat.
820 * For a path which is just a filename, use "." instead.
822 slash = strrchr( path, '/' );
827 if (len >= MAXPATHLEN) {
828 errno = ENAMETOOLONG;
829 return NULL; /* can't do it */
831 memcpy( modebuf, path, len );
833 /* is last char a '/' ? */
835 slash = modebuf+ len;
837 while (modebuf < slash && slash[-1] == '/') {
840 if (modebuf == slash) {
844 while (modebuf < slash && *slash != '/') {
847 if (modebuf == slash) {
850 *slash = '\0'; /* remove pathname component */
855 modebuf[0] = '.'; /* use current directory */
860 int ad_setfuid(const uid_t id)
866 /* ---------------- */
867 uid_t ad_getfuid(void)
873 stat path parent directory
875 int ad_stat(const char *path, struct stat *stbuf)
882 return lstat( p, stbuf );
886 return access right of path parent directory
888 int ad_mode( const char *path, int mode)
891 ad_mode_st(path, &mode, &stbuf);
896 * Use mkdir() with mode bits taken from ad_mode().
898 int ad_mkdir( const char *path, int mode)
904 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
905 path, mode, getcwdpath());
907 st_invalid = ad_mode_st(path, &mode, &stbuf);
908 ret = mkdir( path, mode );
909 if (ret || st_invalid)
911 ad_chown(path, &stbuf);
916 void ad_init(struct adouble *ad, int flags, int options)
918 memset(ad, 0, sizeof(struct adouble));
920 if (flags == AD_VERSION2) {
921 ad->ad_ops = &ad_adouble;
922 ad->ad_md = &ad->ad_resource_fork;
924 else if (flags == AD_VERSION_EA) {
925 ad->ad_ops = &ad_adouble_ea;
926 ad->ad_md = &ad->ad_metadata_fork;
928 LOG(log_error, logtype_default, "ad_init: unknown AD version");
933 ad->ad_flags = flags;
934 ad->ad_options = options;
935 ad_data_fileno(ad) = -1;
936 ad_reso_fileno(ad) = -1;
937 ad_meta_fileno(ad) = -1;
938 ad->ad_inited = AD_INITED;
941 static const char *adflags2logstr(int adflags)
948 if (adflags & ADFLAGS_DF) {
949 strlcat(buf, "DF", 64);
952 if (adflags & ADFLAGS_RF) {
954 strlcat(buf, "|", 64);
955 strlcat(buf, "RF", 64);
958 if (adflags & ADFLAGS_HF) {
960 strlcat(buf, "|", 64);
961 strlcat(buf, "HF", 64);
964 if (adflags & ADFLAGS_NOHF) {
966 strlcat(buf, "|", 64);
967 strlcat(buf, "NOHF", 64);
970 if (adflags & ADFLAGS_DIR) {
972 strlcat(buf, "|", 64);
973 strlcat(buf, "DIR", 64);
976 if (adflags & ADFLAGS_RDONLY) {
978 strlcat(buf, "|", 64);
979 strlcat(buf, "RDONLY", 64);
982 if (adflags & ADFLAGS_OPENFORKS) {
984 strlcat(buf, "|", 64);
985 strlcat(buf, "OF", 64);
991 static const char *oflags2logstr(int oflags)
998 if (oflags == O_RDONLY) {
999 strlcat(buf, "O_RDONLY", 64);
1002 if (oflags & O_RDWR) {
1004 strlcat(buf, "|", 64);
1005 strlcat(buf, "O_RDWR", 64);
1008 if (oflags & O_CREAT) {
1010 strlcat(buf, "|", 64);
1011 strlcat(buf, "O_CREAT", 64);
1014 if (oflags & O_EXCL) {
1016 strlcat(buf, "|", 64);
1017 strlcat(buf, "O_EXCL", 64);
1024 * Open data-, metadata(header)- or ressource fork
1026 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
1029 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1032 * @param path Path to file or directory
1034 * @param adflags ADFLAGS_DF: open data fork \n
1035 * ADFLAGS_RF: open ressource fork \n
1036 * ADFLAGS_HF: open header (metadata) file \n
1037 * ADFLAGS_NOHF: it's not an error if header file couldn't be created \n
1038 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags \n
1039 * ADFLAGS_RDONLY: open read only \n
1040 * ADFLAGS_OPENFORKS: check for open forks from other processes
1042 * @param oflags flags passed through to open syscall: \n
1043 * O_RDONLY: *** FIXME *** \n
1044 * O_RDWR: *** FIXME *** \n
1045 * O_CREAT: create fork \n
1046 * O_EXCL: fail if exists with O_CREAT
1048 * @param mode passed to open with O_CREAT
1049 * @param ad pointer to struct adouble
1051 * @returns 0 on success
1053 * @note It's not possible to open the header file O_RDONLY -- the read
1054 * will fail and return an error. this refcounts things now.\n
1055 * metadata(ressource)-fork only gets created with O_CREAT.
1057 int ad_open(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
1061 LOG(log_debug, logtype_default, "ad_open(\"%s\", %s, %s, 0%04o)",
1062 abspath(path), adflags2logstr(adflags), oflags2logstr(oflags), mode);
1064 if (ad->ad_inited != AD_INITED)
1065 AFP_PANIC("ad_open: not initialized");
1067 if (ad->ad_fullpath == NULL) {
1068 if ((ad->ad_fullpath = bfromcstr(abspath(path))) == NULL) {
1076 if ((adflags & ADFLAGS_DF)) {
1077 if (ad_open_df(path, adflags, oflags, mode, ad) != 0) {
1081 ad->ad_adflags |= ADFLAGS_DF;
1084 if ((adflags & ADFLAGS_HF)) {
1085 if (ad_open_hf(path, adflags, oflags, mode, ad) != 0) {
1089 ad->ad_adflags |= ADFLAGS_HF;
1092 if ((adflags & ADFLAGS_RF)) {
1093 if (ad_open_rf(path, adflags, oflags, mode, ad) != 0) {
1097 ad->ad_adflags |= ADFLAGS_RF;
1102 /* FIXME: ad_close stuff we opened before hitting an error */
1103 /* Dont forget ADFLAGS_NOHF !!! */
1105 if (ad->ad_fullpath) {
1106 bdestroy(ad->ad_fullpath);
1107 ad->ad_fullpath = NULL;
1115 * @brief open metadata, possibly as root
1117 * Return only metadata but try very hard ie at first try as user, then try as root.
1118 * Caller must pass ADFLAGS_DIR for directories.
1120 * @param name name of file/dir
1121 * @param flags ADFLAGS_DIR: name is a directory \n
1122 * ADFLAGS_OPENFORKS: test if name is open by another afpd process
1124 * @param adp pointer to struct adouble
1126 int ad_metadata(const char *name, int flags, struct adouble *adp)
1130 int create = O_RDONLY;
1132 dir = flags & ADFLAGS_DIR;
1134 if ((ret = ad_open(name, ADFLAGS_HF | dir, create, 0666, adp)) < 0 && errno == EACCES) {
1137 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1141 /* we are root open read only */
1142 ret = ad_open(name, ADFLAGS_HF|ADFLAGS_RDONLY| dir, O_RDONLY, 0, adp);
1144 if ( seteuid(uid) < 0) {
1145 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1151 if (!ret && (ADFLAGS_OPENFORKS & flags)) {
1153 we need to check if the file is open by another process.
1154 it's slow so we only do it if we have to:
1156 - we don't already have the answer!
1158 adp->ad_open_forks |= ad_openforks(adp, adp->ad_open_forks);
1164 * @brief openat like wrapper for ad_metadata
1166 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1172 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1178 if (ad_metadata(name, flags, adp) < 0) {
1184 if (fchdir(cwdfd) != 0) {
1185 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1198 int ad_refresh(struct adouble *ad)
1201 if (ad_meta_fileno(ad) == -1)
1204 return ad->ad_ops->ad_header_read(ad, NULL);
1207 int ad_openat(int dirfd, /* dir fd openat like */
1218 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1224 if (ad_open(path, adflags, oflags, mode, ad) < 0) {
1230 if (fchdir(cwdfd) != 0) {
1231 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");