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>
50 #include "ad_private.h"
53 #define MAX(a, b) ((a) < (b) ? (b) : (a))
59 # define AD_SET(a) a = 0
63 * AppleDouble entry default offsets.
64 * The layout looks like this:
66 * this is the v1 layout:
68 * | NAME | COMMENT | FILEI | FINDERI | RFORK |
70 * we need to change it to look like this:
73 * field length (in bytes)
76 * FILEDATESI 16 replaces FILEI
80 * SHORTNAME 12 8.3 new
83 * so, all we need to do is replace FILEI with FILEDATESI, move RFORK,
84 * and add in the new fields.
87 #define ADEDOFF_MAGIC (0)
88 #define ADEDOFF_VERSION (ADEDOFF_MAGIC + ADEDLEN_MAGIC)
89 #define ADEDOFF_FILLER (ADEDOFF_VERSION + ADEDLEN_VERSION)
90 #define ADEDOFF_NENTRIES (ADEDOFF_FILLER + ADEDLEN_FILLER)
92 /* initial lengths of some of the fields */
93 #define ADEDLEN_INIT 0
95 /* make sure we don't redefine ADEDOFF_FILEI */
98 #endif /* ADEDOFF_FILEI */
100 /* i stick things in a slightly different order than their eid order in
101 * case i ever want to separate RootInfo behaviour from the rest of the
105 #define ADEDOFF_NAME_V2 (AD_HEADER_LEN + ADEID_NUM_V2*AD_ENTRY_LEN)
106 #define ADEDOFF_COMMENT_V2 (ADEDOFF_NAME_V2 + ADEDLEN_NAME)
107 #define ADEDOFF_FILEDATESI (ADEDOFF_COMMENT_V2 + ADEDLEN_COMMENT)
108 #define ADEDOFF_FINDERI_V2 (ADEDOFF_FILEDATESI + ADEDLEN_FILEDATESI)
109 #define ADEDOFF_DID (ADEDOFF_FINDERI_V2 + ADEDLEN_FINDERI)
110 #define ADEDOFF_AFPFILEI (ADEDOFF_DID + ADEDLEN_DID)
111 #define ADEDOFF_SHORTNAME (ADEDOFF_AFPFILEI + ADEDLEN_AFPFILEI)
112 #define ADEDOFF_PRODOSFILEI (ADEDOFF_SHORTNAME + ADEDLEN_SHORTNAME)
113 #define ADEDOFF_PRIVDEV (ADEDOFF_PRODOSFILEI + ADEDLEN_PRODOSFILEI)
114 #define ADEDOFF_PRIVINO (ADEDOFF_PRIVDEV + ADEDLEN_PRIVDEV)
115 #define ADEDOFF_PRIVSYN (ADEDOFF_PRIVINO + ADEDLEN_PRIVINO)
116 #define ADEDOFF_PRIVID (ADEDOFF_PRIVSYN + ADEDLEN_PRIVSYN)
117 #define ADEDOFF_RFORK_V2 (ADEDOFF_PRIVID + ADEDLEN_PRIVID)
120 #define ADEDOFF_FINDERI_EA (AD_HEADER_LEN + ADEID_NUM_EA * AD_ENTRY_LEN)
121 #define ADEDOFF_COMMENT_EA (ADEDOFF_FINDERI_EA + ADEDLEN_FINDERI)
122 #define ADEDOFF_FILEDATESI_EA (ADEDOFF_COMMENT_EA + ADEDLEN_COMMENT)
123 #define ADEDOFF_AFPFILEI_EA (ADEDOFF_FILEDATESI_EA + ADEDLEN_FILEDATESI)
125 /* this is to prevent changing timezones from causing problems with
126 localtime volumes. the screw-up is 30 years. we use a delta of 5 years */
127 #define TIMEWARP_DELTA 157680000
130 uint32_t id, offset, len;
133 /* --------------------------- */
134 static uid_t default_uid = -1;
136 /* Forward declarations */
137 static int ad_mkrf(char *path);
138 static int ad_header_read(struct adouble *ad, struct stat *hst);
139 static int ad_header_upgrade(struct adouble *ad, char *name);
141 static int ad_mkrf_ea(char *path);
142 static int ad_header_read_ea(struct adouble *ad, struct stat *hst);
143 static int ad_header_upgrade_ea(struct adouble *ad, char *name);
145 static struct adouble_fops ad_adouble = {
148 &ad_rebuild_adouble_header,
153 static struct adouble_fops ad_adouble_ea = {
156 &ad_rebuild_adouble_header,
158 &ad_header_upgrade_ea,
161 static const struct entry entry_order2[ADEID_NUM_V2 + 1] = {
162 {ADEID_NAME, ADEDOFF_NAME_V2, ADEDLEN_INIT},
163 {ADEID_COMMENT, ADEDOFF_COMMENT_V2, ADEDLEN_INIT},
164 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI, ADEDLEN_FILEDATESI},
165 {ADEID_FINDERI, ADEDOFF_FINDERI_V2, ADEDLEN_FINDERI},
166 {ADEID_DID, ADEDOFF_DID, ADEDLEN_DID},
167 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI, ADEDLEN_AFPFILEI},
168 {ADEID_SHORTNAME, ADEDOFF_SHORTNAME, ADEDLEN_INIT},
169 {ADEID_PRODOSFILEI, ADEDOFF_PRODOSFILEI, ADEDLEN_PRODOSFILEI},
170 {ADEID_PRIVDEV, ADEDOFF_PRIVDEV, ADEDLEN_INIT},
171 {ADEID_PRIVINO, ADEDOFF_PRIVINO, ADEDLEN_INIT},
172 {ADEID_PRIVSYN, ADEDOFF_PRIVSYN, ADEDLEN_INIT},
173 {ADEID_PRIVID, ADEDOFF_PRIVID, ADEDLEN_INIT},
174 {ADEID_RFORK, ADEDOFF_RFORK_V2, ADEDLEN_INIT},
178 /* Using Extended Attributes */
179 static const struct entry entry_order_ea[ADEID_NUM_EA + 1] = {
180 {ADEID_FINDERI, ADEDOFF_FINDERI_EA, ADEDLEN_FINDERI},
181 {ADEID_COMMENT, ADEDOFF_COMMENT_EA, ADEDLEN_INIT},
182 {ADEID_FILEDATESI, ADEDOFF_FILEDATESI_EA, ADEDLEN_FILEDATESI},
183 {ADEID_AFPFILEI, ADEDOFF_AFPFILEI_EA, ADEDLEN_AFPFILEI},
187 uint32_t get_eid(uint32_t eid)
192 return ADEID_PRIVDEV;
194 return ADEID_PRIVINO;
196 return ADEID_PRIVSYN;
203 /* ----------------------------------- */
204 static int new_ad_header(const char *path, struct adouble *ad, int adflags)
206 const struct entry *eid;
210 ad->ad_magic = AD_MAGIC;
211 ad->ad_version = ad->ad_flags & 0x0f0000;
212 if (!ad->ad_version) {
213 ad->ad_version = AD_VERSION;
217 memset(ad->ad_data, 0, sizeof(ad->ad_data));
219 if (ad->ad_flags == AD_VERSION2)
221 else if (ad->ad_flags == AD_VERSION_EA)
222 eid = entry_order_ea;
228 ad->ad_eid[eid->id].ade_off = eid->offset;
229 ad->ad_eid[eid->id].ade_len = eid->len;
233 /* put something sane in the directory finderinfo */
234 if ((adflags & ADFLAGS_DIR)) {
235 /* set default view */
236 ashort = htons(FINDERINFO_CLOSEDVIEW);
237 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
239 /* set default creator/type fields */
240 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRTYPEOFF,"\0\0\0\0", 4);
241 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRCREATOFF,"\0\0\0\0", 4);
244 /* make things invisible */
245 if ((ad->ad_options & ADVOL_INVDOTS) && (*path == '.')) {
246 ashort = htons(ATTRBIT_INVISIBLE);
247 ad_setattr(ad, ashort);
248 ashort = htons(FINDERINFO_INVISIBLE);
249 memcpy(ad_entry(ad, ADEID_FINDERI) + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
252 if (lstat(path, &st) < 0)
255 /* put something sane in the date fields */
256 ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX, st.st_mtime);
257 ad_setdate(ad, AD_DATE_MODIFY | AD_DATE_UNIX, st.st_mtime);
258 ad_setdate(ad, AD_DATE_ACCESS | AD_DATE_UNIX, st.st_mtime);
259 ad_setdate(ad, AD_DATE_BACKUP, AD_DATE_START);
263 /* -------------------------------------
266 static void parse_entries(struct adouble *ad, char *buf, uint16_t nentries)
268 uint32_t eid, len, off;
271 /* now, read in the entry bits */
272 for (; nentries > 0; nentries-- ) {
273 memcpy(&eid, buf, sizeof( eid ));
274 eid = get_eid(ntohl(eid));
275 buf += sizeof( eid );
276 memcpy(&off, buf, sizeof( off ));
278 buf += sizeof( off );
279 memcpy(&len, buf, sizeof( len ));
281 buf += sizeof( len );
283 if (eid && eid < ADEID_MAX && off < sizeof(ad->ad_data) &&
284 (off + len <= sizeof(ad->ad_data) || eid == ADEID_RFORK)) {
285 ad->ad_eid[ eid ].ade_off = off;
286 ad->ad_eid[ eid ].ade_len = len;
287 } else if (!warning) {
289 LOG(log_warning, logtype_default, "parse_entries: bogus eid: %d", eid);
294 /* this reads enough of the header so that we can figure out all of
295 * the entry lengths and offsets. once that's done, we just read/mmap
296 * the rest of the header in.
298 * NOTE: we're assuming that the resource fork is kept at the end of
299 * the file. also, mmapping won't work for the hfs fs until it
300 * understands how to mmap header files. */
301 static int ad_header_read(struct adouble *ad, struct stat *hst)
303 char *buf = ad->ad_data;
309 /* read the header */
310 if ((header_len = adf_pread( ad->ad_md, buf, sizeof(ad->ad_data), 0)) < 0) {
313 if (header_len < AD_HEADER_LEN) {
318 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
319 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
320 ad->ad_magic = ntohl( ad->ad_magic );
321 ad->ad_version = ntohl( ad->ad_version );
323 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
324 LOG(log_error, logtype_default, "ad_open: can't parse AppleDouble header.");
329 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
330 nentries = ntohs( nentries );
332 /* read in all the entry headers. if we have more than the
333 * maximum, just hope that the rfork is specified early on. */
334 len = nentries*AD_ENTRY_LEN;
336 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
337 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
339 buf += AD_HEADER_LEN;
340 if (len > header_len - AD_HEADER_LEN) {
341 LOG(log_error, logtype_default, "ad_header_read: can't read entry info.");
346 /* figure out all of the entry offsets and lengths. if we aren't
347 * able to read a resource fork entry, bail. */
348 nentries = len / AD_ENTRY_LEN;
349 parse_entries(ad, buf, nentries);
350 if (!ad_getentryoff(ad, ADEID_RFORK)
351 || (ad_getentryoff(ad, ADEID_RFORK) > sizeof(ad->ad_data))
353 LOG(log_error, logtype_default, "ad_header_read: problem with rfork entry offset.");
358 if (ad_getentryoff(ad, ADEID_RFORK) > header_len) {
359 LOG(log_error, logtype_default, "ad_header_read: can't read in entries.");
366 if (fstat(ad->ad_md->adf_fd, &st) < 0) {
367 return 1; /* fail silently */
371 ad->ad_rlen = hst->st_size - ad_getentryoff(ad, ADEID_RFORK);
376 static int ad_header_read_ea(struct adouble *ad, struct stat *hst _U_)
381 char *buf = ad->ad_data;
383 /* read the header */
384 if ((header_len = sys_fgetxattr(ad->ad_md->adf_fd, AD_EA_META, ad->ad_data, AD_DATASZ_EA)) < 1) {
385 LOG(log_debug, logtype_default, "ad_header_read_ea: %s (%u)", strerror(errno), errno);
389 if (header_len < AD_HEADER_LEN) {
390 LOG(log_error, logtype_default, "ad_header_read_ea: bogus AppleDouble header.");
395 memcpy(&ad->ad_magic, buf, sizeof( ad->ad_magic ));
396 memcpy(&ad->ad_version, buf + ADEDOFF_VERSION, sizeof( ad->ad_version ));
398 ad->ad_magic = ntohl( ad->ad_magic );
399 ad->ad_version = ntohl( ad->ad_version );
401 if ((ad->ad_magic != AD_MAGIC) || (ad->ad_version != AD_VERSION2)) {
402 LOG(log_error, logtype_default, "ad_header_read_ea: wrong magic or version");
407 memcpy(&nentries, buf + ADEDOFF_NENTRIES, sizeof( nentries ));
408 nentries = ntohs( nentries );
410 /* Protect against bogus nentries */
411 len = nentries * AD_ENTRY_LEN;
412 if (len + AD_HEADER_LEN > sizeof(ad->ad_data))
413 len = sizeof(ad->ad_data) - AD_HEADER_LEN;
414 if (len > header_len - AD_HEADER_LEN) {
415 LOG(log_error, logtype_default, "ad_header_read_ea: can't read entry info.");
419 nentries = len / AD_ENTRY_LEN;
421 /* Now parse entries */
422 parse_entries(ad, buf + AD_HEADER_LEN, nentries);
426 static int ad_mkrf(char *path)
430 * Probably .AppleDouble doesn't exist, try to mkdir it.
432 if (NULL == ( slash = strrchr( path, '/' )) ) {
437 if ( ad_mkdir( path, 0777 ) < 0 ) {
444 static int ad_mkrf_ea(char *path _U_)
446 AFP_PANIC("ad_mkrf_ea: dont use");
451 if we are root change path user/ group
452 It can be a native function for BSD cf. FAQ.Q10
453 path: pathname to chown
454 stbuf: parent directory inode
456 use fstat and fchown or lchown with linux?
458 #define EMULATE_SUIDDIR
460 static int ad_chown(const char *path, struct stat *stbuf)
463 #ifdef EMULATE_SUIDDIR
466 if (default_uid != (uid_t)-1) {
467 /* we are root (admin) */
468 id = (default_uid)?default_uid:stbuf->st_uid;
469 ret = lchown( path, id, stbuf->st_gid );
475 #define DEFMASK 07700 /* be conservative */
478 return access right and inode of path parent directory
480 static int ad_mode_st(const char *path, int *mode, struct stat *stbuf)
485 if (ad_stat(path, stbuf) != 0) {
489 *mode &= stbuf->st_mode;
493 /* ----------------- */
494 static int ad_error(struct adouble *ad, int adflags)
497 if ((adflags & ADFLAGS_NOHF)) {
498 /* FIXME double check : set header offset ?*/
501 if ((adflags & ADFLAGS_DF)) {
502 ad_close( ad, ADFLAGS_DF );
508 /* --------------------------- */
509 static int ad_header_upgrade(struct adouble *ad _U_, char *name _U_)
514 static int ad_header_upgrade_ea(struct adouble *ad _U_, char *name _U_)
516 AFP_PANIC("ad_header_upgrade_ea: dont use");
520 static int ad_open_df(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
526 LOG(log_maxdebug, logtype_default, "ad_open_df(\"%s/%s\", adf: 0x%04x, of: 0x%04x)",
527 getcwdpath(), path, adflags, oflags);
529 if (ad_data_fileno(ad) == -1) {
530 hoflags = (oflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
532 if ((oflags & O_CREAT)) {
533 st_invalid = ad_mode_st(path, &admode, &st_dir);
534 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
539 ad->ad_data_fork.adf_fd = open(path, hoflags | O_NOFOLLOW, admode);
541 if (ad->ad_data_fork.adf_fd == -1) {
542 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
544 ad->ad_data_fork.adf_fd = open( path, hoflags | O_NOFOLLOW, admode );
546 if (ad->ad_data_fork.adf_fd == -1 && errno == OPEN_NOFOLLOW_ERRNO) {
549 ad->ad_data_fork.adf_syml = malloc(MAXPATHLEN+1);
550 lsz = readlink(path, ad->ad_data_fork.adf_syml, MAXPATHLEN);
552 free(ad->ad_data_fork.adf_syml);
555 ad->ad_data_fork.adf_syml[lsz] = 0;
556 ad->ad_data_fork.adf_fd = -2; /* -2 means its a symlink */
560 if ( ad->ad_data_fork.adf_fd == -1 )
563 AD_SET(ad->ad_data_fork.adf_off);
564 ad->ad_data_fork.adf_flags = hoflags;
566 /* just created, set owner if admin (root) */
567 ad_chown(path, &st_dir);
569 adf_lock_init(&ad->ad_data_fork);
571 /* the file is already open... but */
572 if ((oflags & ( O_RDWR | O_WRONLY))
573 /* we want write access */
574 && !(ad->ad_data_fork.adf_flags & ( O_RDWR | O_WRONLY))) {
575 /* and it was denied the first time */
580 * for now ad_open is never called with O_TRUNC or O_EXCL if the file is
581 * already open. Should we check for it? ie
583 * O_TRUNC --> truncate the fork.
584 * idem for ressource fork.
587 ad->ad_data_fork.adf_refcount++;
592 static int ad_open_hf_v2(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
596 struct stat *pst = NULL;
601 ad_p = ad->ad_ops->ad_path( path, adflags );
603 hoflags = oflags & ~(O_CREAT | O_EXCL);
604 if (!(adflags & ADFLAGS_RDONLY)) {
605 hoflags = (hoflags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
607 ad->ad_md->adf_fd = open( ad_p, hoflags | O_NOFOLLOW, 0 );
608 if (ad->ad_md->adf_fd < 0 ) {
609 if ((errno == EACCES || errno == EROFS) && !(oflags & O_RDWR)) {
610 hoflags = oflags & ~(O_CREAT | O_EXCL);
611 ad->ad_md->adf_fd = open( ad_p, hoflags | O_NOFOLLOW, 0 );
615 if ( ad->ad_md->adf_fd < 0 ) {
616 if (errno == ENOENT && (oflags & O_CREAT) ) {
618 * We're expecting to create a new adouble header file here
619 * if ((oflags & O_CREAT) ==> (oflags & O_RDWR)
621 LOG(log_debug, logtype_default, "ad_open(\"%s/%s\"): creating adouble file",
625 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
626 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
629 admode = ad_hf_mode(admode);
630 if ((errno == ENOENT)) {
631 if (ad->ad_ops->ad_mkrf( ad_p) < 0) {
632 return ad_error(ad, adflags);
635 st_invalid = ad_mode_st(ad_p, &admode, &st_dir);
636 if ((ad->ad_options & ADVOL_UNIXPRIV)) {
639 admode = ad_hf_mode(admode);
641 /* retry with O_CREAT */
642 ad->ad_md->adf_fd = open( ad_p, oflags,admode );
643 if ( ad->ad_md->adf_fd < 0 ) {
644 return ad_error(ad, adflags);
646 ad->ad_md->adf_flags = oflags;
647 /* just created, set owner if admin owner (root) */
649 ad_chown(ad_p, &st_dir);
652 return ad_error(ad, adflags);
655 ad->ad_md->adf_flags = hoflags;
656 if (fstat(ad->ad_md->adf_fd, &st_meta) == 0 && st_meta.st_size == 0) {
657 /* for 0 length files, treat them as new. */
658 ad->ad_md->adf_flags |= O_TRUNC;
660 /* we have valid data in st_meta stat structure, reused it in ad_header_read */
664 AD_SET(ad->ad_md->adf_off);
666 if ((ad->ad_md->adf_flags & ( O_TRUNC | O_CREAT ))) {
667 /* This is a new adouble header file, create it */
668 if (new_ad_header(path, ad, adflags) < 0) {
670 /* the file is already deleted, perm, whatever, so return an error */
671 ad_close(ad, adflags);
677 /* Read the adouble header in and parse it.*/
678 if (ad->ad_ops->ad_header_read( ad , pst) < 0
679 || ad->ad_ops->ad_header_upgrade(ad, ad_p) < 0) {
681 ad_close( ad, adflags );
690 static int ad_open_hf_ea(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
696 LOG(log_maxdebug, logtype_default, "ad_open_hf_ea(\"%s/%s\", adf: 0x%04x, of: 0x%04x)",
697 getcwdpath(), path, adflags, oflags);
699 if ((ad->ad_md->adf_fd = open(path, O_RDONLY | O_NOFOLLOW)) == -1)
702 /* Read the adouble header in and parse it.*/
703 if (ad->ad_ops->ad_header_read(ad, NULL) != 0) {
704 if (!(oflags & O_CREAT)) {
706 ad_close(ad, adflags);
711 /* It doesnt exist, EPERM or another error */
712 if (errno != ENOATTR && errno != ENOENT) {
713 LOG(log_error, logtype_default, "ad_open_hf_ea: unexpected: %s", strerror(errno));
719 if (new_ad_header(path, ad, adflags) < 0) {
720 LOG(log_error, logtype_default, "ad_open_hf_ea: can't create new header: %s/%s",
729 ad->ad_md->adf_flags = O_RDWR; /* Pretend its rw, in fact for the EA API it is */
731 if ((rforklen = sys_fgetxattr(ad->ad_md->adf_fd, AD_EA_RESO, NULL, 0)) > 0)
732 ad->ad_rlen = rforklen;
738 ad_close(ad, adflags);
743 static int ad_open_hf(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
747 LOG(log_maxdebug, logtype_default, "ad_open_hf(\"%s/%s\", adf: 0x%04x, of: 0x%04x)",
748 getcwdpath(), path, adflags, oflags);
750 if (ad_meta_fileno(ad) != -1) { /* the file is already open */
751 LOG(log_maxdebug, logtype_default,"the file is already open");
752 if ((oflags & ( O_RDWR | O_WRONLY))
753 && !(ad->ad_md->adf_flags & ( O_RDWR | O_WRONLY))) {
754 if ((adflags & ADFLAGS_HF) && ad->ad_data_fork.adf_refcount)
755 /* We just have openend the df, so we have to close it now */
756 ad_close(ad, ADFLAGS_DF);
761 /* it's not new anymore */
762 ad->ad_md->adf_flags &= ~( O_TRUNC | O_CREAT );
763 ad->ad_md->adf_refcount++;
767 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
769 adf_lock_init(ad->ad_md);
771 switch (ad->ad_flags) {
773 ret = ad_open_hf_v2(path, adflags, oflags, mode, ad);
776 ret = ad_open_hf_ea(path, adflags, oflags, mode, ad);
783 ad->ad_md->adf_refcount = 1;
788 static int ad_open_rf(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
793 /***********************************************************************************
795 ********************************************************************************* */
797 char *ad_path_ea( const char *path, int adflags _U_)
803 * Put the .AppleDouble where it needs to be:
807 * \ b/.AppleDouble/.Parent
809 * FIXME: should do something for pathname > MAXPATHLEN
811 char *ad_path( const char *path, int adflags)
813 static char pathbuf[ MAXPATHLEN + 1];
817 if ( adflags & ADFLAGS_DIR ) {
818 l = strlcpy( pathbuf, path, sizeof(pathbuf));
820 if ( l && l < MAXPATHLEN) {
823 strlcpy(pathbuf +l, ".AppleDouble/.Parent", sizeof(pathbuf) -l);
825 if (NULL != ( slash = strrchr( path, '/' )) ) {
828 /* XXX we must return NULL here and test in the caller */
831 memcpy( pathbuf, path, l);
836 l += strlcpy( pathbuf +l, ".AppleDouble/", sizeof(pathbuf) -l);
837 strlcpy( pathbuf + l, slash, sizeof(pathbuf) -l);
843 /* -------------------------
844 * Support inherited protection modes for AppleDouble files. The supplied
845 * mode is ANDed with the parent directory's mask value in lieu of "umask",
846 * and that value is returned.
848 char *ad_dir(const char *path)
850 static char modebuf[ MAXPATHLEN + 1];
853 * For a path with directories in it, remove the final component
854 * (path or subdirectory name) to get the name we want to stat.
855 * For a path which is just a filename, use "." instead.
857 slash = strrchr( path, '/' );
862 if (len >= MAXPATHLEN) {
863 errno = ENAMETOOLONG;
864 return NULL; /* can't do it */
866 memcpy( modebuf, path, len );
868 /* is last char a '/' ? */
870 slash = modebuf+ len;
872 while (modebuf < slash && slash[-1] == '/') {
875 if (modebuf == slash) {
879 while (modebuf < slash && *slash != '/') {
882 if (modebuf == slash) {
885 *slash = '\0'; /* remove pathname component */
890 modebuf[0] = '.'; /* use current directory */
895 int ad_setfuid(const uid_t id)
901 /* ---------------- */
902 uid_t ad_getfuid(void)
908 stat path parent directory
910 int ad_stat(const char *path, struct stat *stbuf)
917 return lstat( p, stbuf );
921 return access right of path parent directory
923 int ad_mode( const char *path, int mode)
926 ad_mode_st(path, &mode, &stbuf);
931 * Use mkdir() with mode bits taken from ad_mode().
933 int ad_mkdir( const char *path, int mode)
939 LOG(log_debug, logtype_default, "ad_mkdir(\"%s\", %04o) {cwd: \"%s\"}",
940 path, mode, getcwdpath());
942 st_invalid = ad_mode_st(path, &mode, &stbuf);
943 ret = mkdir( path, mode );
944 if (ret || st_invalid)
946 ad_chown(path, &stbuf);
951 void ad_init(struct adouble *ad, int flags, int options)
954 ad->ad_flags = flags;
955 if (flags == AD_VERSION2) {
956 ad->ad_ops = &ad_adouble;
957 ad->ad_md = &ad->ad_resource_fork;
959 else if (flags == AD_VERSION_EA) {
960 ad->ad_ops = &ad_adouble_ea;
961 ad->ad_md = &ad->ad_metadata_fork;
963 LOG(log_error, logtype_default, "ad_init: unknown AD version");
968 ad->ad_options = options;
970 ad_data_fileno(ad) = -1;
971 ad_reso_fileno(ad) = -1;
972 ad_meta_fileno(ad) = -1;
974 /* following can be read even if there's no meda data. */
975 memset(ad->ad_eid, 0, sizeof( ad->ad_eid ));
979 static const char *adflags2logstr(int adflags)
986 if (adflags & ADFLAGS_DF) {
987 strlcat(buf, "DF", 64);
990 if (adflags & ADFLAGS_RF) {
992 strlcat(buf, "|", 64);
993 strlcat(buf, "RF", 64);
996 if (adflags & ADFLAGS_HF) {
998 strlcat(buf, "|", 64);
999 strlcat(buf, "HF", 64);
1002 if (adflags & ADFLAGS_NOHF) {
1004 strlcat(buf, "|", 64);
1005 strlcat(buf, "NOHF", 64);
1008 if (adflags & ADFLAGS_DIR) {
1010 strlcat(buf, "|", 64);
1011 strlcat(buf, "DIR", 64);
1014 if (adflags & ADFLAGS_RDONLY) {
1016 strlcat(buf, "|", 64);
1017 strlcat(buf, "RDONLY", 64);
1020 if (adflags & ADFLAGS_OPENFORKS) {
1022 strlcat(buf, "|", 64);
1023 strlcat(buf, "OF", 64);
1029 static const char *oflags2logstr(int oflags)
1032 static char buf[64];
1036 if (oflags & O_RDONLY) {
1037 strlcat(buf, "O_RDONLY", 64);
1040 if (oflags & O_RDWR) {
1042 strlcat(buf, "|", 64);
1043 strlcat(buf, "O_RDWR", 64);
1046 if (oflags & O_CREAT) {
1048 strlcat(buf, "|", 64);
1049 strlcat(buf, "O_CREAT", 64);
1052 if (oflags & O_EXCL) {
1054 strlcat(buf, "|", 64);
1055 strlcat(buf, "O_EXCL", 64);
1062 * Open data-, metadata(header)- or ressource fork
1064 * You must call ad_init() before ad_open, usually you'll just call it like this: \n
1067 * ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1070 * @param path Path to file or directory
1072 * @param adflags ADFLAGS_DF: open data fork \n
1073 * ADFLAGS_RF: open ressource fork \n
1074 * ADFLAGS_HF: open header (metadata) file \n
1075 * ADFLAGS_NOHF: it's not an error if header file couldn't be created \n
1076 * ADFLAGS_DIR: if path is a directory you MUST or ADFLAGS_DIR to adflags \n
1077 * ADFLAGS_RDONLY: open read only \n
1078 * ADFLAGS_OPENFORKS: check for open forks from other processes
1080 * @param oflags flags passed through to open syscall: \n
1081 * O_RDONLY: *** FIXME *** \n
1082 * O_RDWR: *** FIXME *** \n
1083 * O_CREAT: create fork \n
1084 * O_EXCL: fail if exists with O_CREAT
1086 * @param mode passed to open with O_CREAT
1087 * @param ad pointer to struct adouble
1089 * @returns 0 on success
1091 * @note It's not possible to open the header file O_RDONLY -- the read
1092 * will fail and return an error. this refcounts things now.\n
1093 * metadata(ressource)-fork only gets created with O_CREAT.
1095 int ad_open(const char *path, int adflags, int oflags, int mode, struct adouble *ad)
1097 LOG(log_debug, logtype_default, "ad_open(\"%s\", %s, %s, 0%04o)",
1098 abspath(path), adflags2logstr(adflags), oflags2logstr(oflags), mode);
1100 if (ad->ad_inited != AD_INITED) {
1101 ad->ad_inited = AD_INITED;
1102 ad->ad_refcount = 1;
1103 ad->ad_open_forks = 0;
1104 ad->ad_adflags = adflags;
1105 ad->ad_resource_fork.adf_refcount = 0;
1106 ad->ad_data_fork.adf_refcount = 0;
1107 ad->ad_data_fork.adf_syml = 0;
1109 ad->ad_open_forks = ((ad->ad_data_fork.adf_refcount > 0) ? ATTRBIT_DOPEN : 0);
1110 /* XXX not true if we have a meta data fork ? */
1111 if ((ad->ad_resource_fork.adf_refcount > ad->ad_data_fork.adf_refcount))
1112 ad->ad_open_forks |= ATTRBIT_ROPEN;
1115 if ((adflags & ADFLAGS_DF)) {
1116 if (ad_open_df(path, adflags, oflags, mode, ad) != 0)
1120 if ((adflags & ADFLAGS_HF)) {
1121 if (ad_open_hf(path, adflags, oflags, mode, ad) != 0)
1126 if ((adflags & ADFLAGS_RF)) {
1127 if (ad_open_rf(path, adflags, oflags, mode, ad) != 0)
1136 * @brief open metadata, possibly as root
1138 * Return only metadata but try very hard ie at first try as user, then try as root.
1139 * Caller must pass ADFLAGS_DIR for directories.
1141 * @param name name of file/dir
1142 * @param flags ADFLAGS_DIR: name is a directory \n
1143 * ADFLAGS_OPENFORKS: test if name is open by another afpd process
1145 * @param adp pointer to struct adouble
1147 int ad_metadata(const char *name, int flags, struct adouble *adp)
1151 int create = O_RDONLY;
1153 dir = flags & ADFLAGS_DIR;
1155 if ((ret = ad_open(name, ADFLAGS_HF | dir, create, 0666, adp)) < 0 && errno == EACCES) {
1158 LOG(log_error, logtype_default, "ad_metadata(%s): seteuid failed %s", name, strerror(errno));
1162 /* we are root open read only */
1163 ret = ad_open(name, ADFLAGS_HF|ADFLAGS_RDONLY| dir, O_RDONLY, 0, adp);
1165 if ( seteuid(uid) < 0) {
1166 LOG(log_error, logtype_default, "ad_metadata: can't seteuid back");
1172 if (!ret && (ADFLAGS_OPENFORKS & flags)) {
1174 we need to check if the file is open by another process.
1175 it's slow so we only do it if we have to:
1177 - we don't already have the answer!
1179 adp->ad_open_forks |= ad_openforks(adp, adp->ad_open_forks);
1185 * @brief openat like wrapper for ad_metadata
1187 int ad_metadataat(int dirfd, const char *name, int flags, struct adouble *adp)
1193 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1199 if (ad_metadata(name, flags, adp) < 0) {
1205 if (fchdir(cwdfd) != 0) {
1206 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");
1219 int ad_refresh(struct adouble *ad)
1222 if (ad_meta_fileno(ad) == -1)
1225 return ad->ad_ops->ad_header_read(ad, NULL);
1228 int ad_openat(int dirfd, /* dir fd openat like */
1239 if ((cwdfd = open(".", O_RDONLY) == -1) || (fchdir(dirfd) != 0)) {
1245 if (ad_open(path, adflags, oflags, mode, ad) < 0) {
1251 if (fchdir(cwdfd) != 0) {
1252 LOG(log_error, logtype_afpd, "ad_openat: cant chdir back, exiting");