2 * $Id: directory.c,v 1.131.2.10 2010-02-05 10:27:58 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
17 #include <sys/param.h>
23 #include <atalk/adouble.h>
24 #include <atalk/vfs.h>
25 #include <atalk/afp.h>
26 #include <atalk/util.h>
27 #include <atalk/cnid.h>
28 #include <atalk/logger.h>
29 #include <atalk/uuid.h>
30 #include <atalk/unix.h>
31 #include <atalk/bstrlib.h>
32 #include <atalk/bstradd.h>
34 #include "directory.h"
46 #ifdef HAVE_NFSv4_ACLS
47 extern void addir_inherit_acl(const struct vol *vol);
51 * FIXMEs, loose ends after the dircache rewrite:
52 * o merge dircache_search_by_name and dir_add ??
53 * o case-insensitivity is gone from cname
54 * o directory offspring count calculation probably broken
55 * o doesn't work with CNID backend last and the like,
56 * CNID backend must support persistent CNIDs.
60 /*******************************************************************************************
62 ******************************************************************************************/
65 struct dir rootParent = {
66 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
67 NULL, NULL, 0, 0, /* qidx_node, d_ofork, ctime, d_flags */
68 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
70 struct dir *curdir = &rootParent;
71 struct path Cur_Path = {
77 0, /* stat is not set */
81 /*******************************************************************************************
83 ******************************************************************************************/
86 /* -------------------------
87 appledouble mkdir afp error code.
89 static int netatalk_mkdir(const char *name)
91 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
94 return( AFPERR_NOOBJ );
96 return( AFPERR_VLOCK );
99 return( AFPERR_ACCESS );
101 return( AFPERR_EXIST );
104 return( AFPERR_DFULL );
106 return( AFPERR_PARAM );
112 /* ------------------- */
113 static int deletedir(char *dir)
115 char path[MAXPATHLEN + 1];
123 if ((len = strlen(dir)) +2 > sizeof(path))
127 if ((dp = opendir(dir)) == NULL)
133 remain = sizeof(path) -len -1;
134 while ((de = readdir(dp)) && err == AFP_OK) {
135 /* skip this and previous directory */
136 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
139 if (strlen(de->d_name) > remain) {
143 strcpy(path + len, de->d_name);
144 if (stat(path, &st)) {
147 if (S_ISDIR(st.st_mode)) {
148 err = deletedir(path);
150 err = netatalk_unlink(path);
155 /* okay. the directory is empty. delete it. note: we already got rid
158 err = netatalk_rmdir(dir);
163 /* do a recursive copy. */
164 static int copydir(const struct vol *vol, char *src, char *dst)
166 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
175 /* doesn't exist or the path is too long. */
176 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
177 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
178 ((dp = opendir(src)) == NULL))
181 /* try to create the destination directory */
182 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
187 /* set things up to copy */
191 srem = sizeof(spath) - slen -1;
196 drem = sizeof(dpath) - dlen -1;
199 while ((de = readdir(dp))) {
200 /* skip this and previous directory */
201 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
204 if (strlen(de->d_name) > srem) {
208 strcpy(spath + slen, de->d_name);
210 if (stat(spath, &st) == 0) {
211 if (strlen(de->d_name) > drem) {
215 strcpy(dpath + dlen, de->d_name);
217 if (S_ISDIR(st.st_mode)) {
218 if (AFP_OK != (err = copydir(vol, spath, dpath)))
220 } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL, NULL))) {
224 /* keep the same time stamp. */
225 ut.actime = ut.modtime = st.st_mtime;
231 /* keep the same time stamp. */
232 if (stat(src, &st) == 0) {
233 ut.actime = ut.modtime = st.st_mtime;
242 /* ---------------------
243 * is our cached offspring count valid?
245 static int diroffcnt(struct dir *dir, struct stat *st)
247 return st->st_ctime == dir->ctime;
250 /* --------------------- */
251 static int invisible_dots(const struct vol *vol, const char *name)
253 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
256 /* ------------------ */
257 static int set_dir_errors(struct path *path, const char *where, int err)
262 return AFPERR_ACCESS;
266 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
271 * @brief Convert name in client encoding to server encoding
273 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
274 * This only gets called from cname().
276 * @returns 0 on success, -1 on error
278 * @note If the passed ret->m_name is mangled, we'll demangle it
280 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
282 static char temp[ MAXPATHLEN + 1];
286 if (afp_version >= 30) {
288 if (dir->d_did == DIRDID_ROOT_PARENT) {
290 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
291 * So we compare it with the longname from the current volume and if they match
292 * we overwrite the requested path with the utf8 volume name so that the following
295 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
296 if (strcasecmp(ret->m_name, temp) == 0)
297 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
300 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
301 afp_errno = AFPERR_PARAM;
304 strcpy(ret->m_name, temp);
308 /* check for OS X mangled filename :( */
309 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
310 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
311 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
313 if (t != ret->m_name) {
315 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
316 * flags weren't the same
318 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
319 /* at last got our view of mac name */
320 strcpy(ret->m_name, t);
323 } /* afp_version >= 30 */
325 /* If we haven't got it by now, get it */
326 if (ret->u_name == NULL) {
327 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
328 afp_errno = AFPERR_PARAM;
337 * @brief Build struct path from struct dir
339 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
340 * 1. move cwd into parent dir (we're often already there, but not always)
341 * 2. set struct path to the dirname
343 * AFPERR_ACCESS: the dir is there, we just cant chdir into it
344 * AFPERR_NOOBJ: the dir was there when we stated it in cname, so we have a race
345 * 4. indicate there's no dir for this path
348 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
350 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
356 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
359 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1); /* 3 */
360 if (dir->d_m_name == dir->d_u_name) {
361 ret->u_name = ret->m_name;
363 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
364 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
369 LOG(log_debug, logtype_afpd, "cname('%s') {path-from-dir: AFPERR_ACCESS. curdir:'%s', path:'%s'}",
370 cfrombstring(dir->d_fullpath),
371 cfrombstring(curdir->d_fullpath),
377 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
380 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
381 if (dir->d_m_name == dir->d_u_name) {
382 ret->u_name = ret->m_name;
384 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
385 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
388 ret->d_dir = NULL; /* 4 */
389 dir_remove(vol, dir); /* 5 */
396 /* DEADC0DE: never get here */
401 /*********************************************************************************************
403 ********************************************************************************************/
405 int get_afp_errno(const int param)
407 if (afp_errno != AFPERR_DID1)
413 * @brief Resolve a DID
415 * Resolve a DID, allocate a struct dir for it
416 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
417 * 2. Check if the DID is in the cache.
418 * 3. If it's not in the cache resolve it via the database.
419 * 4. Build complete server-side path to the dir.
420 * 5. Check if it exists and is a directory.
421 * 6. Create the struct dir and populate it.
422 * 7. Add it to the cache.
424 * @param vol (r) pointer to struct vol
425 * @param did (r) DID to resolve
427 * @returns pointer to struct dir
429 * @note FIXME: OSX calls it with bogus id, ie file ID not folder ID,
430 * and we are really bad in this case.
432 struct dir *dirlookup(const struct vol *vol, cnid_t did)
434 static char buffer[12 + MAXPATHLEN + 1];
435 struct bstrList *pathlist = NULL;
436 bstring fullpath = NULL;
438 struct dir *ret = NULL;
442 int buflen = 12 + MAXPATHLEN + 1;
446 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {start}", ntohl(did));
448 /* check for did 0, 1 and 2 */
449 if (did == 0 || vol == NULL) { /* 1 */
450 afp_errno = AFPERR_PARAM;
452 } else if (did == DIRDID_ROOT_PARENT) {
453 rootParent.d_vid = vol->v_vid;
454 return (&rootParent);
455 } else if (did == DIRDID_ROOT) {
459 /* Search the cache */
460 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2 */
461 if (lstat(cfrombstring(ret->d_fullpath), &st) != 0) {
462 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {lstat: %s}", ntohl(did), strerror(errno));
466 /* It's not there anymore, so remove it */
467 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {calling dir_remove()}", ntohl(did));
468 dir_remove(vol, ret);
469 afp_errno = AFPERR_NOOBJ;
480 utf8 = utf8_encoding();
481 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
483 /* Create list for path elements, request 16 list elements for now*/
484 if ((pathlist = bstListCreateMin(16)) == NULL) { /* 4 */
485 LOG(log_error, logtype_afpd, "dirlookup(did: %u): OOM: %s", ntohl(did), strerror(errno));
489 /* Get it from the database */
491 if ( (upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
492 afp_errno = AFPERR_NOOBJ;
498 /* construct path, copy already found uname to path element list*/
499 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
500 afp_errno = AFPERR_MISC;
505 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {%u, %s}", ntohl(did), ntohl(pdid), upath);
507 /* The stuff that follows is for building the full path to the directory */
509 /* work upwards until we reach volume root */
510 while (cnid != DIRDID_ROOT) {
511 /* construct path, copy already found uname to path element list*/
512 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
513 afp_errno = AFPERR_MISC;
519 if ((upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
520 afp_errno = AFPERR_NOOBJ;
526 if ((bstrListPush(pathlist, bfromcstr(vol->v_path))) != BSTR_OK) { /* 4 */
527 afp_errno = AFPERR_MISC;
532 if ((fullpath = bjoinInv(pathlist, bfromcstr("/"))) == NULL) { /* 4 */
533 afp_errno = AFPERR_MISC;
537 /* Finished building the fullpath */
539 /* stat it and check if it's a dir */
540 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstring(fullpath));
542 if (stat(cfrombstring(fullpath), &st) != 0) { /* 5a */
545 afp_errno = AFPERR_NOOBJ;
549 afp_errno = AFPERR_ACCESS;
553 afp_errno = AFPERR_MISC;
558 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
559 afp_errno = AFPERR_BADTYPE;
565 /* Get macname from unix name */
566 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
567 afp_errno = AFPERR_NOOBJ;
572 /* Create struct dir */
573 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath)) == NULL) { /* 6 */
574 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
579 /* Add it to the cache only if it's a dir */
580 if (dircache_add(ret) != 0) { /* 7 */
585 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {end: did:%u, path:'%s'}",
586 ntohl(did), ntohl(pdid), cfrombstring(ret->d_fullpath));
590 bstrListDestroy(pathlist);
593 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
594 ntohl(did), AfpErr2name(afp_errno));
605 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
607 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
612 static u_int32_t did = 0;
613 static char cname[MAXPATHLEN];
614 static char lname[MAXPATHLEN];
615 ucs2_t u2_path[MAXPATHLEN];
616 ucs2_t u2_dename[MAXPATHLEN];
617 char *tmp, *savepath;
619 if (!(vol->v_flags & AFPVOL_CASEINSEN))
622 if (veto_file(ENUMVETO, path->u_name))
625 savepath = path->u_name;
627 /* very simple cache */
628 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
629 path->u_name = cname;
631 if (of_stat( path ) == 0 ) {
634 /* something changed, we cannot stat ... */
638 if (NULL == ( dp = opendir( "." )) ) {
639 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
644 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
645 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
646 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
648 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
650 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
651 if (NULL == check_dirent(vol, de->d_name))
654 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
657 if (strcasecmp_w( u2_path, u2_dename) == 0) {
659 strlcpy(cname, de->d_name, sizeof(cname));
660 path->u_name = cname;
662 if (of_stat( path ) == 0 ) {
663 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
664 strlcpy(lname, tmp, sizeof(lname));
677 /* invalidate cache */
680 path->u_name = savepath;
682 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
688 * @brief Construct struct dir
690 * Construct struct dir from parameters.
692 * @param m_name (r) directory name in UTF8-dec
693 * @param u_name (r) directory name in server side encoding
694 * @param vol (r) pointer to struct vol
695 * @param pdid (r) Parent CNID
696 * @param did (r) CNID
697 * @param fullpath (r) Full unix path to dir
699 * @returns pointer to new struct dir or NULL on error
701 * @note Most of the time mac name and unix name are the same.
703 struct dir *dir_new(const char *m_name,
705 const struct vol *vol,
712 dir = (struct dir *) calloc(1, sizeof( struct dir ));
716 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
721 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
724 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
725 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
726 dir->d_m_name_ucs2 = NULL;
729 if (m_name == u_name || !strcmp(m_name, u_name)) {
730 dir->d_u_name = dir->d_m_name;
732 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
733 bdestroy(dir->d_m_name);
740 dir->d_vid = vol->v_vid;
741 dir->d_fullpath = path;
746 * @brief Free a struct dir and all its members
748 * @param (rw) pointer to struct dir
750 void dir_free(struct dir *dir)
752 if (dir->d_u_name != dir->d_m_name) {
753 bdestroy(dir->d_u_name);
755 if (dir->d_m_name_ucs2)
756 free(dir->d_m_name_ucs2);
757 bdestroy(dir->d_m_name);
758 bdestroy(dir->d_fullpath);
763 * @brief Create struct dir from struct path
765 * Create a new struct dir from struct path. Then add it to the cache.
766 * The caller must have assured that the dir is not already in the cache,
768 * 1. Open adouble file, get CNID from it.
769 * 2. Search the database, hinting with the CNID from (1).
770 * 3. Build fullpath and create struct dir.
771 * 4. Add it to the cache.
773 * @param vol (r) pointer to struct vol
774 * @param dir (r) pointer to parrent directory
775 * @param path (rw) pointer to struct path with valid path->u_name
776 * @param len (r) strlen of path->u_name
778 * @returns Pointer to new struct dir or NULL on error.
780 * @note Function also assigns path->m_name from path->u_name.
782 struct dir *dir_add(const struct vol *vol, const struct dir *dir, struct path *path, int len)
785 struct dir *cdir = NULL;
788 struct adouble *adp = NULL;
796 if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name))) != NULL) {
797 /* there's a stray entry in the dircache */
798 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
799 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
800 ntohl(cdir->d_did), cfrombstring(dir->d_fullpath));
801 if (dir_remove(vol, cdir) != 0) {
807 /* get_id needs adp for reading CNID from adouble file */
808 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
809 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
813 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
819 ad_close_metadata(adp);
821 /* Get macname from unixname */
822 if (path->m_name == NULL) {
823 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
830 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
831 || (bconchar(fullpath, '/') != BSTR_OK)
832 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
833 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
838 /* Allocate and initialize struct dir */
839 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath)) == NULL) { /* 3 */
844 if ((dircache_add(cdir)) != 0) { /* 4 */
845 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstring(fullpath));
851 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
852 cfrombstring(dir->d_u_name), path->u_name, err);
855 ad_close_metadata(adp);
856 if (!cdir && fullpath)
863 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
864 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
865 ntohl(cdir->d_did), cfrombstring(cdir->d_fullpath));
873 FIXME: open forks ??!!!
875 * @brief Remove a dir from a cache and free it and any ressources with it
877 * @param (r) pointer to struct vol
878 * @param (rw) pointer to struct dir
880 int dir_remove(const struct vol *vol, struct dir *dir)
885 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
888 if (dir->d_flags & DIRF_CACHELOCK) {
889 LOG(log_warning, logtype_afpd, "dir_remove(did:%u,'%s'): attempt to remove a locked dir",
890 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
895 if (movecwd(vol, vol->v_root) < 0) {
896 LOG(log_error, logtype_afpd, "dir_remove: can't chdir to : %s", vol->v_root);
900 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
901 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
903 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
910 * @brief Modify a struct dir, adjust cache
912 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
913 * If given new_uname == new_mname, new_uname will point to new_mname.
915 * @param vol (r) pointer to struct vol
916 * @param dir (rw) pointer to struct dir
917 * @param pdid (r) new parent DID
918 * @param did (r) new DID
919 * @param new_mname (r) new mac-name
920 * @param new_uname (r) new unix-name
922 int dir_modify(const struct vol *vol,
926 const char *new_mname,
927 const char *new_uname,
928 bstring pdir_fullpath)
932 /* Remove it from the cache */
933 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
941 /* free uname if it's not the same as mname */
942 if (dir->d_m_name != dir->d_u_name)
943 bdestroy(dir->d_u_name);
945 if (new_uname == NULL)
946 new_uname = new_mname;
948 /* assign new name */
949 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
950 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
954 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
955 dir->d_u_name = dir->d_m_name;
957 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
958 LOG(log_error, logtype_afpd, "renamedir: bassigncstr: %s", strerror(errno) );
965 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
967 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
969 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
973 if (dir->d_m_name_ucs2)
974 free(dir->d_m_name_ucs2);
975 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))
976 dir->d_m_name_ucs2 = NULL;
978 /* Re-add it to the cache */
979 if ((dircache_add(dir)) != 0) {
988 * @brief Resolve a catalog node name path
990 * If it's a filename:
991 * 1. compute unix name
992 * 2. stat the file, storing struct stat or errno in struct path
993 * 3. cwd (and curdir) is filename parent directory
994 * 4. return path with with filename
997 * 5. search the dircache
998 * 6. if not in the cache:
999 * 7. compute unix name
1000 * 8. stat the dir, storing struct stat or errno in struct path
1002 * 10. if chdir failed, return path with dirname
1003 * cwd is dir parent directory
1004 * if chdir succeeded, add to dircache
1005 * return path with "" and "."
1007 * 11. else in the cache:
1008 * 12. stat the dir, storing struct stat or errno in struct path
1010 * 14. if chdir failed
1013 * if not last path part, return NULL
1018 curdir: dir parent directory
1023 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1025 static char path[ MAXPATHLEN + 1];
1026 static struct path ret;
1036 LOG(log_maxdebug, logtype_afpd, "came('%s'): {start}", cfrombstring(dir->d_fullpath));
1039 afp_errno = AFPERR_NOOBJ;
1040 memset(&ret, 0, sizeof(ret));
1042 switch (ret.m_type = *data) { /* path type */
1045 len = (unsigned char) *data++;
1047 if (afp_version >= 30) {
1053 if (afp_version >= 30) {
1055 memcpy(&hint, data, sizeof(hint));
1057 data += sizeof(hint);
1059 memcpy(&len16, data, sizeof(len16));
1065 /* else it's an error */
1067 afp_errno = AFPERR_PARAM;
1070 *cpath += len + size;
1075 if (movecwd(vol, dir) < 0 ) {
1076 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1077 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1079 return path_from_dir(vol, dir, &ret);
1087 * 1. single 0 -> delimiter
1088 * 2. additional 0 -> chdir(..)
1090 * a) name is a file, build struct path from it etc., exit while loop
1091 * b) name is a dir, add it to the dircache, chdir to it, continue
1093 if (*data == 0) { /* case 1 or 2 */
1096 while (len > 0 && *data == 0) { /* case 2 */
1097 /* chdir to parrent dir */
1098 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1100 if (movecwd( vol, dir ) < 0 ) {
1101 dir_remove(vol, dir);
1110 /* case 3: copy name from packet buffer to ret.m_name and process it */
1111 for ( p = path; *data != 0 && len > 0; len-- ) {
1113 if (p > &path[ MAXPATHLEN]) {
1114 afp_errno = AFPERR_PARAM;
1118 *p = 0; /* Terminate string */
1121 /* Get u_name from m_name */
1122 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) {
1123 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1127 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstring(dir->d_fullpath), ret.u_name);
1129 /* Prevent access to our special folders like .AppleDouble */
1130 if (check_name(vol, ret.u_name)) {
1131 /* the name is illegal */
1132 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1133 afp_errno = AFPERR_PARAM;
1137 if (dir->d_did == DIRDID_ROOT_PARENT) {
1139 * Special case: CNID 1
1140 * root parent (did 1) has one child: the volume. Requests for did=1 with
1141 * some <name> must check against the volume name.
1143 if ((strcmp(cfrombstring(vol->v_root->d_m_name), ret.m_name)) == 0)
1149 * CNID != 1, eg. most of the times we take this way.
1150 * Now check if current path-part is a file or dir:
1151 * o if it's dir we have to step into it
1152 * o if it's a file we expect it to be the last part of the requested path
1153 * and thus call continue which should terminate the while loop because
1156 if (of_stat(&ret) != 0) {
1158 * ret.u_name doesn't exist, might be afp_createfile|dir
1159 * that means it should have been the last part
1162 /* it wasn't the last part, so we have a bogus path request */
1163 afp_errno = AFPERR_NOOBJ;
1167 * this will terminate clean in while (1) because len == 0,
1168 * probably afp_createfile|dir
1170 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1174 switch (ret.st.st_mode & S_IFMT) {
1176 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1178 /* it wasn't the last part, so we have a bogus path request */
1179 afp_errno = AFPERR_PARAM;
1182 continue; /* continues while loop */
1184 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1186 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1187 afp_errno = AFPERR_PARAM;
1190 continue; /* continues while loop */
1194 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1195 afp_errno = AFPERR_NODIR;
1199 /* Search the cache */
1200 int unamelen = strlen(ret.u_name);
1201 cdir = dircache_search_by_name(vol, dir, ret.u_name, unamelen);
1203 /* Not in cache, create one */
1204 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) {
1205 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1206 ntohl(dir->d_did), ret.u_name, getcwdpath());
1210 } /* if/else cnid==1 */
1212 /* Now chdir to the evaluated dir */
1213 if (movecwd( vol, cdir ) < 0 ) {
1214 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1215 cfrombstring(curdir->d_fullpath), cfrombstring(cdir->d_fullpath), strerror(errno));
1217 return path_from_dir(vol, cdir, &ret);
1222 ret.m_name[0] = 0; /* so we later know last token was a dir */
1225 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1226 afp_errno = AFPERR_DID1;
1230 if (ret.m_name[0] == 0) {
1231 /* Last part was a dir */
1232 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1236 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1237 cfrombstring(dir->d_fullpath),
1238 cfrombstring(curdir->d_fullpath),
1245 * @brief chdir() to dir
1247 * @param vol (r) pointer to struct vol
1248 * @param dir (r) pointer to struct dir
1250 * @returns 0 on success, -1 on error with afp_errno set appropiately
1252 int movecwd(const struct vol *vol, struct dir *dir)
1259 LOG(log_maxdebug, logtype_afpd, "movecwd(curdir:'%s', cwd:'%s')",
1260 cfrombstring(curdir->d_fullpath), getcwdpath());
1264 if (dir->d_did == DIRDID_ROOT_PARENT) {
1265 curdir = &rootParent;
1269 LOG(log_debug, logtype_afpd, "movecwd(did:%u, '%s')", ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1271 if ( chdir(cfrombstring(dir->d_fullpath)) < 0 ) {
1272 LOG(log_debug, logtype_afpd, "movecwd('%s'): %s", cfrombstring(dir->d_fullpath), strerror(errno));
1276 afp_errno = AFPERR_ACCESS;
1279 afp_errno = AFPERR_NOOBJ;
1289 * We can't use unix file's perm to support Apple's inherited protection modes.
1290 * If we aren't the file's owner we can't change its perms when moving it and smb
1291 * nfs,... don't even try.
1293 #define AFP_CHECK_ACCESS
1295 int check_access(char *path, int mode)
1297 #ifdef AFP_CHECK_ACCESS
1305 accessmode(p, &ma, curdir, NULL);
1306 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1308 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1314 /* --------------------- */
1315 int file_access(struct path *path, int mode)
1319 accessmode(path->u_name, &ma, curdir, &path->st);
1320 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1322 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1328 /* --------------------- */
1329 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1331 dir->offcnt = count;
1332 dir->ctime = st->st_ctime;
1333 dir->d_flags &= ~DIRF_CNID;
1337 /* ---------------------
1338 * is our cached also for reenumerate id?
1340 int dirreenumerate(struct dir *dir, struct stat *st)
1342 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1345 /* ------------------------------
1347 (name, dir) with curdir:name == dir, from afp_enumerate
1350 int getdirparams(const struct vol *vol,
1351 u_int16_t bitmap, struct path *s_path,
1353 char *buf, size_t *buflen )
1357 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1358 int bit = 0, isad = 0;
1364 struct stat *st = &s_path->st;
1365 char *upath = s_path->u_name;
1367 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1368 (1 << DIRPBIT_CDATE) |
1369 (1 << DIRPBIT_MDATE) |
1370 (1 << DIRPBIT_BDATE) |
1371 (1 << DIRPBIT_FINFO)))) {
1373 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1374 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1376 if (ad.ad_md->adf_flags & O_CREAT) {
1377 /* We just created it */
1378 ad_setname(&ad, s_path->m_name);
1393 while ( bitmap != 0 ) {
1394 while (( bitmap & 1 ) == 0 ) {
1402 ad_getattr(&ad, &ashort);
1403 } else if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1404 ashort = htons(ATTRBIT_INVISIBLE);
1407 ashort |= htons(ATTRBIT_SHARED);
1408 memcpy( data, &ashort, sizeof( ashort ));
1409 data += sizeof( ashort );
1413 memcpy( data, &pdid, sizeof( pdid ));
1414 data += sizeof( pdid );
1415 LOG(log_debug, logtype_afpd, "metadata('%s'): Parent DID: %u",
1416 s_path->u_name, ntohl(pdid));
1419 case DIRPBIT_CDATE :
1420 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1421 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1422 memcpy( data, &aint, sizeof( aint ));
1423 data += sizeof( aint );
1426 case DIRPBIT_MDATE :
1427 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1428 memcpy( data, &aint, sizeof( aint ));
1429 data += sizeof( aint );
1432 case DIRPBIT_BDATE :
1433 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1434 aint = AD_DATE_START;
1435 memcpy( data, &aint, sizeof( aint ));
1436 data += sizeof( aint );
1439 case DIRPBIT_FINFO :
1441 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1442 } else { /* no appledouble */
1443 memset( data, 0, 32 );
1444 /* set default view -- this also gets done in ad_open() */
1445 ashort = htons(FINDERINFO_CLOSEDVIEW);
1446 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1448 /* dot files are by default visible */
1449 if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1450 ashort = htons(FINDERINFO_INVISIBLE);
1451 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1457 case DIRPBIT_LNAME :
1458 if (dir->d_m_name) /* root of parent can have a null name */
1461 memset(data, 0, sizeof(u_int16_t));
1462 data += sizeof( u_int16_t );
1465 case DIRPBIT_SNAME :
1466 memset(data, 0, sizeof(u_int16_t));
1467 data += sizeof( u_int16_t );
1471 memcpy( data, &dir->d_did, sizeof( aint ));
1472 data += sizeof( aint );
1473 LOG(log_debug, logtype_afpd, "metadata('%s'): DID: %u",
1474 s_path->u_name, ntohl(dir->d_did));
1477 case DIRPBIT_OFFCNT :
1479 /* this needs to handle current directory access rights */
1480 if (diroffcnt(dir, st)) {
1481 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1483 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1484 setdiroffcnt(dir, st, ret);
1485 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1487 ashort = htons( ashort );
1488 memcpy( data, &ashort, sizeof( ashort ));
1489 data += sizeof( ashort );
1493 aint = htonl(st->st_uid);
1494 memcpy( data, &aint, sizeof( aint ));
1495 data += sizeof( aint );
1499 aint = htonl(st->st_gid);
1500 memcpy( data, &aint, sizeof( aint ));
1501 data += sizeof( aint );
1504 case DIRPBIT_ACCESS :
1505 accessmode( upath, &ma, dir , st);
1507 *data++ = ma.ma_user;
1508 *data++ = ma.ma_world;
1509 *data++ = ma.ma_group;
1510 *data++ = ma.ma_owner;
1513 /* Client has requested the ProDOS information block.
1514 Just pass back the same basic block for all
1515 directories. <shirsch@ibm.net> */
1516 case DIRPBIT_PDINFO :
1517 if (afp_version >= 30) { /* UTF8 name */
1518 utf8 = kTextEncodingUTF8;
1519 if (dir->d_m_name) /* root of parent can have a null name */
1522 memset(data, 0, sizeof(u_int16_t));
1523 data += sizeof( u_int16_t );
1525 memcpy(data, &aint, sizeof( aint ));
1526 data += sizeof( aint );
1528 else { /* ProDOS Info Block */
1531 ashort = htons( 0x0200 );
1532 memcpy( data, &ashort, sizeof( ashort ));
1533 data += sizeof( ashort );
1534 memset( data, 0, sizeof( ashort ));
1535 data += sizeof( ashort );
1539 case DIRPBIT_UNIXPR :
1540 aint = htonl(st->st_uid);
1541 memcpy( data, &aint, sizeof( aint ));
1542 data += sizeof( aint );
1543 aint = htonl(st->st_gid);
1544 memcpy( data, &aint, sizeof( aint ));
1545 data += sizeof( aint );
1548 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1549 memcpy( data, &aint, sizeof( aint ));
1550 data += sizeof( aint );
1552 accessmode( upath, &ma, dir , st);
1554 *data++ = ma.ma_user;
1555 *data++ = ma.ma_world;
1556 *data++ = ma.ma_group;
1557 *data++ = ma.ma_owner;
1562 ad_close_metadata( &ad );
1564 return( AFPERR_BITMAP );
1570 ashort = htons( data - buf );
1571 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1572 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, 0);
1574 if ( utf_nameoff ) {
1575 ashort = htons( data - buf );
1576 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1577 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, utf8);
1580 ad_close_metadata( &ad );
1582 *buflen = data - buf;
1586 /* ----------------------------- */
1587 int path_error(struct path *path, int error)
1589 /* - a dir with access error
1590 * - no error it's a file
1593 if (path_isadir(path))
1595 if (path->st_valid && path->st_errno)
1597 return AFPERR_BADTYPE ;
1600 /* ----------------------------- */
1601 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1606 u_int16_t vid, bitmap;
1612 memcpy( &vid, ibuf, sizeof( vid ));
1613 ibuf += sizeof( vid );
1615 if (NULL == ( vol = getvolbyvid( vid )) ) {
1616 return( AFPERR_PARAM );
1619 if (vol->v_flags & AFPVOL_RO)
1620 return AFPERR_VLOCK;
1622 memcpy( &did, ibuf, sizeof( did ));
1623 ibuf += sizeof( int );
1625 if (NULL == ( dir = dirlookup( vol, did )) ) {
1629 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1630 bitmap = ntohs( bitmap );
1631 ibuf += sizeof( bitmap );
1633 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1634 return get_afp_errno(AFPERR_NOOBJ);
1637 if ( *path->m_name != '\0' ) {
1638 rc = path_error(path, AFPERR_NOOBJ);
1639 /* maybe we are trying to set perms back */
1640 if (rc != AFPERR_ACCESS)
1645 * If ibuf is odd, make it even.
1647 if ((u_long)ibuf & 1 ) {
1651 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1652 setvoltime(obj, vol );
1658 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1660 * assume path == '\0' eg. it's a directory in canonical form
1662 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1674 u_int16_t ashort, bshort, oshort;
1676 int change_mdate = 0;
1677 int change_parent_mdate = 0;
1679 u_int16_t bitmap = d_bitmap;
1680 u_char finder_buf[32];
1683 u_int16_t upriv_bit = 0;
1686 upath = path->u_name;
1688 while ( bitmap != 0 ) {
1689 while (( bitmap & 1 ) == 0 ) {
1697 memcpy( &ashort, buf, sizeof( ashort ));
1698 buf += sizeof( ashort );
1700 case DIRPBIT_CDATE :
1702 memcpy(&cdate, buf, sizeof(cdate));
1703 buf += sizeof( cdate );
1705 case DIRPBIT_MDATE :
1706 memcpy(&newdate, buf, sizeof(newdate));
1707 buf += sizeof( newdate );
1709 case DIRPBIT_BDATE :
1711 memcpy(&bdate, buf, sizeof(bdate));
1712 buf += sizeof( bdate );
1714 case DIRPBIT_FINFO :
1716 memcpy( finder_buf, buf, 32 );
1719 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1720 change_parent_mdate = 1;
1721 memcpy( &owner, buf, sizeof(owner));
1722 buf += sizeof( owner );
1725 change_parent_mdate = 1;
1726 memcpy( &group, buf, sizeof( group ));
1727 buf += sizeof( group );
1729 case DIRPBIT_ACCESS :
1731 change_parent_mdate = 1;
1732 ma.ma_user = *buf++;
1733 ma.ma_world = *buf++;
1734 ma.ma_group = *buf++;
1735 ma.ma_owner = *buf++;
1736 mpriv = mtoumode( &ma ) | vol->v_dperm;
1737 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1738 err = set_dir_errors(path, "setdirmode", errno);
1742 /* Ignore what the client thinks we should do to the
1743 ProDOS information block. Skip over the data and
1744 report nothing amiss. <shirsch@ibm.net> */
1745 case DIRPBIT_PDINFO :
1746 if (afp_version < 30) {
1750 err = AFPERR_BITMAP;
1754 case DIRPBIT_UNIXPR :
1755 if (vol_unix_priv(vol)) {
1756 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1757 buf += sizeof( owner );
1758 memcpy( &group, buf, sizeof( group ));
1759 buf += sizeof( group );
1762 change_parent_mdate = 1;
1763 memcpy( &upriv, buf, sizeof( upriv ));
1764 buf += sizeof( upriv );
1765 upriv = ntohl (upriv) | vol->v_dperm;
1766 if (dir_rx_set(upriv)) {
1767 /* maybe we are trying to set perms back */
1768 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1770 err = set_dir_errors(path, "setdirunixmode", errno);
1781 err = AFPERR_BITMAP;
1789 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1791 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1793 * Check to see what we're trying to set. If it's anything
1794 * but ACCESS, UID, or GID, give an error. If it's any of those
1795 * three, we don't need the ad to be open, so just continue.
1797 * note: we also don't need to worry about mdate. also, be quiet
1798 * if we're using the noadouble option.
1800 if (!vol_noadouble(vol) && (d_bitmap &
1801 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1802 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1803 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1804 return AFPERR_ACCESS;
1810 * Check to see if a create was necessary. If it was, we'll want
1811 * to set our name, etc.
1813 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1814 ad_setname(&ad, cfrombstring(curdir->d_m_name));
1820 while ( bitmap != 0 ) {
1821 while (( bitmap & 1 ) == 0 ) {
1829 ad_getattr(&ad, &bshort);
1831 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1832 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1836 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1837 change_parent_mdate = 1;
1838 ad_setattr(&ad, bshort);
1841 case DIRPBIT_CDATE :
1843 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1846 case DIRPBIT_MDATE :
1848 case DIRPBIT_BDATE :
1850 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1853 case DIRPBIT_FINFO :
1855 /* Fixes #2802236 */
1856 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1857 *fflags &= htons(~FINDERINFO_ISHARED);
1859 if ( dir->d_did == DIRDID_ROOT ) {
1861 * Alright, we admit it, this is *really* sick!
1862 * The 4 bytes that we don't copy, when we're dealing
1863 * with the root of a volume, are the directory's
1864 * location information. This eliminates that annoying
1865 * behavior one sees when mounting above another mount
1868 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1869 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1871 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1875 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1876 if ( (dir->d_did == DIRDID_ROOT) &&
1877 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1878 err = set_dir_errors(path, "setdeskowner", errno);
1879 if (isad && err == AFPERR_PARAM) {
1880 err = AFP_OK; /* ???*/
1883 goto setdirparam_done;
1886 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1887 err = set_dir_errors(path, "setdirowner", errno);
1888 goto setdirparam_done;
1892 if (dir->d_did == DIRDID_ROOT)
1893 setdeskowner( -1, ntohl(group) );
1894 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1895 err = set_dir_errors(path, "setdirowner", errno);
1896 goto setdirparam_done;
1899 case DIRPBIT_ACCESS :
1900 if (dir->d_did == DIRDID_ROOT) {
1902 if (!dir_rx_set(mpriv)) {
1903 /* we can't remove read and search for owner on volume root */
1904 err = AFPERR_ACCESS;
1905 goto setdirparam_done;
1909 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1910 err = set_dir_errors(path, "setdirmode", errno);
1911 goto setdirparam_done;
1914 case DIRPBIT_PDINFO :
1915 if (afp_version >= 30) {
1916 err = AFPERR_BITMAP;
1917 goto setdirparam_done;
1920 case DIRPBIT_UNIXPR :
1921 if (vol_unix_priv(vol)) {
1922 if (dir->d_did == DIRDID_ROOT) {
1923 if (!dir_rx_set(upriv)) {
1924 /* we can't remove read and search for owner on volume root */
1925 err = AFPERR_ACCESS;
1926 goto setdirparam_done;
1928 setdeskowner( -1, ntohl(group) );
1929 setdeskmode( upriv );
1931 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1932 err = set_dir_errors(path, "setdirowner", errno);
1933 goto setdirparam_done;
1936 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1937 err = set_dir_errors(path, "setdirunixmode", errno);
1938 goto setdirparam_done;
1942 err = AFPERR_BITMAP;
1943 goto setdirparam_done;
1947 err = AFPERR_BITMAP;
1948 goto setdirparam_done;
1957 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1958 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1962 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1963 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1968 if (path->st_valid && !path->st_errno) {
1969 struct stat *st = &path->st;
1971 if (dir && dir->d_pdid) {
1972 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1976 ad_close_metadata( &ad);
1979 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1980 && gettimeofday(&tv, NULL) == 0) {
1981 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) == 0) {
1982 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1983 /* be careful with bitmap because now dir is null */
1984 bitmap = 1<<DIRPBIT_MDATE;
1985 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1986 /* should we reset curdir ?*/
1993 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2007 memcpy( &vid, ibuf, sizeof( vid ));
2008 ibuf += sizeof( vid );
2009 if (NULL == (vol = getvolbyvid( vid )) ) {
2010 return( AFPERR_PARAM );
2013 memcpy( &did, ibuf, sizeof( did ));
2014 ibuf += sizeof( did );
2018 * if it's CNID 2 our only choice to meet the specs is call sync.
2019 * For any other CNID just sync that dir. To my knowledge the
2020 * intended use of FPSyncDir is to sync the volume so all we're
2021 * ever going to see here is probably CNID 2. Anyway, we' prepared.
2024 if ( ntohl(did) == 2 ) {
2027 if (NULL == ( dir = dirlookup( vol, did )) ) {
2028 return afp_errno; /* was AFPERR_NOOBJ */
2031 if (movecwd( vol, dir ) < 0 )
2032 return ( AFPERR_NOOBJ );
2035 * Assuming only OSens that have dirfd also may require fsyncing directories
2036 * in order to flush metadata e.g. Linux.
2040 if (NULL == ( dp = opendir( "." )) ) {
2043 return( AFPERR_NOOBJ );
2045 return( AFPERR_ACCESS );
2047 return( AFPERR_PARAM );
2051 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2054 if ( fsync ( dfd ) < 0 )
2055 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2056 dir->d_u_name, strerror(errno) );
2057 closedir(dp); /* closes dfd too */
2060 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2063 return( AFPERR_NOOBJ );
2065 return( AFPERR_ACCESS );
2067 return( AFPERR_PARAM );
2071 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2072 vol->ad_path(".", ADFLAGS_DIR) );
2074 if ( fsync(dfd) < 0 )
2075 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2076 vol->ad_path(cfrombstring(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2083 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2089 struct path *s_path;
2097 memcpy( &vid, ibuf, sizeof( vid ));
2098 ibuf += sizeof( vid );
2099 if (NULL == ( vol = getvolbyvid( vid )) ) {
2100 return( AFPERR_PARAM );
2103 if (vol->v_flags & AFPVOL_RO)
2104 return AFPERR_VLOCK;
2106 memcpy( &did, ibuf, sizeof( did ));
2107 ibuf += sizeof( did );
2108 if (NULL == ( dir = dirlookup( vol, did )) ) {
2109 return afp_errno; /* was AFPERR_NOOBJ */
2111 /* for concurrent access we need to be sure we are not in the
2112 * folder we want to create...
2116 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2117 return get_afp_errno(AFPERR_PARAM);
2119 /* cname was able to move curdir to it! */
2120 if (*s_path->m_name == '\0')
2121 return AFPERR_EXIST;
2123 upath = s_path->u_name;
2125 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2129 if (of_stat(s_path) < 0) {
2135 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2139 if ( movecwd( vol, dir ) < 0 ) {
2140 return( AFPERR_PARAM );
2143 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2144 if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2145 if (vol_noadouble(vol))
2146 goto createdir_done;
2147 return( AFPERR_ACCESS );
2149 ad_setname(&ad, s_path->m_name);
2150 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2153 ad_close_metadata( &ad);
2156 #ifdef HAVE_NFSv4_ACLS
2157 /* FIXME: are we really inside the created dir? */
2158 addir_inherit_acl(vol);
2161 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2162 *rbuflen = sizeof( u_int32_t );
2163 setvoltime(obj, vol );
2168 * dst new unix filename (not a pathname)
2169 * newname new mac name
2172 int renamedir(const struct vol *vol, char *src, char *dst,
2174 struct dir *newparent,
2180 /* existence check moved to afp_moveandrename */
2181 if ( unix_rename( src, dst ) < 0 ) {
2184 return( AFPERR_NOOBJ );
2186 return( AFPERR_ACCESS );
2188 return AFPERR_VLOCK;
2190 /* tried to move directory into a subdirectory of itself */
2191 return AFPERR_CANTMOVE;
2193 /* this needs to copy and delete. bleah. that means we have
2194 * to deal with entire directory hierarchies. */
2195 if ((err = copydir(vol, src, dst)) < 0) {
2199 if ((err = deletedir(src)) < 0)
2203 return( AFPERR_PARAM );
2207 vol->vfs->vfs_renamedir(vol, src, dst);
2209 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2211 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2212 ad_setname(&ad, newname);
2214 ad_close_metadata( &ad);
2217 if (dir_modify(vol, dir, curdir->d_did, 0, newname, dst, curdir->d_fullpath) != 0) {
2218 LOG(log_error, logtype_afpd, "renamedir: fatal error from dir_modify: %s -> %s", src, dst);
2225 /* delete an empty directory */
2226 int deletecurdir(struct vol *vol)
2236 if ( dirlookup(vol, curdir->d_pdid) == NULL ) {
2237 return( AFPERR_ACCESS );
2242 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2243 /* we never want to create a resource fork here, we are going to delete it */
2244 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2246 ad_getattr(&ad, &ashort);
2247 ad_close( &ad, ADFLAGS_HF );
2248 if ((ashort & htons(ATTRBIT_NODELETE))) {
2249 return AFPERR_OLOCK;
2252 err = vol->vfs->vfs_deletecurdir(vol);
2257 /* now get rid of dangling symlinks */
2258 if ((dp = opendir("."))) {
2259 while ((de = readdir(dp))) {
2260 /* skip this and previous directory */
2261 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2264 /* bail if it's not a symlink */
2265 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2267 return AFPERR_DIRNEMPT;
2270 if ((err = netatalk_unlink(de->d_name))) {
2277 if ( movecwd(vol, dirlookup(vol, curdir->d_pdid)) < 0 ) {
2282 err = netatalk_rmdir_all_errors(cfrombstring(fdir->d_u_name));
2283 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2284 cnid_delete(vol->v_cdb, fdir->d_did);
2285 dir_remove( vol, fdir );
2289 /* inode is used as key for cnid.
2290 * Close the descriptor only after cnid_delete
2298 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2308 sfunc = (unsigned char) *ibuf++;
2312 if (sfunc >= 3 && sfunc <= 6) {
2313 if (afp_version < 30) {
2314 return( AFPERR_PARAM );
2321 case 3 :/* unicode */
2322 memcpy( &id, ibuf, sizeof( id ));
2325 if (( pw = getpwuid( id )) == NULL ) {
2326 return( AFPERR_NOITEM );
2328 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2329 pw->pw_name, -1, &name);
2336 case 4 : /* unicode */
2337 memcpy( &id, ibuf, sizeof( id ));
2340 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2341 return( AFPERR_NOITEM );
2343 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2344 gr->gr_name, -1, &name);
2350 #ifdef HAVE_NFSv4_ACLS
2351 case 5 : /* UUID -> username */
2352 case 6 : /* UUID -> groupname */
2353 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2354 return AFPERR_PARAM;
2355 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2357 len = getnamefromuuid( ibuf, &name, &type);
2358 if (len != 0) /* its a error code, not len */
2359 return AFPERR_NOITEM;
2360 if (type == UUID_USER) {
2361 if (( pw = getpwnam( name )) == NULL )
2362 return( AFPERR_NOITEM );
2363 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2364 id = htonl(UUID_USER);
2365 memcpy( rbuf, &id, sizeof( id ));
2366 id = htonl( pw->pw_uid);
2367 rbuf += sizeof( id );
2368 memcpy( rbuf, &id, sizeof( id ));
2369 rbuf += sizeof( id );
2370 *rbuflen = 2 * sizeof( id );
2371 } else { /* type == UUID_GROUP */
2372 if (( gr = getgrnam( name )) == NULL )
2373 return( AFPERR_NOITEM );
2374 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2375 id = htonl(UUID_GROUP);
2376 memcpy( rbuf, &id, sizeof( id ));
2377 rbuf += sizeof( id );
2378 id = htonl( gr->gr_gid);
2379 memcpy( rbuf, &id, sizeof( id ));
2380 rbuf += sizeof( id );
2381 *rbuflen = 2 * sizeof( id );
2386 return( AFPERR_PARAM );
2390 len = strlen( name );
2393 u_int16_t tp = htons(len);
2394 memcpy(rbuf, &tp, sizeof(tp));
2403 memcpy( rbuf, name, len );
2411 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2420 sfunc = (unsigned char) *ibuf++;
2422 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2425 case 2 : /* unicode */
2426 if (afp_version < 30) {
2427 return( AFPERR_PARAM );
2429 memcpy(&ulen, ibuf, sizeof(ulen));
2432 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2436 len = (unsigned char) *ibuf++;
2438 #ifdef HAVE_NFSv4_ACLS
2439 case 5 : /* username -> UUID */
2440 case 6 : /* groupname -> UUID */
2441 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2442 return AFPERR_PARAM;
2443 memcpy(&ulen, ibuf, sizeof(ulen));
2449 return( AFPERR_PARAM );
2455 return AFPERR_PARAM;
2458 case 1 : /* unicode */
2460 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2461 return( AFPERR_NOITEM );
2465 memcpy( rbuf, &id, sizeof( id ));
2466 *rbuflen = sizeof( id );
2469 case 2 : /* unicode */
2471 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2472 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2473 return( AFPERR_NOITEM );
2476 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2478 memcpy( rbuf, &id, sizeof( id ));
2479 *rbuflen = sizeof( id );
2481 #ifdef HAVE_NFSv4_ACLS
2482 case 5 : /* username -> UUID */
2483 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2484 if (0 != getuuidfromname(ibuf, UUID_USER, rbuf))
2485 return AFPERR_NOITEM;
2486 *rbuflen = UUID_BINSIZE;
2488 case 6 : /* groupname -> UUID */
2489 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2490 if (0 != getuuidfromname(ibuf, UUID_GROUP, rbuf))
2491 return AFPERR_NOITEM;
2492 *rbuflen = UUID_BINSIZE;
2500 /* ------------------------------------
2501 variable DID support
2503 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2514 /* do nothing as dids are static for the life of the process. */
2518 memcpy(&vid, ibuf, sizeof( vid ));
2519 ibuf += sizeof( vid );
2520 if (( vol = getvolbyvid( vid )) == NULL ) {
2521 return( AFPERR_PARAM );
2524 memcpy( &did, ibuf, sizeof( did ));
2525 ibuf += sizeof( did );
2526 if (( dir = dirlookup( vol, did )) == NULL ) {
2527 return( AFPERR_PARAM );
2530 /* dir_remove -- deletedid */
2536 /* did creation gets done automatically
2537 * there's a pb again with case but move it to cname
2539 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2542 struct dir *parentdir;
2550 memcpy(&vid, ibuf, sizeof(vid));
2551 ibuf += sizeof( vid );
2553 if (NULL == ( vol = getvolbyvid( vid )) ) {
2554 return( AFPERR_PARAM );
2557 memcpy(&did, ibuf, sizeof(did));
2558 ibuf += sizeof(did);
2560 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2564 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2565 return get_afp_errno(AFPERR_PARAM);
2568 if ( *path->m_name != '\0' ) {
2569 return path_error(path, AFPERR_NOOBJ);
2572 if ( !path->st_valid && of_stat(path ) < 0 ) {
2573 return( AFPERR_NOOBJ );
2575 if ( path->st_errno ) {
2576 return( AFPERR_NOOBJ );
2579 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2580 *rbuflen = sizeof(curdir->d_did);