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>
52 #include "ad_private.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);
526 adf_lock_init(&ad->ad_data_fork);
528 /* the file is already open... but */
529 if ((oflags & ( O_RDWR | O_WRONLY))
530 /* we want write access */
531 && !(ad->ad_data_fork.adf_flags & ( O_RDWR | O_WRONLY))) {
532 /* and it was denied the first time */
537 * for now ad_open is never called with O_TRUNC or O_EXCL if the file is
538 * already open. Should we check for it? ie
540 * O_TRUNC --> truncate the fork.
541 * idem for ressource fork.
544 ad->ad_data_fork.adf_refcount++;
549 static int ad_open_hf_v2(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
553 struct stat *pst = NULL;
558 ad_p = ad->ad_ops->ad_path( path, adflags );
560 hoflags = oflags & ~(O_CREAT | O_EXCL);
561 if (!(adflags & ADFLAGS_RDONLY)) {
562 hoflags = (hoflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
564 ad->ad_md->adf_fd = open( ad_p, hoflags | O_NOFOLLOW, 0 );
565 if (ad->ad_md->adf_fd < 0 ) {
566 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
567 hoflags = oflags & ~(O_CREAT | O_EXCL);
568 ad->ad_md->adf_fd = open( ad_p, hoflags | O_NOFOLLOW, 0 );
572 if ( ad->ad_md->adf_fd < 0 ) {
573 if (errno == ENOENT && (oflags & O_CREAT) ) {
575 * We're expecting to create a new adouble header file here
576 * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
578 LOG(log_debug, logtype_default, "ad_open(\"%s/%s\"): creating adouble file",
582 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
583 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
586 admode = ad_hf_mode(admode);
587 if ((errno == ENOENT)) {
588 if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
592 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
593 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
596 admode = ad_hf_mode(admode);
598 /* retry with O_CREAT */
599 ad->ad_md->adf_fd = open( ad_p, oflags,admode );
600 if ( ad->ad_md->adf_fd < 0 ) {
603 ad->ad_md->adf_flags = oflags;
604 /* just created, set owner if admin owner (root) */
606 ad_chown(ad_p, &st_dir);
612 ad->ad_md->adf_flags = hoflags;
613 if (fstat(ad->ad_md->adf_fd, &st_meta) == 0 && st_meta.st_size == 0) {
614 /* for 0 length files, treat them as new. */
615 ad->ad_md->adf_flags |= O_TRUNC;
617 /* we have valid data in st_meta stat structure, reused it in ad_header_read */
621 AD_SET(ad->ad_md->adf_off);
623 if ((ad->ad_md->adf_flags & ( O_TRUNC | O_CREAT ))) {
624 /* This is a new adouble header file, create it */
625 if (new_ad_header(path, ad, adflags) < 0) {
627 /* the file is already deleted, perm, whatever, so return an error */
633 /* Read the adouble header in and parse it.*/
634 if (ad->ad_ops->ad_header_read( ad , pst) < 0
635 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0) {
645 static int ad_open_hf_ea(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
650 LOG(log_maxdebug, logtype_default, "ad_open_hf_ea(\"%s\", adf: 0x%04x, of: 0x%04x)",
651 abspath(path), adflags, oflags);
653 if ((ad->ad_md->adf_fd = open(path, O_RDONLY | O_NOFOLLOW)) == -1)
656 /* Read the adouble header in and parse it.*/
657 if (ad->ad_ops->ad_header_read(ad, NULL) != 0) {
658 if (!(oflags & O_CREAT))
661 /* It doesnt exist, EPERM or another error */
662 if (errno != ENOATTR && errno != ENOENT) {
663 LOG(log_error, logtype_default, "ad_open_hf_ea: unexpected: %s", strerror(errno));
669 if (new_ad_header(path, ad, adflags) < 0) {
670 LOG(log_error, logtype_default, "ad_open_hf_ea: can't create new header: %s",
679 ad->ad_md->adf_flags = O_RDWR; /* Pretend its rw, in fact for the EA API it is */
681 if ((rforklen = sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, NULL, 0)) > 0)
682 ad->ad_rlen = rforklen;
690 static int ad_open_hf(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
694 LOG(log_maxdebug, logtype_default, "ad_open_hf(\"%s/%s\", adf: 0x%04x, of: 0x%04x)",
695 getcwdpath(), path, adflags, oflags);
697 if (ad->ad_md->adf_refcount) {
698 /* the file is already open */
699 if ((oflags & ( O_RDWR | O_WRONLY))
700 && !(ad->ad_md->adf_flags & ( O_RDWR | O_WRONLY))) {
701 if ((adflags & ADFLAGS_HF) && ad->ad_data_fork.adf_refcount)
702 /* We just have openend the df, so we have to close it now */
703 ad_close(ad, ADFLAGS_DF);
708 /* it's not new anymore */
709 ad->ad_md->adf_flags &= ~( O_TRUNC | O_CREAT );
710 ad->ad_md->adf_refcount++;
714 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
717 switch (ad->ad_flags) {
719 adf_lock_init(ad->ad_md);
720 ret = ad_open_hf_v2(path, adflags, oflags, mode, ad);
723 ret = ad_open_hf_ea(path, adflags, oflags, mode, ad);
730 ad->ad_md->adf_refcount = 1;
735 #define RFORK_EA_ALLOCSIZE (128*1024) /* 128k */
737 * Open EA with resfork, only for AD_VERSION_EA, a nullop otherwise
739 static int ad_open_rf(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
743 if (ad->ad_flags != AD_VERSION_EA)
746 LOG(log_maxdebug, logtype_default, "ad_open_hf_ea(\"%s\", adf: 0x%04x, of: 0x%04x)",
747 abspath(path), adflags, oflags);
749 if (ad->ad_resource_fork.adf_refcount) {
751 ad->ad_resource_fork.adf_flags &= ~( O_TRUNC | O_CREAT ); /* not new anymore */
752 free(ad->ad_resforkbuf);
753 ad->ad_resforkbuf = NULL;
756 if ((ad->ad_rlen = sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_RESO, NULL, 0)) <= 0)
759 /* Round up and allocate buffer */
760 size_t roundup = ((ad->ad_rlen / RFORK_EA_ALLOCSIZE) + 1) * RFORK_EA_ALLOCSIZE;
761 if ((ad->ad_resforkbuf = malloc(roundup)) == NULL)
764 ad->ad_resforkbufsize = roundup;
766 /* Read the EA into the buffer */
767 if (sys_lgetxattr(cfrombstr(ad->ad_fullpath), AD_EA_META, ad->ad_resforkbuf, ad->ad_rlen) == -1)
770 ad->ad_resource_fork.adf_refcount++;
774 free(ad->ad_resforkbuf);
775 ad->ad_resforkbuf = NULL;
781 /***********************************************************************************
783 ********************************************************************************* */
785 char *ad_path_ea( const char *path, int adflags _U_)
791 * Put the .AppleDouble where it needs to be:
795 * \ b/.AppleDouble/.Parent
797 * FIXME: should do something for pathname > MAXPATHLEN
799 char *ad_path( const char *path, int adflags)
801 static char pathbuf[ MAXPATHLEN + 1];
805 if ( adflags & ADFLAGS_DIR ) {
806 l = strlcpy( pathbuf, path, sizeof(pathbuf));
808 if ( l && l < MAXPATHLEN) {
811 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
813 if (NULL != ( slash = strrchr( path, '/' )) ) {
816 /* XXX we must return NULL here and test in the caller */
819 memcpy( pathbuf, path, l);
824 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
825 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
831 /* -------------------------
832 * Support inherited protection modes for AppleDouble files. The supplied
833 * mode is ANDed with the parent directory's mask value in lieu of "umask",
834 * and that value is returned.
836 char *ad_dir(const char *path)
838 static char modebuf[ MAXPATHLEN + 1];
841 * For a path with directories in it, remove the final component
842 * (path or subdirectory name) to get the name we want to stat.
843 * For a path which is just a filename, use "." instead.
845 slash = strrchr( path, '/' );
850 if (len >= MAXPATHLEN) {
851 errno = ENAMETOOLONG;
852 return NULL; /* can't do it */
854 memcpy( modebuf, path, len );
856 /* is last char a '/' ? */
858 slash = modebuf+ len;
860 while (modebuf < slash && slash[-1] == '/') {
863 if (modebuf == slash) {
867 while (modebuf < slash && *slash != '/') {
870 if (modebuf == slash) {
873 *slash = '\0'; /* remove pathname component */
878 modebuf[0] = '.'; /* use current directory */
883 int ad_setfuid(const uid_t id)
889 /* ---------------- */
890 uid_t ad_getfuid(void)
896 stat path parent directory
898 int ad_stat(const char *path, struct stat *stbuf)
905 return lstat( p, stbuf );
909 return access right of path parent directory
911 int ad_mode( const char *path, int mode)
914 ad_mode_st(path, &mode, &stbuf);
919 * Use mkdir() with mode bits taken from ad_mode().
921 int ad_mkdir( const char *path, int mode)
927 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
928 path, mode, getcwdpath());
930 st_invalid = ad_mode_st(path, &mode, &stbuf);
931 ret = mkdir( path, mode );
932 if (ret || st_invalid)
934 ad_chown(path, &stbuf);
939 void ad_init(struct adouble *ad, int flags, int options)
942 ad->ad_flags = flags;
943 if (flags == AD_VERSION2) {
944 ad->ad_ops = &ad_adouble;
945 ad->ad_md = &ad->ad_resource_fork;
947 else if (flags == AD_VERSION_EA) {
948 ad->ad_ops = &ad_adouble_ea;
949 ad->ad_md = &ad->ad_metadata_fork;
951 LOG(log_error, logtype_default, "ad_init: unknown AD version");
956 ad->ad_options = options;
958 ad_data_fileno(ad) = -1;
959 ad_reso_fileno(ad) = -1;
960 ad_meta_fileno(ad) = -1;
962 /* following can be read even if there's no meda data. */
963 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
967 static const char *adflags2logstr(int adflags)
974 if (adflags & ADFLAGS_DF) {
975 strlcat(buf, "DF", 64);
978 if (adflags & ADFLAGS_RF) {
980 strlcat(buf, "|", 64);
981 strlcat(buf, "RF", 64);
984 if (adflags & ADFLAGS_HF) {
986 strlcat(buf, "|", 64);
987 strlcat(buf, "HF", 64);
990 if (adflags & ADFLAGS_NOHF) {
992 strlcat(buf, "|", 64);
993 strlcat(buf, "NOHF", 64);
996 if (adflags & ADFLAGS_DIR) {
998 strlcat(buf, "|", 64);
999 strlcat(buf, "DIR", 64);
1002 if (adflags & ADFLAGS_RDONLY) {
1004 strlcat(buf, "|", 64);
1005 strlcat(buf, "RDONLY", 64);
1008 if (adflags & ADFLAGS_OPENFORKS) {
1010 strlcat(buf, "|", 64);
1011 strlcat(buf, "OF", 64);
1017 static const char *oflags2logstr(int oflags)
1020 static char buf[64];
1024 if ((oflags & O_RDONLY) == O_RDONLY) {
1025 strlcat(buf, "O_RDONLY", 64);
1028 if (oflags & O_RDWR) {
1030 strlcat(buf, "|", 64);
1031 strlcat(buf, "O_RDWR", 64);
1034 if (oflags & O_CREAT) {
1036 strlcat(buf, "|", 64);
1037 strlcat(buf, "O_CREAT", 64);
1040 if (oflags & O_EXCL) {
1042 strlcat(buf, "|", 64);
1043 strlcat(buf, "O_EXCL", 64);
1050 * Open data-, metadata(header)- or ressource fork
1052 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
1055 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1058 * @param path Path to file or directory
1060 * @param adflags ADFLAGS_DF: open data fork \n
1061 * ADFLAGS_RF: open ressource fork \n
1062 * ADFLAGS_HF: open header (metadata) file \n
1063 * ADFLAGS_NOHF: it's not an error if header file couldn't be created \n
1064 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags \n
1065 * ADFLAGS_RDONLY: open read only \n
1066 * ADFLAGS_OPENFORKS: check for open forks from other processes
1068 * @param oflags flags passed through to open syscall: \n
1069 * O_RDONLY: *** FIXME *** \n
1070 * O_RDWR: *** FIXME *** \n
1071 * O_CREAT: create fork \n
1072 * O_EXCL: fail if exists with O_CREAT
1074 * @param mode passed to open with O_CREAT
1075 * @param ad pointer to struct adouble
1077 * @returns 0 on success
1079 * @note It's not possible to open the header file O_RDONLY -- the read
1080 * will fail and return an error. this refcounts things now.\n
1081 * metadata(ressource)-fork only gets created with O_CREAT.
1083 int ad_open(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
1087 LOG(log_debug, logtype_default, "ad_open(\"%s\", %s, %s, 0%04o)",
1088 abspath(path), adflags2logstr(adflags), oflags2logstr(oflags), mode);
1090 if (ad->ad_inited != AD_INITED) {
1091 if ((ad->ad_fullpath = bfromcstr(abspath(path))) == NULL) {
1095 ad->ad_inited = AD_INITED;
1096 ad->ad_refcount = 1;
1097 ad->ad_open_forks = 0;
1098 ad->ad_resource_fork.adf_refcount = 0;
1099 ad->ad_data_fork.adf_refcount = 0;
1100 ad->ad_data_fork.adf_syml = 0;
1102 ad->ad_open_forks = ((ad->ad_data_fork.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
1103 /* XXX not true if we have a meta data fork ? */
1104 if ((ad->ad_resource_fork.adf_refcount > ad->ad_data_fork.adf_refcount))
1105 ad->ad_open_forks |= ATTRBIT_ROPEN;
1108 if ((adflags & ADFLAGS_DF)) {
1109 if (ad_open_df(path, adflags, oflags, mode, ad) != 0) {
1113 ad->ad_adflags |= ADFLAGS_DF;
1116 if ((adflags & ADFLAGS_HF)) {
1117 if (ad_open_hf(path, adflags, oflags, mode, ad) != 0) {
1121 ad->ad_adflags |= ADFLAGS_HF;
1124 if ((adflags & ADFLAGS_RF)) {
1125 if (ad_open_rf(path, adflags, oflags, mode, ad) != 0) {
1129 ad->ad_adflags |= ADFLAGS_RF;
1134 /* FIXME: ad_close stuff we opened before hitting an error */
1135 /* Dont forget ADFLAGS_NOHF !!! */
1137 if (ad->ad_fullpath) {
1138 bdestroy(ad->ad_fullpath);
1139 ad->ad_fullpath = NULL;
1147 * @brief open metadata, possibly as root
1149 * Return only metadata but try very hard ie at first try as user, then try as root.
1150 * Caller must pass ADFLAGS_DIR for directories.
1152 * @param name name of file/dir
1153 * @param flags ADFLAGS_DIR: name is a directory \n
1154 * ADFLAGS_OPENFORKS: test if name is open by another afpd process
1156 * @param adp pointer to struct adouble
1158 int ad_metadata(const char *name, int flags, struct adouble *adp)
1162 int create = O_RDONLY;
1164 dir = flags & ADFLAGS_DIR;
1166 if ((ret = ad_open(name, ADFLAGS_HF | dir, create, 0666, adp)) < 0 && errno == EACCES) {
1169 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1173 /* we are root open read only */
1174 ret = ad_open(name, ADFLAGS_HF|ADFLAGS_RDONLY| dir, O_RDONLY, 0, adp);
1176 if ( seteuid(uid) < 0) {
1177 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1183 if (!ret && (ADFLAGS_OPENFORKS & flags)) {
1185 we need to check if the file is open by another process.
1186 it's slow so we only do it if we have to:
1188 - we don't already have the answer!
1190 adp->ad_open_forks |= ad_openforks(adp, adp->ad_open_forks);
1196 * @brief openat like wrapper for ad_metadata
1198 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1204 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1210 if (ad_metadata(name, flags, adp) < 0) {
1216 if (fchdir(cwdfd) != 0) {
1217 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1230 int ad_refresh(struct adouble *ad)
1233 if (ad_meta_fileno(ad) == -1)
1236 return ad->ad_ops->ad_header_read(ad, NULL);
1239 int ad_openat(int dirfd, /* dir fd openat like */
1250 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1256 if (ad_open(path, adflags, oflags, mode, ad) < 0) {
1262 if (fchdir(cwdfd) != 0) {
1263 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");