2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
16 #include <sys/param.h>
18 #include <atalk/adouble.h>
19 #include <atalk/vfs.h>
20 #include <atalk/logger.h>
21 #include <atalk/afp.h>
22 #include <atalk/util.h>
23 #include <atalk/cnid.h>
24 #include <atalk/unix.h>
25 #include <atalk/globals.h>
26 #include <atalk/fce_api.h>
27 #include <atalk/netatalk_conf.h>
29 #include "directory.h"
37 #include "spotlight.h"
39 /* the format for the finderinfo fields (from IM: Toolbox Essentials):
40 * field bytes subfield bytes
43 * ioFlFndrInfo 16 -> type 4 type field
44 * creator 4 creator field
45 * flags 2 finder flags:
47 * location 4 location in window
48 * folder 2 window that contains file
50 * ioFlXFndrInfo 16 -> iconID 2 icon id
52 * script 1 script system
54 * commentID 2 comment id
55 * putawayID 4 home directory id
58 const u_char ufinderi[ADEDLEN_FINDERI] = {
59 0, 0, 0, 0, 0, 0, 0, 0,
60 1, 0, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 0, 0, 0, 0, 0
65 static const u_char old_ufinderi[] = {
66 'T', 'E', 'X', 'T', 'U', 'N', 'I', 'X'
69 /* ----------------------
71 static int default_type(void *finder)
73 if (!memcmp(finder, ufinderi, 8) || !memcmp(finder, old_ufinderi, 8))
78 /* FIXME path : unix or mac name ? (for now it's unix name ) */
79 void *get_finderinfo(const struct vol *vol, const char *upath, struct adouble *adp, void *data, int islink)
82 void *ad_finder = NULL;
86 ad_finder = ad_entry(adp, ADEID_FINDERI);
89 memcpy(data, ad_finder, ADEDLEN_FINDERI);
91 if (default_type(ad_finder))
95 memcpy(data, ufinderi, ADEDLEN_FINDERI);
96 if (vol_inv_dots(vol) && *upath == '.') { /* make it invisible */
99 ashort = htons(FINDERINFO_INVISIBLE);
100 memcpy((char *)data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
106 memcpy(&linkflag, (char *)data + FINDERINFO_FRFLAGOFF, 2);
107 linkflag |= htons(FINDERINFO_ISALIAS);
108 memcpy((char *)data + FINDERINFO_FRFLAGOFF, &linkflag, 2);
109 memcpy((char *)data + FINDERINFO_FRTYPEOFF,"slnk",4);
110 memcpy((char *)data + FINDERINFO_FRCREATOFF,"rhap",4);
113 /** Only enter if no appledouble information and no finder information found. */
114 if (chk_ext && (em = getextmap( upath ))) {
115 memcpy(data, em->em_type, sizeof( em->em_type ));
116 memcpy((char *)data + 4, em->em_creator, sizeof(em->em_creator));
122 /* ---------------------
124 char *set_name(const struct vol *vol, char *data, cnid_t pid, char *name, cnid_t id, uint32_t utf8)
129 aint = strlen( name );
133 if (utf8_encoding(vol->v_obj)) {
134 /* but name is an utf8 mac name */
137 /* global static variable... */
139 if (!(u = mtoupath(vol, name, pid, 1)) || !(m = utompath(vol, u, id, 0))) {
148 if (aint > MACFILELEN)
155 if (aint > UTF8FILELEN_EARLY) /* FIXME safeguard, anyway if no ascii char it's game over*/
156 aint = UTF8FILELEN_EARLY;
158 utf8 = vol->v_kTextEncoding;
159 memcpy(data, &utf8, sizeof(utf8));
160 data += sizeof(utf8);
163 memcpy(data, &temp, sizeof(temp));
164 data += sizeof(temp);
167 memcpy( data, src, aint );
177 * FIXME: PDINFO is UTF8 and doesn't need adp
179 #define PARAM_NEED_ADP(b) ((b) & ((1 << FILPBIT_ATTR) |\
180 (1 << FILPBIT_CDATE) |\
181 (1 << FILPBIT_MDATE) |\
182 (1 << FILPBIT_BDATE) |\
183 (1 << FILPBIT_FINFO) |\
184 (1 << FILPBIT_RFLEN) |\
185 (1 << FILPBIT_EXTRFLEN) |\
186 (1 << FILPBIT_PDINFO) |\
187 (1 << FILPBIT_FNUM) |\
188 (1 << FILPBIT_UNIXPR)))
191 * @brief Get CNID for did/upath args both from database and adouble file
193 * 1. Get the objects CNID as stored in its adouble file
194 * 2. Get the objects CNID from the database
195 * 3. If there's a problem with a "dbd" database, fallback to "tdb" in memory
196 * 4. In case 2 and 3 differ, store 3 in the adouble file
198 * @param vol (rw) volume
199 * @param adp (rw) adouble struct of object upath, might be NULL
200 * @param st (r) stat of upath, must NOT be NULL
201 * @param did (r) parent CNID of upath
202 * @param upath (r) name of object
203 * @param len (r) strlen of upath
205 uint32_t get_id(struct vol *vol,
207 const struct stat *st,
212 static int first = 1; /* mark if this func is called the first time */
214 uint32_t dbcnid = CNID_INVALID;
217 if (vol->v_cdb != NULL) {
218 /* prime aint with what we think is the cnid, set did to zero for
219 catching moved files */
220 adcnid = ad_getid(adp, st->st_dev, st->st_ino, 0, vol->v_stamp); /* (1) */
222 dbcnid = cnid_add(vol->v_cdb, st, did, upath, len, adcnid); /* (2) */
223 /* Throw errors if cnid_add fails. */
224 if (dbcnid == CNID_INVALID) {
226 case CNID_ERR_CLOSE: /* the db is closed */
229 LOG(log_error, logtype_afpd, "get_id: Incorrect parameters passed to cnid_add");
230 afp_errno = AFPERR_PARAM;
233 afp_errno = AFPERR_PARAM;
236 /* Close CNID backend if "dbd" and switch to temp in-memory "tdb" */
237 /* we have to do it here for "dbd" because it uses "lazy opening" */
238 /* In order to not end in a loop somehow with goto restart below */
240 if (first && (strcmp(vol->v_cnidscheme, "dbd") == 0)) { /* (3) */
241 cnid_close(vol->v_cdb);
242 free(vol->v_cnidscheme);
243 vol->v_cnidscheme = strdup("tdb");
245 int flags = CNID_FLAG_MEMORY;
246 if ((vol->v_flags & AFPVOL_NODEV)) {
247 flags |= CNID_FLAG_NODEV;
249 LOG(log_error, logtype_afpd, "Reopen volume %s using in memory temporary CNID DB.",
251 vol->v_cdb = cnid_open(vol->v_path, vol->v_umask, "tdb", flags, NULL, NULL);
253 if (!(vol->v_flags & AFPVOL_TM)) {
254 vol->v_flags |= AFPVOL_RO;
255 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB instead."
256 "Check server messages for details. Switching to read-only mode.");
257 kill(getpid(), SIGUSR2);
259 goto restart; /* now try again with the temp CNID db */
261 setmessage("Something wrong with the volume's CNID DB, using temporary CNID DB failed too!"
262 "Check server messages for details, can't recover from this state!");
265 afp_errno = AFPERR_MISC;
269 else if (adp && adcnid && (adcnid != dbcnid)) { /* 4 */
270 /* Update the ressource fork. For a folder adp is always null */
271 LOG(log_debug, logtype_afpd, "get_id(%s/%s): calling ad_setid(old: %u, new: %u)",
272 getcwdpath(), upath, htonl(adcnid), htonl(dbcnid));
273 if (ad_setid(adp, st->st_dev, st->st_ino, dbcnid, did, vol->v_stamp)) {
274 if (ad_flush(adp) != 0)
275 LOG(log_error, logtype_afpd, "get_id(\"%s\"): can't flush", fullpathname(upath));
285 /* -------------------------- */
286 int getmetadata(const AFPObj *obj,
289 struct path *path, struct dir *dir,
290 char *buf, size_t *buflen, struct adouble *adp)
292 char *data, *l_nameoff = NULL, *upath;
293 char *utf_nameoff = NULL;
298 u_char achar, fdType[4];
303 LOG(log_debug, logtype_afpd, "getmetadata(\"%s\")", path->u_name);
305 upath = path->u_name;
309 if ( ((bitmap & ( (1 << FILPBIT_FINFO)|(1 << FILPBIT_LNAME)|(1 <<FILPBIT_PDINFO) ) ) && !path->m_name)
310 || (bitmap & ( (1 << FILPBIT_LNAME) ) && utf8_encoding(obj)) /* FIXME should be m_name utf8 filename */
311 || (bitmap & (1 << FILPBIT_FNUM))) {
314 struct dir *cachedfile;
315 int len = strlen(upath);
316 if ((cachedfile = dircache_search_by_name(vol, dir, upath, len)) != NULL)
317 id = cachedfile->d_did;
319 id = get_id(vol, adp, st, dir->d_did, upath, len);
321 /* Add it to the cache */
322 LOG(log_debug, logtype_afpd, "getmetadata: caching: did:%u, \"%s\", cnid:%u",
323 ntohl(dir->d_did), upath, ntohl(id));
325 /* Get macname from unixname first */
326 if (path->m_name == NULL) {
327 if ((path->m_name = utompath(vol, upath, id, utf8_encoding(obj))) == NULL) {
328 LOG(log_error, logtype_afpd, "getmetadata: utompath error");
334 if (((fullpath = bstrcpy(dir->d_fullpath)) == NULL)
335 || (bconchar(fullpath, '/') != BSTR_OK)
336 || (bcatcstr(fullpath, upath)) != BSTR_OK) {
337 LOG(log_error, logtype_afpd, "getmetadata: fullpath: %s", strerror(errno));
341 if ((cachedfile = dir_new(path->m_name, upath, vol, dir->d_did, id, fullpath, st)) == NULL) {
342 LOG(log_error, logtype_afpd, "getmetadata: error from dir_new");
346 if ((dircache_add(vol, cachedfile)) != 0) {
347 LOG(log_error, logtype_afpd, "getmetadata: fatal dircache error");
355 if (id == CNID_INVALID)
359 path->m_name = utompath(vol, upath, id, utf8_encoding(vol->v_obj));
362 while ( bitmap != 0 ) {
363 while (( bitmap & 1 ) == 0 ) {
371 ad_getattr(adp, &ashort);
372 } else if (vol_inv_dots(vol) && *upath == '.') {
373 ashort = htons(ATTRBIT_INVISIBLE);
377 /* FIXME do we want a visual clue if the file is read only
380 accessmode(vol, ".", &ma, dir , NULL);
381 if ((ma.ma_user & AR_UWRITE)) {
382 accessmode(vol, upath, &ma, dir , st);
383 if (!(ma.ma_user & AR_UWRITE)) {
384 ashort |= htons(ATTRBIT_NOWRITE);
388 memcpy(data, &ashort, sizeof( ashort ));
389 data += sizeof( ashort );
390 LOG(log_debug, logtype_afpd, "metadata('%s'): AFP Attributes: %04x",
391 path->u_name, ntohs(ashort));
395 memcpy(data, &dir->d_did, sizeof( uint32_t ));
396 data += sizeof( uint32_t );
397 LOG(log_debug, logtype_afpd, "metadata('%s'): Parent DID: %u",
398 path->u_name, ntohl(dir->d_did));
402 if (!adp || (ad_getdate(adp, AD_DATE_CREATE, &aint) < 0))
403 aint = AD_DATE_FROM_UNIX(st->st_mtime);
404 memcpy(data, &aint, sizeof( aint ));
405 data += sizeof( aint );
409 if ( adp && (ad_getdate(adp, AD_DATE_MODIFY, &aint) == 0)) {
410 if ((st->st_mtime > AD_DATE_TO_UNIX(aint))) {
411 aint = AD_DATE_FROM_UNIX(st->st_mtime);
414 aint = AD_DATE_FROM_UNIX(st->st_mtime);
416 memcpy(data, &aint, sizeof( int ));
417 data += sizeof( int );
421 if (!adp || (ad_getdate(adp, AD_DATE_BACKUP, &aint) < 0))
422 aint = AD_DATE_START;
423 memcpy(data, &aint, sizeof( int ));
424 data += sizeof( int );
428 get_finderinfo(vol, upath, adp, (char *)data,S_ISLNK(st->st_mode));
429 data += ADEDLEN_FINDERI;
434 data += sizeof( uint16_t );
438 memset(data, 0, sizeof(uint16_t));
439 data += sizeof( uint16_t );
443 memcpy(data, &id, sizeof( id ));
444 data += sizeof( id );
445 LOG(log_debug, logtype_afpd, "metadata('%s'): CNID: %u",
446 path->u_name, ntohl(id));
450 if (st->st_size > 0xffffffff)
453 aint = htonl( st->st_size );
454 memcpy(data, &aint, sizeof( aint ));
455 data += sizeof( aint );
460 if (adp->ad_rlen > 0xffffffff)
463 aint = htonl( adp->ad_rlen);
467 memcpy(data, &aint, sizeof( aint ));
468 data += sizeof( aint );
471 /* Current client needs ProDOS info block for this file.
472 Use simple heuristic and let the Mac "type" string tell
473 us what the PD file code should be. Everything gets a
474 subtype of 0x0000 unless the original value was hashed
475 to "pXYZ" when we created it. See IA, Ver 2.
476 <shirsch@adelphia.net> */
477 case FILPBIT_PDINFO :
478 if (obj->afp_version >= 30) { /* UTF8 name */
479 utf8 = kTextEncodingUTF8;
481 data += sizeof( uint16_t );
483 memcpy(data, &aint, sizeof( aint ));
484 data += sizeof( aint );
488 memcpy(fdType, ad_entry( adp, ADEID_FINDERI ), 4 );
490 if ( memcmp( fdType, "TEXT", 4 ) == 0 ) {
494 else if ( memcmp( fdType, "PSYS", 4 ) == 0 ) {
498 else if ( memcmp( fdType, "PS16", 4 ) == 0 ) {
502 else if ( memcmp( fdType, "BINA", 4 ) == 0 ) {
506 else if ( fdType[0] == 'p' ) {
508 ashort = (fdType[2] * 256) + fdType[3];
522 memcpy(data, &ashort, sizeof( ashort ));
523 data += sizeof( ashort );
524 memset(data, 0, sizeof( ashort ));
525 data += sizeof( ashort );
528 case FILPBIT_EXTDFLEN:
529 aint = htonl(st->st_size >> 32);
530 memcpy(data, &aint, sizeof( aint ));
531 data += sizeof( aint );
532 aint = htonl(st->st_size);
533 memcpy(data, &aint, sizeof( aint ));
534 data += sizeof( aint );
536 case FILPBIT_EXTRFLEN:
539 aint = htonl(adp->ad_rlen >> 32);
540 memcpy(data, &aint, sizeof( aint ));
541 data += sizeof( aint );
543 aint = htonl(adp->ad_rlen);
544 memcpy(data, &aint, sizeof( aint ));
545 data += sizeof( aint );
547 case FILPBIT_UNIXPR :
548 /* accessmode may change st_mode with ACLs */
549 accessmode(obj, vol, upath, &ma, dir , st);
551 aint = htonl(st->st_uid);
552 memcpy( data, &aint, sizeof( aint ));
553 data += sizeof( aint );
554 aint = htonl(st->st_gid);
555 memcpy( data, &aint, sizeof( aint ));
556 data += sizeof( aint );
559 type == slnk indicates an OSX style symlink,
560 we have to add S_IFLNK to the mode, otherwise
561 10.3 clients freak out. */
565 memcpy(fdType, ad_entry( adp, ADEID_FINDERI ), 4 );
566 if ( memcmp( fdType, "slnk", 4 ) == 0 ) {
572 memcpy( data, &aint, sizeof( aint ));
573 data += sizeof( aint );
575 *data++ = ma.ma_user;
576 *data++ = ma.ma_world;
577 *data++ = ma.ma_group;
578 *data++ = ma.ma_owner;
582 return( AFPERR_BITMAP );
588 ashort = htons( data - buf );
589 memcpy(l_nameoff, &ashort, sizeof( ashort ));
590 data = set_name(vol, data, dir->d_did, path->m_name, id, 0);
593 ashort = htons( data - buf );
594 memcpy(utf_nameoff, &ashort, sizeof( ashort ));
595 data = set_name(vol, data, dir->d_did, path->m_name, id, utf8);
597 *buflen = data - buf;
601 /* ----------------------- */
602 int getfilparams(const AFPObj *obj, struct vol *vol, uint16_t bitmap, struct path *path,
603 struct dir *dir, char *buf, size_t *buflen, int in_enumerate)
605 struct adouble ad, *adp;
610 LOG(log_debug, logtype_afpd, "getfilparams(\"%s\")", path->u_name);
612 opened = PARAM_NEED_ADP(bitmap);
618 * Dont check for and resturn open fork attributes when enumerating
619 * This saves a lot of syscalls, the client will hopefully only use the result
620 * in FPGetFileParms where we return the correct value
622 flags = (!in_enumerate &&(bitmap & (1 << FILPBIT_ATTR))) ? ADFLAGS_CHECK_OF : 0;
624 adp = of_ad(vol, path, &ad);
625 upath = path->u_name;
627 if ( ad_metadata( upath, flags, adp) < 0 ) {
630 LOG(log_error, logtype_afpd, "getfilparams(%s): %s: check resource fork permission?",
631 upath, strerror(errno));
632 return AFPERR_ACCESS;
634 LOG(log_error, logtype_afpd, "getfilparams(%s): bad resource fork", upath);
643 rc = getmetadata(obj, vol, bitmap, path, dir, buf, buflen, adp);
644 ad_close(adp, ADFLAGS_HF | flags);
649 /* ----------------------------- */
650 int afp_createfile(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
655 struct ofork *of = NULL;
657 int creatf, did, openf, retvalue = AFP_OK;
663 creatf = (unsigned char) *ibuf++;
665 memcpy(&vid, ibuf, sizeof( vid ));
666 ibuf += sizeof( vid );
668 if (NULL == ( vol = getvolbyvid( vid )) )
669 return( AFPERR_PARAM );
671 if (vol->v_flags & AFPVOL_RO)
674 memcpy(&did, ibuf, sizeof( did));
675 ibuf += sizeof( did );
677 if (NULL == ( dir = dirlookup( vol, did )) )
680 if (NULL == ( s_path = cname( vol, dir, &ibuf )) )
681 return get_afp_errno(AFPERR_PARAM);
682 if ( *s_path->m_name == '\0' )
683 return( AFPERR_BADTYPE );
685 upath = s_path->u_name;
688 /* if upath is deleted we already in trouble anyway */
689 if ((of = of_findname(s_path))) {
697 openf = ADFLAGS_RDWR | ADFLAGS_CREATE | ADFLAGS_TRUNC;
699 /* on a soft create, if the file is open then ad_open won't fail
700 because open syscall is not called */
701 openf = ADFLAGS_RDWR | ADFLAGS_CREATE | ADFLAGS_EXCL;
703 if (ad_open(&ad, upath, ADFLAGS_DF | ADFLAGS_HF | ADFLAGS_NOHF | openf, 0666) < 0) {
707 case ENOENT : /* we were already in 'did folder' so chdir() didn't fail */
708 return ( AFPERR_NOOBJ );
710 return( AFPERR_EXIST );
712 return( AFPERR_ACCESS );
715 LOG(log_info, logtype_afpd, "afp_createfile: DISK FULL");
716 return( AFPERR_DFULL );
718 return( AFPERR_PARAM );
721 if ( ad_meta_fileno( &ad ) == -1 ) { /* Hard META / HF */
722 /* FIXME with hard create on an existing file, we already
723 * corrupted the data file.
725 netatalk_unlink( upath );
726 ad_close( &ad, ADFLAGS_DF );
727 return AFPERR_ACCESS;
730 path = s_path->m_name;
731 ad_setname(&ad, path);
734 if (lstat(upath, &st) != 0) {
735 LOG(log_error, logtype_afpd, "afp_createfile(\"%s\"): stat: %s",
736 upath, strerror(errno));
737 ad_close(&ad, ADFLAGS_DF|ADFLAGS_HF);
742 if ((id = get_id(vol, &ad, &st, dir->d_did, upath, strlen(upath))) == CNID_INVALID) {
743 LOG(log_error, logtype_afpd, "afp_createfile(\"%s\"): CNID error", upath);
744 goto createfile_iderr;
746 (void)ad_setid(&ad, st.st_dev, st.st_ino, id, dir->d_did, vol->v_stamp);
750 ad_close(&ad, ADFLAGS_DF|ADFLAGS_HF );
751 fce_register(FCE_FILE_CREATE, fullpathname(upath), NULL, fce_file);
756 setvoltime(obj, vol );
761 int afp_setfilparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
767 uint16_t vid, bitmap;
772 memcpy(&vid, ibuf, sizeof( vid ));
773 ibuf += sizeof( vid );
774 if (NULL == ( vol = getvolbyvid( vid )) ) {
775 return( AFPERR_PARAM );
778 if (vol->v_flags & AFPVOL_RO)
781 memcpy(&did, ibuf, sizeof( did ));
782 ibuf += sizeof( did );
783 if (NULL == ( dir = dirlookup( vol, did )) ) {
784 return afp_errno; /* was AFPERR_NOOBJ */
787 memcpy(&bitmap, ibuf, sizeof( bitmap ));
788 bitmap = ntohs( bitmap );
789 ibuf += sizeof( bitmap );
791 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
792 return get_afp_errno(AFPERR_PARAM);
795 if (path_isadir(s_path)) {
796 return( AFPERR_BADTYPE ); /* it's a directory */
799 if ( s_path->st_errno != 0 ) {
800 return( AFPERR_NOOBJ );
803 if ((u_long)ibuf & 1 ) {
807 if (AFP_OK == ( rc = setfilparams(obj, vol, s_path, bitmap, ibuf )) ) {
808 setvoltime(obj, vol );
815 * cf AFP3.0.pdf page 252 for change_mdate and change_parent_mdate logic
818 extern struct path Cur_Path;
820 int setfilparams(const AFPObj *obj, struct vol *vol,
821 struct path *path, uint16_t f_bitmap, char *buf )
823 struct adouble ad, *adp;
825 int bit, isad = 1, err = AFP_OK;
827 u_char achar, *fdType, xyy[4]; /* uninitialized, OK 310105 */
828 uint16_t ashort, bshort, oshort;
831 uint16_t upriv_bit = 0;
833 int change_mdate = 0;
834 int change_parent_mdate = 0;
839 uint16_t bitmap = f_bitmap;
840 uint32_t cdate,bdate;
841 u_char finder_buf[32];
842 int symlinked = S_ISLNK(path->st.st_mode);
845 LOG(log_debug9, logtype_afpd, "begin setfilparams:");
848 adp = of_ad(vol, path, &ad);
849 upath = path->u_name;
851 if (!vol_unix_priv(vol) && check_access(obj, vol, upath, OPENACC_WR ) < 0) {
852 return AFPERR_ACCESS;
855 /* with unix priv maybe we have to change adouble file priv first */
857 while ( bitmap != 0 ) {
858 while (( bitmap & 1 ) == 0 ) {
865 memcpy(&ashort, buf, sizeof( ashort ));
866 buf += sizeof( ashort );
870 memcpy(&cdate, buf, sizeof(cdate));
871 buf += sizeof( cdate );
874 memcpy(&newdate, buf, sizeof( newdate ));
875 buf += sizeof( newdate );
879 memcpy(&bdate, buf, sizeof( bdate));
880 buf += sizeof( bdate );
884 memcpy(finder_buf, buf, 32 );
885 if (memcmp(buf, "slnkrhap", 8) == 0 && !S_ISLNK(path->st.st_mode)) {
889 char buf[PATH_MAX+1];
890 if ((fp = open(path->u_name, O_RDONLY)) >= 0) {
891 if ((len = read(fp, buf, PATH_MAX+1))) {
892 if (unlink(path->u_name) == 0) {
894 erc = symlink(buf, path->u_name);
903 goto setfilparam_done;
909 case FILPBIT_UNIXPR :
910 if (!vol_unix_priv(vol)) {
911 /* this volume doesn't use unix priv */
917 change_parent_mdate = 1;
919 memcpy( &aint, buf, sizeof( aint ));
920 f_uid = ntohl (aint);
921 buf += sizeof( aint );
922 memcpy( &aint, buf, sizeof( aint ));
923 f_gid = ntohl (aint);
924 buf += sizeof( aint );
925 setfilowner(vol, f_uid, f_gid, path);
927 memcpy( &upriv, buf, sizeof( upriv ));
928 buf += sizeof( upriv );
929 upriv = ntohl (upriv);
930 if ((upriv & S_IWUSR)) {
931 setfilunixmode(vol, path, upriv);
938 case FILPBIT_PDINFO :
939 if (obj->afp_version < 30) { /* else it's UTF8 name */
942 /* Keep special case to support crlf translations */
943 if ((unsigned int) achar == 0x04) {
944 fdType = (u_char *)"TEXT";
947 xyy[0] = ( u_char ) 'p';
958 /* break while loop */
967 /* second try with adouble open
969 if (ad_open(adp, upath, ADFLAGS_HF | ADFLAGS_RDWR | ADFLAGS_CREATE, 0666) < 0) {
970 LOG(log_debug, logtype_afpd, "setfilparams: ad_open_metadata error");
972 * For some things, we don't need an adouble header:
973 * - change of modification date
974 * - UNIX privs (Bug-ID #2863424)
976 if (!symlinked && f_bitmap & ~(1<<FILPBIT_MDATE | 1<<FILPBIT_UNIXPR)) {
977 LOG(log_debug, logtype_afpd, "setfilparams: need adouble access");
978 return AFPERR_ACCESS;
980 LOG(log_debug, logtype_afpd, "setfilparams: no adouble perms, but only FILPBIT_MDATE and/or FILPBIT_UNIXPR");
982 } else if ((ad_get_MD_flags( adp ) & O_CREAT) ) {
983 ad_setname(adp, path->m_name);
988 while ( bitmap != 0 ) {
989 while (( bitmap & 1 ) == 0 ) {
996 ad_getattr(adp, &bshort);
998 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
999 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1003 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1004 change_parent_mdate = 1;
1005 ad_setattr(adp, bshort);
1007 case FILPBIT_CDATE :
1008 ad_setdate(adp, AD_DATE_CREATE, cdate);
1010 case FILPBIT_MDATE :
1012 case FILPBIT_BDATE :
1013 ad_setdate(adp, AD_DATE_BACKUP, bdate);
1015 case FILPBIT_FINFO :
1016 if (default_type( ad_entry( adp, ADEID_FINDERI ))
1018 ((em = getextmap( path->m_name )) &&
1019 !memcmp(finder_buf, em->em_type, sizeof( em->em_type )) &&
1020 !memcmp(finder_buf + 4, em->em_creator,sizeof( em->em_creator)))
1021 || ((em = getdefextmap()) &&
1022 !memcmp(finder_buf, em->em_type, sizeof( em->em_type )) &&
1023 !memcmp(finder_buf + 4, em->em_creator,sizeof( em->em_creator)))
1025 memcpy(finder_buf, ufinderi, 8 );
1027 memcpy(ad_entry( adp, ADEID_FINDERI ), finder_buf, 32 );
1029 case FILPBIT_UNIXPR :
1031 setfilunixmode(vol, path, upriv);
1034 case FILPBIT_PDINFO :
1035 if (obj->afp_version < 30) { /* else it's UTF8 name */
1036 memcpy(ad_entry( adp, ADEID_FINDERI ), fdType, 4 );
1037 memcpy(ad_entry( adp, ADEID_FINDERI ) + 4, "pdos", 4 );
1042 err = AFPERR_BITMAP;
1043 goto setfilparam_done;
1050 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1051 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1055 ad_setdate(adp, AD_DATE_MODIFY, newdate);
1056 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1062 ad_close(adp, ADFLAGS_HF);
1065 if (change_parent_mdate && gettimeofday(&tv, NULL) == 0) {
1066 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1067 bitmap = 1<<FILPBIT_MDATE;
1068 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1072 LOG(log_debug9, logtype_afpd, "end setfilparams:");
1078 * renamefile and copyfile take the old and new unix pathnames
1079 * and the new mac name.
1081 * sdir_fd source dir fd to which src path is relative (for openat et al semantics)
1082 * passing -1 means this is not used, src path is a full path
1083 * src the source path
1084 * dst the dest filename in current dir
1085 * newname the dest mac name
1086 * adp adouble struct of src file, if open, or & zeroed one
1089 int renamefile(const struct vol *vol, int sdir_fd, char *src, char *dst, char *newname, struct adouble *adp)
1093 LOG(log_debug, logtype_afpd,
1094 "renamefile: src[%d, \"%s\"] -> dst[\"%s\"]", sdir_fd, src, dst);
1096 if ( unix_rename( sdir_fd, src, -1, dst ) < 0 ) {
1099 return( AFPERR_NOOBJ );
1102 return( AFPERR_ACCESS );
1104 return AFPERR_VLOCK;
1105 case EXDEV : /* Cross device move -- try copy */
1106 /* NOTE: with open file it's an error because after the copy we will
1107 * get two files, it's fixable for our process (eg reopen the new file, get the
1108 * locks, and so on. But it doesn't solve the case with a second process
1110 if (adp->ad_open_forks) {
1111 /* FIXME warning in syslog so admin'd know there's a conflict ?*/
1112 return AFPERR_OLOCK; /* little lie */
1114 if (AFP_OK != ( rc = copyfile(vol, vol, sdir_fd, src, dst, newname, NULL )) ) {
1115 /* on error copyfile delete dest */
1118 return deletefile(vol, sdir_fd, src, 0);
1120 return( AFPERR_PARAM );
1124 if (vol->vfs->vfs_renamefile(vol, sdir_fd, src, dst) < 0 ) {
1128 /* try to undo the data fork rename,
1129 * we know we are on the same device
1132 unix_rename(-1, dst, sdir_fd, src );
1133 /* return the first error */
1136 return AFPERR_NOOBJ;
1139 return AFPERR_ACCESS ;
1141 return AFPERR_VLOCK;
1143 return AFPERR_PARAM ;
1148 /* don't care if we can't open the newly renamed ressource fork */
1149 if (ad_open(adp, dst, ADFLAGS_HF | ADFLAGS_RDWR) == 0) {
1150 ad_setname(adp, newname);
1152 ad_close( adp, ADFLAGS_HF );
1159 convert a Mac long name to an utf8 name,
1161 size_t mtoUTF8(const struct vol *vol, const char *src, size_t srclen, char *dest, size_t destlen)
1165 if ((size_t)-1 == (outlen = convert_string ( vol->v_maccharset, CH_UTF8_MAC, src, srclen, dest, destlen)) ) {
1171 /* ---------------- */
1172 int copy_path_name(const struct vol *vol, char *newname, char *ibuf)
1179 if ( type != 2 && !(vol->v_obj->afp_version >= 30 && type == 3) ) {
1185 if (( plen = (unsigned char)*ibuf++ ) != 0 ) {
1186 if (vol->v_obj->afp_version >= 30) {
1187 /* convert it to UTF8
1189 if ((plen = mtoUTF8(vol, ibuf, plen, newname, AFPOBJ_TMPSIZ)) == (size_t)-1)
1193 strncpy( newname, ibuf, plen );
1194 newname[ plen ] = '\0';
1196 if (strlen(newname) != plen) {
1197 /* there's \0 in newname, e.g. it's a pathname not
1205 memcpy(&hint, ibuf, sizeof(hint));
1206 ibuf += sizeof(hint);
1208 memcpy(&len16, ibuf, sizeof(len16));
1209 ibuf += sizeof(len16);
1210 plen = ntohs(len16);
1213 if (plen > AFPOBJ_TMPSIZ) {
1216 strncpy( newname, ibuf, plen );
1217 newname[ plen ] = '\0';
1218 if (strlen(newname) != plen) {
1227 /* -----------------------------------
1229 int afp_copyfile(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1231 struct vol *s_vol, *d_vol;
1233 char *newname, *p, *upath;
1234 struct path *s_path;
1235 uint32_t sdid, ddid;
1236 int err, retvalue = AFP_OK;
1237 uint16_t svid, dvid;
1239 struct adouble ad, *adp;
1245 memcpy(&svid, ibuf, sizeof( svid ));
1246 ibuf += sizeof( svid );
1247 if (NULL == ( s_vol = getvolbyvid( svid )) ) {
1248 return( AFPERR_PARAM );
1251 memcpy(&sdid, ibuf, sizeof( sdid ));
1252 ibuf += sizeof( sdid );
1253 if (NULL == ( dir = dirlookup( s_vol, sdid )) ) {
1257 memcpy(&dvid, ibuf, sizeof( dvid ));
1258 ibuf += sizeof( dvid );
1259 memcpy(&ddid, ibuf, sizeof( ddid ));
1260 ibuf += sizeof( ddid );
1262 if (NULL == ( s_path = cname( s_vol, dir, &ibuf )) ) {
1263 return get_afp_errno(AFPERR_PARAM);
1265 if ( path_isadir(s_path) ) {
1266 return( AFPERR_BADTYPE );
1269 /* don't allow copies when the file is open.
1270 * XXX: the spec only calls for read/deny write access.
1271 * however, copyfile doesn't have any of that info,
1272 * and locks need to stay coherent. as a result,
1273 * we just balk if the file is opened already. */
1275 adp = of_ad(s_vol, s_path, &ad);
1277 if (ad_open(adp, s_path->u_name, ADFLAGS_DF | ADFLAGS_HF | ADFLAGS_NOHF | ADFLAGS_RDONLY | ADFLAGS_SETSHRMD) < 0) {
1278 return AFPERR_DENYCONF;
1280 #ifdef HAVE_FSHARE_T
1282 shmd.f_access = F_RDACC;
1283 shmd.f_deny = F_NODNY;
1284 if (fcntl(ad_data_fileno(adp), F_SHARE, &shmd) != 0) {
1285 retvalue = AFPERR_DENYCONF;
1288 if (AD_RSRC_OPEN(adp) && fcntl(ad_reso_fileno(adp), F_SHARE, &shmd) != 0) {
1289 retvalue = AFPERR_DENYCONF;
1293 denyreadset = (ad_testlock(adp, ADEID_DFORK, AD_FILELOCK_DENY_RD) != 0 ||
1294 ad_testlock(adp, ADEID_RFORK, AD_FILELOCK_DENY_RD) != 0 );
1297 retvalue = AFPERR_DENYCONF;
1301 newname = obj->newtmp;
1302 strcpy( newname, s_path->m_name );
1304 p = ctoupath( s_vol, curdir, newname );
1306 retvalue = AFPERR_PARAM;
1310 if (NULL == ( d_vol = getvolbyvid( dvid )) ) {
1311 retvalue = AFPERR_PARAM;
1315 if (d_vol->v_flags & AFPVOL_RO) {
1316 retvalue = AFPERR_VLOCK;
1320 if (NULL == ( dir = dirlookup( d_vol, ddid )) ) {
1321 retvalue = afp_errno;
1325 if (( s_path = cname( d_vol, dir, &ibuf )) == NULL ) {
1326 retvalue = get_afp_errno(AFPERR_NOOBJ);
1330 if ( *s_path->m_name != '\0' ) {
1331 retvalue =path_error(s_path, AFPERR_NOOBJ);
1335 /* one of the handful of places that knows about the path type */
1336 if (copy_path_name(d_vol, newname, ibuf) < 0) {
1337 retvalue = AFPERR_PARAM;
1340 /* newname is always only a filename so curdir *is* its
1343 if (NULL == (upath = mtoupath(d_vol, newname, curdir->d_did, utf8_encoding(d_vol->v_obj)))) {
1344 retvalue =AFPERR_PARAM;
1348 if ( (err = copyfile(s_vol, d_vol, -1, p, upath , newname, adp)) < 0 ) {
1354 setvoltime(obj, d_vol );
1357 ad_close( adp, ADFLAGS_DF |ADFLAGS_HF | ADFLAGS_SETSHRMD);
1361 /* ----------------------------------
1362 * if newname is NULL (from directory.c) we don't want to copy the resource fork.
1363 * because we are doing it elsewhere.
1364 * currently if newname is NULL then adp is NULL.
1366 int copyfile(const struct vol *s_vol,
1367 const struct vol *d_vol,
1372 struct adouble *adp)
1374 struct adouble ads, add;
1381 LOG(log_debug, logtype_afpd, "copyfile(sfd:%d,s:'%s',d:'%s',n:'%s')",
1382 sfd, src, dst, newname);
1385 ad_init(&ads, s_vol);
1389 adflags = ADFLAGS_DF | ADFLAGS_RF | ADFLAGS_NORF;
1391 if (ad_openat(adp, sfd, src, adflags | ADFLAGS_RDONLY) < 0) {
1396 if (!AD_RSRC_OPEN(adp))
1397 /* no resource fork, don't create one for dst file */
1398 adflags &= ~ADFLAGS_RF;
1400 stat_result = fstat(ad_data_fileno(adp), &st); /* saving stat exit code, thus saving us on one more stat later on */
1402 if (stat_result < 0) {
1403 /* unlikely but if fstat fails, the default file mode will be 0666. */
1404 st.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1407 ad_init(&add, d_vol);
1408 if (ad_open(&add, dst, adflags | ADFLAGS_RDWR | ADFLAGS_CREATE | ADFLAGS_EXCL, st.st_mode | S_IRUSR | S_IWUSR) < 0) {
1410 ad_close( adp, adflags );
1411 if (EEXIST != ret_err) {
1412 deletefile(d_vol, -1, dst, 0);
1415 return AFPERR_EXIST;
1418 if ((err = copy_fork(ADEID_DFORK, &add, adp)) != 0)
1419 LOG(log_error, logtype_afpd, "copyfile('%s'): %s", src, strerror(errno));
1422 if ((err = d_vol->vfs->vfs_copyfile(d_vol, sfd, src, dst)) != 0)
1423 LOG(log_error, logtype_afpd, "copyfile('%s'): %s", src, strerror(errno));
1428 if (!ret_err && newname && (adflags & ADFLAGS_HF)) {
1429 /* set the new name in the resource fork */
1430 ad_copy_header(&add, adp);
1431 ad_setname(&add, newname);
1434 ad_close( adp, adflags );
1436 if (ad_close( &add, adflags ) <0) {
1441 deletefile(d_vol, -1, dst, 0);
1443 else if (stat_result == 0) {
1444 /* set dest modification date to src date */
1447 ut.actime = ut.modtime = st.st_mtime;
1449 /* FIXME netatalk doesn't use resource fork file date
1450 * but maybe we should set its modtime too.
1455 switch ( ret_err ) {
1461 LOG(log_info, logtype_afpd, "copyfile: DISK FULL");
1462 return AFPERR_DFULL;
1464 return AFPERR_NOOBJ;
1466 return AFPERR_ACCESS;
1468 return AFPERR_VLOCK;
1470 return AFPERR_PARAM;
1474 /* -----------------------------------
1475 vol: not NULL delete cnid entry. then we are in curdir and file is a only filename
1476 checkAttrib: 1 check kFPDeleteInhibitBit (deletfile called by afp_delete)
1478 when deletefile is called we don't have lock on it, file is closed (for us)
1479 untrue if called by renamefile
1481 ad_open always try to open file RDWR first and ad_lock takes care of
1482 WRITE lock on read only file.
1485 static int check_attrib(struct adouble *adp)
1487 uint16_t bshort = 0;
1489 ad_getattr(adp, &bshort);
1491 * Does kFPDeleteInhibitBit (bit 8) set?
1493 if ((bshort & htons(ATTRBIT_NODELETE))) {
1494 return AFPERR_OLOCK;
1496 if ((bshort & htons(ATTRBIT_DOPEN | ATTRBIT_ROPEN))) {
1502 * dirfd can be used for unlinkat semantics
1504 int deletefile(const struct vol *vol, int dirfd, char *file, int checkAttrib)
1507 struct adouble *adp = NULL;
1508 int adflags, err = AFP_OK;
1511 LOG(log_debug, logtype_afpd, "deletefile('%s')", file);
1515 /* was EACCESS error try to get only metadata */
1516 /* we never want to create a resource fork here, we are going to delete it
1517 * moreover sometimes deletefile is called with a no existent file and
1518 * ad_open would create a 0 byte resource fork
1520 if ( ad_metadataat(dirfd, file, ADFLAGS_CHECK_OF, &ad) == 0 ) {
1521 if ((err = check_attrib(&ad))) {
1522 ad_close(&ad, ADFLAGS_HF | ADFLAGS_CHECK_OF);
1529 /* try to open both forks at once */
1530 adflags = ADFLAGS_DF;
1531 if (ad_openat(&ad, dirfd, file, adflags | ADFLAGS_RF | ADFLAGS_NORF | ADFLAGS_RDONLY) < 0 ) {
1536 case EACCES: /* maybe it's a file with no write mode for us */
1537 break; /* was return AFPERR_ACCESS;*/
1550 if ( adp && AD_RSRC_OPEN(adp) != -1 ) { /* there's a resource fork */
1551 adflags |= ADFLAGS_RF;
1552 /* FIXME we have a pb here because we want to know if a file is open
1553 * there's a 'priority inversion' if you can't open the ressource fork RW
1554 * you can delete it if it's open because you can't get a write lock.
1556 * ADLOCK_FILELOCK means the whole ressource fork, not only after the
1559 * FIXME it doesn't work for RFORK open read only and fork open without deny mode
1561 if (ad_tmplock(&ad, ADEID_RFORK, ADLOCK_WR |ADLOCK_FILELOCK, 0, 0, 0) < 0 ) {
1567 if (adp && ad_tmplock( &ad, ADEID_DFORK, ADLOCK_WR, 0, 0, 0 ) < 0) {
1568 LOG(log_error, logtype_afpd, "deletefile('%s'): ad_tmplock error: %s", file, strerror(errno));
1570 } else if (!(err = vol->vfs->vfs_deletefile(vol, dirfd, file)) && !(err = netatalk_unlinkat(dirfd, file )) ) {
1572 if (checkAttrib && (id = cnid_get(vol->v_cdb, curdir->d_did, file, strlen(file)))) {
1573 cnid_delete(vol->v_cdb, id);
1579 ad_close(&ad, ADFLAGS_HF | ADFLAGS_CHECK_OF);
1582 ad_close( &ad, adflags ); /* ad_close removes locks if any */
1587 /* ------------------------------------ */
1588 /* return a file id */
1589 int afp_createid(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1598 struct path *s_path;
1604 memcpy(&vid, ibuf, sizeof(vid));
1605 ibuf += sizeof(vid);
1607 if (NULL == ( vol = getvolbyvid( vid )) ) {
1608 return( AFPERR_PARAM);
1611 if (vol->v_cdb == NULL || !(vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1615 if (vol->v_flags & AFPVOL_RO)
1616 return AFPERR_VLOCK;
1618 memcpy(&did, ibuf, sizeof( did ));
1619 ibuf += sizeof(did);
1621 if (NULL == ( dir = dirlookup( vol, did )) ) {
1622 return afp_errno; /* was AFPERR_PARAM */
1625 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
1626 return get_afp_errno(AFPERR_NOOBJ); /* was AFPERR_PARAM */
1629 if ( path_isadir(s_path) ) {
1630 return( AFPERR_BADTYPE );
1633 upath = s_path->u_name;
1634 switch (s_path->st_errno) {
1636 break; /* success */
1639 return AFPERR_ACCESS;
1641 return AFPERR_NOOBJ;
1643 return AFPERR_PARAM;
1646 if ((id = cnid_lookup(vol->v_cdb, st, did, upath, len = strlen(upath)))) {
1647 memcpy(rbuf, &id, sizeof(id));
1648 *rbuflen = sizeof(id);
1649 return AFPERR_EXISTID;
1652 if ((id = get_id(vol, NULL, st, did, upath, len)) != CNID_INVALID) {
1653 memcpy(rbuf, &id, sizeof(id));
1654 *rbuflen = sizeof(id);
1661 /* ------------------------------- */
1667 static int reenumerate_loop(struct dirent *de, char *mname _U_, void *data)
1670 struct reenum *param = data;
1671 struct vol *vol = param->vol;
1672 cnid_t did = param->did;
1675 if ( lstat(de->d_name, &path.st) < 0 )
1678 /* update or add to cnid */
1679 aint = cnid_add(vol->v_cdb, &path.st, did, de->d_name, strlen(de->d_name), 0); /* ignore errors */
1684 /* --------------------
1685 * Ok the db is out of synch with the dir.
1686 * but if it's a deleted file we don't want to do it again and again.
1689 reenumerate_id(struct vol *vol, char *name, struct dir *dir)
1695 if (vol->v_cdb == NULL) {
1699 /* FIXME use of_statdir ? */
1700 if (lstat(name, &st)) {
1704 if (dirreenumerate(dir, &st)) {
1705 /* we already did it once and the dir haven't been modified */
1706 return dir->d_offcnt;
1710 data.did = dir->d_did;
1711 if ((ret = for_each_dirent(vol, name, reenumerate_loop, (void *)&data)) >= 0) {
1712 setdiroffcnt(curdir, &st, ret);
1713 dir->d_flags |= DIRF_CNID;
1719 /* ------------------------------
1720 resolve a file id */
1721 int afp_resolveid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
1730 uint16_t vid, bitmap;
1732 static char buffer[12 + MAXPATHLEN + 1];
1733 int len = 12 + MAXPATHLEN + 1;
1738 memcpy(&vid, ibuf, sizeof(vid));
1739 ibuf += sizeof(vid);
1741 if (NULL == ( vol = getvolbyvid( vid )) ) {
1742 return( AFPERR_PARAM);
1745 if (vol->v_cdb == NULL || !(vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1749 memcpy(&id, ibuf, sizeof( id ));
1754 /* some MacOS versions after a catsearch do a *lot* of afp_resolveid with 0 */
1758 if (NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, len)) ) {
1759 return AFPERR_NOID; /* was AFPERR_BADID, but help older Macs */
1762 if (NULL == ( dir = dirlookup( vol, id )) ) {
1763 return AFPERR_NOID; /* idem AFPERR_PARAM */
1765 if (movecwd(vol, dir) < 0) {
1769 return AFPERR_ACCESS;
1773 return AFPERR_PARAM;
1777 memset(&path, 0, sizeof(path));
1778 path.u_name = upath;
1779 if ( of_stat(&path) < 0 ) {
1781 /* with nfs and our working directory is deleted */
1782 if (errno == ESTALE) {
1786 if ( errno == ENOENT && !retry) {
1787 /* cnid db is out of sync, reenumerate the directory and update ids */
1788 reenumerate_id(vol, ".", dir);
1796 return AFPERR_ACCESS;
1800 return AFPERR_PARAM;
1804 /* directories are bad */
1805 if (S_ISDIR(path.st.st_mode)) {
1806 /* OS9 and OSX don't return the same error code */
1807 return (obj->afp_version >=30)?AFPERR_NOID:AFPERR_BADTYPE;
1810 memcpy(&bitmap, ibuf, sizeof(bitmap));
1811 bitmap = ntohs( bitmap );
1812 if (NULL == (path.m_name = utompath(vol, upath, cnid, utf8_encoding(obj)))) {
1816 if (AFP_OK != (err = getfilparams(obj, vol, bitmap, &path , curdir,
1817 rbuf + sizeof(bitmap), &buflen, 0))) {
1820 *rbuflen = buflen + sizeof(bitmap);
1821 memcpy(rbuf, ibuf, sizeof(bitmap));
1826 /* ------------------------------ */
1827 int afp_deleteid(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1837 static char buffer[12 + MAXPATHLEN + 1];
1838 int len = 12 + MAXPATHLEN + 1;
1843 memcpy(&vid, ibuf, sizeof(vid));
1844 ibuf += sizeof(vid);
1846 if (NULL == ( vol = getvolbyvid( vid )) ) {
1847 return( AFPERR_PARAM);
1850 if (vol->v_cdb == NULL || !(vol->v_cdb->flags & CNID_FLAG_PERSISTENT)) {
1854 if (vol->v_flags & AFPVOL_RO)
1855 return AFPERR_VLOCK;
1857 memcpy(&id, ibuf, sizeof( id ));
1861 if (NULL == (upath = cnid_resolve(vol->v_cdb, &id, buffer, len)) ) {
1865 if (NULL == ( dir = dirlookup( vol, id )) ) {
1866 if (afp_errno == AFPERR_NOOBJ) {
1870 return( AFPERR_PARAM );
1874 if ((movecwd(vol, dir) < 0) || (lstat(upath, &st) < 0)) {
1878 return AFPERR_ACCESS;
1883 /* still try to delete the id */
1887 return AFPERR_PARAM;
1890 else if (S_ISDIR(st.st_mode)) /* directories are bad */
1891 return AFPERR_BADTYPE;
1894 if (cnid_delete(vol->v_cdb, fileid)) {
1897 return AFPERR_VLOCK;
1900 return AFPERR_ACCESS;
1902 return AFPERR_PARAM;
1909 /* ------------------------------ */
1910 static struct adouble *find_adouble(const AFPObj *obj, struct vol *vol, struct path *path, struct ofork **of, struct adouble *adp)
1914 if (path->st_errno) {
1915 switch (path->st_errno) {
1917 afp_errno = AFPERR_NOID;
1921 afp_errno = AFPERR_ACCESS;
1924 afp_errno = AFPERR_PARAM;
1929 /* we use file_access both for legacy Mac perm and
1930 * for unix privilege, rename will take care of folder perms
1932 if (file_access(obj, vol, path, OPENACC_WR ) < 0) {
1933 afp_errno = AFPERR_ACCESS;
1937 if ((*of = of_findname(path))) {
1938 /* reuse struct adouble so it won't break locks */
1942 ret = ad_open(adp, path->u_name, ADFLAGS_HF | ADFLAGS_RDWR);
1944 if ( !ret && ad_reso_fileno(adp) != -1 && !(adp->ad_resource_fork.adf_flags & ( O_RDWR | O_WRONLY))) {
1946 * The user must have the Read & Write privilege for both files in order to use this command.
1948 ad_close(adp, ADFLAGS_HF);
1949 afp_errno = AFPERR_ACCESS;
1956 #define APPLETEMP ".AppleTempXXXXXX"
1958 int afp_exchangefiles(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1960 struct stat srcst, destst;
1962 struct dir *dir, *sdir;
1963 char *spath, temp[17], *p;
1964 char *supath, *upath;
1969 struct adouble *adsp = NULL;
1970 struct adouble *addp = NULL;
1971 struct ofork *s_of = NULL;
1972 struct ofork *d_of = NULL;
1982 memcpy(&vid, ibuf, sizeof(vid));
1983 ibuf += sizeof(vid);
1985 if (NULL == ( vol = getvolbyvid( vid )) ) {
1986 return( AFPERR_PARAM);
1989 if ((vol->v_flags & AFPVOL_RO))
1990 return AFPERR_VLOCK;
1992 /* source and destination dids */
1993 memcpy(&sid, ibuf, sizeof(sid));
1994 ibuf += sizeof(sid);
1995 memcpy(&did, ibuf, sizeof(did));
1996 ibuf += sizeof(did);
1999 if (NULL == (dir = dirlookup( vol, sid )) ) {
2000 return afp_errno; /* was AFPERR_PARAM */
2003 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
2004 return get_afp_errno(AFPERR_NOOBJ);
2007 if ( path_isadir(path) ) {
2008 return AFPERR_BADTYPE; /* it's a dir */
2011 /* save some stuff */
2014 spath = obj->oldtmp;
2015 supath = obj->newtmp;
2016 strcpy(spath, path->m_name);
2017 strcpy(supath, path->u_name); /* this is for the cnid changing */
2018 p = absupath( vol, sdir, supath);
2020 /* pathname too long */
2021 return AFPERR_PARAM ;
2025 if (!(adsp = find_adouble(obj, vol, path, &s_of, &ads))) {
2029 /* ***** from here we may have resource fork open **** */
2031 /* look for the source cnid. if it doesn't exist, don't worry about
2033 sid = cnid_lookup(vol->v_cdb, &srcst, sdir->d_did, supath,slen = strlen(supath));
2035 if (NULL == ( dir = dirlookup( vol, did )) ) {
2036 err = afp_errno; /* was AFPERR_PARAM */
2037 goto err_exchangefile;
2040 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
2041 err = get_afp_errno(AFPERR_NOOBJ);
2042 goto err_exchangefile;
2045 if ( path_isadir(path) ) {
2046 err = AFPERR_BADTYPE;
2047 goto err_exchangefile;
2050 /* FPExchangeFiles is the only call that can return the SameObj
2052 if ((curdir == sdir) && strcmp(spath, path->m_name) == 0) {
2053 err = AFPERR_SAMEOBJ;
2054 goto err_exchangefile;
2058 if (!(addp = find_adouble(obj, vol, path, &d_of, &add))) {
2060 goto err_exchangefile;
2064 /* they are not on the same device and at least one is open
2065 * FIXME broken for for crossdev and adouble v2
2068 crossdev = (srcst.st_dev != destst.st_dev);
2069 if (/* (d_of || s_of) && */ crossdev) {
2071 goto err_exchangefile;
2074 /* look for destination id. */
2075 upath = path->u_name;
2076 did = cnid_lookup(vol->v_cdb, &destst, curdir->d_did, upath, dlen = strlen(upath));
2078 /* construct a temp name.
2079 * NOTE: the temp file will be in the dest file's directory. it
2080 * will also be inaccessible from AFP. */
2081 memcpy(temp, APPLETEMP, sizeof(APPLETEMP));
2082 if (!mktemp(temp)) {
2084 goto err_exchangefile;
2088 /* FIXME we need to close fork for copy, both s_of and d_of are null */
2089 ad_close(adsp, ADFLAGS_HF);
2090 ad_close(addp, ADFLAGS_HF);
2093 /* now, quickly rename the file. we error if we can't. */
2094 if ((err = renamefile(vol, -1, p, temp, temp, adsp)) != AFP_OK)
2095 goto err_exchangefile;
2096 of_rename(vol, s_of, sdir, spath, curdir, temp);
2098 /* rename destination to source */
2099 if ((err = renamefile(vol, -1, upath, p, spath, addp)) != AFP_OK)
2100 goto err_src_to_tmp;
2101 of_rename(vol, d_of, curdir, path->m_name, sdir, spath);
2103 /* rename temp to destination */
2104 if ((err = renamefile(vol, -1, temp, upath, path->m_name, adsp)) != AFP_OK)
2105 goto err_dest_to_src;
2106 of_rename(vol, s_of, curdir, temp, curdir, path->m_name);
2108 /* id's need switching. src -> dest and dest -> src.
2109 * we need to re-stat() if it was a cross device copy.
2112 cnid_delete(vol->v_cdb, sid);
2114 cnid_delete(vol->v_cdb, did);
2116 if ((did && ( (crossdev && lstat( upath, &srcst) < 0) ||
2117 cnid_update(vol->v_cdb, did, &srcst, curdir->d_did,upath, dlen) < 0))
2119 (sid && ( (crossdev && lstat(p, &destst) < 0) ||
2120 cnid_update(vol->v_cdb, sid, &destst, sdir->d_did,supath, slen) < 0))
2125 err = AFPERR_ACCESS;
2130 goto err_temp_to_dest;
2133 /* here we need to reopen if crossdev */
2134 if (sid && ad_setid(addp, destst.st_dev, destst.st_ino, sid, sdir->d_did, vol->v_stamp))
2139 if (did && ad_setid(adsp, srcst.st_dev, srcst.st_ino, did, curdir->d_did, vol->v_stamp))
2144 /* change perms, src gets dest perm and vice versa */
2149 * we need to exchange ACL entries as well
2151 /* exchange_acls(vol, p, upath); */
2156 path->m_name = NULL;
2157 path->u_name = upath;
2159 setfilunixmode(vol, path, destst.st_mode);
2160 setfilowner(vol, destst.st_uid, destst.st_gid, path);
2167 setfilunixmode(vol, path, srcst.st_mode);
2168 setfilowner(vol, srcst.st_uid, srcst.st_gid, path);
2173 goto err_exchangefile;
2175 /* all this stuff is so that we can unwind a failed operation
2178 /* rename dest to temp */
2179 renamefile(vol, -1, upath, temp, temp, adsp);
2180 of_rename(vol, s_of, curdir, upath, curdir, temp);
2183 /* rename source back to dest */
2184 renamefile(vol, -1, p, upath, path->m_name, addp);
2185 of_rename(vol, d_of, sdir, spath, curdir, path->m_name);
2188 /* rename temp back to source */
2189 renamefile(vol, -1, temp, p, spath, adsp);
2190 of_rename(vol, s_of, curdir, temp, sdir, spath);
2193 if ( !s_of && adsp && ad_meta_fileno(adsp) != -1 ) { /* META */
2194 ad_close(adsp, ADFLAGS_HF);
2196 if ( !d_of && addp && ad_meta_fileno(addp) != -1 ) {/* META */
2197 ad_close(addp, ADFLAGS_HF);
2201 if ((cached = dircache_search_by_did(vol, sid)) != NULL)
2202 (void)dir_remove(vol, cached);
2203 if ((cached = dircache_search_by_did(vol, did)) != NULL)
2204 (void)dir_remove(vol, cached);