2 * $Id: directory.c,v 1.140 2010/03/12 15:16:49 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"
47 extern void addir_inherit_acl(const struct vol *vol);
51 * FIXMEs, loose ends after the dircache rewrite:
52 * o merge dircache_search_by_name and dir_add ??
53 * o case-insensitivity is gone from cname
57 /*******************************************************************************************
59 ******************************************************************************************/
62 struct dir rootParent = {
63 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
64 NULL, NULL, 0, 0, /* qidx_node, d_ofork, ctime, d_flags */
65 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
67 struct dir *curdir = &rootParent;
68 struct path Cur_Path = {
73 NULL,/* struct dir * */
74 0, /* stat is not set */
80 /*******************************************************************************************
82 ******************************************************************************************/
85 /* -------------------------
86 appledouble mkdir afp error code.
88 static int netatalk_mkdir(const struct vol *vol, const char *name)
93 if (vol->v_flags & AFPVOL_UNIX_PRIV) {
94 if (lstat(".", &st) < 0)
96 int mode = (DIRBITS & (~S_ISGID & st.st_mode)) | (0777 & ~vol->v_umask);
97 LOG(log_maxdebug, logtype_afpd, "netatalk_mkdir(\"%s\") {parent mode: %04o, vol umask: %04o}",
98 name, st.st_mode, vol->v_umask);
100 ret = mkdir(name, mode);
102 ret = ad_mkdir(name, DIRBITS | 0777);
108 return( AFPERR_NOOBJ );
110 return( AFPERR_VLOCK );
113 return( AFPERR_ACCESS );
115 return( AFPERR_EXIST );
118 return( AFPERR_DFULL );
120 return( AFPERR_PARAM );
126 /* ------------------- */
127 static int deletedir(int dirfd, char *dir)
129 char path[MAXPATHLEN + 1];
137 if ((len = strlen(dir)) +2 > sizeof(path))
141 if ((dp = opendirat(dirfd, dir)) == NULL)
147 remain = sizeof(path) -len -1;
148 while ((de = readdir(dp)) && err == AFP_OK) {
149 /* skip this and previous directory */
150 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
153 if (strlen(de->d_name) > remain) {
157 strcpy(path + len, de->d_name);
158 if (lstatat(dirfd, path, &st)) {
161 if (S_ISDIR(st.st_mode)) {
162 err = deletedir(dirfd, path);
164 err = netatalk_unlinkat(dirfd, path);
169 /* okay. the directory is empty. delete it. note: we already got rid
172 err = netatalk_rmdir(dirfd, dir);
177 /* do a recursive copy. */
178 static int copydir(const struct vol *vol, int dirfd, char *src, char *dst)
180 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
189 /* doesn't exist or the path is too long. */
190 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
191 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
192 ((dp = opendirat(dirfd, src)) == NULL))
195 /* try to create the destination directory */
196 if (AFP_OK != (err = netatalk_mkdir(vol, dst)) ) {
201 /* set things up to copy */
205 srem = sizeof(spath) - slen -1;
210 drem = sizeof(dpath) - dlen -1;
213 while ((de = readdir(dp))) {
214 /* skip this and previous directory */
215 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
218 if (strlen(de->d_name) > srem) {
222 strcpy(spath + slen, de->d_name);
224 if (lstatat(dirfd, spath, &st) == 0) {
225 if (strlen(de->d_name) > drem) {
229 strcpy(dpath + dlen, de->d_name);
231 if (S_ISDIR(st.st_mode)) {
232 if (AFP_OK != (err = copydir(vol, dirfd, spath, dpath)))
234 } else if (AFP_OK != (err = copyfile(vol, vol, dirfd, spath, dpath, NULL, NULL))) {
238 /* keep the same time stamp. */
239 ut.actime = ut.modtime = st.st_mtime;
245 /* keep the same time stamp. */
246 if (lstatat(dirfd, src, &st) == 0) {
247 ut.actime = ut.modtime = st.st_mtime;
256 /* ---------------------
257 * is our cached offspring count valid?
259 static int diroffcnt(struct dir *dir, struct stat *st)
261 return st->st_ctime == dir->ctime;
264 /* --------------------- */
265 static int invisible_dots(const struct vol *vol, const char *name)
267 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
270 /* ------------------ */
271 static int set_dir_errors(struct path *path, const char *where, int err)
276 return AFPERR_ACCESS;
280 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
285 * @brief Convert name in client encoding to server encoding
287 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
288 * This only gets called from cname().
290 * @returns 0 on success, -1 on error
292 * @note If the passed ret->m_name is mangled, we'll demangle it
294 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
296 static char temp[ MAXPATHLEN + 1];
300 if (afp_version >= 30) {
302 if (dir->d_did == DIRDID_ROOT_PARENT) {
304 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
305 * So we compare it with the longname from the current volume and if they match
306 * we overwrite the requested path with the utf8 volume name so that the following
309 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
310 if (strcasecmp(ret->m_name, temp) == 0)
311 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
314 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
315 afp_errno = AFPERR_PARAM;
318 strcpy(ret->m_name, temp);
322 /* check for OS X mangled filename :( */
323 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
324 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
325 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
327 if (t != ret->m_name) {
329 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
330 * flags weren't the same
332 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
333 /* at last got our view of mac name */
334 strcpy(ret->m_name, t);
337 } /* afp_version >= 30 */
339 /* If we haven't got it by now, get it */
340 if (ret->u_name == NULL) {
341 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
342 afp_errno = AFPERR_PARAM;
351 * @brief Build struct path from struct dir
353 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
354 * 1. move cwd into parent dir (we're often already there, but not always)
355 * 2. set struct path to the dirname
357 * AFPERR_ACCESS: the dir is there, we just cant chdir into it
358 * AFPERR_NOOBJ: the dir was there when we stated it in cname, so we have a race
359 * 4. indicate there's no dir for this path
362 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
364 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
370 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
373 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1); /* 3 */
374 if (dir->d_m_name == dir->d_u_name) {
375 ret->u_name = ret->m_name;
377 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
378 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
383 LOG(log_debug, logtype_afpd, "cname('%s') {path-from-dir: AFPERR_ACCESS. curdir:'%s', path:'%s'}",
384 cfrombstring(dir->d_fullpath),
385 cfrombstring(curdir->d_fullpath),
391 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
394 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
395 if (dir->d_m_name == dir->d_u_name) {
396 ret->u_name = ret->m_name;
398 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
399 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
402 ret->d_dir = NULL; /* 4 */
403 dir_remove(vol, dir); /* 5 */
410 /* DEADC0DE: never get here */
415 /*********************************************************************************************
417 ********************************************************************************************/
419 int get_afp_errno(const int param)
421 if (afp_errno != AFPERR_DID1)
427 * @brief Resolve a DID
429 * Resolve a DID, allocate a struct dir for it
430 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
431 * 2a. Check if the DID is in the cache.
432 * 2b. Check if it's really a dir (d_fullpath != NULL) because we cache files too.
433 * 3. If it's not in the cache resolve it via the database.
434 * 4. Build complete server-side path to the dir.
435 * 5. Check if it exists and is a directory.
436 * 6. Create the struct dir and populate it.
437 * 7. Add it to the cache.
439 * @param vol (r) pointer to struct vol
440 * @param did (r) DID to resolve
442 * @returns pointer to struct dir
444 * @note FIXME: OSX calls it with bogus id, ie file ID not folder ID,
445 * and we are really bad in this case.
447 struct dir *dirlookup(const struct vol *vol, cnid_t did)
449 static char buffer[12 + MAXPATHLEN + 1];
451 struct dir *ret = NULL, *pdir;
452 bstring fullpath = NULL;
453 char *upath = NULL, *mpath;
456 int buflen = 12 + MAXPATHLEN + 1;
460 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {start}", ntohl(did));
462 /* check for did 0, 1 and 2 */
463 if (did == 0 || vol == NULL) { /* 1 */
464 afp_errno = AFPERR_PARAM;
466 } else if (did == DIRDID_ROOT_PARENT) {
467 rootParent.d_vid = vol->v_vid;
468 return (&rootParent);
469 } else if (did == DIRDID_ROOT) {
473 /* Search the cache */
474 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2a */
475 if (ret->d_fullpath == NULL) { /* 2b */
476 afp_errno = AFPERR_BADTYPE;
479 if (lstat(cfrombstring(ret->d_fullpath), &st) != 0) {
480 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {lstat: %s}", ntohl(did), strerror(errno));
484 /* It's not there anymore, so remove it */
485 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {calling dir_remove()}", ntohl(did));
486 dir_remove(vol, ret);
487 afp_errno = AFPERR_NOOBJ;
498 utf8 = utf8_encoding();
499 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
501 /* Get it from the database */
503 if ( (upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL
504 || (upath = strdup(upath)) == NULL) { /* 3 */
505 afp_errno = AFPERR_NOOBJ;
512 * Recurse up the tree, terminates in dirlookup when either
513 * - DIRDID_ROOT is hit
514 * - a cached entry is found
516 if ((pdir = dirlookup(vol, pdid)) == NULL) {
521 /* build the fullpath */
522 if ((fullpath = bstrcpy(pdir->d_fullpath)) == NULL
523 || bconchar(fullpath, '/') != BSTR_OK
524 || bcatcstr(fullpath, upath) != BSTR_OK) {
529 /* stat it and check if it's a dir */
530 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstring(fullpath));
532 if (stat(cfrombstring(fullpath), &st) != 0) { /* 5a */
535 afp_errno = AFPERR_NOOBJ;
539 afp_errno = AFPERR_ACCESS;
543 afp_errno = AFPERR_MISC;
548 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
549 afp_errno = AFPERR_BADTYPE;
555 /* Get macname from unix name */
556 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
557 afp_errno = AFPERR_NOOBJ;
562 /* Create struct dir */
563 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath)) == NULL) { /* 6 */
564 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
569 /* Add it to the cache only if it's a dir */
570 if (dircache_add(ret) != 0) { /* 7 */
575 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {end: did:%u, path:'%s'}",
576 ntohl(did), ntohl(pdid), cfrombstring(ret->d_fullpath));
580 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
581 ntohl(did), AfpErr2name(afp_errno));
593 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
595 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
600 static u_int32_t did = 0;
601 static char cname[MAXPATHLEN];
602 static char lname[MAXPATHLEN];
603 ucs2_t u2_path[MAXPATHLEN];
604 ucs2_t u2_dename[MAXPATHLEN];
605 char *tmp, *savepath;
607 if (!(vol->v_flags & AFPVOL_CASEINSEN))
610 if (veto_file(ENUMVETO, path->u_name))
613 savepath = path->u_name;
615 /* very simple cache */
616 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
617 path->u_name = cname;
619 if (of_stat( path ) == 0 ) {
622 /* something changed, we cannot stat ... */
626 if (NULL == ( dp = opendir( "." )) ) {
627 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
632 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
633 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
634 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
636 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
638 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
639 if (NULL == check_dirent(vol, de->d_name))
642 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
645 if (strcasecmp_w( u2_path, u2_dename) == 0) {
647 strlcpy(cname, de->d_name, sizeof(cname));
648 path->u_name = cname;
650 if (of_stat( path ) == 0 ) {
651 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
652 strlcpy(lname, tmp, sizeof(lname));
665 /* invalidate cache */
668 path->u_name = savepath;
670 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
676 * @brief Construct struct dir
678 * Construct struct dir from parameters.
680 * @param m_name (r) directory name in UTF8-dec
681 * @param u_name (r) directory name in server side encoding
682 * @param vol (r) pointer to struct vol
683 * @param pdid (r) Parent CNID
684 * @param did (r) CNID
685 * @param fullpath (r) Full unix path to dir or NULL for files
687 * @returns pointer to new struct dir or NULL on error
689 * @note Most of the time mac name and unix name are the same.
691 struct dir *dir_new(const char *m_name,
693 const struct vol *vol,
700 dir = (struct dir *) calloc(1, sizeof( struct dir ));
704 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
709 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
712 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
713 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
714 dir->d_m_name_ucs2 = NULL;
717 if (m_name == u_name || !strcmp(m_name, u_name)) {
718 dir->d_u_name = dir->d_m_name;
720 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
721 bdestroy(dir->d_m_name);
728 dir->d_vid = vol->v_vid;
729 dir->d_fullpath = path;
734 * @brief Free a struct dir and all its members
736 * @param (rw) pointer to struct dir
738 void dir_free(struct dir *dir)
740 if (dir->d_u_name != dir->d_m_name) {
741 bdestroy(dir->d_u_name);
743 if (dir->d_m_name_ucs2)
744 free(dir->d_m_name_ucs2);
745 bdestroy(dir->d_m_name);
746 bdestroy(dir->d_fullpath);
751 * @brief Create struct dir from struct path
753 * Create a new struct dir from struct path. Then add it to the cache.
754 * The caller must have assured that the dir is not already in the cache,
755 * cf theAFP_ASSERTion.
756 * 1. Open adouble file, get CNID from it.
757 * 2. Search the database, hinting with the CNID from (1).
758 * 3. Build fullpath and create struct dir.
759 * 4. Add it to the cache.
761 * @param vol (r) pointer to struct vol
762 * @param dir (r) pointer to parrent directory
763 * @param path (rw) pointer to struct path with valid path->u_name
764 * @param len (r) strlen of path->u_name
766 * @returns Pointer to new struct dir or NULL on error.
768 * @note Function also assigns path->m_name from path->u_name.
770 struct dir *dir_add(const struct vol *vol, const struct dir *dir, struct path *path, int len)
773 struct dir *cdir = NULL;
776 struct adouble *adp = NULL;
784 if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name))) != NULL) {
785 /* there's a stray entry in the dircache */
786 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
787 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
788 ntohl(cdir->d_did), cfrombstring(dir->d_fullpath));
789 if (dir_remove(vol, cdir) != 0) {
795 /* get_id needs adp for reading CNID from adouble file */
796 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
797 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
801 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
807 ad_close_metadata(adp);
809 /* Get macname from unixname */
810 if (path->m_name == NULL) {
811 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
818 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
819 || (bconchar(fullpath, '/') != BSTR_OK)
820 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
821 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
826 /* Allocate and initialize struct dir */
827 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath)) == NULL) { /* 3 */
832 if ((dircache_add(cdir)) != 0) { /* 4 */
833 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstring(fullpath));
839 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
840 cfrombstring(dir->d_u_name), path->u_name, err);
843 ad_close_metadata(adp);
844 if (!cdir && fullpath)
851 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
852 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
853 ntohl(cdir->d_did), cfrombstring(cdir->d_fullpath));
860 * @brief Remove a dir from a cache and free it and any ressources with it
862 * 1. Check if the dir is locked or has opened forks
863 * 2. If it's a request to remove curdir, just chdir to volume root
864 * 3. Remove it from the cache
865 * 4. Remove the dir plus any allocated resources it references
867 * @param (r) pointer to struct vol
868 * @param (rw) pointer to struct dir
870 int dir_remove(const struct vol *vol, struct dir *dir)
875 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
878 if (dir->d_flags & DIRF_CACHELOCK || dir->d_ofork) { /* 1 */
879 LOG(log_warning, logtype_afpd, "dir_remove(did:%u,'%s'): dir is locked or has opened forks",
880 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
884 if (curdir == dir) { /* 2 */
885 if (movecwd(vol, vol->v_root) < 0) {
886 LOG(log_error, logtype_afpd, "dir_remove: can't chdir to : %s", vol->v_root);
890 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
891 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
893 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX); /* 3 */
894 dir_free(dir); /* 4 */
900 * @brief Modify a struct dir, adjust cache
902 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
903 * If given new_uname == new_mname, new_uname will point to new_mname.
905 * @param vol (r) pointer to struct vol
906 * @param dir (rw) pointer to struct dir
907 * @param pdid (r) new parent DID
908 * @param did (r) new DID
909 * @param new_mname (r) new mac-name
910 * @param new_uname (r) new unix-name
912 int dir_modify(const struct vol *vol,
916 const char *new_mname,
917 const char *new_uname,
918 bstring pdir_fullpath)
922 /* Remove it from the cache */
923 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
931 /* free uname if it's not the same as mname */
932 if (dir->d_m_name != dir->d_u_name)
933 bdestroy(dir->d_u_name);
935 if (new_uname == NULL)
936 new_uname = new_mname;
938 /* assign new name */
939 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
940 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
944 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
945 dir->d_u_name = dir->d_m_name;
947 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
948 LOG(log_error, logtype_afpd, "renamedir: bassigncstr: %s", strerror(errno) );
955 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
957 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
959 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
963 if (dir->d_m_name_ucs2)
964 free(dir->d_m_name_ucs2);
965 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))
966 dir->d_m_name_ucs2 = NULL;
968 /* Re-add it to the cache */
969 if ((dircache_add(dir)) != 0) {
978 * @brief Resolve a catalog node name path
980 * 1. Evaluate path type
981 * 2. Move to start dir, if we cant, it might eg because of EACCES, build
982 * path from dirname, so eg getdirparams has sth it can chew on. curdir
983 * is dir parent then. All this is done in path_from_dir().
984 * 3. Parse next cnode name in path, cases:
985 * 4. single "\0" -> do nothing
986 * 5. two or more consecutive "\0" -> chdir("..") one or more times
987 * 6. cnode name -> copy it to path.m_name
988 * 7. Get unix name from mac name
989 * 8. Special handling of request with did 1
990 * 9. stat the cnode name
991 * 10. If it's not there, it's probably an afp_createfile|dir,
992 * return with curdir = dir parent, struct path = dirname
993 * 11. If it's there and it's a file, it must should be the last element of the requested
994 * path. Return with curdir = cnode name parent dir, struct path = filename
995 * 12. Treat symlinks like files, dont follow them
997 * 14. Search the dircache for it
998 * 15. If it's not in the cache, create a struct dir for it and add it to the cache
999 * 16. chdir into the dir and
1000 * 17. set m_name to the mac equivalent of "."
1003 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1005 static char path[ MAXPATHLEN + 1];
1006 static struct path ret;
1016 LOG(log_maxdebug, logtype_afpd, "came('%s'): {start}", cfrombstring(dir->d_fullpath));
1019 afp_errno = AFPERR_NOOBJ;
1020 memset(&ret, 0, sizeof(ret));
1022 switch (ret.m_type = *data) { /* 1 */
1025 len = (unsigned char) *data++;
1027 if (afp_version >= 30) {
1033 if (afp_version >= 30) {
1035 memcpy(&hint, data, sizeof(hint));
1037 data += sizeof(hint);
1039 memcpy(&len16, data, sizeof(len16));
1045 /* else it's an error */
1047 afp_errno = AFPERR_PARAM;
1050 *cpath += len + size;
1055 if (movecwd(vol, dir) < 0 ) {
1056 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1057 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1059 return path_from_dir(vol, dir, &ret);
1064 while (len) { /* 3 */
1065 if (*data == 0) { /* 4 or 5 */
1068 while (len > 0 && *data == 0) { /* 5 */
1069 /* chdir to parrent dir */
1070 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1072 if (movecwd( vol, dir ) < 0 ) {
1073 dir_remove(vol, dir);
1083 for ( p = path; *data != 0 && len > 0; len-- ) {
1085 if (p > &path[ MAXPATHLEN]) {
1086 afp_errno = AFPERR_PARAM;
1090 *p = 0; /* Terminate string */
1093 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) { /* 7 */
1094 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1098 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstring(dir->d_fullpath), ret.u_name);
1100 /* Prevent access to our special folders like .AppleDouble */
1101 if (check_name(vol, ret.u_name)) {
1102 /* the name is illegal */
1103 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1104 afp_errno = AFPERR_PARAM;
1108 if (dir->d_did == DIRDID_ROOT_PARENT) { /* 8 */
1110 * Special case: CNID 1
1111 * root parent (did 1) has one child: the volume. Requests for did=1 with
1112 * some <name> must check against the volume name.
1114 if ((strcmp(cfrombstring(vol->v_root->d_m_name), ret.m_name)) == 0)
1120 * CNID != 1, eg. most of the times we take this way.
1121 * Now check if current path-part is a file or dir:
1122 * o if it's dir we have to step into it
1123 * o if it's a file we expect it to be the last part of the requested path
1124 * and thus call continue which should terminate the while loop because
1127 if (of_stat(&ret) != 0) { /* 9 */
1129 * ret.u_name doesn't exist, might be afp_createfile|dir
1130 * that means it should have been the last part
1133 /* it wasn't the last part, so we have a bogus path request */
1134 afp_errno = AFPERR_NOOBJ;
1138 * this will terminate clean in while (1) because len == 0,
1139 * probably afp_createfile|dir
1141 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1145 switch (ret.st.st_mode & S_IFMT) {
1146 case S_IFREG: /* 11 */
1147 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}", cfrombstring(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'}", cfrombstring(dir->d_fullpath), ret.u_name);
1157 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1158 afp_errno = AFPERR_PARAM;
1161 continue; /* continues while loop */
1162 case S_IFDIR: /* 13 */
1165 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1166 afp_errno = AFPERR_NODIR;
1170 /* Search the cache */
1171 int unamelen = strlen(ret.u_name);
1172 cdir = dircache_search_by_name(vol, dir, ret.u_name, unamelen); /* 14 */
1174 /* Not in cache, create one */
1175 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) { /* 15 */
1176 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1177 ntohl(dir->d_did), ret.u_name, getcwdpath());
1181 } /* if/else cnid==1 */
1183 /* Now chdir to the evaluated dir */
1184 if (movecwd( vol, cdir ) < 0 ) { /* 16 */
1185 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1186 cfrombstring(curdir->d_fullpath), cfrombstring(cdir->d_fullpath), strerror(errno));
1188 return path_from_dir(vol, cdir, &ret);
1193 ret.m_name[0] = 0; /* 17, so we later know last token was a dir */
1196 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1197 afp_errno = AFPERR_DID1;
1201 if (ret.m_name[0] == 0) {
1202 /* Last part was a dir */
1203 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1207 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1208 cfrombstring(dir->d_fullpath),
1209 cfrombstring(curdir->d_fullpath),
1216 * @brief chdir() to dir
1218 * @param vol (r) pointer to struct vol
1219 * @param dir (r) pointer to struct dir
1221 * @returns 0 on success, -1 on error with afp_errno set appropiately
1223 int movecwd(const struct vol *vol, struct dir *dir)
1230 LOG(log_maxdebug, logtype_afpd, "movecwd(curdir:'%s', cwd:'%s')",
1231 cfrombstring(curdir->d_fullpath), getcwdpath());
1235 if (dir->d_did == DIRDID_ROOT_PARENT) {
1236 curdir = &rootParent;
1240 LOG(log_debug, logtype_afpd, "movecwd(did:%u, '%s')", ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1242 if ((ret = lchdir(cfrombstring(dir->d_fullpath))) != 0 ) {
1243 LOG(log_debug, logtype_afpd, "movecwd('%s'): ret: %u, %s",
1244 cfrombstring(dir->d_fullpath), ret, strerror(errno));
1246 /* p is a symlink or getcwd failed */
1247 afp_errno = AFPERR_BADTYPE;
1249 if (chdir(vol->v_path ) < 0) {
1250 LOG(log_error, logtype_afpd, "can't chdir back'%s': %s", vol->v_path, strerror(errno));
1251 /* XXX what do we do here? */
1253 curdir = vol->v_root;
1260 afp_errno = AFPERR_ACCESS;
1263 afp_errno = AFPERR_NOOBJ;
1273 * We can't use unix file's perm to support Apple's inherited protection modes.
1274 * If we aren't the file's owner we can't change its perms when moving it and smb
1275 * nfs,... don't even try.
1277 #define AFP_CHECK_ACCESS
1279 int check_access(char *path, int mode)
1281 #ifdef AFP_CHECK_ACCESS
1289 accessmode(p, &ma, curdir, NULL);
1290 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1292 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1298 /* --------------------- */
1299 int file_access(struct path *path, int mode)
1303 accessmode(path->u_name, &ma, curdir, &path->st);
1304 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1306 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1312 /* --------------------- */
1313 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1315 dir->offcnt = count;
1316 dir->ctime = st->st_ctime;
1317 dir->d_flags &= ~DIRF_CNID;
1321 /* ---------------------
1322 * is our cached also for reenumerate id?
1324 int dirreenumerate(struct dir *dir, struct stat *st)
1326 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1329 /* ------------------------------
1331 (name, dir) with curdir:name == dir, from afp_enumerate
1334 int getdirparams(const struct vol *vol,
1335 u_int16_t bitmap, struct path *s_path,
1337 char *buf, size_t *buflen )
1341 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1342 int bit = 0, isad = 0;
1348 struct stat *st = &s_path->st;
1349 char *upath = s_path->u_name;
1351 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1352 (1 << DIRPBIT_CDATE) |
1353 (1 << DIRPBIT_MDATE) |
1354 (1 << DIRPBIT_BDATE) |
1355 (1 << DIRPBIT_FINFO)))) {
1357 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1358 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1360 if (ad.ad_md->adf_flags & O_CREAT) {
1361 /* We just created it */
1362 ad_setname(&ad, s_path->m_name);
1377 while ( bitmap != 0 ) {
1378 while (( bitmap & 1 ) == 0 ) {
1386 ad_getattr(&ad, &ashort);
1387 } else if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1388 ashort = htons(ATTRBIT_INVISIBLE);
1391 ashort |= htons(ATTRBIT_SHARED);
1392 memcpy( data, &ashort, sizeof( ashort ));
1393 data += sizeof( ashort );
1397 memcpy( data, &pdid, sizeof( pdid ));
1398 data += sizeof( pdid );
1399 LOG(log_debug, logtype_afpd, "metadata('%s'): Parent DID: %u",
1400 s_path->u_name, ntohl(pdid));
1403 case DIRPBIT_CDATE :
1404 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1405 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1406 memcpy( data, &aint, sizeof( aint ));
1407 data += sizeof( aint );
1410 case DIRPBIT_MDATE :
1411 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1412 memcpy( data, &aint, sizeof( aint ));
1413 data += sizeof( aint );
1416 case DIRPBIT_BDATE :
1417 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1418 aint = AD_DATE_START;
1419 memcpy( data, &aint, sizeof( aint ));
1420 data += sizeof( aint );
1423 case DIRPBIT_FINFO :
1425 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1426 } else { /* no appledouble */
1427 memset( data, 0, 32 );
1428 /* set default view -- this also gets done in ad_open() */
1429 ashort = htons(FINDERINFO_CLOSEDVIEW);
1430 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1432 /* dot files are by default visible */
1433 if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1434 ashort = htons(FINDERINFO_INVISIBLE);
1435 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1441 case DIRPBIT_LNAME :
1442 if (dir->d_m_name) /* root of parent can have a null name */
1445 memset(data, 0, sizeof(u_int16_t));
1446 data += sizeof( u_int16_t );
1449 case DIRPBIT_SNAME :
1450 memset(data, 0, sizeof(u_int16_t));
1451 data += sizeof( u_int16_t );
1455 memcpy( data, &dir->d_did, sizeof( aint ));
1456 data += sizeof( aint );
1457 LOG(log_debug, logtype_afpd, "metadata('%s'): DID: %u",
1458 s_path->u_name, ntohl(dir->d_did));
1461 case DIRPBIT_OFFCNT :
1463 /* this needs to handle current directory access rights */
1464 if (diroffcnt(dir, st)) {
1465 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1467 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1468 setdiroffcnt(dir, st, ret);
1469 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1471 ashort = htons( ashort );
1472 memcpy( data, &ashort, sizeof( ashort ));
1473 data += sizeof( ashort );
1477 aint = htonl(st->st_uid);
1478 memcpy( data, &aint, sizeof( aint ));
1479 data += sizeof( aint );
1483 aint = htonl(st->st_gid);
1484 memcpy( data, &aint, sizeof( aint ));
1485 data += sizeof( aint );
1488 case DIRPBIT_ACCESS :
1489 accessmode( upath, &ma, dir , st);
1491 *data++ = ma.ma_user;
1492 *data++ = ma.ma_world;
1493 *data++ = ma.ma_group;
1494 *data++ = ma.ma_owner;
1497 /* Client has requested the ProDOS information block.
1498 Just pass back the same basic block for all
1499 directories. <shirsch@ibm.net> */
1500 case DIRPBIT_PDINFO :
1501 if (afp_version >= 30) { /* UTF8 name */
1502 utf8 = kTextEncodingUTF8;
1503 if (dir->d_m_name) /* root of parent can have a null name */
1506 memset(data, 0, sizeof(u_int16_t));
1507 data += sizeof( u_int16_t );
1509 memcpy(data, &aint, sizeof( aint ));
1510 data += sizeof( aint );
1512 else { /* ProDOS Info Block */
1515 ashort = htons( 0x0200 );
1516 memcpy( data, &ashort, sizeof( ashort ));
1517 data += sizeof( ashort );
1518 memset( data, 0, sizeof( ashort ));
1519 data += sizeof( ashort );
1523 case DIRPBIT_UNIXPR :
1524 aint = htonl(st->st_uid);
1525 memcpy( data, &aint, sizeof( aint ));
1526 data += sizeof( aint );
1527 aint = htonl(st->st_gid);
1528 memcpy( data, &aint, sizeof( aint ));
1529 data += sizeof( aint );
1532 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1533 memcpy( data, &aint, sizeof( aint ));
1534 data += sizeof( aint );
1536 accessmode( upath, &ma, dir , st);
1538 *data++ = ma.ma_user;
1539 *data++ = ma.ma_world;
1540 *data++ = ma.ma_group;
1541 *data++ = ma.ma_owner;
1546 ad_close_metadata( &ad );
1548 return( AFPERR_BITMAP );
1554 ashort = htons( data - buf );
1555 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1556 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, 0);
1558 if ( utf_nameoff ) {
1559 ashort = htons( data - buf );
1560 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1561 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, utf8);
1564 ad_close_metadata( &ad );
1566 *buflen = data - buf;
1570 /* ----------------------------- */
1571 int path_error(struct path *path, int error)
1573 /* - a dir with access error
1574 * - no error it's a file
1577 if (path_isadir(path))
1579 if (path->st_valid && path->st_errno)
1581 return AFPERR_BADTYPE ;
1584 /* ----------------------------- */
1585 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1590 u_int16_t vid, bitmap;
1596 memcpy( &vid, ibuf, sizeof( vid ));
1597 ibuf += sizeof( vid );
1599 if (NULL == ( vol = getvolbyvid( vid )) ) {
1600 return( AFPERR_PARAM );
1603 if (vol->v_flags & AFPVOL_RO)
1604 return AFPERR_VLOCK;
1606 memcpy( &did, ibuf, sizeof( did ));
1607 ibuf += sizeof( int );
1609 if (NULL == ( dir = dirlookup( vol, did )) ) {
1613 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1614 bitmap = ntohs( bitmap );
1615 ibuf += sizeof( bitmap );
1617 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1618 return get_afp_errno(AFPERR_NOOBJ);
1621 if ( *path->m_name != '\0' ) {
1622 rc = path_error(path, AFPERR_NOOBJ);
1623 /* maybe we are trying to set perms back */
1624 if (rc != AFPERR_ACCESS)
1629 * If ibuf is odd, make it even.
1631 if ((u_long)ibuf & 1 ) {
1635 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1636 setvoltime(obj, vol );
1642 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1644 * assume path == '\0' eg. it's a directory in canonical form
1646 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1658 u_int16_t ashort, bshort, oshort;
1660 int change_mdate = 0;
1661 int change_parent_mdate = 0;
1663 u_int16_t bitmap = d_bitmap;
1664 u_char finder_buf[32];
1667 u_int16_t upriv_bit = 0;
1670 upath = path->u_name;
1672 while ( bitmap != 0 ) {
1673 while (( bitmap & 1 ) == 0 ) {
1681 memcpy( &ashort, buf, sizeof( ashort ));
1682 buf += sizeof( ashort );
1684 case DIRPBIT_CDATE :
1686 memcpy(&cdate, buf, sizeof(cdate));
1687 buf += sizeof( cdate );
1689 case DIRPBIT_MDATE :
1690 memcpy(&newdate, buf, sizeof(newdate));
1691 buf += sizeof( newdate );
1693 case DIRPBIT_BDATE :
1695 memcpy(&bdate, buf, sizeof(bdate));
1696 buf += sizeof( bdate );
1698 case DIRPBIT_FINFO :
1700 memcpy( finder_buf, buf, 32 );
1703 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1704 change_parent_mdate = 1;
1705 memcpy( &owner, buf, sizeof(owner));
1706 buf += sizeof( owner );
1709 change_parent_mdate = 1;
1710 memcpy( &group, buf, sizeof( group ));
1711 buf += sizeof( group );
1713 case DIRPBIT_ACCESS :
1715 change_parent_mdate = 1;
1716 ma.ma_user = *buf++;
1717 ma.ma_world = *buf++;
1718 ma.ma_group = *buf++;
1719 ma.ma_owner = *buf++;
1720 mpriv = mtoumode( &ma ) | vol->v_dperm;
1721 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1722 err = set_dir_errors(path, "setdirmode", errno);
1726 /* Ignore what the client thinks we should do to the
1727 ProDOS information block. Skip over the data and
1728 report nothing amiss. <shirsch@ibm.net> */
1729 case DIRPBIT_PDINFO :
1730 if (afp_version < 30) {
1734 err = AFPERR_BITMAP;
1738 case DIRPBIT_UNIXPR :
1739 if (vol_unix_priv(vol)) {
1740 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1741 buf += sizeof( owner );
1742 memcpy( &group, buf, sizeof( group ));
1743 buf += sizeof( group );
1746 change_parent_mdate = 1;
1747 memcpy( &upriv, buf, sizeof( upriv ));
1748 buf += sizeof( upriv );
1749 upriv = ntohl (upriv) | vol->v_dperm;
1750 if (dir_rx_set(upriv)) {
1751 /* maybe we are trying to set perms back */
1752 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1754 err = set_dir_errors(path, "setdirunixmode", errno);
1765 err = AFPERR_BITMAP;
1773 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1775 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1777 * Check to see what we're trying to set. If it's anything
1778 * but ACCESS, UID, or GID, give an error. If it's any of those
1779 * three, we don't need the ad to be open, so just continue.
1781 * note: we also don't need to worry about mdate. also, be quiet
1782 * if we're using the noadouble option.
1784 if (!vol_noadouble(vol) && (d_bitmap &
1785 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1786 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1787 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1788 return AFPERR_ACCESS;
1794 * Check to see if a create was necessary. If it was, we'll want
1795 * to set our name, etc.
1797 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1798 ad_setname(&ad, cfrombstring(curdir->d_m_name));
1804 while ( bitmap != 0 ) {
1805 while (( bitmap & 1 ) == 0 ) {
1813 ad_getattr(&ad, &bshort);
1815 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1816 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1820 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1821 change_parent_mdate = 1;
1822 ad_setattr(&ad, bshort);
1825 case DIRPBIT_CDATE :
1827 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1830 case DIRPBIT_MDATE :
1832 case DIRPBIT_BDATE :
1834 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1837 case DIRPBIT_FINFO :
1839 /* Fixes #2802236 */
1840 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1841 *fflags &= htons(~FINDERINFO_ISHARED);
1843 if ( dir->d_did == DIRDID_ROOT ) {
1845 * Alright, we admit it, this is *really* sick!
1846 * The 4 bytes that we don't copy, when we're dealing
1847 * with the root of a volume, are the directory's
1848 * location information. This eliminates that annoying
1849 * behavior one sees when mounting above another mount
1852 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1853 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1855 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1859 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1860 if ( (dir->d_did == DIRDID_ROOT) &&
1861 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1862 err = set_dir_errors(path, "setdeskowner", errno);
1863 if (isad && err == AFPERR_PARAM) {
1864 err = AFP_OK; /* ???*/
1867 goto setdirparam_done;
1870 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1871 err = set_dir_errors(path, "setdirowner", errno);
1872 goto setdirparam_done;
1876 if (dir->d_did == DIRDID_ROOT)
1877 setdeskowner( -1, ntohl(group) );
1878 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1879 err = set_dir_errors(path, "setdirowner", errno);
1880 goto setdirparam_done;
1883 case DIRPBIT_ACCESS :
1884 if (dir->d_did == DIRDID_ROOT) {
1886 if (!dir_rx_set(mpriv)) {
1887 /* we can't remove read and search for owner on volume root */
1888 err = AFPERR_ACCESS;
1889 goto setdirparam_done;
1893 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1894 err = set_dir_errors(path, "setdirmode", errno);
1895 goto setdirparam_done;
1898 case DIRPBIT_PDINFO :
1899 if (afp_version >= 30) {
1900 err = AFPERR_BITMAP;
1901 goto setdirparam_done;
1904 case DIRPBIT_UNIXPR :
1905 if (vol_unix_priv(vol)) {
1906 if (dir->d_did == DIRDID_ROOT) {
1907 if (!dir_rx_set(upriv)) {
1908 /* we can't remove read and search for owner on volume root */
1909 err = AFPERR_ACCESS;
1910 goto setdirparam_done;
1912 setdeskowner( -1, ntohl(group) );
1913 setdeskmode( upriv );
1915 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1916 err = set_dir_errors(path, "setdirowner", errno);
1917 goto setdirparam_done;
1920 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1921 err = set_dir_errors(path, "setdirunixmode", errno);
1922 goto setdirparam_done;
1926 err = AFPERR_BITMAP;
1927 goto setdirparam_done;
1931 err = AFPERR_BITMAP;
1932 goto setdirparam_done;
1941 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1942 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1946 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1947 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1952 if (path->st_valid && !path->st_errno) {
1953 struct stat *st = &path->st;
1955 if (dir && dir->d_pdid) {
1956 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1960 ad_close_metadata( &ad);
1963 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1964 && gettimeofday(&tv, NULL) == 0) {
1965 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) == 0) {
1966 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1967 /* be careful with bitmap because now dir is null */
1968 bitmap = 1<<DIRPBIT_MDATE;
1969 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1970 /* should we reset curdir ?*/
1977 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1991 memcpy( &vid, ibuf, sizeof( vid ));
1992 ibuf += sizeof( vid );
1993 if (NULL == (vol = getvolbyvid( vid )) ) {
1994 return( AFPERR_PARAM );
1997 memcpy( &did, ibuf, sizeof( did ));
1998 ibuf += sizeof( did );
2002 * if it's CNID 2 our only choice to meet the specs is call sync.
2003 * For any other CNID just sync that dir. To my knowledge the
2004 * intended use of FPSyncDir is to sync the volume so all we're
2005 * ever going to see here is probably CNID 2. Anyway, we' prepared.
2008 if ( ntohl(did) == 2 ) {
2011 if (NULL == ( dir = dirlookup( vol, did )) ) {
2012 return afp_errno; /* was AFPERR_NOOBJ */
2015 if (movecwd( vol, dir ) < 0 )
2016 return ( AFPERR_NOOBJ );
2019 * Assuming only OSens that have dirfd also may require fsyncing directories
2020 * in order to flush metadata e.g. Linux.
2024 if (NULL == ( dp = opendir( "." )) ) {
2027 return( AFPERR_NOOBJ );
2029 return( AFPERR_ACCESS );
2031 return( AFPERR_PARAM );
2035 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2038 if ( fsync ( dfd ) < 0 )
2039 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2040 dir->d_u_name, strerror(errno) );
2041 closedir(dp); /* closes dfd too */
2044 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2047 return( AFPERR_NOOBJ );
2049 return( AFPERR_ACCESS );
2051 return( AFPERR_PARAM );
2055 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2056 vol->ad_path(".", ADFLAGS_DIR) );
2058 if ( fsync(dfd) < 0 )
2059 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2060 vol->ad_path(cfrombstring(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2067 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2073 struct path *s_path;
2081 memcpy( &vid, ibuf, sizeof( vid ));
2082 ibuf += sizeof( vid );
2083 if (NULL == ( vol = getvolbyvid( vid )) ) {
2084 return( AFPERR_PARAM );
2087 if (vol->v_flags & AFPVOL_RO)
2088 return AFPERR_VLOCK;
2090 memcpy( &did, ibuf, sizeof( did ));
2091 ibuf += sizeof( did );
2092 if (NULL == ( dir = dirlookup( vol, did )) ) {
2093 return afp_errno; /* was AFPERR_NOOBJ */
2095 /* for concurrent access we need to be sure we are not in the
2096 * folder we want to create...
2100 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2101 return get_afp_errno(AFPERR_PARAM);
2103 /* cname was able to move curdir to it! */
2104 if (*s_path->m_name == '\0')
2105 return AFPERR_EXIST;
2107 upath = s_path->u_name;
2109 if (AFP_OK != (err = netatalk_mkdir(vol, upath))) {
2113 if (of_stat(s_path) < 0) {
2119 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2123 if ( movecwd( vol, dir ) < 0 ) {
2124 return( AFPERR_PARAM );
2127 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2128 if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2129 if (vol_noadouble(vol))
2130 goto createdir_done;
2131 return( AFPERR_ACCESS );
2133 ad_setname(&ad, s_path->m_name);
2134 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2137 ad_close_metadata( &ad);
2141 /* FIXME: are we really inside the created dir? */
2142 addir_inherit_acl(vol);
2143 #endif /* HAVE_ACLS */
2145 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2146 *rbuflen = sizeof( u_int32_t );
2147 setvoltime(obj, vol );
2152 * dst new unix filename (not a pathname)
2153 * newname new mac name
2155 * dirfd -1 means ignore dirfd (or use AT_FDCWD), otherwise src is relative to dirfd
2157 int renamedir(const struct vol *vol,
2162 struct dir *newparent,
2168 /* existence check moved to afp_moveandrename */
2169 if ( unix_rename(dirfd, src, -1, dst ) < 0 ) {
2172 return( AFPERR_NOOBJ );
2174 return( AFPERR_ACCESS );
2176 return AFPERR_VLOCK;
2178 /* tried to move directory into a subdirectory of itself */
2179 return AFPERR_CANTMOVE;
2181 /* this needs to copy and delete. bleah. that means we have
2182 * to deal with entire directory hierarchies. */
2183 if ((err = copydir(vol, dirfd, src, dst)) < 0) {
2187 if ((err = deletedir(dirfd, src)) < 0)
2191 return( AFPERR_PARAM );
2195 vol->vfs->vfs_renamedir(vol, dirfd, src, dst);
2197 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2199 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2200 ad_setname(&ad, newname);
2202 ad_close_metadata( &ad);
2205 if (dir_modify(vol, dir, curdir->d_did, 0, newname, dst, curdir->d_fullpath) != 0) {
2206 LOG(log_error, logtype_afpd, "renamedir: fatal error from dir_modify: %s -> %s", src, dst);
2213 /* delete an empty directory */
2214 int deletecurdir(struct vol *vol)
2224 if ( dirlookup(vol, curdir->d_pdid) == NULL ) {
2225 return( AFPERR_ACCESS );
2230 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2231 /* we never want to create a resource fork here, we are going to delete it */
2232 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2234 ad_getattr(&ad, &ashort);
2235 ad_close_metadata(&ad);
2236 if ((ashort & htons(ATTRBIT_NODELETE))) {
2237 return AFPERR_OLOCK;
2240 err = vol->vfs->vfs_deletecurdir(vol);
2245 /* now get rid of dangling symlinks */
2246 if ((dp = opendir("."))) {
2247 while ((de = readdir(dp))) {
2248 /* skip this and previous directory */
2249 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2252 /* bail if it's not a symlink */
2253 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2255 return AFPERR_DIRNEMPT;
2258 if ((err = netatalk_unlink(de->d_name))) {
2265 if ( movecwd(vol, dirlookup(vol, curdir->d_pdid)) < 0 ) {
2270 err = netatalk_rmdir_all_errors(-1, cfrombstring(fdir->d_u_name));
2271 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2272 cnid_delete(vol->v_cdb, fdir->d_did);
2273 dir_remove( vol, fdir );
2277 /* inode is used as key for cnid.
2278 * Close the descriptor only after cnid_delete
2286 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2296 sfunc = (unsigned char) *ibuf++;
2300 if (sfunc >= 3 && sfunc <= 6) {
2301 if (afp_version < 30) {
2302 return( AFPERR_PARAM );
2309 case 3 :/* unicode */
2310 memcpy( &id, ibuf, sizeof( id ));
2313 if (( pw = getpwuid( id )) == NULL ) {
2314 return( AFPERR_NOITEM );
2316 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2317 pw->pw_name, -1, &name);
2324 case 4 : /* unicode */
2325 memcpy( &id, ibuf, sizeof( id ));
2328 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2329 return( AFPERR_NOITEM );
2331 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2332 gr->gr_name, -1, &name);
2339 case 5 : /* UUID -> username */
2340 case 6 : /* UUID -> groupname */
2341 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2342 return AFPERR_PARAM;
2343 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2345 len = getnamefromuuid( ibuf, &name, &type);
2346 if (len != 0) /* its a error code, not len */
2347 return AFPERR_NOITEM;
2348 if (type == UUID_USER) {
2349 if (( pw = getpwnam( name )) == NULL )
2350 return( AFPERR_NOITEM );
2351 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2352 id = htonl(UUID_USER);
2353 memcpy( rbuf, &id, sizeof( id ));
2354 id = htonl( pw->pw_uid);
2355 rbuf += sizeof( id );
2356 memcpy( rbuf, &id, sizeof( id ));
2357 rbuf += sizeof( id );
2358 *rbuflen = 2 * sizeof( id );
2359 } else { /* type == UUID_GROUP */
2360 if (( gr = getgrnam( name )) == NULL )
2361 return( AFPERR_NOITEM );
2362 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2363 id = htonl(UUID_GROUP);
2364 memcpy( rbuf, &id, sizeof( id ));
2365 rbuf += sizeof( id );
2366 id = htonl( gr->gr_gid);
2367 memcpy( rbuf, &id, sizeof( id ));
2368 rbuf += sizeof( id );
2369 *rbuflen = 2 * sizeof( id );
2372 #endif /* HAVE_ACLS */
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++;
2427 case 5 : /* username -> UUID */
2428 case 6 : /* groupname -> UUID */
2429 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2430 return AFPERR_PARAM;
2431 memcpy(&ulen, ibuf, sizeof(ulen));
2435 #endif /* HAVE_ACLS */
2437 return( AFPERR_PARAM );
2443 return AFPERR_PARAM;
2446 case 1 : /* unicode */
2448 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2449 return( AFPERR_NOITEM );
2453 memcpy( rbuf, &id, sizeof( id ));
2454 *rbuflen = sizeof( id );
2457 case 2 : /* unicode */
2459 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2460 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2461 return( AFPERR_NOITEM );
2464 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2466 memcpy( rbuf, &id, sizeof( id ));
2467 *rbuflen = sizeof( id );
2470 case 5 : /* username -> UUID */
2471 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2472 if (0 != getuuidfromname(ibuf, UUID_USER, rbuf))
2473 return AFPERR_NOITEM;
2474 *rbuflen = UUID_BINSIZE;
2476 case 6 : /* groupname -> UUID */
2477 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2478 if (0 != getuuidfromname(ibuf, UUID_GROUP, rbuf))
2479 return AFPERR_NOITEM;
2480 *rbuflen = UUID_BINSIZE;
2482 #endif /* HAVE_ACLS */
2488 /* ------------------------------------
2489 variable DID support
2491 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2502 /* do nothing as dids are static for the life of the process. */
2506 memcpy(&vid, ibuf, sizeof( vid ));
2507 ibuf += sizeof( vid );
2508 if (( vol = getvolbyvid( vid )) == NULL ) {
2509 return( AFPERR_PARAM );
2512 memcpy( &did, ibuf, sizeof( did ));
2513 ibuf += sizeof( did );
2514 if (( dir = dirlookup( vol, did )) == NULL ) {
2515 return( AFPERR_PARAM );
2518 /* dir_remove -- deletedid */
2524 /* did creation gets done automatically
2525 * there's a pb again with case but move it to cname
2527 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2530 struct dir *parentdir;
2538 memcpy(&vid, ibuf, sizeof(vid));
2539 ibuf += sizeof( vid );
2541 if (NULL == ( vol = getvolbyvid( vid )) ) {
2542 return( AFPERR_PARAM );
2545 memcpy(&did, ibuf, sizeof(did));
2546 ibuf += sizeof(did);
2548 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2552 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2553 return get_afp_errno(AFPERR_PARAM);
2556 if ( *path->m_name != '\0' ) {
2557 return path_error(path, AFPERR_NOOBJ);
2560 if ( !path->st_valid && of_stat(path ) < 0 ) {
2561 return( AFPERR_NOOBJ );
2563 if ( path->st_errno ) {
2564 return( AFPERR_NOOBJ );
2567 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2568 *rbuflen = sizeof(curdir->d_did);