2 * $Id: directory.c,v 1.131.2.9 2010-02-04 14:34:31 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
17 #include <sys/param.h>
23 #include <atalk/adouble.h>
24 #include <atalk/vfs.h>
25 #include <atalk/afp.h>
26 #include <atalk/util.h>
27 #include <atalk/cnid.h>
28 #include <atalk/logger.h>
29 #include <atalk/uuid.h>
30 #include <atalk/unix.h>
31 #include <atalk/bstrlib.h>
32 #include <atalk/bstradd.h>
34 #include "directory.h"
46 #ifdef HAVE_NFSv4_ACLS
47 extern void addir_inherit_acl(const struct vol *vol);
51 * FIXMEs, loose ends after the dircache rewrite:
52 * o dircache aging, place dirlookup'ed dirs on front of queue ??
53 * o merge dircache_search_by_name and dir_add ??
54 * o case-insensitivity is gone from cname
55 * o catsearch doesn't work, see FIXMEs in catsearch.c
56 * o curdir per volume caching is gone
57 * o directory offspring count calculation probably broken
58 * o doesn't work with CNID backend last and the like,
59 * CNID backend must support persistent CNIDs.
63 /*******************************************************************************************
65 ******************************************************************************************/
68 struct dir rootParent = {
69 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
70 NULL, NULL, 0, 0, /* qidx_node, d_ofork, ctime, d_flags */
71 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
73 struct dir *curdir = &rootParent;
74 struct path Cur_Path = {
80 0, /* stat is not set */
84 /*******************************************************************************************
86 ******************************************************************************************/
89 /* -------------------------
90 appledouble mkdir afp error code.
92 static int netatalk_mkdir(const char *name)
94 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
97 return( AFPERR_NOOBJ );
99 return( AFPERR_VLOCK );
102 return( AFPERR_ACCESS );
104 return( AFPERR_EXIST );
107 return( AFPERR_DFULL );
109 return( AFPERR_PARAM );
115 /* ------------------- */
116 static int deletedir(char *dir)
118 char path[MAXPATHLEN + 1];
126 if ((len = strlen(dir)) +2 > sizeof(path))
130 if ((dp = opendir(dir)) == NULL)
136 remain = sizeof(path) -len -1;
137 while ((de = readdir(dp)) && err == AFP_OK) {
138 /* skip this and previous directory */
139 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
142 if (strlen(de->d_name) > remain) {
146 strcpy(path + len, de->d_name);
147 if (stat(path, &st)) {
150 if (S_ISDIR(st.st_mode)) {
151 err = deletedir(path);
153 err = netatalk_unlink(path);
158 /* okay. the directory is empty. delete it. note: we already got rid
161 err = netatalk_rmdir(dir);
166 /* do a recursive copy. */
167 static int copydir(const struct vol *vol, char *src, char *dst)
169 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
178 /* doesn't exist or the path is too long. */
179 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
180 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
181 ((dp = opendir(src)) == NULL))
184 /* try to create the destination directory */
185 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
190 /* set things up to copy */
194 srem = sizeof(spath) - slen -1;
199 drem = sizeof(dpath) - dlen -1;
202 while ((de = readdir(dp))) {
203 /* skip this and previous directory */
204 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
207 if (strlen(de->d_name) > srem) {
211 strcpy(spath + slen, de->d_name);
213 if (stat(spath, &st) == 0) {
214 if (strlen(de->d_name) > drem) {
218 strcpy(dpath + dlen, de->d_name);
220 if (S_ISDIR(st.st_mode)) {
221 if (AFP_OK != (err = copydir(vol, spath, dpath)))
223 } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL, NULL))) {
227 /* keep the same time stamp. */
228 ut.actime = ut.modtime = st.st_mtime;
234 /* keep the same time stamp. */
235 if (stat(src, &st) == 0) {
236 ut.actime = ut.modtime = st.st_mtime;
245 /* ---------------------
246 * is our cached offspring count valid?
248 static int diroffcnt(struct dir *dir, struct stat *st)
250 return st->st_ctime == dir->ctime;
253 /* --------------------- */
254 static int invisible_dots(const struct vol *vol, const char *name)
256 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
259 /* ------------------ */
260 static int set_dir_errors(struct path *path, const char *where, int err)
265 return AFPERR_ACCESS;
269 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
274 * @brief Convert name in client encoding to server encoding
276 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
277 * This only gets called from cname().
279 * @returns 0 on success, -1 on error
281 * @note If the passed ret->m_name is mangled, we'll demangle it
283 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
285 static char temp[ MAXPATHLEN + 1];
289 if (afp_version >= 30) {
291 if (dir->d_did == DIRDID_ROOT_PARENT) {
293 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
294 * So we compare it with the longname from the current volume and if they match
295 * we overwrite the requested path with the utf8 volume name so that the following
298 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
299 if (strcasecmp(ret->m_name, temp) == 0)
300 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
303 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
304 afp_errno = AFPERR_PARAM;
307 strcpy(ret->m_name, temp);
311 /* check for OS X mangled filename :( */
312 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
313 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
314 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
316 if (t != ret->m_name) {
318 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
319 * flags weren't the same
321 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
322 /* at last got our view of mac name */
323 strcpy(ret->m_name, t);
326 } /* afp_version >= 30 */
328 /* If we haven't got it by now, get it */
329 if (ret->u_name == NULL) {
330 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
331 afp_errno = AFPERR_PARAM;
340 * @brief Build struct path from struct dir
342 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
343 * 1. move cwd into parent dir (we're often already there, but not always)
345 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
348 * it's tricky: movecwd failed some of dir path are not there anymore.
349 * FIXME: Is it true with other errors?
351 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
357 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 )
360 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
361 if (dir->d_m_name == dir->d_u_name) {
362 ret->u_name = ret->m_name;
364 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
365 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
371 ret->st_errno = EACCES;
374 LOG(log_debug, logtype_afpd, "cname(AFPERR_ACCESS:'%s') {path-from-dir: curdir:'%s', path:'%s'}",
375 cfrombstring(dir->d_fullpath),
376 cfrombstring(curdir->d_fullpath),
382 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 )
385 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
386 if (dir->d_m_name == dir->d_u_name) {
387 ret->u_name = ret->m_name;
389 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
390 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
395 ret->st_errno = ENOENT;
398 dir_remove(vol, dir);
405 /* DEADC0DE: never get here */
410 /*********************************************************************************************
412 ********************************************************************************************/
414 int get_afp_errno(const int param)
416 if (afp_errno != AFPERR_DID1)
422 * @brief Resolve a DID
424 * Resolve a DID, allocate a struct dir for it
425 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
426 * 2. Check if the DID is in the cache.
427 * 3. If it's not in the cache resolve it via the database.
428 * 4. Build complete server-side path to the dir.
429 * 5. Check if it exists and is a directory.
430 * 6. Create the struct dir and populate it.
431 * 7. Add it to the cache.
433 * @param vol (r) pointer to struct vol
434 * @param did (r) DID to resolve
436 * @returns pointer to struct dir
438 * @note FIXME: OSX calls it with bogus id, ie file ID not folder ID,
439 * and we are really bad in this case.
441 struct dir *dirlookup(const struct vol *vol, cnid_t did)
443 static char buffer[12 + MAXPATHLEN + 1];
444 struct bstrList *pathlist = NULL;
445 bstring fullpath = NULL;
447 struct dir *ret = NULL;
451 int buflen = 12 + MAXPATHLEN + 1;
455 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {start}", ntohl(did));
457 /* check for did 0, 1 and 2 */
458 if (did == 0 || vol == NULL) { /* 1 */
459 afp_errno = AFPERR_PARAM;
461 } else if (did == DIRDID_ROOT_PARENT) {
462 rootParent.d_vid = vol->v_vid;
463 return (&rootParent);
464 } else if (did == DIRDID_ROOT) {
468 /* Search the cache */
469 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2 */
470 if (lstat(cfrombstring(ret->d_fullpath), &st) != 0) {
471 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {lstat: %s}", ntohl(did), strerror(errno));
475 /* It's not there anymore, so remove it */
476 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {calling dir_remove()}", ntohl(did));
477 dir_remove(vol, ret);
478 afp_errno = AFPERR_NOOBJ;
489 utf8 = utf8_encoding();
490 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
492 /* Create list for path elements, request 16 list elements for now*/
493 if ((pathlist = bstListCreateMin(16)) == NULL) { /* 4 */
494 LOG(log_error, logtype_afpd, "dirlookup(did: %u): OOM: %s", ntohl(did), strerror(errno));
498 /* Get it from the database */
500 if ( (upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
501 afp_errno = AFPERR_NOOBJ;
507 /* construct path, copy already found uname to path element list*/
508 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
509 afp_errno = AFPERR_MISC;
514 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {%u, %s}", ntohl(did), ntohl(pdid), upath);
516 /* The stuff that follows is for building the full path to the directory */
518 /* work upwards until we reach volume root */
519 while (cnid != DIRDID_ROOT) {
520 /* construct path, copy already found uname to path element list*/
521 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
522 afp_errno = AFPERR_MISC;
528 if ((upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
529 afp_errno = AFPERR_NOOBJ;
535 if ((bstrListPush(pathlist, bfromcstr(vol->v_path))) != BSTR_OK) { /* 4 */
536 afp_errno = AFPERR_MISC;
541 if ((fullpath = bjoinInv(pathlist, bfromcstr("/"))) == NULL) { /* 4 */
542 afp_errno = AFPERR_MISC;
546 /* Finished building the fullpath */
548 /* stat it and check if it's a dir */
549 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstring(fullpath));
551 if (stat(cfrombstring(fullpath), &st) != 0) { /* 5a */
554 afp_errno = AFPERR_NOOBJ;
558 afp_errno = AFPERR_ACCESS;
562 afp_errno = AFPERR_MISC;
567 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
568 afp_errno = AFPERR_BADTYPE;
574 /* Get macname from unix name */
575 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
576 afp_errno = AFPERR_NOOBJ;
581 /* Create struct dir */
582 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath)) == NULL) { /* 6 */
583 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
588 /* Add it to the cache only if it's a dir */
589 if (dircache_add(ret) != 0) { /* 7 */
594 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {end: did:%u, path:'%s'}",
595 ntohl(did), ntohl(pdid), cfrombstring(ret->d_fullpath));
599 bstrListDestroy(pathlist);
602 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
603 ntohl(did), AfpErr2name(afp_errno));
614 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
616 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
621 static u_int32_t did = 0;
622 static char cname[MAXPATHLEN];
623 static char lname[MAXPATHLEN];
624 ucs2_t u2_path[MAXPATHLEN];
625 ucs2_t u2_dename[MAXPATHLEN];
626 char *tmp, *savepath;
628 if (!(vol->v_flags & AFPVOL_CASEINSEN))
631 if (veto_file(ENUMVETO, path->u_name))
634 savepath = path->u_name;
636 /* very simple cache */
637 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
638 path->u_name = cname;
640 if (of_stat( path ) == 0 ) {
643 /* something changed, we cannot stat ... */
647 if (NULL == ( dp = opendir( "." )) ) {
648 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
653 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
654 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
655 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
657 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
659 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
660 if (NULL == check_dirent(vol, de->d_name))
663 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
666 if (strcasecmp_w( u2_path, u2_dename) == 0) {
668 strlcpy(cname, de->d_name, sizeof(cname));
669 path->u_name = cname;
671 if (of_stat( path ) == 0 ) {
672 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
673 strlcpy(lname, tmp, sizeof(lname));
686 /* invalidate cache */
689 path->u_name = savepath;
691 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
697 * @brief Construct struct dir
699 * Construct struct dir from parameters.
701 * @param m_name (r) directory name in UTF8-dec
702 * @param u_name (r) directory name in server side encoding
703 * @param vol (r) pointer to struct vol
704 * @param pdid (r) Parent CNID
705 * @param did (r) CNID
706 * @param fullpath (r) Full unix path to dir
708 * @returns pointer to new struct dir or NULL on error
710 * @note Most of the time mac name and unix name are the same.
712 struct dir *dir_new(const char *m_name,
714 const struct vol *vol,
721 dir = (struct dir *) calloc(1, sizeof( struct dir ));
725 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
730 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
733 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
734 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
735 dir->d_m_name_ucs2 = NULL;
738 if (m_name == u_name || !strcmp(m_name, u_name)) {
739 dir->d_u_name = dir->d_m_name;
741 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
742 bdestroy(dir->d_m_name);
749 dir->d_vid = vol->v_vid;
750 dir->d_fullpath = path;
755 * @brief Free a struct dir and all its members
757 * @param (rw) pointer to struct dir
759 void dir_free(struct dir *dir)
761 if (dir->d_u_name != dir->d_m_name) {
762 bdestroy(dir->d_u_name);
764 if (dir->d_m_name_ucs2)
765 free(dir->d_m_name_ucs2);
766 bdestroy(dir->d_m_name);
767 bdestroy(dir->d_fullpath);
772 * @brief Create struct dir from struct path
774 * Create a new struct dir from struct path. Then add it to the cache.
775 * The caller must have assured that the dir is not already in the cache,
777 * 1. Open adouble file, get CNID from it.
778 * 2. Search the database, hinting with the CNID from (1).
779 * 3. Build fullpath and create struct dir.
780 * 4. Add it to the cache.
782 * @param vol (r) pointer to struct vol
783 * @param dir (r) pointer to parrent directory
784 * @param path (rw) pointer to struct path with valid path->u_name
785 * @param len (r) strlen of path->u_name
787 * @returns Pointer to new struct dir or NULL on error.
789 * @note Function also assigns path->m_name from path->u_name.
791 struct dir *dir_add(const struct vol *vol, const struct dir *dir, struct path *path, int len)
794 struct dir *cdir = NULL;
797 struct adouble *adp = NULL;
805 if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name))) != NULL) {
806 /* there's a stray entry in the dircache */
807 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
808 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
809 ntohl(cdir->d_did), cfrombstring(dir->d_fullpath));
810 if (dir_remove(vol, cdir) != 0) {
816 /* get_id needs adp for reading CNID from adouble file */
817 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
818 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
822 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
828 ad_close_metadata(adp);
830 /* Get macname from unixname */
831 if (path->m_name == NULL) {
832 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
839 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
840 || (bconchar(fullpath, '/') != BSTR_OK)
841 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
842 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
847 /* Allocate and initialize struct dir */
848 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath)) == NULL) { /* 3 */
853 if ((dircache_add(cdir)) != 0) { /* 4 */
854 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstring(fullpath));
860 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
861 cfrombstring(dir->d_u_name), path->u_name, err);
864 ad_close_metadata(adp);
865 if (!cdir && fullpath)
872 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
873 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
874 ntohl(cdir->d_did), cfrombstring(cdir->d_fullpath));
881 * @brief Remove a dir from a cache and free it and any ressources with it
883 * @param (r) pointer to struct vol
884 * @param (rw) pointer to struct dir
886 int dir_remove(const struct vol *vol, struct dir *dir)
891 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
895 if (movecwd(vol, vol->v_root) < 0) {
896 LOG(log_error, logtype_afpd, "dir_remove: can't chdir to : %s", vol->v_root);
901 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
902 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
904 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
911 * @brief Modify a struct dir, adjust cache
913 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
914 * If given new_uname == new_mname, new_uname will point to new_mname.
916 * @param vol (r) pointer to struct vol
917 * @param dir (rw) pointer to struct dir
918 * @param pdid (r) new parent DID
919 * @param did (r) new DID
920 * @param new_mname (r) new mac-name
921 * @param new_uname (r) new unix-name
923 int dir_modify(const struct vol *vol,
927 const char *new_mname,
928 const char *new_uname,
929 bstring pdir_fullpath)
933 /* Remove it from the cache */
934 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
942 /* free uname if it's not the same as mname */
943 if (dir->d_m_name != dir->d_u_name)
944 bdestroy(dir->d_u_name);
946 if (new_uname == NULL)
947 new_uname = new_mname;
949 /* assign new name */
950 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
951 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
955 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
956 dir->d_u_name = dir->d_m_name;
958 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
959 LOG(log_error, logtype_afpd, "renamedir: bassigncstr: %s", strerror(errno) );
966 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
968 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
970 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
974 if (dir->d_m_name_ucs2)
975 free(dir->d_m_name_ucs2);
976 if ((size_t)-1 == convert_string_allocate((utf8_encoding())?CH_UTF8_MAC:vol->v_maccharset, CH_UCS2, dir->d_m_name, -1, (char**)&dir->d_m_name_ucs2))
977 dir->d_m_name_ucs2 = NULL;
979 /* Re-add it to the cache */
980 if ((dircache_add(dir)) != 0) {
989 * @brief Resolve a catalog node name path
991 * If it's a filename:
992 * 1. compute unix name
993 * 2. stat the file, storing struct stat or errno in struct path
994 * 3. cwd (and curdir) is filename parent directory
995 * 4. return path with with filename
998 * 5. search the dircache
999 * 6. if not in the cache:
1000 * 7. compute unix name
1001 * 8. stat the dir, storing struct stat or errno in struct path
1003 * 10. if chdir failed, return path with dirname
1004 * cwd is dir parent directory
1005 * if chdir succeeded, add to dircache
1006 * return path with "" and "."
1008 * 11. else in the cache:
1009 * 12. stat the dir, storing struct stat or errno in struct path
1011 * 14. if chdir failed
1014 * if not last path part, return NULL
1019 curdir: dir parent directory
1024 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1026 static char path[ MAXPATHLEN + 1];
1027 static struct path ret;
1037 LOG(log_maxdebug, logtype_afpd, "came('%s'): {start}", cfrombstring(dir->d_fullpath));
1040 afp_errno = AFPERR_NOOBJ;
1041 memset(&ret, 0, sizeof(ret));
1043 switch (ret.m_type = *data) { /* path type */
1046 len = (unsigned char) *data++;
1048 if (afp_version >= 30) {
1054 if (afp_version >= 30) {
1056 memcpy(&hint, data, sizeof(hint));
1058 data += sizeof(hint);
1060 memcpy(&len16, data, sizeof(len16));
1066 /* else it's an error */
1068 afp_errno = AFPERR_PARAM;
1071 *cpath += len + size;
1076 if (movecwd(vol, dir) < 0 ) {
1077 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1078 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1080 return path_from_dir(vol, dir, &ret);
1088 * 1. single 0 -> delimiter
1089 * 2. additional 0 -> chdir(..)
1091 * a) name is a file, build struct path from it etc., exit while loop
1092 * b) name is a dir, add it to the dircache, chdir to it, continue
1094 if (*data == 0) { /* case 1 or 2 */
1097 while (len > 0 && *data == 0) { /* case 2 */
1098 /* chdir to parrent dir */
1099 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1101 if (movecwd( vol, dir ) < 0 ) {
1102 dir_remove(vol, dir);
1111 /* case 3: copy name from packet buffer to ret.m_name and process it */
1112 for ( p = path; *data != 0 && len > 0; len-- ) {
1114 if (p > &path[ MAXPATHLEN]) {
1115 afp_errno = AFPERR_PARAM;
1119 *p = 0; /* Terminate string */
1122 /* Get u_name from m_name */
1123 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) {
1124 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1128 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstring(dir->d_fullpath), ret.u_name);
1130 /* Prevent access to our special folders like .AppleDouble */
1131 if (check_name(vol, ret.u_name)) {
1132 /* the name is illegal */
1133 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1134 afp_errno = AFPERR_PARAM;
1138 if (dir->d_did == DIRDID_ROOT_PARENT) {
1140 * Special case: CNID 1
1141 * root parent (did 1) has one child: the volume. Requests for did=1 with
1142 * some <name> must check against the volume name.
1144 if ((strcmp(cfrombstring(vol->v_root->d_m_name), ret.m_name)) == 0)
1150 * CNID != 1, eg. most of the times we take this way.
1151 * Now check if current path-part is a file or dir:
1152 * o if it's dir we have to step into it
1153 * o if it's a file we expect it to be the last part of the requested path
1154 * and thus call continue which should terminate the while loop because
1157 if (of_stat(&ret) != 0) {
1159 * ret.u_name doesn't exist, might be afp_createfile|dir
1160 * that means it should have been the last part
1163 /* it wasn't the last part, so we have a bogus path request */
1164 afp_errno = AFPERR_NOOBJ;
1168 * this will terminate clean in while (1) because len == 0,
1169 * probably afp_createfile|dir
1171 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1175 switch (ret.st.st_mode & S_IFMT) {
1177 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1179 /* it wasn't the last part, so we have a bogus path request */
1180 afp_errno = AFPERR_PARAM;
1183 continue; /* continues while loop */
1185 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1187 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1188 afp_errno = AFPERR_PARAM;
1191 continue; /* continues while loop */
1195 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1196 afp_errno = AFPERR_NODIR;
1200 /* Search the cache */
1201 int unamelen = strlen(ret.u_name);
1202 cdir = dircache_search_by_name(vol, dir, ret.u_name, unamelen);
1204 /* Not in cache, create one */
1205 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) {
1206 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1207 ntohl(dir->d_did), ret.u_name, getcwdpath());
1211 } /* if/else cnid==1 */
1213 /* Now chdir to the evaluated dir */
1214 if (movecwd( vol, cdir ) < 0 ) {
1215 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1216 cfrombstring(curdir->d_fullpath), cfrombstring(cdir->d_fullpath), strerror(errno));
1218 return path_from_dir(vol, cdir, &ret);
1223 ret.m_name[0] = 0; /* so we later know last token was a dir */
1226 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1227 afp_errno = AFPERR_DID1;
1231 if (ret.m_name[0] == 0) {
1232 /* Last part was a dir */
1233 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1237 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1238 cfrombstring(dir->d_fullpath),
1239 cfrombstring(curdir->d_fullpath),
1246 * @brief chdir() to dir
1248 * @param vol (r) pointer to struct vol
1249 * @param dir (r) pointer to struct dir
1251 * @returns 0 on success, -1 on error with afp_errno set appropiately
1253 int movecwd(const struct vol *vol, struct dir *dir)
1260 LOG(log_maxdebug, logtype_afpd, "movecwd(curdir:'%s', cwd:'%s')",
1261 cfrombstring(curdir->d_fullpath), getcwdpath());
1265 if (dir->d_did == DIRDID_ROOT_PARENT) {
1266 curdir = &rootParent;
1270 LOG(log_debug, logtype_afpd, "movecwd(did:%u, '%s')", ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1272 if ( chdir(cfrombstring(dir->d_fullpath)) < 0 ) {
1273 LOG(log_debug, logtype_afpd, "movecwd('%s'): %s", cfrombstring(dir->d_fullpath), strerror(errno));
1277 afp_errno = AFPERR_ACCESS;
1280 afp_errno = AFPERR_NOOBJ;
1290 * We can't use unix file's perm to support Apple's inherited protection modes.
1291 * If we aren't the file's owner we can't change its perms when moving it and smb
1292 * nfs,... don't even try.
1294 #define AFP_CHECK_ACCESS
1296 int check_access(char *path, int mode)
1298 #ifdef AFP_CHECK_ACCESS
1306 accessmode(p, &ma, curdir, NULL);
1307 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1309 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1315 /* --------------------- */
1316 int file_access(struct path *path, int mode)
1320 accessmode(path->u_name, &ma, curdir, &path->st);
1321 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1323 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1329 /* --------------------- */
1330 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1332 dir->offcnt = count;
1333 dir->ctime = st->st_ctime;
1334 dir->d_flags &= ~DIRF_CNID;
1338 /* ---------------------
1339 * is our cached also for reenumerate id?
1341 int dirreenumerate(struct dir *dir, struct stat *st)
1343 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1346 /* ------------------------------
1348 (name, dir) with curdir:name == dir, from afp_enumerate
1351 int getdirparams(const struct vol *vol,
1352 u_int16_t bitmap, struct path *s_path,
1354 char *buf, size_t *buflen )
1358 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1359 int bit = 0, isad = 0;
1365 struct stat *st = &s_path->st;
1366 char *upath = s_path->u_name;
1368 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1369 (1 << DIRPBIT_CDATE) |
1370 (1 << DIRPBIT_MDATE) |
1371 (1 << DIRPBIT_BDATE) |
1372 (1 << DIRPBIT_FINFO)))) {
1374 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1375 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1377 if (ad.ad_md->adf_flags & O_CREAT) {
1378 /* We just created it */
1379 ad_setname(&ad, s_path->m_name);
1394 while ( bitmap != 0 ) {
1395 while (( bitmap & 1 ) == 0 ) {
1403 ad_getattr(&ad, &ashort);
1404 } else if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1405 ashort = htons(ATTRBIT_INVISIBLE);
1408 ashort |= htons(ATTRBIT_SHARED);
1409 memcpy( data, &ashort, sizeof( ashort ));
1410 data += sizeof( ashort );
1414 memcpy( data, &pdid, sizeof( pdid ));
1415 data += sizeof( pdid );
1416 LOG(log_debug, logtype_afpd, "metadata('%s'): Parent DID: %u",
1417 s_path->u_name, ntohl(pdid));
1420 case DIRPBIT_CDATE :
1421 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1422 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1423 memcpy( data, &aint, sizeof( aint ));
1424 data += sizeof( aint );
1427 case DIRPBIT_MDATE :
1428 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1429 memcpy( data, &aint, sizeof( aint ));
1430 data += sizeof( aint );
1433 case DIRPBIT_BDATE :
1434 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1435 aint = AD_DATE_START;
1436 memcpy( data, &aint, sizeof( aint ));
1437 data += sizeof( aint );
1440 case DIRPBIT_FINFO :
1442 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1443 } else { /* no appledouble */
1444 memset( data, 0, 32 );
1445 /* set default view -- this also gets done in ad_open() */
1446 ashort = htons(FINDERINFO_CLOSEDVIEW);
1447 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1449 /* dot files are by default visible */
1450 if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1451 ashort = htons(FINDERINFO_INVISIBLE);
1452 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1458 case DIRPBIT_LNAME :
1459 if (dir->d_m_name) /* root of parent can have a null name */
1462 memset(data, 0, sizeof(u_int16_t));
1463 data += sizeof( u_int16_t );
1466 case DIRPBIT_SNAME :
1467 memset(data, 0, sizeof(u_int16_t));
1468 data += sizeof( u_int16_t );
1472 memcpy( data, &dir->d_did, sizeof( aint ));
1473 data += sizeof( aint );
1474 LOG(log_debug, logtype_afpd, "metadata('%s'): DID: %u",
1475 s_path->u_name, ntohl(dir->d_did));
1478 case DIRPBIT_OFFCNT :
1480 /* this needs to handle current directory access rights */
1481 if (diroffcnt(dir, st)) {
1482 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1484 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1485 setdiroffcnt(dir, st, ret);
1486 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1488 ashort = htons( ashort );
1489 memcpy( data, &ashort, sizeof( ashort ));
1490 data += sizeof( ashort );
1494 aint = htonl(st->st_uid);
1495 memcpy( data, &aint, sizeof( aint ));
1496 data += sizeof( aint );
1500 aint = htonl(st->st_gid);
1501 memcpy( data, &aint, sizeof( aint ));
1502 data += sizeof( aint );
1505 case DIRPBIT_ACCESS :
1506 accessmode( upath, &ma, dir , st);
1508 *data++ = ma.ma_user;
1509 *data++ = ma.ma_world;
1510 *data++ = ma.ma_group;
1511 *data++ = ma.ma_owner;
1514 /* Client has requested the ProDOS information block.
1515 Just pass back the same basic block for all
1516 directories. <shirsch@ibm.net> */
1517 case DIRPBIT_PDINFO :
1518 if (afp_version >= 30) { /* UTF8 name */
1519 utf8 = kTextEncodingUTF8;
1520 if (dir->d_m_name) /* root of parent can have a null name */
1523 memset(data, 0, sizeof(u_int16_t));
1524 data += sizeof( u_int16_t );
1526 memcpy(data, &aint, sizeof( aint ));
1527 data += sizeof( aint );
1529 else { /* ProDOS Info Block */
1532 ashort = htons( 0x0200 );
1533 memcpy( data, &ashort, sizeof( ashort ));
1534 data += sizeof( ashort );
1535 memset( data, 0, sizeof( ashort ));
1536 data += sizeof( ashort );
1540 case DIRPBIT_UNIXPR :
1541 aint = htonl(st->st_uid);
1542 memcpy( data, &aint, sizeof( aint ));
1543 data += sizeof( aint );
1544 aint = htonl(st->st_gid);
1545 memcpy( data, &aint, sizeof( aint ));
1546 data += sizeof( aint );
1549 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1550 memcpy( data, &aint, sizeof( aint ));
1551 data += sizeof( aint );
1553 accessmode( upath, &ma, dir , st);
1555 *data++ = ma.ma_user;
1556 *data++ = ma.ma_world;
1557 *data++ = ma.ma_group;
1558 *data++ = ma.ma_owner;
1563 ad_close_metadata( &ad );
1565 return( AFPERR_BITMAP );
1571 ashort = htons( data - buf );
1572 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1573 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, 0);
1575 if ( utf_nameoff ) {
1576 ashort = htons( data - buf );
1577 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1578 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, utf8);
1581 ad_close_metadata( &ad );
1583 *buflen = data - buf;
1587 /* ----------------------------- */
1588 int path_error(struct path *path, int error)
1590 /* - a dir with access error
1591 * - no error it's a file
1594 if (path_isadir(path))
1596 if (path->st_valid && path->st_errno)
1598 return AFPERR_BADTYPE ;
1601 /* ----------------------------- */
1602 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1607 u_int16_t vid, bitmap;
1613 memcpy( &vid, ibuf, sizeof( vid ));
1614 ibuf += sizeof( vid );
1616 if (NULL == ( vol = getvolbyvid( vid )) ) {
1617 return( AFPERR_PARAM );
1620 if (vol->v_flags & AFPVOL_RO)
1621 return AFPERR_VLOCK;
1623 memcpy( &did, ibuf, sizeof( did ));
1624 ibuf += sizeof( int );
1626 if (NULL == ( dir = dirlookup( vol, did )) ) {
1630 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1631 bitmap = ntohs( bitmap );
1632 ibuf += sizeof( bitmap );
1634 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1635 return get_afp_errno(AFPERR_NOOBJ);
1638 if ( *path->m_name != '\0' ) {
1639 rc = path_error(path, AFPERR_NOOBJ);
1640 /* maybe we are trying to set perms back */
1641 if (rc != AFPERR_ACCESS)
1646 * If ibuf is odd, make it even.
1648 if ((u_long)ibuf & 1 ) {
1652 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1653 setvoltime(obj, vol );
1659 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1661 * assume path == '\0' eg. it's a directory in canonical form
1663 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1675 u_int16_t ashort, bshort, oshort;
1677 int change_mdate = 0;
1678 int change_parent_mdate = 0;
1680 u_int16_t bitmap = d_bitmap;
1681 u_char finder_buf[32];
1684 u_int16_t upriv_bit = 0;
1687 upath = path->u_name;
1689 while ( bitmap != 0 ) {
1690 while (( bitmap & 1 ) == 0 ) {
1698 memcpy( &ashort, buf, sizeof( ashort ));
1699 buf += sizeof( ashort );
1701 case DIRPBIT_CDATE :
1703 memcpy(&cdate, buf, sizeof(cdate));
1704 buf += sizeof( cdate );
1706 case DIRPBIT_MDATE :
1707 memcpy(&newdate, buf, sizeof(newdate));
1708 buf += sizeof( newdate );
1710 case DIRPBIT_BDATE :
1712 memcpy(&bdate, buf, sizeof(bdate));
1713 buf += sizeof( bdate );
1715 case DIRPBIT_FINFO :
1717 memcpy( finder_buf, buf, 32 );
1720 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1721 change_parent_mdate = 1;
1722 memcpy( &owner, buf, sizeof(owner));
1723 buf += sizeof( owner );
1726 change_parent_mdate = 1;
1727 memcpy( &group, buf, sizeof( group ));
1728 buf += sizeof( group );
1730 case DIRPBIT_ACCESS :
1732 change_parent_mdate = 1;
1733 ma.ma_user = *buf++;
1734 ma.ma_world = *buf++;
1735 ma.ma_group = *buf++;
1736 ma.ma_owner = *buf++;
1737 mpriv = mtoumode( &ma ) | vol->v_dperm;
1738 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1739 err = set_dir_errors(path, "setdirmode", errno);
1743 /* Ignore what the client thinks we should do to the
1744 ProDOS information block. Skip over the data and
1745 report nothing amiss. <shirsch@ibm.net> */
1746 case DIRPBIT_PDINFO :
1747 if (afp_version < 30) {
1751 err = AFPERR_BITMAP;
1755 case DIRPBIT_UNIXPR :
1756 if (vol_unix_priv(vol)) {
1757 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1758 buf += sizeof( owner );
1759 memcpy( &group, buf, sizeof( group ));
1760 buf += sizeof( group );
1763 change_parent_mdate = 1;
1764 memcpy( &upriv, buf, sizeof( upriv ));
1765 buf += sizeof( upriv );
1766 upriv = ntohl (upriv) | vol->v_dperm;
1767 if (dir_rx_set(upriv)) {
1768 /* maybe we are trying to set perms back */
1769 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1771 err = set_dir_errors(path, "setdirunixmode", errno);
1782 err = AFPERR_BITMAP;
1790 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1792 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1794 * Check to see what we're trying to set. If it's anything
1795 * but ACCESS, UID, or GID, give an error. If it's any of those
1796 * three, we don't need the ad to be open, so just continue.
1798 * note: we also don't need to worry about mdate. also, be quiet
1799 * if we're using the noadouble option.
1801 if (!vol_noadouble(vol) && (d_bitmap &
1802 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1803 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1804 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1805 return AFPERR_ACCESS;
1811 * Check to see if a create was necessary. If it was, we'll want
1812 * to set our name, etc.
1814 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1815 ad_setname(&ad, cfrombstring(curdir->d_m_name));
1821 while ( bitmap != 0 ) {
1822 while (( bitmap & 1 ) == 0 ) {
1830 ad_getattr(&ad, &bshort);
1832 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1833 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1837 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1838 change_parent_mdate = 1;
1839 ad_setattr(&ad, bshort);
1842 case DIRPBIT_CDATE :
1844 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1847 case DIRPBIT_MDATE :
1849 case DIRPBIT_BDATE :
1851 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1854 case DIRPBIT_FINFO :
1856 /* Fixes #2802236 */
1857 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1858 *fflags &= htons(~FINDERINFO_ISHARED);
1860 if ( dir->d_did == DIRDID_ROOT ) {
1862 * Alright, we admit it, this is *really* sick!
1863 * The 4 bytes that we don't copy, when we're dealing
1864 * with the root of a volume, are the directory's
1865 * location information. This eliminates that annoying
1866 * behavior one sees when mounting above another mount
1869 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1870 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1872 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1876 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1877 if ( (dir->d_did == DIRDID_ROOT) &&
1878 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1879 err = set_dir_errors(path, "setdeskowner", errno);
1880 if (isad && err == AFPERR_PARAM) {
1881 err = AFP_OK; /* ???*/
1884 goto setdirparam_done;
1887 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1888 err = set_dir_errors(path, "setdirowner", errno);
1889 goto setdirparam_done;
1893 if (dir->d_did == DIRDID_ROOT)
1894 setdeskowner( -1, ntohl(group) );
1895 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1896 err = set_dir_errors(path, "setdirowner", errno);
1897 goto setdirparam_done;
1900 case DIRPBIT_ACCESS :
1901 if (dir->d_did == DIRDID_ROOT) {
1903 if (!dir_rx_set(mpriv)) {
1904 /* we can't remove read and search for owner on volume root */
1905 err = AFPERR_ACCESS;
1906 goto setdirparam_done;
1910 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1911 err = set_dir_errors(path, "setdirmode", errno);
1912 goto setdirparam_done;
1915 case DIRPBIT_PDINFO :
1916 if (afp_version >= 30) {
1917 err = AFPERR_BITMAP;
1918 goto setdirparam_done;
1921 case DIRPBIT_UNIXPR :
1922 if (vol_unix_priv(vol)) {
1923 if (dir->d_did == DIRDID_ROOT) {
1924 if (!dir_rx_set(upriv)) {
1925 /* we can't remove read and search for owner on volume root */
1926 err = AFPERR_ACCESS;
1927 goto setdirparam_done;
1929 setdeskowner( -1, ntohl(group) );
1930 setdeskmode( upriv );
1932 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1933 err = set_dir_errors(path, "setdirowner", errno);
1934 goto setdirparam_done;
1937 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1938 err = set_dir_errors(path, "setdirunixmode", errno);
1939 goto setdirparam_done;
1943 err = AFPERR_BITMAP;
1944 goto setdirparam_done;
1948 err = AFPERR_BITMAP;
1949 goto setdirparam_done;
1958 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1959 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1963 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1964 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1969 if (path->st_valid && !path->st_errno) {
1970 struct stat *st = &path->st;
1972 if (dir && dir->d_pdid) {
1973 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1977 ad_close_metadata( &ad);
1980 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1981 && gettimeofday(&tv, NULL) == 0) {
1982 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) == 0) {
1983 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1984 /* be careful with bitmap because now dir is null */
1985 bitmap = 1<<DIRPBIT_MDATE;
1986 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1987 /* should we reset curdir ?*/
1994 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2008 memcpy( &vid, ibuf, sizeof( vid ));
2009 ibuf += sizeof( vid );
2010 if (NULL == (vol = getvolbyvid( vid )) ) {
2011 return( AFPERR_PARAM );
2014 memcpy( &did, ibuf, sizeof( did ));
2015 ibuf += sizeof( did );
2019 * if it's CNID 2 our only choice to meet the specs is call sync.
2020 * For any other CNID just sync that dir. To my knowledge the
2021 * intended use of FPSyncDir is to sync the volume so all we're
2022 * ever going to see here is probably CNID 2. Anyway, we' prepared.
2025 if ( ntohl(did) == 2 ) {
2028 if (NULL == ( dir = dirlookup( vol, did )) ) {
2029 return afp_errno; /* was AFPERR_NOOBJ */
2032 if (movecwd( vol, dir ) < 0 )
2033 return ( AFPERR_NOOBJ );
2036 * Assuming only OSens that have dirfd also may require fsyncing directories
2037 * in order to flush metadata e.g. Linux.
2041 if (NULL == ( dp = opendir( "." )) ) {
2044 return( AFPERR_NOOBJ );
2046 return( AFPERR_ACCESS );
2048 return( AFPERR_PARAM );
2052 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2055 if ( fsync ( dfd ) < 0 )
2056 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2057 dir->d_u_name, strerror(errno) );
2058 closedir(dp); /* closes dfd too */
2061 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2064 return( AFPERR_NOOBJ );
2066 return( AFPERR_ACCESS );
2068 return( AFPERR_PARAM );
2072 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2073 vol->ad_path(".", ADFLAGS_DIR) );
2075 if ( fsync(dfd) < 0 )
2076 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2077 vol->ad_path(cfrombstring(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2084 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2090 struct path *s_path;
2098 memcpy( &vid, ibuf, sizeof( vid ));
2099 ibuf += sizeof( vid );
2100 if (NULL == ( vol = getvolbyvid( vid )) ) {
2101 return( AFPERR_PARAM );
2104 if (vol->v_flags & AFPVOL_RO)
2105 return AFPERR_VLOCK;
2107 memcpy( &did, ibuf, sizeof( did ));
2108 ibuf += sizeof( did );
2109 if (NULL == ( dir = dirlookup( vol, did )) ) {
2110 return afp_errno; /* was AFPERR_NOOBJ */
2112 /* for concurrent access we need to be sure we are not in the
2113 * folder we want to create...
2117 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2118 return get_afp_errno(AFPERR_PARAM);
2120 /* cname was able to move curdir to it! */
2121 if (*s_path->m_name == '\0')
2122 return AFPERR_EXIST;
2124 upath = s_path->u_name;
2126 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2130 if (of_stat(s_path) < 0) {
2136 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2140 if ( movecwd( vol, dir ) < 0 ) {
2141 return( AFPERR_PARAM );
2144 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2145 if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2146 if (vol_noadouble(vol))
2147 goto createdir_done;
2148 return( AFPERR_ACCESS );
2150 ad_setname(&ad, s_path->m_name);
2151 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2154 ad_close_metadata( &ad);
2157 #ifdef HAVE_NFSv4_ACLS
2158 /* FIXME: are we really inside the created dir? */
2159 addir_inherit_acl(vol);
2162 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2163 *rbuflen = sizeof( u_int32_t );
2164 setvoltime(obj, vol );
2169 * dst new unix filename (not a pathname)
2170 * newname new mac name
2173 int renamedir(const struct vol *vol, char *src, char *dst,
2175 struct dir *newparent,
2181 /* existence check moved to afp_moveandrename */
2182 if ( unix_rename( src, dst ) < 0 ) {
2185 return( AFPERR_NOOBJ );
2187 return( AFPERR_ACCESS );
2189 return AFPERR_VLOCK;
2191 /* tried to move directory into a subdirectory of itself */
2192 return AFPERR_CANTMOVE;
2194 /* this needs to copy and delete. bleah. that means we have
2195 * to deal with entire directory hierarchies. */
2196 if ((err = copydir(vol, src, dst)) < 0) {
2200 if ((err = deletedir(src)) < 0)
2204 return( AFPERR_PARAM );
2208 vol->vfs->vfs_renamedir(vol, src, dst);
2210 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2212 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2213 ad_setname(&ad, newname);
2215 ad_close_metadata( &ad);
2218 if (dir_modify(vol, dir, curdir->d_did, 0, newname, dst, curdir->d_fullpath) != 0) {
2219 LOG(log_error, logtype_afpd, "renamedir: fatal error from dir_modify: %s -> %s", src, dst);
2226 /* delete an empty directory */
2227 int deletecurdir(struct vol *vol)
2237 if ( dirlookup(vol, curdir->d_pdid) == NULL ) {
2238 return( AFPERR_ACCESS );
2243 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2244 /* we never want to create a resource fork here, we are going to delete it */
2245 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2247 ad_getattr(&ad, &ashort);
2248 ad_close( &ad, ADFLAGS_HF );
2249 if ((ashort & htons(ATTRBIT_NODELETE))) {
2250 return AFPERR_OLOCK;
2253 err = vol->vfs->vfs_deletecurdir(vol);
2258 /* now get rid of dangling symlinks */
2259 if ((dp = opendir("."))) {
2260 while ((de = readdir(dp))) {
2261 /* skip this and previous directory */
2262 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2265 /* bail if it's not a symlink */
2266 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2268 return AFPERR_DIRNEMPT;
2271 if ((err = netatalk_unlink(de->d_name))) {
2278 if ( movecwd(vol, dirlookup(vol, curdir->d_pdid)) < 0 ) {
2283 err = netatalk_rmdir_all_errors(cfrombstring(fdir->d_u_name));
2284 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2285 cnid_delete(vol->v_cdb, fdir->d_did);
2286 dir_remove( vol, fdir );
2290 /* inode is used as key for cnid.
2291 * Close the descriptor only after cnid_delete
2299 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2309 sfunc = (unsigned char) *ibuf++;
2313 if (sfunc >= 3 && sfunc <= 6) {
2314 if (afp_version < 30) {
2315 return( AFPERR_PARAM );
2322 case 3 :/* unicode */
2323 memcpy( &id, ibuf, sizeof( id ));
2326 if (( pw = getpwuid( id )) == NULL ) {
2327 return( AFPERR_NOITEM );
2329 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2330 pw->pw_name, -1, &name);
2337 case 4 : /* unicode */
2338 memcpy( &id, ibuf, sizeof( id ));
2341 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2342 return( AFPERR_NOITEM );
2344 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2345 gr->gr_name, -1, &name);
2351 #ifdef HAVE_NFSv4_ACLS
2352 case 5 : /* UUID -> username */
2353 case 6 : /* UUID -> groupname */
2354 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2355 return AFPERR_PARAM;
2356 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2358 len = getnamefromuuid( ibuf, &name, &type);
2359 if (len != 0) /* its a error code, not len */
2360 return AFPERR_NOITEM;
2361 if (type == UUID_USER) {
2362 if (( pw = getpwnam( name )) == NULL )
2363 return( AFPERR_NOITEM );
2364 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2365 id = htonl(UUID_USER);
2366 memcpy( rbuf, &id, sizeof( id ));
2367 id = htonl( pw->pw_uid);
2368 rbuf += sizeof( id );
2369 memcpy( rbuf, &id, sizeof( id ));
2370 rbuf += sizeof( id );
2371 *rbuflen = 2 * sizeof( id );
2372 } else { /* type == UUID_GROUP */
2373 if (( gr = getgrnam( name )) == NULL )
2374 return( AFPERR_NOITEM );
2375 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2376 id = htonl(UUID_GROUP);
2377 memcpy( rbuf, &id, sizeof( id ));
2378 rbuf += sizeof( id );
2379 id = htonl( gr->gr_gid);
2380 memcpy( rbuf, &id, sizeof( id ));
2381 rbuf += sizeof( id );
2382 *rbuflen = 2 * sizeof( id );
2387 return( AFPERR_PARAM );
2391 len = strlen( name );
2394 u_int16_t tp = htons(len);
2395 memcpy(rbuf, &tp, sizeof(tp));
2404 memcpy( rbuf, name, len );
2412 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2421 sfunc = (unsigned char) *ibuf++;
2423 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2426 case 2 : /* unicode */
2427 if (afp_version < 30) {
2428 return( AFPERR_PARAM );
2430 memcpy(&ulen, ibuf, sizeof(ulen));
2433 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2437 len = (unsigned char) *ibuf++;
2439 #ifdef HAVE_NFSv4_ACLS
2440 case 5 : /* username -> UUID */
2441 case 6 : /* groupname -> UUID */
2442 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2443 return AFPERR_PARAM;
2444 memcpy(&ulen, ibuf, sizeof(ulen));
2450 return( AFPERR_PARAM );
2456 return AFPERR_PARAM;
2459 case 1 : /* unicode */
2461 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2462 return( AFPERR_NOITEM );
2466 memcpy( rbuf, &id, sizeof( id ));
2467 *rbuflen = sizeof( id );
2470 case 2 : /* unicode */
2472 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2473 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2474 return( AFPERR_NOITEM );
2477 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2479 memcpy( rbuf, &id, sizeof( id ));
2480 *rbuflen = sizeof( id );
2482 #ifdef HAVE_NFSv4_ACLS
2483 case 5 : /* username -> UUID */
2484 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2485 if (0 != getuuidfromname(ibuf, UUID_USER, rbuf))
2486 return AFPERR_NOITEM;
2487 *rbuflen = UUID_BINSIZE;
2489 case 6 : /* groupname -> UUID */
2490 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2491 if (0 != getuuidfromname(ibuf, UUID_GROUP, rbuf))
2492 return AFPERR_NOITEM;
2493 *rbuflen = UUID_BINSIZE;
2501 /* ------------------------------------
2502 variable DID support
2504 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2515 /* do nothing as dids are static for the life of the process. */
2519 memcpy(&vid, ibuf, sizeof( vid ));
2520 ibuf += sizeof( vid );
2521 if (( vol = getvolbyvid( vid )) == NULL ) {
2522 return( AFPERR_PARAM );
2525 memcpy( &did, ibuf, sizeof( did ));
2526 ibuf += sizeof( did );
2527 if (( dir = dirlookup( vol, did )) == NULL ) {
2528 return( AFPERR_PARAM );
2531 /* dir_remove -- deletedid */
2537 /* did creation gets done automatically
2538 * there's a pb again with case but move it to cname
2540 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2543 struct dir *parentdir;
2551 memcpy(&vid, ibuf, sizeof(vid));
2552 ibuf += sizeof( vid );
2554 if (NULL == ( vol = getvolbyvid( vid )) ) {
2555 return( AFPERR_PARAM );
2558 memcpy(&did, ibuf, sizeof(did));
2559 ibuf += sizeof(did);
2561 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2565 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2566 return get_afp_errno(AFPERR_PARAM);
2569 if ( *path->m_name != '\0' ) {
2570 return path_error(path, AFPERR_NOOBJ);
2573 if ( !path->st_valid && of_stat(path ) < 0 ) {
2574 return( AFPERR_NOOBJ );
2576 if ( path->st_errno ) {
2577 return( AFPERR_NOOBJ );
2580 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2581 *rbuflen = sizeof(curdir->d_did);