2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
15 #include <sys/param.h>
21 #include <atalk/adouble.h>
22 #include <atalk/vfs.h>
23 #include <atalk/afp.h>
24 #include <atalk/util.h>
25 #include <atalk/cnid.h>
26 #include <atalk/logger.h>
27 #include <atalk/uuid.h>
28 #include <atalk/unix.h>
29 #include <atalk/bstrlib.h>
30 #include <atalk/bstradd.h>
32 #include "directory.h"
45 * FIXMEs, loose ends after the dircache rewrite:
46 * o merge dircache_search_by_name and dir_add ??
47 * o case-insensitivity is gone from cname
51 /*******************************************************************************************
53 ******************************************************************************************/
56 /* As long as directory.c hasn't got its own init call, this get initialized in dircache_init */
57 struct dir rootParent = {
58 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
59 NULL, 0, 0, /* qidx_node, ctime, d_flags */
60 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
62 struct dir *curdir = &rootParent;
63 struct path Cur_Path = {
68 NULL,/* struct dir * */
69 0, /* stat is not set */
75 /*******************************************************************************************
77 ******************************************************************************************/
80 /* -------------------------
81 appledouble mkdir afp error code.
83 static int netatalk_mkdir(const struct vol *vol, const char *name)
88 if (vol->v_flags & AFPVOL_UNIX_PRIV) {
89 if (lstat(".", &st) < 0)
91 int mode = (DIRBITS & (~S_ISGID & st.st_mode)) | (0777 & ~vol->v_umask);
92 LOG(log_maxdebug, logtype_afpd, "netatalk_mkdir(\"%s\") {parent mode: %04o, vol umask: %04o}",
93 name, st.st_mode, vol->v_umask);
95 ret = mkdir(name, mode);
97 ret = ad_mkdir(name, DIRBITS | 0777);
103 return( AFPERR_NOOBJ );
105 return( AFPERR_VLOCK );
108 return( AFPERR_ACCESS );
110 return( AFPERR_EXIST );
113 return( AFPERR_DFULL );
115 return( AFPERR_PARAM );
121 /* ------------------- */
122 static int deletedir(int dirfd, char *dir)
124 char path[MAXPATHLEN + 1];
132 if ((len = strlen(dir)) +2 > sizeof(path))
136 if ((dp = opendirat(dirfd, dir)) == NULL)
142 remain = sizeof(path) -len -1;
143 while ((de = readdir(dp)) && err == AFP_OK) {
144 /* skip this and previous directory */
145 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
148 if (strlen(de->d_name) > remain) {
152 strcpy(path + len, de->d_name);
153 if (lstatat(dirfd, path, &st)) {
156 if (S_ISDIR(st.st_mode)) {
157 err = deletedir(dirfd, path);
159 err = netatalk_unlinkat(dirfd, path);
164 /* okay. the directory is empty. delete it. note: we already got rid
167 err = netatalk_rmdir(dirfd, dir);
172 /* do a recursive copy. */
173 static int copydir(const struct vol *vol, int dirfd, char *src, char *dst)
175 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
184 /* doesn't exist or the path is too long. */
185 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
186 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
187 ((dp = opendirat(dirfd, src)) == NULL))
190 /* try to create the destination directory */
191 if (AFP_OK != (err = netatalk_mkdir(vol, dst)) ) {
196 /* set things up to copy */
200 srem = sizeof(spath) - slen -1;
205 drem = sizeof(dpath) - dlen -1;
208 while ((de = readdir(dp))) {
209 /* skip this and previous directory */
210 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
213 if (strlen(de->d_name) > srem) {
217 strcpy(spath + slen, de->d_name);
219 if (lstatat(dirfd, spath, &st) == 0) {
220 if (strlen(de->d_name) > drem) {
224 strcpy(dpath + dlen, de->d_name);
226 if (S_ISDIR(st.st_mode)) {
227 if (AFP_OK != (err = copydir(vol, dirfd, spath, dpath)))
229 } else if (AFP_OK != (err = copyfile(vol, vol, dirfd, spath, dpath, NULL, NULL))) {
233 /* keep the same time stamp. */
234 ut.actime = ut.modtime = st.st_mtime;
240 /* keep the same time stamp. */
241 if (lstatat(dirfd, src, &st) == 0) {
242 ut.actime = ut.modtime = st.st_mtime;
251 /* ---------------------
252 * is our cached offspring count valid?
254 static int diroffcnt(struct dir *dir, struct stat *st)
256 return st->st_ctime == dir->ctime;
259 /* --------------------- */
260 static int invisible_dots(const struct vol *vol, const char *name)
262 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
265 /* ------------------ */
266 static int set_dir_errors(struct path *path, const char *where, int err)
271 return AFPERR_ACCESS;
275 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
280 * @brief Convert name in client encoding to server encoding
282 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
283 * This only gets called from cname().
285 * @returns 0 on success, -1 on error
287 * @note If the passed ret->m_name is mangled, we'll demangle it
289 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
291 static char temp[ MAXPATHLEN + 1];
295 if (afp_version >= 30) {
297 if (dir->d_did == DIRDID_ROOT_PARENT) {
299 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
300 * So we compare it with the longname from the current volume and if they match
301 * we overwrite the requested path with the utf8 volume name so that the following
304 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
305 if (strcasecmp(ret->m_name, temp) == 0)
306 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
309 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
310 afp_errno = AFPERR_PARAM;
313 strcpy(ret->m_name, temp);
317 /* check for OS X mangled filename :( */
318 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
319 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
320 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
322 if (t != ret->m_name) {
324 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
325 * flags weren't the same
327 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
328 /* at last got our view of mac name */
329 strcpy(ret->m_name, t);
332 } /* afp_version >= 30 */
334 /* If we haven't got it by now, get it */
335 if (ret->u_name == NULL) {
336 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
337 afp_errno = AFPERR_PARAM;
346 * @brief Build struct path from struct dir
348 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
349 * 1. move cwd into parent dir (we're often already there, but not always)
350 * 2. set struct path to the dirname
352 * AFPERR_ACCESS: the dir is there, we just cant chdir into it
353 * AFPERR_NOOBJ: the dir was there when we stated it in cname, so we have a race
354 * 4. indicate there's no dir for this path
357 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
359 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
365 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
368 memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1); /* 3 */
369 if (dir->d_m_name == dir->d_u_name) {
370 ret->u_name = ret->m_name;
372 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
373 memcpy(ret->u_name, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
378 LOG(log_debug, logtype_afpd, "cname('%s') {path-from-dir: AFPERR_ACCESS. curdir:'%s', path:'%s'}",
379 cfrombstr(dir->d_fullpath),
380 cfrombstr(curdir->d_fullpath),
386 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
389 memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1);
390 if (dir->d_m_name == dir->d_u_name) {
391 ret->u_name = ret->m_name;
393 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
394 memcpy(ret->u_name, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
397 ret->d_dir = NULL; /* 4 */
398 dir_remove(vol, dir); /* 5 */
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 * 2a. Check if the DID is in the cache.
427 * 2b. Check if it's really a dir (d_fullpath != NULL) because we cache files too.
428 * 3. If it's not in the cache resolve it via the database.
429 * 4. Build complete server-side path to the dir.
430 * 5. Check if it exists and is a directory.
431 * 6. Create the struct dir and populate it.
432 * 7. Add it to the cache.
434 * @param vol (r) pointer to struct vol
435 * @param did (r) DID to resolve
437 * @returns pointer to struct dir
439 * @note FIXME: OSX calls it with bogus id, ie file ID not folder ID,
440 * and we are really bad in this case.
442 struct dir *dirlookup(const struct vol *vol, cnid_t did)
444 static char buffer[12 + MAXPATHLEN + 1];
446 struct dir *ret = NULL, *pdir;
447 bstring fullpath = NULL;
448 char *upath = NULL, *mpath;
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) { /* 2a */
470 if (ret->d_fullpath == NULL) { /* 2b */
471 afp_errno = AFPERR_BADTYPE;
474 if (lstat(cfrombstr(ret->d_fullpath), &st) != 0) {
475 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {lstat: %s}", ntohl(did), strerror(errno));
479 /* It's not there anymore, so remove it */
480 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {calling dir_remove()}", ntohl(did));
481 dir_remove(vol, ret);
482 afp_errno = AFPERR_NOOBJ;
493 utf8 = utf8_encoding();
494 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
496 /* Get it from the database */
498 if ( (upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL
499 || (upath = strdup(upath)) == NULL) { /* 3 */
500 afp_errno = AFPERR_NOOBJ;
507 * Recurse up the tree, terminates in dirlookup when either
508 * - DIRDID_ROOT is hit
509 * - a cached entry is found
511 if ((pdir = dirlookup(vol, pdid)) == NULL) {
516 /* build the fullpath */
517 if ((fullpath = bstrcpy(pdir->d_fullpath)) == NULL
518 || bconchar(fullpath, '/') != BSTR_OK
519 || bcatcstr(fullpath, upath) != BSTR_OK) {
524 /* stat it and check if it's a dir */
525 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstr(fullpath));
527 if (stat(cfrombstr(fullpath), &st) != 0) { /* 5a */
530 afp_errno = AFPERR_NOOBJ;
534 afp_errno = AFPERR_ACCESS;
538 afp_errno = AFPERR_MISC;
543 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
544 afp_errno = AFPERR_BADTYPE;
550 /* Get macname from unix name */
551 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
552 afp_errno = AFPERR_NOOBJ;
557 /* Create struct dir */
558 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath, st.st_ctime)) == NULL) { /* 6 */
559 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
564 /* Add it to the cache only if it's a dir */
565 if (dircache_add(ret) != 0) { /* 7 */
570 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {end: did:%u, path:'%s'}",
571 ntohl(did), ntohl(pdid), cfrombstr(ret->d_fullpath));
574 if (upath) free(upath);
576 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
577 ntohl(did), AfpErr2name(afp_errno));
588 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
590 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
595 static u_int32_t did = 0;
596 static char cname[MAXPATHLEN];
597 static char lname[MAXPATHLEN];
598 ucs2_t u2_path[MAXPATHLEN];
599 ucs2_t u2_dename[MAXPATHLEN];
600 char *tmp, *savepath;
602 if (!(vol->v_flags & AFPVOL_CASEINSEN))
605 if (veto_file(ENUMVETO, path->u_name))
608 savepath = path->u_name;
610 /* very simple cache */
611 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
612 path->u_name = cname;
614 if (of_stat( path ) == 0 ) {
617 /* something changed, we cannot stat ... */
621 if (NULL == ( dp = opendir( "." )) ) {
622 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
627 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
628 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
629 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
631 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
633 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
634 if (NULL == check_dirent(vol, de->d_name))
637 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
640 if (strcasecmp_w( u2_path, u2_dename) == 0) {
642 strlcpy(cname, de->d_name, sizeof(cname));
643 path->u_name = cname;
645 if (of_stat( path ) == 0 ) {
646 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
647 strlcpy(lname, tmp, sizeof(lname));
660 /* invalidate cache */
663 path->u_name = savepath;
665 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
671 * @brief Construct struct dir
673 * Construct struct dir from parameters.
675 * @param m_name (r) directory name in UTF8-dec
676 * @param u_name (r) directory name in server side encoding
677 * @param vol (r) pointer to struct vol
678 * @param pdid (r) Parent CNID
679 * @param did (r) CNID
680 * @param path (r) Full unix path to dir or NULL for files
681 * @param ctime (r) st_ctime from stat
683 * @returns pointer to new struct dir or NULL on error
685 * @note Most of the time mac name and unix name are the same.
687 struct dir *dir_new(const char *m_name,
689 const struct vol *vol,
697 dir = (struct dir *) calloc(1, sizeof( struct dir ));
701 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
706 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
709 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
710 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
711 dir->d_m_name_ucs2 = NULL;
714 if (m_name == u_name || !strcmp(m_name, u_name)) {
715 dir->d_u_name = dir->d_m_name;
717 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
718 bdestroy(dir->d_m_name);
725 dir->d_vid = vol->v_vid;
726 dir->d_fullpath = path;
727 dir->ctime_dircache = ctime;
732 * @brief Free a struct dir and all its members
734 * @param (rw) pointer to struct dir
736 void dir_free(struct dir *dir)
738 if (dir->d_u_name != dir->d_m_name) {
739 bdestroy(dir->d_u_name);
741 if (dir->d_m_name_ucs2)
742 free(dir->d_m_name_ucs2);
743 bdestroy(dir->d_m_name);
744 bdestroy(dir->d_fullpath);
749 * @brief Create struct dir from struct path
751 * Create a new struct dir from struct path. Then add it to the cache.
753 * 1. Open adouble file, get CNID from it.
754 * 2. Search the database, hinting with the CNID from (1).
755 * 3. Build fullpath and create struct dir.
756 * 4. Add it to the cache.
758 * @param vol (r) pointer to struct vol, possibly modified in callee
759 * @param dir (r) pointer to parrent directory
760 * @param path (rw) pointer to struct path with valid path->u_name
761 * @param len (r) strlen of path->u_name
763 * @returns Pointer to new struct dir or NULL on error.
765 * @note Function also assigns path->m_name from path->u_name.
767 struct dir *dir_add(struct vol *vol, const struct dir *dir, struct path *path, int len)
770 struct dir *cdir = NULL;
773 struct adouble *adp = NULL;
781 if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name), path->st.st_ctime)) != NULL) {
782 /* there's a stray entry in the dircache */
783 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
784 ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
785 ntohl(cdir->d_did), cfrombstr(dir->d_fullpath));
786 if (dir_remove(vol, cdir) != 0) {
788 AFP_PANIC("dir_add");
792 /* get_id needs adp for reading CNID from adouble file */
793 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
794 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
798 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
804 ad_close_metadata(adp);
806 /* Get macname from unixname */
807 if (path->m_name == NULL) {
808 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
815 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
816 || (bconchar(fullpath, '/') != BSTR_OK)
817 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
818 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
823 /* Allocate and initialize struct dir */
824 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath, path->st.st_ctime)) == NULL) { /* 3 */
829 if ((dircache_add(cdir)) != 0) { /* 4 */
830 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstr(fullpath));
836 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
837 cfrombstr(dir->d_u_name), path->u_name, err);
840 ad_close_metadata(adp);
841 if (!cdir && fullpath)
848 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
849 ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
850 ntohl(cdir->d_did), cfrombstr(cdir->d_fullpath));
857 * @brief Remove a dir from a cache and free it and any ressources with it
859 * 1. Check if the dir is locked or has opened forks
860 * 2. If it's a request to remove curdir, just chdir to volume root
861 * 3. Remove it from the cache
862 * 4. Remove the dir plus any allocated resources it references
864 * @param (r) pointer to struct vol
865 * @param (rw) pointer to struct dir
867 int dir_remove(const struct vol *vol, struct dir *dir)
872 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
875 if (dir->d_flags & DIRF_CACHELOCK) { /* 1 */
876 LOG(log_warning, logtype_afpd, "dir_remove(did:%u,'%s'): dir is locked",
877 ntohl(dir->d_did), cfrombstr(dir->d_u_name));
881 if (curdir == dir) { /* 2 */
882 if (movecwd(vol, vol->v_root) < 0) {
883 LOG(log_error, logtype_afpd, "dir_remove: can't chdir to : %s", vol->v_root);
887 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
888 ntohl(dir->d_did), cfrombstr(dir->d_u_name));
890 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX); /* 3 */
891 dir_free(dir); /* 4 */
897 * @brief Modify a struct dir, adjust cache
899 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
900 * If given new_uname == new_mname, new_uname will point to new_mname.
902 * @param vol (r) pointer to struct vol
903 * @param dir (rw) pointer to struct dir
904 * @param pdid (r) new parent DID
905 * @param did (r) new DID
906 * @param new_mname (r) new mac-name
907 * @param new_uname (r) new unix-name
908 * @param pdir_fullpath (r) new fullpath of parent dir
910 int dir_modify(const struct vol *vol,
914 const char *new_mname,
915 const char *new_uname,
916 bstring pdir_fullpath)
920 /* Remove it from the cache */
921 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
929 /* free uname if it's not the same as mname */
930 if (dir->d_m_name != dir->d_u_name)
931 bdestroy(dir->d_u_name);
933 if (new_uname == NULL)
934 new_uname = new_mname;
936 /* assign new name */
937 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
938 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
942 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
943 dir->d_u_name = dir->d_m_name;
945 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
946 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
953 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
955 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
957 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
961 if (dir->d_m_name_ucs2)
962 free(dir->d_m_name_ucs2);
963 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))
964 dir->d_m_name_ucs2 = NULL;
966 /* Re-add it to the cache */
967 if ((dircache_add(dir)) != 0) {
969 AFP_PANIC("dir_modify");
976 * @brief Resolve a catalog node name path
978 * 1. Evaluate path type
979 * 2. Move to start dir, if we cant, it might eg because of EACCES, build
980 * path from dirname, so eg getdirparams has sth it can chew on. curdir
981 * is dir parent then. All this is done in path_from_dir().
982 * 3. Parse next cnode name in path, cases:
983 * 4. single "\0" -> do nothing
984 * 5. two or more consecutive "\0" -> chdir("..") one or more times
985 * 6. cnode name -> copy it to path.m_name
986 * 7. Get unix name from mac name
987 * 8. Special handling of request with did 1
988 * 9. stat the cnode name
989 * 10. If it's not there, it's probably an afp_createfile|dir,
990 * return with curdir = dir parent, struct path = dirname
991 * 11. If it's there and it's a file, it must should be the last element of the requested
992 * path. Return with curdir = cnode name parent dir, struct path = filename
993 * 12. Treat symlinks like files, dont follow them
995 * 14. Search the dircache for it
996 * 15. If it's not in the cache, create a struct dir for it and add it to the cache
997 * 16. chdir into the dir and
998 * 17. set m_name to the mac equivalent of "."
1001 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1003 static char path[ MAXPATHLEN + 1];
1004 static struct path ret;
1014 LOG(log_maxdebug, logtype_afpd, "came('%s'): {start}", cfrombstr(dir->d_fullpath));
1017 afp_errno = AFPERR_NOOBJ;
1018 memset(&ret, 0, sizeof(ret));
1020 switch (ret.m_type = *data) { /* 1 */
1023 len = (unsigned char) *data++;
1025 if (afp_version >= 30) {
1031 if (afp_version >= 30) {
1033 memcpy(&hint, data, sizeof(hint));
1035 data += sizeof(hint);
1037 memcpy(&len16, data, sizeof(len16));
1043 /* else it's an error */
1045 afp_errno = AFPERR_PARAM;
1048 *cpath += len + size;
1053 if (movecwd(vol, dir) < 0 ) {
1054 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1055 ntohl(dir->d_did), cfrombstr(dir->d_fullpath));
1057 return path_from_dir(vol, dir, &ret);
1062 while (len) { /* 3 */
1063 if (*data == 0) { /* 4 or 5 */
1066 while (len > 0 && *data == 0) { /* 5 */
1067 /* chdir to parrent dir */
1068 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1070 if (movecwd( vol, dir ) < 0 ) {
1071 dir_remove(vol, dir);
1081 for ( p = path; *data != 0 && len > 0; len-- ) {
1083 if (p > &path[ MAXPATHLEN]) {
1084 afp_errno = AFPERR_PARAM;
1088 *p = 0; /* Terminate string */
1091 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) { /* 7 */
1092 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1096 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstr(dir->d_fullpath), ret.u_name);
1098 /* Prevent access to our special folders like .AppleDouble */
1099 if (check_name(vol, ret.u_name)) {
1100 /* the name is illegal */
1101 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1102 afp_errno = AFPERR_PARAM;
1106 if (dir->d_did == DIRDID_ROOT_PARENT) { /* 8 */
1108 * Special case: CNID 1
1109 * root parent (did 1) has one child: the volume. Requests for did=1 with
1110 * some <name> must check against the volume name.
1112 if ((strcmp(cfrombstr(vol->v_root->d_m_name), ret.m_name)) == 0)
1118 * CNID != 1, eg. most of the times we take this way.
1119 * Now check if current path-part is a file or dir:
1120 * o if it's dir we have to step into it
1121 * o if it's a file we expect it to be the last part of the requested path
1122 * and thus call continue which should terminate the while loop because
1125 if (of_stat(&ret) != 0) { /* 9 */
1127 * ret.u_name doesn't exist, might be afp_createfile|dir
1128 * that means it should have been the last part
1131 /* it wasn't the last part, so we have a bogus path request */
1132 afp_errno = AFPERR_NOOBJ;
1136 * this will terminate clean in while (1) because len == 0,
1137 * probably afp_createfile|dir
1139 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}",
1140 cfrombstr(dir->d_fullpath), ret.u_name);
1144 switch (ret.st.st_mode & S_IFMT) {
1145 case S_IFREG: /* 11 */
1146 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}",
1147 cfrombstr(dir->d_fullpath), ret.u_name);
1149 /* it wasn't the last part, so we have a bogus path request */
1150 afp_errno = AFPERR_PARAM;
1153 continue; /* continues while loop */
1154 case S_IFLNK: /* 12 */
1155 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}",
1156 cfrombstr(dir->d_fullpath), ret.u_name);
1158 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}",
1159 cfrombstr(dir->d_fullpath), ret.u_name);
1160 afp_errno = AFPERR_PARAM;
1163 continue; /* continues while loop */
1164 case S_IFDIR: /* 13 */
1167 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1168 afp_errno = AFPERR_NODIR;
1172 /* Search the cache */
1173 int unamelen = strlen(ret.u_name);
1174 cdir = dircache_search_by_name(vol, dir, ret.u_name, unamelen, ret.st.st_ctime); /* 14 */
1176 /* Not in cache, create one */
1177 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) { /* 15 */
1178 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1179 ntohl(dir->d_did), ret.u_name, getcwdpath());
1183 } /* if/else cnid==1 */
1185 /* Now chdir to the evaluated dir */
1186 if (movecwd( vol, cdir ) < 0 ) { /* 16 */
1187 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1188 cfrombstr(curdir->d_fullpath), cfrombstr(cdir->d_fullpath), strerror(errno));
1190 return path_from_dir(vol, cdir, &ret);
1195 ret.m_name[0] = 0; /* 17, so we later know last token was a dir */
1198 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1199 afp_errno = AFPERR_DID1;
1203 if (ret.m_name[0] == 0) {
1204 /* Last part was a dir */
1205 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1209 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1210 cfrombstr(dir->d_fullpath),
1211 cfrombstr(curdir->d_fullpath),
1218 * @brief chdir() to dir
1220 * @param vol (r) pointer to struct vol
1221 * @param dir (r) pointer to struct dir
1223 * @returns 0 on success, -1 on error with afp_errno set appropiately
1225 int movecwd(const struct vol *vol, struct dir *dir)
1232 LOG(log_maxdebug, logtype_afpd, "movecwd(curdir:'%s', cwd:'%s')",
1233 cfrombstr(curdir->d_fullpath), getcwdpath());
1237 if (dir->d_did == DIRDID_ROOT_PARENT) {
1238 curdir = &rootParent;
1242 LOG(log_debug, logtype_afpd, "movecwd(did:%u, '%s')",
1243 ntohl(dir->d_did), cfrombstr(dir->d_fullpath));
1245 if ((ret = lchdir(cfrombstr(dir->d_fullpath))) != 0 ) {
1246 LOG(log_debug, logtype_afpd, "movecwd('%s'): ret: %u, %s",
1247 cfrombstr(dir->d_fullpath), ret, strerror(errno));
1249 /* p is a symlink or getcwd failed */
1250 afp_errno = AFPERR_BADTYPE;
1252 if (chdir(vol->v_path ) < 0) {
1253 LOG(log_error, logtype_afpd, "can't chdir back'%s': %s", vol->v_path, strerror(errno));
1254 /* XXX what do we do here? */
1256 curdir = vol->v_root;
1263 afp_errno = AFPERR_ACCESS;
1266 afp_errno = AFPERR_NOOBJ;
1276 * We can't use unix file's perm to support Apple's inherited protection modes.
1277 * If we aren't the file's owner we can't change its perms when moving it and smb
1278 * nfs,... don't even try.
1280 #define AFP_CHECK_ACCESS
1282 int check_access(char *path, int mode)
1284 #ifdef AFP_CHECK_ACCESS
1292 accessmode(p, &ma, curdir, NULL);
1293 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1295 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1301 /* --------------------- */
1302 int file_access(struct path *path, int mode)
1306 accessmode(path->u_name, &ma, curdir, &path->st);
1307 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1309 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1315 /* --------------------- */
1316 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1318 dir->offcnt = count;
1319 dir->ctime = st->st_ctime;
1320 dir->d_flags &= ~DIRF_CNID;
1324 /* ---------------------
1325 * is our cached also for reenumerate id?
1327 int dirreenumerate(struct dir *dir, struct stat *st)
1329 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1332 /* ------------------------------
1334 (name, dir) with curdir:name == dir, from afp_enumerate
1337 int getdirparams(const struct vol *vol,
1338 u_int16_t bitmap, struct path *s_path,
1340 char *buf, size_t *buflen )
1344 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1345 int bit = 0, isad = 0;
1351 struct stat *st = &s_path->st;
1352 char *upath = s_path->u_name;
1354 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1355 (1 << DIRPBIT_CDATE) |
1356 (1 << DIRPBIT_MDATE) |
1357 (1 << DIRPBIT_BDATE) |
1358 (1 << DIRPBIT_FINFO)))) {
1360 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1361 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1363 if (ad.ad_md->adf_flags & O_CREAT) {
1364 /* We just created it */
1365 ad_setname(&ad, s_path->m_name);
1380 while ( bitmap != 0 ) {
1381 while (( bitmap & 1 ) == 0 ) {
1389 ad_getattr(&ad, &ashort);
1390 } else if (invisible_dots(vol, cfrombstr(dir->d_u_name))) {
1391 ashort = htons(ATTRBIT_INVISIBLE);
1394 ashort |= htons(ATTRBIT_SHARED);
1395 memcpy( data, &ashort, sizeof( ashort ));
1396 data += sizeof( ashort );
1400 memcpy( data, &pdid, sizeof( pdid ));
1401 data += sizeof( pdid );
1402 LOG(log_debug, logtype_afpd, "metadata('%s'): Parent DID: %u",
1403 s_path->u_name, ntohl(pdid));
1406 case DIRPBIT_CDATE :
1407 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1408 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1409 memcpy( data, &aint, sizeof( aint ));
1410 data += sizeof( aint );
1413 case DIRPBIT_MDATE :
1414 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1415 memcpy( data, &aint, sizeof( aint ));
1416 data += sizeof( aint );
1419 case DIRPBIT_BDATE :
1420 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1421 aint = AD_DATE_START;
1422 memcpy( data, &aint, sizeof( aint ));
1423 data += sizeof( aint );
1426 case DIRPBIT_FINFO :
1428 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1429 } else { /* no appledouble */
1430 memset( data, 0, 32 );
1431 /* set default view -- this also gets done in ad_open() */
1432 ashort = htons(FINDERINFO_CLOSEDVIEW);
1433 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1435 /* dot files are by default visible */
1436 if (invisible_dots(vol, cfrombstr(dir->d_u_name))) {
1437 ashort = htons(FINDERINFO_INVISIBLE);
1438 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1444 case DIRPBIT_LNAME :
1445 if (dir->d_m_name) /* root of parent can have a null name */
1448 memset(data, 0, sizeof(u_int16_t));
1449 data += sizeof( u_int16_t );
1452 case DIRPBIT_SNAME :
1453 memset(data, 0, sizeof(u_int16_t));
1454 data += sizeof( u_int16_t );
1458 memcpy( data, &dir->d_did, sizeof( aint ));
1459 data += sizeof( aint );
1460 LOG(log_debug, logtype_afpd, "metadata('%s'): DID: %u",
1461 s_path->u_name, ntohl(dir->d_did));
1464 case DIRPBIT_OFFCNT :
1466 /* this needs to handle current directory access rights */
1467 if (diroffcnt(dir, st)) {
1468 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1470 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1471 setdiroffcnt(dir, st, ret);
1472 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1474 ashort = htons( ashort );
1475 memcpy( data, &ashort, sizeof( ashort ));
1476 data += sizeof( ashort );
1480 aint = htonl(st->st_uid);
1481 memcpy( data, &aint, sizeof( aint ));
1482 data += sizeof( aint );
1486 aint = htonl(st->st_gid);
1487 memcpy( data, &aint, sizeof( aint ));
1488 data += sizeof( aint );
1491 case DIRPBIT_ACCESS :
1492 accessmode( upath, &ma, dir , st);
1494 *data++ = ma.ma_user;
1495 *data++ = ma.ma_world;
1496 *data++ = ma.ma_group;
1497 *data++ = ma.ma_owner;
1500 /* Client has requested the ProDOS information block.
1501 Just pass back the same basic block for all
1502 directories. <shirsch@ibm.net> */
1503 case DIRPBIT_PDINFO :
1504 if (afp_version >= 30) { /* UTF8 name */
1505 utf8 = kTextEncodingUTF8;
1506 if (dir->d_m_name) /* root of parent can have a null name */
1509 memset(data, 0, sizeof(u_int16_t));
1510 data += sizeof( u_int16_t );
1512 memcpy(data, &aint, sizeof( aint ));
1513 data += sizeof( aint );
1515 else { /* ProDOS Info Block */
1518 ashort = htons( 0x0200 );
1519 memcpy( data, &ashort, sizeof( ashort ));
1520 data += sizeof( ashort );
1521 memset( data, 0, sizeof( ashort ));
1522 data += sizeof( ashort );
1526 case DIRPBIT_UNIXPR :
1527 aint = htonl(st->st_uid);
1528 memcpy( data, &aint, sizeof( aint ));
1529 data += sizeof( aint );
1530 aint = htonl(st->st_gid);
1531 memcpy( data, &aint, sizeof( aint ));
1532 data += sizeof( aint );
1535 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1536 memcpy( data, &aint, sizeof( aint ));
1537 data += sizeof( aint );
1539 accessmode( upath, &ma, dir , st);
1541 *data++ = ma.ma_user;
1542 *data++ = ma.ma_world;
1543 *data++ = ma.ma_group;
1544 *data++ = ma.ma_owner;
1549 ad_close_metadata( &ad );
1551 return( AFPERR_BITMAP );
1557 ashort = htons( data - buf );
1558 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1559 data = set_name(vol, data, pdid, cfrombstr(dir->d_m_name), dir->d_did, 0);
1561 if ( utf_nameoff ) {
1562 ashort = htons( data - buf );
1563 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1564 data = set_name(vol, data, pdid, cfrombstr(dir->d_m_name), dir->d_did, utf8);
1567 ad_close_metadata( &ad );
1569 *buflen = data - buf;
1573 /* ----------------------------- */
1574 int path_error(struct path *path, int error)
1576 /* - a dir with access error
1577 * - no error it's a file
1580 if (path_isadir(path))
1582 if (path->st_valid && path->st_errno)
1584 return AFPERR_BADTYPE ;
1587 /* ----------------------------- */
1588 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1593 u_int16_t vid, bitmap;
1599 memcpy( &vid, ibuf, sizeof( vid ));
1600 ibuf += sizeof( vid );
1602 if (NULL == ( vol = getvolbyvid( vid )) ) {
1603 return( AFPERR_PARAM );
1606 if (vol->v_flags & AFPVOL_RO)
1607 return AFPERR_VLOCK;
1609 memcpy( &did, ibuf, sizeof( did ));
1610 ibuf += sizeof( int );
1612 if (NULL == ( dir = dirlookup( vol, did )) ) {
1616 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1617 bitmap = ntohs( bitmap );
1618 ibuf += sizeof( bitmap );
1620 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1621 return get_afp_errno(AFPERR_NOOBJ);
1624 if ( *path->m_name != '\0' ) {
1625 rc = path_error(path, AFPERR_NOOBJ);
1626 /* maybe we are trying to set perms back */
1627 if (rc != AFPERR_ACCESS)
1632 * If ibuf is odd, make it even.
1634 if ((u_long)ibuf & 1 ) {
1638 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1639 setvoltime(obj, vol );
1645 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1647 * assume path == '\0' eg. it's a directory in canonical form
1649 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1661 u_int16_t ashort, bshort, oshort;
1663 int change_mdate = 0;
1664 int change_parent_mdate = 0;
1666 u_int16_t bitmap = d_bitmap;
1667 u_char finder_buf[32];
1670 u_int16_t upriv_bit = 0;
1673 upath = path->u_name;
1675 while ( bitmap != 0 ) {
1676 while (( bitmap & 1 ) == 0 ) {
1684 memcpy( &ashort, buf, sizeof( ashort ));
1685 buf += sizeof( ashort );
1687 case DIRPBIT_CDATE :
1689 memcpy(&cdate, buf, sizeof(cdate));
1690 buf += sizeof( cdate );
1692 case DIRPBIT_MDATE :
1693 memcpy(&newdate, buf, sizeof(newdate));
1694 buf += sizeof( newdate );
1696 case DIRPBIT_BDATE :
1698 memcpy(&bdate, buf, sizeof(bdate));
1699 buf += sizeof( bdate );
1701 case DIRPBIT_FINFO :
1703 memcpy( finder_buf, buf, 32 );
1706 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1707 change_parent_mdate = 1;
1708 memcpy( &owner, buf, sizeof(owner));
1709 buf += sizeof( owner );
1712 change_parent_mdate = 1;
1713 memcpy( &group, buf, sizeof( group ));
1714 buf += sizeof( group );
1716 case DIRPBIT_ACCESS :
1718 change_parent_mdate = 1;
1719 ma.ma_user = *buf++;
1720 ma.ma_world = *buf++;
1721 ma.ma_group = *buf++;
1722 ma.ma_owner = *buf++;
1723 mpriv = mtoumode( &ma ) | vol->v_dperm;
1724 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1725 err = set_dir_errors(path, "setdirmode", errno);
1729 /* Ignore what the client thinks we should do to the
1730 ProDOS information block. Skip over the data and
1731 report nothing amiss. <shirsch@ibm.net> */
1732 case DIRPBIT_PDINFO :
1733 if (afp_version < 30) {
1737 err = AFPERR_BITMAP;
1741 case DIRPBIT_UNIXPR :
1742 if (vol_unix_priv(vol)) {
1743 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1744 buf += sizeof( owner );
1745 memcpy( &group, buf, sizeof( group ));
1746 buf += sizeof( group );
1749 change_parent_mdate = 1;
1750 memcpy( &upriv, buf, sizeof( upriv ));
1751 buf += sizeof( upriv );
1752 upriv = ntohl (upriv) | vol->v_dperm;
1753 if (dir_rx_set(upriv)) {
1754 /* maybe we are trying to set perms back */
1755 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1757 err = set_dir_errors(path, "setdirunixmode", errno);
1768 err = AFPERR_BITMAP;
1776 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1778 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1780 * Check to see what we're trying to set. If it's anything
1781 * but ACCESS, UID, or GID, give an error. If it's any of those
1782 * three, we don't need the ad to be open, so just continue.
1784 * note: we also don't need to worry about mdate. also, be quiet
1785 * if we're using the noadouble option.
1787 if (!vol_noadouble(vol) && (d_bitmap &
1788 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1789 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1790 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1791 return AFPERR_ACCESS;
1797 * Check to see if a create was necessary. If it was, we'll want
1798 * to set our name, etc.
1800 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1801 ad_setname(&ad, cfrombstr(curdir->d_m_name));
1807 while ( bitmap != 0 ) {
1808 while (( bitmap & 1 ) == 0 ) {
1816 ad_getattr(&ad, &bshort);
1818 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1819 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1823 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1824 change_parent_mdate = 1;
1825 ad_setattr(&ad, bshort);
1828 case DIRPBIT_CDATE :
1830 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1833 case DIRPBIT_MDATE :
1835 case DIRPBIT_BDATE :
1837 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1840 case DIRPBIT_FINFO :
1842 /* Fixes #2802236 */
1843 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1844 *fflags &= htons(~FINDERINFO_ISHARED);
1846 if ( dir->d_did == DIRDID_ROOT ) {
1848 * Alright, we admit it, this is *really* sick!
1849 * The 4 bytes that we don't copy, when we're dealing
1850 * with the root of a volume, are the directory's
1851 * location information. This eliminates that annoying
1852 * behavior one sees when mounting above another mount
1855 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1856 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1858 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1862 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1863 if ( (dir->d_did == DIRDID_ROOT) &&
1864 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1865 err = set_dir_errors(path, "setdeskowner", errno);
1866 if (isad && err == AFPERR_PARAM) {
1867 err = AFP_OK; /* ???*/
1870 goto setdirparam_done;
1873 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1874 err = set_dir_errors(path, "setdirowner", errno);
1875 goto setdirparam_done;
1879 if (dir->d_did == DIRDID_ROOT)
1880 setdeskowner( -1, ntohl(group) );
1881 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1882 err = set_dir_errors(path, "setdirowner", errno);
1883 goto setdirparam_done;
1886 case DIRPBIT_ACCESS :
1887 if (dir->d_did == DIRDID_ROOT) {
1889 if (!dir_rx_set(mpriv)) {
1890 /* we can't remove read and search for owner on volume root */
1891 err = AFPERR_ACCESS;
1892 goto setdirparam_done;
1896 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1897 err = set_dir_errors(path, "setdirmode", errno);
1898 goto setdirparam_done;
1901 case DIRPBIT_PDINFO :
1902 if (afp_version >= 30) {
1903 err = AFPERR_BITMAP;
1904 goto setdirparam_done;
1907 case DIRPBIT_UNIXPR :
1908 if (vol_unix_priv(vol)) {
1909 if (dir->d_did == DIRDID_ROOT) {
1910 if (!dir_rx_set(upriv)) {
1911 /* we can't remove read and search for owner on volume root */
1912 err = AFPERR_ACCESS;
1913 goto setdirparam_done;
1915 setdeskowner( -1, ntohl(group) );
1916 setdeskmode( upriv );
1918 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1919 err = set_dir_errors(path, "setdirowner", errno);
1920 goto setdirparam_done;
1923 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1924 err = set_dir_errors(path, "setdirunixmode", errno);
1925 goto setdirparam_done;
1929 err = AFPERR_BITMAP;
1930 goto setdirparam_done;
1934 err = AFPERR_BITMAP;
1935 goto setdirparam_done;
1944 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1945 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1949 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1950 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1955 if (path->st_valid && !path->st_errno) {
1956 struct stat *st = &path->st;
1958 if (dir && dir->d_pdid) {
1959 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1963 ad_close_metadata( &ad);
1966 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1967 && gettimeofday(&tv, NULL) == 0) {
1968 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) == 0) {
1969 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1970 /* be careful with bitmap because now dir is null */
1971 bitmap = 1<<DIRPBIT_MDATE;
1972 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1973 /* should we reset curdir ?*/
1980 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1994 memcpy( &vid, ibuf, sizeof( vid ));
1995 ibuf += sizeof( vid );
1996 if (NULL == (vol = getvolbyvid( vid )) ) {
1997 return( AFPERR_PARAM );
2000 memcpy( &did, ibuf, sizeof( did ));
2001 ibuf += sizeof( did );
2005 * if it's CNID 2 our only choice to meet the specs is call sync.
2006 * For any other CNID just sync that dir. To my knowledge the
2007 * intended use of FPSyncDir is to sync the volume so all we're
2008 * ever going to see here is probably CNID 2. Anyway, we' prepared.
2011 if ( ntohl(did) == 2 ) {
2014 if (NULL == ( dir = dirlookup( vol, did )) ) {
2015 return afp_errno; /* was AFPERR_NOOBJ */
2018 if (movecwd( vol, dir ) < 0 )
2019 return ( AFPERR_NOOBJ );
2022 * Assuming only OSens that have dirfd also may require fsyncing directories
2023 * in order to flush metadata e.g. Linux.
2027 if (NULL == ( dp = opendir( "." )) ) {
2030 return( AFPERR_NOOBJ );
2032 return( AFPERR_ACCESS );
2034 return( AFPERR_PARAM );
2038 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2041 if ( fsync ( dfd ) < 0 )
2042 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2043 dir->d_u_name, strerror(errno) );
2044 closedir(dp); /* closes dfd too */
2047 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2050 return( AFPERR_NOOBJ );
2052 return( AFPERR_ACCESS );
2054 return( AFPERR_PARAM );
2058 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2059 vol->ad_path(".", ADFLAGS_DIR) );
2061 if ( fsync(dfd) < 0 )
2062 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2063 vol->ad_path(cfrombstr(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2070 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2076 struct path *s_path;
2084 memcpy( &vid, ibuf, sizeof( vid ));
2085 ibuf += sizeof( vid );
2086 if (NULL == ( vol = getvolbyvid( vid )) ) {
2087 return( AFPERR_PARAM );
2090 if (vol->v_flags & AFPVOL_RO)
2091 return AFPERR_VLOCK;
2093 memcpy( &did, ibuf, sizeof( did ));
2094 ibuf += sizeof( did );
2095 if (NULL == ( dir = dirlookup( vol, did )) ) {
2096 return afp_errno; /* was AFPERR_NOOBJ */
2098 /* for concurrent access we need to be sure we are not in the
2099 * folder we want to create...
2103 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2104 return get_afp_errno(AFPERR_PARAM);
2106 /* cname was able to move curdir to it! */
2107 if (*s_path->m_name == '\0')
2108 return AFPERR_EXIST;
2110 upath = s_path->u_name;
2112 if (AFP_OK != (err = netatalk_mkdir(vol, upath))) {
2116 if (of_stat(s_path) < 0) {
2122 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2126 if ( movecwd( vol, dir ) < 0 ) {
2127 return( AFPERR_PARAM );
2130 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2131 if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2132 if (vol_noadouble(vol))
2133 goto createdir_done;
2134 return( AFPERR_ACCESS );
2136 ad_setname(&ad, s_path->m_name);
2137 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2140 ad_close_metadata( &ad);
2143 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2144 *rbuflen = sizeof( u_int32_t );
2145 setvoltime(obj, vol );
2150 * dst new unix filename (not a pathname)
2151 * newname new mac name
2153 * dirfd -1 means ignore dirfd (or use AT_FDCWD), otherwise src is relative to dirfd
2155 int renamedir(const struct vol *vol,
2160 struct dir *newparent,
2166 /* existence check moved to afp_moveandrename */
2167 if ( unix_rename(dirfd, src, -1, dst ) < 0 ) {
2170 return( AFPERR_NOOBJ );
2172 return( AFPERR_ACCESS );
2174 return AFPERR_VLOCK;
2176 /* tried to move directory into a subdirectory of itself */
2177 return AFPERR_CANTMOVE;
2179 /* this needs to copy and delete. bleah. that means we have
2180 * to deal with entire directory hierarchies. */
2181 if ((err = copydir(vol, dirfd, src, dst)) < 0) {
2185 if ((err = deletedir(dirfd, src)) < 0)
2189 return( AFPERR_PARAM );
2193 vol->vfs->vfs_renamedir(vol, dirfd, src, dst);
2195 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2197 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2198 ad_setname(&ad, newname);
2200 ad_close_metadata( &ad);
2206 /* delete an empty directory */
2207 int deletecurdir(struct vol *vol)
2217 if ( dirlookup(vol, curdir->d_pdid) == NULL ) {
2218 return( AFPERR_ACCESS );
2223 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2224 /* we never want to create a resource fork here, we are going to delete it */
2225 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2227 ad_getattr(&ad, &ashort);
2228 ad_close_metadata(&ad);
2229 if ((ashort & htons(ATTRBIT_NODELETE))) {
2230 return AFPERR_OLOCK;
2233 err = vol->vfs->vfs_deletecurdir(vol);
2238 /* now get rid of dangling symlinks */
2239 if ((dp = opendir("."))) {
2240 while ((de = readdir(dp))) {
2241 /* skip this and previous directory */
2242 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2245 /* bail if it's not a symlink */
2246 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2248 return AFPERR_DIRNEMPT;
2251 if ((err = netatalk_unlink(de->d_name))) {
2258 if ( movecwd(vol, dirlookup(vol, curdir->d_pdid)) < 0 ) {
2263 err = netatalk_rmdir_all_errors(-1, cfrombstr(fdir->d_u_name));
2264 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2265 cnid_delete(vol->v_cdb, fdir->d_did);
2266 dir_remove( vol, fdir );
2270 /* inode is used as key for cnid.
2271 * Close the descriptor only after cnid_delete
2279 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2289 sfunc = (unsigned char) *ibuf++;
2292 if (sfunc >= 3 && sfunc <= 6) {
2293 if (afp_version < 30) {
2294 return( AFPERR_PARAM );
2301 case 3 :/* unicode */
2302 memcpy( &id, ibuf, sizeof( id ));
2305 if (( pw = getpwuid( id )) == NULL ) {
2306 return( AFPERR_NOITEM );
2308 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2309 pw->pw_name, -1, &name);
2316 case 4 : /* unicode */
2317 memcpy( &id, ibuf, sizeof( id ));
2320 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2321 return( AFPERR_NOITEM );
2323 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2324 gr->gr_name, -1, &name);
2331 case 5 : /* UUID -> username */
2332 case 6 : /* UUID -> groupname */
2333 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2334 return AFPERR_PARAM;
2335 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2337 len = getnamefromuuid((unsigned char*) ibuf, &name, &type);
2338 if (len != 0) /* its a error code, not len */
2339 return AFPERR_NOITEM;
2342 if (( pw = getpwnam( name )) == NULL )
2343 return( AFPERR_NOITEM );
2344 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2345 id = htonl(UUID_USER);
2346 memcpy( rbuf, &id, sizeof( id ));
2347 id = htonl( pw->pw_uid);
2348 rbuf += sizeof( id );
2349 memcpy( rbuf, &id, sizeof( id ));
2350 rbuf += sizeof( id );
2351 *rbuflen = 2 * sizeof( id );
2354 if (( gr = getgrnam( name )) == NULL )
2355 return( AFPERR_NOITEM );
2356 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2357 id = htonl(UUID_GROUP);
2358 memcpy( rbuf, &id, sizeof( id ));
2359 rbuf += sizeof( id );
2360 id = htonl( gr->gr_gid);
2361 memcpy( rbuf, &id, sizeof( id ));
2362 rbuf += sizeof( id );
2363 *rbuflen = 2 * sizeof( id );
2367 return (AFPERR_NOITEM);
2374 return( AFPERR_PARAM );
2378 len = strlen( name );
2381 u_int16_t tp = htons(len);
2382 memcpy(rbuf, &tp, sizeof(tp));
2391 memcpy( rbuf, name, len );
2399 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2408 sfunc = (unsigned char) *ibuf++;
2410 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2413 case 2 : /* unicode */
2414 if (afp_version < 30) {
2415 return( AFPERR_PARAM );
2417 memcpy(&ulen, ibuf, sizeof(ulen));
2420 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2424 len = (unsigned char) *ibuf++;
2426 case 5 : /* username -> UUID */
2427 case 6 : /* groupname -> UUID */
2428 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2429 return AFPERR_PARAM;
2430 memcpy(&ulen, ibuf, sizeof(ulen));
2435 return( AFPERR_PARAM );
2441 return AFPERR_PARAM;
2444 case 1 : /* unicode */
2446 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2447 return( AFPERR_NOITEM );
2451 memcpy( rbuf, &id, sizeof( id ));
2452 *rbuflen = sizeof( id );
2455 case 2 : /* unicode */
2457 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2458 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2459 return( AFPERR_NOITEM );
2462 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2464 memcpy( rbuf, &id, sizeof( id ));
2465 *rbuflen = sizeof( id );
2467 case 5 : /* username -> UUID */
2468 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2469 if (0 != getuuidfromname(ibuf, UUID_USER, (unsigned char *)rbuf))
2470 return AFPERR_NOITEM;
2471 *rbuflen = UUID_BINSIZE;
2473 case 6 : /* groupname -> UUID */
2474 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2475 if (0 != getuuidfromname(ibuf, UUID_GROUP, (unsigned char *)rbuf))
2476 return AFPERR_NOITEM;
2477 *rbuflen = UUID_BINSIZE;
2484 /* ------------------------------------
2485 variable DID support
2487 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2498 /* do nothing as dids are static for the life of the process. */
2502 memcpy(&vid, ibuf, sizeof( vid ));
2503 ibuf += sizeof( vid );
2504 if (( vol = getvolbyvid( vid )) == NULL ) {
2505 return( AFPERR_PARAM );
2508 memcpy( &did, ibuf, sizeof( did ));
2509 ibuf += sizeof( did );
2510 if (( dir = dirlookup( vol, did )) == NULL ) {
2511 return( AFPERR_PARAM );
2514 /* dir_remove -- deletedid */
2520 /* did creation gets done automatically
2521 * there's a pb again with case but move it to cname
2523 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2526 struct dir *parentdir;
2534 memcpy(&vid, ibuf, sizeof(vid));
2535 ibuf += sizeof( vid );
2537 if (NULL == ( vol = getvolbyvid( vid )) ) {
2538 return( AFPERR_PARAM );
2541 memcpy(&did, ibuf, sizeof(did));
2542 ibuf += sizeof(did);
2544 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2548 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2549 return get_afp_errno(AFPERR_PARAM);
2552 if ( *path->m_name != '\0' ) {
2553 return path_error(path, AFPERR_NOOBJ);
2556 if ( !path->st_valid && of_stat(path ) < 0 ) {
2557 return( AFPERR_NOOBJ );
2559 if ( path->st_errno ) {
2560 return( AFPERR_NOOBJ );
2563 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2564 *rbuflen = sizeof(curdir->d_did);