2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * All Rights Reserved. See COPYRIGHT.
8 #endif /* HAVE_CONFIG_H */
15 #include <sys/param.h>
21 #include <atalk/adouble.h>
22 #include <atalk/vfs.h>
23 #include <atalk/afp.h>
24 #include <atalk/util.h>
25 #include <atalk/cnid.h>
26 #include <atalk/logger.h>
27 #include <atalk/uuid.h>
28 #include <atalk/unix.h>
29 #include <atalk/bstrlib.h>
30 #include <atalk/bstradd.h>
32 #include "directory.h"
45 extern void addir_inherit_acl(const struct vol *vol);
49 * FIXMEs, loose ends after the dircache rewrite:
50 * o merge dircache_search_by_name and dir_add ??
51 * o case-insensitivity is gone from cname
55 /*******************************************************************************************
57 ******************************************************************************************/
60 struct dir rootParent = {
61 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
62 NULL, NULL, 0, 0, /* qidx_node, d_ofork, ctime, d_flags */
63 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
65 struct dir *curdir = &rootParent;
66 struct path Cur_Path = {
71 NULL,/* struct dir * */
72 0, /* stat is not set */
78 /*******************************************************************************************
80 ******************************************************************************************/
83 /* -------------------------
84 appledouble mkdir afp error code.
86 static int netatalk_mkdir(const struct vol *vol, const char *name)
91 if (vol->v_flags & AFPVOL_UNIX_PRIV) {
92 if (lstat(".", &st) < 0)
94 int mode = (DIRBITS & (~S_ISGID & st.st_mode)) | (0777 & ~vol->v_umask);
95 LOG(log_maxdebug, logtype_afpd, "netatalk_mkdir(\"%s\") {parent mode: %04o, vol umask: %04o}",
96 name, st.st_mode, vol->v_umask);
98 ret = mkdir(name, mode);
100 ret = ad_mkdir(name, DIRBITS | 0777);
106 return( AFPERR_NOOBJ );
108 return( AFPERR_VLOCK );
111 return( AFPERR_ACCESS );
113 return( AFPERR_EXIST );
116 return( AFPERR_DFULL );
118 return( AFPERR_PARAM );
124 /* ------------------- */
125 static int deletedir(int dirfd, char *dir)
127 char path[MAXPATHLEN + 1];
135 if ((len = strlen(dir)) +2 > sizeof(path))
139 if ((dp = opendirat(dirfd, dir)) == NULL)
145 remain = sizeof(path) -len -1;
146 while ((de = readdir(dp)) && err == AFP_OK) {
147 /* skip this and previous directory */
148 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
151 if (strlen(de->d_name) > remain) {
155 strcpy(path + len, de->d_name);
156 if (lstatat(dirfd, path, &st)) {
159 if (S_ISDIR(st.st_mode)) {
160 err = deletedir(dirfd, path);
162 err = netatalk_unlinkat(dirfd, path);
167 /* okay. the directory is empty. delete it. note: we already got rid
170 err = netatalk_rmdir(dirfd, dir);
175 /* do a recursive copy. */
176 static int copydir(const struct vol *vol, int dirfd, char *src, char *dst)
178 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
187 /* doesn't exist or the path is too long. */
188 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
189 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
190 ((dp = opendirat(dirfd, src)) == NULL))
193 /* try to create the destination directory */
194 if (AFP_OK != (err = netatalk_mkdir(vol, dst)) ) {
199 /* set things up to copy */
203 srem = sizeof(spath) - slen -1;
208 drem = sizeof(dpath) - dlen -1;
211 while ((de = readdir(dp))) {
212 /* skip this and previous directory */
213 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
216 if (strlen(de->d_name) > srem) {
220 strcpy(spath + slen, de->d_name);
222 if (lstatat(dirfd, spath, &st) == 0) {
223 if (strlen(de->d_name) > drem) {
227 strcpy(dpath + dlen, de->d_name);
229 if (S_ISDIR(st.st_mode)) {
230 if (AFP_OK != (err = copydir(vol, dirfd, spath, dpath)))
232 } else if (AFP_OK != (err = copyfile(vol, vol, dirfd, spath, dpath, NULL, NULL))) {
236 /* keep the same time stamp. */
237 ut.actime = ut.modtime = st.st_mtime;
243 /* keep the same time stamp. */
244 if (lstatat(dirfd, src, &st) == 0) {
245 ut.actime = ut.modtime = st.st_mtime;
254 /* ---------------------
255 * is our cached offspring count valid?
257 static int diroffcnt(struct dir *dir, struct stat *st)
259 return st->st_ctime == dir->ctime;
262 /* --------------------- */
263 static int invisible_dots(const struct vol *vol, const char *name)
265 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
268 /* ------------------ */
269 static int set_dir_errors(struct path *path, const char *where, int err)
274 return AFPERR_ACCESS;
278 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
283 * @brief Convert name in client encoding to server encoding
285 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
286 * This only gets called from cname().
288 * @returns 0 on success, -1 on error
290 * @note If the passed ret->m_name is mangled, we'll demangle it
292 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
294 static char temp[ MAXPATHLEN + 1];
298 if (afp_version >= 30) {
300 if (dir->d_did == DIRDID_ROOT_PARENT) {
302 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
303 * So we compare it with the longname from the current volume and if they match
304 * we overwrite the requested path with the utf8 volume name so that the following
307 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
308 if (strcasecmp(ret->m_name, temp) == 0)
309 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
312 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
313 afp_errno = AFPERR_PARAM;
316 strcpy(ret->m_name, temp);
320 /* check for OS X mangled filename :( */
321 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
322 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
323 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
325 if (t != ret->m_name) {
327 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
328 * flags weren't the same
330 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
331 /* at last got our view of mac name */
332 strcpy(ret->m_name, t);
335 } /* afp_version >= 30 */
337 /* If we haven't got it by now, get it */
338 if (ret->u_name == NULL) {
339 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
340 afp_errno = AFPERR_PARAM;
349 * @brief Build struct path from struct dir
351 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
352 * 1. move cwd into parent dir (we're often already there, but not always)
353 * 2. set struct path to the dirname
355 * AFPERR_ACCESS: the dir is there, we just cant chdir into it
356 * AFPERR_NOOBJ: the dir was there when we stated it in cname, so we have a race
357 * 4. indicate there's no dir for this path
360 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
362 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
368 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
371 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1); /* 3 */
372 if (dir->d_m_name == dir->d_u_name) {
373 ret->u_name = ret->m_name;
375 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
376 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
381 LOG(log_debug, logtype_afpd, "cname('%s') {path-from-dir: AFPERR_ACCESS. curdir:'%s', path:'%s'}",
382 cfrombstring(dir->d_fullpath),
383 cfrombstring(curdir->d_fullpath),
389 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
392 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
393 if (dir->d_m_name == dir->d_u_name) {
394 ret->u_name = ret->m_name;
396 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
397 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
400 ret->d_dir = NULL; /* 4 */
401 dir_remove(vol, dir); /* 5 */
408 /* DEADC0DE: never get here */
413 /*********************************************************************************************
415 ********************************************************************************************/
417 int get_afp_errno(const int param)
419 if (afp_errno != AFPERR_DID1)
425 * @brief Resolve a DID
427 * Resolve a DID, allocate a struct dir for it
428 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
429 * 2a. Check if the DID is in the cache.
430 * 2b. Check if it's really a dir (d_fullpath != NULL) because we cache files too.
431 * 3. If it's not in the cache resolve it via the database.
432 * 4. Build complete server-side path to the dir.
433 * 5. Check if it exists and is a directory.
434 * 6. Create the struct dir and populate it.
435 * 7. Add it to the cache.
437 * @param vol (r) pointer to struct vol
438 * @param did (r) DID to resolve
440 * @returns pointer to struct dir
442 * @note FIXME: OSX calls it with bogus id, ie file ID not folder ID,
443 * and we are really bad in this case.
445 struct dir *dirlookup(const struct vol *vol, cnid_t did)
447 static char buffer[12 + MAXPATHLEN + 1];
449 struct dir *ret = NULL, *pdir;
450 bstring fullpath = NULL;
451 char *upath = NULL, *mpath;
454 int buflen = 12 + MAXPATHLEN + 1;
458 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {start}", ntohl(did));
460 /* check for did 0, 1 and 2 */
461 if (did == 0 || vol == NULL) { /* 1 */
462 afp_errno = AFPERR_PARAM;
464 } else if (did == DIRDID_ROOT_PARENT) {
465 rootParent.d_vid = vol->v_vid;
466 return (&rootParent);
467 } else if (did == DIRDID_ROOT) {
471 /* Search the cache */
472 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2a */
473 if (ret->d_fullpath == NULL) { /* 2b */
474 afp_errno = AFPERR_BADTYPE;
477 if (lstat(cfrombstring(ret->d_fullpath), &st) != 0) {
478 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {lstat: %s}", ntohl(did), strerror(errno));
482 /* It's not there anymore, so remove it */
483 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {calling dir_remove()}", ntohl(did));
484 dir_remove(vol, ret);
485 afp_errno = AFPERR_NOOBJ;
496 utf8 = utf8_encoding();
497 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
499 /* Get it from the database */
501 if ( (upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL
502 || (upath = strdup(upath)) == NULL) { /* 3 */
503 afp_errno = AFPERR_NOOBJ;
510 * Recurse up the tree, terminates in dirlookup when either
511 * - DIRDID_ROOT is hit
512 * - a cached entry is found
514 if ((pdir = dirlookup(vol, pdid)) == NULL) {
519 /* build the fullpath */
520 if ((fullpath = bstrcpy(pdir->d_fullpath)) == NULL
521 || bconchar(fullpath, '/') != BSTR_OK
522 || bcatcstr(fullpath, upath) != BSTR_OK) {
527 /* stat it and check if it's a dir */
528 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstring(fullpath));
530 if (stat(cfrombstring(fullpath), &st) != 0) { /* 5a */
533 afp_errno = AFPERR_NOOBJ;
537 afp_errno = AFPERR_ACCESS;
541 afp_errno = AFPERR_MISC;
546 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
547 afp_errno = AFPERR_BADTYPE;
553 /* Get macname from unix name */
554 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
555 afp_errno = AFPERR_NOOBJ;
560 /* Create struct dir */
561 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath)) == NULL) { /* 6 */
562 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
567 /* Add it to the cache only if it's a dir */
568 if (dircache_add(ret) != 0) { /* 7 */
573 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {end: did:%u, path:'%s'}",
574 ntohl(did), ntohl(pdid), cfrombstring(ret->d_fullpath));
578 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
579 ntohl(did), AfpErr2name(afp_errno));
591 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
593 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
598 static u_int32_t did = 0;
599 static char cname[MAXPATHLEN];
600 static char lname[MAXPATHLEN];
601 ucs2_t u2_path[MAXPATHLEN];
602 ucs2_t u2_dename[MAXPATHLEN];
603 char *tmp, *savepath;
605 if (!(vol->v_flags & AFPVOL_CASEINSEN))
608 if (veto_file(ENUMVETO, path->u_name))
611 savepath = path->u_name;
613 /* very simple cache */
614 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
615 path->u_name = cname;
617 if (of_stat( path ) == 0 ) {
620 /* something changed, we cannot stat ... */
624 if (NULL == ( dp = opendir( "." )) ) {
625 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
630 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
631 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
632 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
634 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
636 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
637 if (NULL == check_dirent(vol, de->d_name))
640 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
643 if (strcasecmp_w( u2_path, u2_dename) == 0) {
645 strlcpy(cname, de->d_name, sizeof(cname));
646 path->u_name = cname;
648 if (of_stat( path ) == 0 ) {
649 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
650 strlcpy(lname, tmp, sizeof(lname));
663 /* invalidate cache */
666 path->u_name = savepath;
668 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
674 * @brief Construct struct dir
676 * Construct struct dir from parameters.
678 * @param m_name (r) directory name in UTF8-dec
679 * @param u_name (r) directory name in server side encoding
680 * @param vol (r) pointer to struct vol
681 * @param pdid (r) Parent CNID
682 * @param did (r) CNID
683 * @param fullpath (r) Full unix path to dir or NULL for files
685 * @returns pointer to new struct dir or NULL on error
687 * @note Most of the time mac name and unix name are the same.
689 struct dir *dir_new(const char *m_name,
691 const struct vol *vol,
698 dir = (struct dir *) calloc(1, sizeof( struct dir ));
702 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
707 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
710 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
711 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
712 dir->d_m_name_ucs2 = NULL;
715 if (m_name == u_name || !strcmp(m_name, u_name)) {
716 dir->d_u_name = dir->d_m_name;
718 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
719 bdestroy(dir->d_m_name);
726 dir->d_vid = vol->v_vid;
727 dir->d_fullpath = path;
732 * @brief Free a struct dir and all its members
734 * @param (rw) pointer to struct dir
736 void dir_free(struct dir *dir)
738 if (dir->d_u_name != dir->d_m_name) {
739 bdestroy(dir->d_u_name);
741 if (dir->d_m_name_ucs2)
742 free(dir->d_m_name_ucs2);
743 bdestroy(dir->d_m_name);
744 bdestroy(dir->d_fullpath);
749 * @brief Create struct dir from struct path
751 * Create a new struct dir from struct path. Then add it to the cache.
752 * The caller must have assured that the dir is not already in the cache,
753 * cf theAFP_ASSERTion.
754 * 1. Open adouble file, get CNID from it.
755 * 2. Search the database, hinting with the CNID from (1).
756 * 3. Build fullpath and create struct dir.
757 * 4. Add it to the cache.
759 * @param vol (r) pointer to struct vol
760 * @param dir (r) pointer to parrent directory
761 * @param path (rw) pointer to struct path with valid path->u_name
762 * @param len (r) strlen of path->u_name
764 * @returns Pointer to new struct dir or NULL on error.
766 * @note Function also assigns path->m_name from path->u_name.
768 struct dir *dir_add(const struct vol *vol, const struct dir *dir, struct path *path, int len)
771 struct dir *cdir = NULL;
774 struct adouble *adp = NULL;
782 if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name))) != NULL) {
783 /* there's a stray entry in the dircache */
784 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
785 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
786 ntohl(cdir->d_did), cfrombstring(dir->d_fullpath));
787 if (dir_remove(vol, cdir) != 0) {
793 /* get_id needs adp for reading CNID from adouble file */
794 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
795 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
799 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
805 ad_close_metadata(adp);
807 /* Get macname from unixname */
808 if (path->m_name == NULL) {
809 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
816 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
817 || (bconchar(fullpath, '/') != BSTR_OK)
818 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
819 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
824 /* Allocate and initialize struct dir */
825 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath)) == NULL) { /* 3 */
830 if ((dircache_add(cdir)) != 0) { /* 4 */
831 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstring(fullpath));
837 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
838 cfrombstring(dir->d_u_name), path->u_name, err);
841 ad_close_metadata(adp);
842 if (!cdir && fullpath)
849 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
850 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
851 ntohl(cdir->d_did), cfrombstring(cdir->d_fullpath));
858 * @brief Remove a dir from a cache and free it and any ressources with it
860 * 1. Check if the dir is locked or has opened forks
861 * 2. If it's a request to remove curdir, just chdir to volume root
862 * 3. Remove it from the cache
863 * 4. Remove the dir plus any allocated resources it references
865 * @param (r) pointer to struct vol
866 * @param (rw) pointer to struct dir
868 int dir_remove(const struct vol *vol, struct dir *dir)
873 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
876 if (dir->d_flags & DIRF_CACHELOCK || dir->d_ofork) { /* 1 */
877 LOG(log_warning, logtype_afpd, "dir_remove(did:%u,'%s'): dir is locked or has opened forks",
878 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
882 if (curdir == dir) { /* 2 */
883 if (movecwd(vol, vol->v_root) < 0) {
884 LOG(log_error, logtype_afpd, "dir_remove: can't chdir to : %s", vol->v_root);
888 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
889 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
891 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX); /* 3 */
892 dir_free(dir); /* 4 */
898 * @brief Modify a struct dir, adjust cache
900 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
901 * If given new_uname == new_mname, new_uname will point to new_mname.
903 * @param vol (r) pointer to struct vol
904 * @param dir (rw) pointer to struct dir
905 * @param pdid (r) new parent DID
906 * @param did (r) new DID
907 * @param new_mname (r) new mac-name
908 * @param new_uname (r) new unix-name
910 int dir_modify(const struct vol *vol,
914 const char *new_mname,
915 const char *new_uname,
916 bstring pdir_fullpath)
920 /* Remove it from the cache */
921 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
929 /* free uname if it's not the same as mname */
930 if (dir->d_m_name != dir->d_u_name)
931 bdestroy(dir->d_u_name);
933 if (new_uname == NULL)
934 new_uname = new_mname;
936 /* assign new name */
937 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
938 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
942 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
943 dir->d_u_name = dir->d_m_name;
945 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
946 LOG(log_error, logtype_afpd, "renamedir: bassigncstr: %s", strerror(errno) );
953 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
955 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
957 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
961 if (dir->d_m_name_ucs2)
962 free(dir->d_m_name_ucs2);
963 if ((size_t)-1 == convert_string_allocate((utf8_encoding())?CH_UTF8_MAC:vol->v_maccharset, CH_UCS2, dir->d_m_name, -1, (char**)&dir->d_m_name_ucs2))
964 dir->d_m_name_ucs2 = NULL;
966 /* Re-add it to the cache */
967 if ((dircache_add(dir)) != 0) {
976 * @brief Resolve a catalog node name path
978 * 1. Evaluate path type
979 * 2. Move to start dir, if we cant, it might eg because of EACCES, build
980 * path from dirname, so eg getdirparams has sth it can chew on. curdir
981 * is dir parent then. All this is done in path_from_dir().
982 * 3. Parse next cnode name in path, cases:
983 * 4. single "\0" -> do nothing
984 * 5. two or more consecutive "\0" -> chdir("..") one or more times
985 * 6. cnode name -> copy it to path.m_name
986 * 7. Get unix name from mac name
987 * 8. Special handling of request with did 1
988 * 9. stat the cnode name
989 * 10. If it's not there, it's probably an afp_createfile|dir,
990 * return with curdir = dir parent, struct path = dirname
991 * 11. If it's there and it's a file, it must should be the last element of the requested
992 * path. Return with curdir = cnode name parent dir, struct path = filename
993 * 12. Treat symlinks like files, dont follow them
995 * 14. Search the dircache for it
996 * 15. If it's not in the cache, create a struct dir for it and add it to the cache
997 * 16. chdir into the dir and
998 * 17. set m_name to the mac equivalent of "."
1001 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1003 static char path[ MAXPATHLEN + 1];
1004 static struct path ret;
1014 LOG(log_maxdebug, logtype_afpd, "came('%s'): {start}", cfrombstring(dir->d_fullpath));
1017 afp_errno = AFPERR_NOOBJ;
1018 memset(&ret, 0, sizeof(ret));
1020 switch (ret.m_type = *data) { /* 1 */
1023 len = (unsigned char) *data++;
1025 if (afp_version >= 30) {
1031 if (afp_version >= 30) {
1033 memcpy(&hint, data, sizeof(hint));
1035 data += sizeof(hint);
1037 memcpy(&len16, data, sizeof(len16));
1043 /* else it's an error */
1045 afp_errno = AFPERR_PARAM;
1048 *cpath += len + size;
1053 if (movecwd(vol, dir) < 0 ) {
1054 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1055 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1057 return path_from_dir(vol, dir, &ret);
1062 while (len) { /* 3 */
1063 if (*data == 0) { /* 4 or 5 */
1066 while (len > 0 && *data == 0) { /* 5 */
1067 /* chdir to parrent dir */
1068 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1070 if (movecwd( vol, dir ) < 0 ) {
1071 dir_remove(vol, dir);
1081 for ( p = path; *data != 0 && len > 0; len-- ) {
1083 if (p > &path[ MAXPATHLEN]) {
1084 afp_errno = AFPERR_PARAM;
1088 *p = 0; /* Terminate string */
1091 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) { /* 7 */
1092 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1096 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstring(dir->d_fullpath), ret.u_name);
1098 /* Prevent access to our special folders like .AppleDouble */
1099 if (check_name(vol, ret.u_name)) {
1100 /* the name is illegal */
1101 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1102 afp_errno = AFPERR_PARAM;
1106 if (dir->d_did == DIRDID_ROOT_PARENT) { /* 8 */
1108 * Special case: CNID 1
1109 * root parent (did 1) has one child: the volume. Requests for did=1 with
1110 * some <name> must check against the volume name.
1112 if ((strcmp(cfrombstring(vol->v_root->d_m_name), ret.m_name)) == 0)
1118 * CNID != 1, eg. most of the times we take this way.
1119 * Now check if current path-part is a file or dir:
1120 * o if it's dir we have to step into it
1121 * o if it's a file we expect it to be the last part of the requested path
1122 * and thus call continue which should terminate the while loop because
1125 if (of_stat(&ret) != 0) { /* 9 */
1127 * ret.u_name doesn't exist, might be afp_createfile|dir
1128 * that means it should have been the last part
1131 /* it wasn't the last part, so we have a bogus path request */
1132 afp_errno = AFPERR_NOOBJ;
1136 * this will terminate clean in while (1) because len == 0,
1137 * probably afp_createfile|dir
1139 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1143 switch (ret.st.st_mode & S_IFMT) {
1144 case S_IFREG: /* 11 */
1145 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1147 /* it wasn't the last part, so we have a bogus path request */
1148 afp_errno = AFPERR_PARAM;
1151 continue; /* continues while loop */
1152 case S_IFLNK: /* 12 */
1153 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1155 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1156 afp_errno = AFPERR_PARAM;
1159 continue; /* continues while loop */
1160 case S_IFDIR: /* 13 */
1163 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1164 afp_errno = AFPERR_NODIR;
1168 /* Search the cache */
1169 int unamelen = strlen(ret.u_name);
1170 cdir = dircache_search_by_name(vol, dir, ret.u_name, unamelen); /* 14 */
1172 /* Not in cache, create one */
1173 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) { /* 15 */
1174 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1175 ntohl(dir->d_did), ret.u_name, getcwdpath());
1179 } /* if/else cnid==1 */
1181 /* Now chdir to the evaluated dir */
1182 if (movecwd( vol, cdir ) < 0 ) { /* 16 */
1183 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1184 cfrombstring(curdir->d_fullpath), cfrombstring(cdir->d_fullpath), strerror(errno));
1186 return path_from_dir(vol, cdir, &ret);
1191 ret.m_name[0] = 0; /* 17, so we later know last token was a dir */
1194 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1195 afp_errno = AFPERR_DID1;
1199 if (ret.m_name[0] == 0) {
1200 /* Last part was a dir */
1201 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1205 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1206 cfrombstring(dir->d_fullpath),
1207 cfrombstring(curdir->d_fullpath),
1214 * @brief chdir() to dir
1216 * @param vol (r) pointer to struct vol
1217 * @param dir (r) pointer to struct dir
1219 * @returns 0 on success, -1 on error with afp_errno set appropiately
1221 int movecwd(const struct vol *vol, struct dir *dir)
1228 LOG(log_maxdebug, logtype_afpd, "movecwd(curdir:'%s', cwd:'%s')",
1229 cfrombstring(curdir->d_fullpath), getcwdpath());
1233 if (dir->d_did == DIRDID_ROOT_PARENT) {
1234 curdir = &rootParent;
1238 LOG(log_debug, logtype_afpd, "movecwd(did:%u, '%s')", ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1240 if ((ret = lchdir(cfrombstring(dir->d_fullpath))) != 0 ) {
1241 LOG(log_debug, logtype_afpd, "movecwd('%s'): ret: %u, %s",
1242 cfrombstring(dir->d_fullpath), ret, strerror(errno));
1244 /* p is a symlink or getcwd failed */
1245 afp_errno = AFPERR_BADTYPE;
1247 if (chdir(vol->v_path ) < 0) {
1248 LOG(log_error, logtype_afpd, "can't chdir back'%s': %s", vol->v_path, strerror(errno));
1249 /* XXX what do we do here? */
1251 curdir = vol->v_root;
1258 afp_errno = AFPERR_ACCESS;
1261 afp_errno = AFPERR_NOOBJ;
1271 * We can't use unix file's perm to support Apple's inherited protection modes.
1272 * If we aren't the file's owner we can't change its perms when moving it and smb
1273 * nfs,... don't even try.
1275 #define AFP_CHECK_ACCESS
1277 int check_access(char *path, int mode)
1279 #ifdef AFP_CHECK_ACCESS
1287 accessmode(p, &ma, curdir, NULL);
1288 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1290 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1296 /* --------------------- */
1297 int file_access(struct path *path, int mode)
1301 accessmode(path->u_name, &ma, curdir, &path->st);
1302 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1304 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1310 /* --------------------- */
1311 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1313 dir->offcnt = count;
1314 dir->ctime = st->st_ctime;
1315 dir->d_flags &= ~DIRF_CNID;
1319 /* ---------------------
1320 * is our cached also for reenumerate id?
1322 int dirreenumerate(struct dir *dir, struct stat *st)
1324 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1327 /* ------------------------------
1329 (name, dir) with curdir:name == dir, from afp_enumerate
1332 int getdirparams(const struct vol *vol,
1333 u_int16_t bitmap, struct path *s_path,
1335 char *buf, size_t *buflen )
1339 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1340 int bit = 0, isad = 0;
1346 struct stat *st = &s_path->st;
1347 char *upath = s_path->u_name;
1349 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1350 (1 << DIRPBIT_CDATE) |
1351 (1 << DIRPBIT_MDATE) |
1352 (1 << DIRPBIT_BDATE) |
1353 (1 << DIRPBIT_FINFO)))) {
1355 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1356 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1358 if (ad.ad_md->adf_flags & O_CREAT) {
1359 /* We just created it */
1360 ad_setname(&ad, s_path->m_name);
1375 while ( bitmap != 0 ) {
1376 while (( bitmap & 1 ) == 0 ) {
1384 ad_getattr(&ad, &ashort);
1385 } else if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1386 ashort = htons(ATTRBIT_INVISIBLE);
1389 ashort |= htons(ATTRBIT_SHARED);
1390 memcpy( data, &ashort, sizeof( ashort ));
1391 data += sizeof( ashort );
1395 memcpy( data, &pdid, sizeof( pdid ));
1396 data += sizeof( pdid );
1397 LOG(log_debug, logtype_afpd, "metadata('%s'): Parent DID: %u",
1398 s_path->u_name, ntohl(pdid));
1401 case DIRPBIT_CDATE :
1402 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1403 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1404 memcpy( data, &aint, sizeof( aint ));
1405 data += sizeof( aint );
1408 case DIRPBIT_MDATE :
1409 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1410 memcpy( data, &aint, sizeof( aint ));
1411 data += sizeof( aint );
1414 case DIRPBIT_BDATE :
1415 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1416 aint = AD_DATE_START;
1417 memcpy( data, &aint, sizeof( aint ));
1418 data += sizeof( aint );
1421 case DIRPBIT_FINFO :
1423 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1424 } else { /* no appledouble */
1425 memset( data, 0, 32 );
1426 /* set default view -- this also gets done in ad_open() */
1427 ashort = htons(FINDERINFO_CLOSEDVIEW);
1428 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1430 /* dot files are by default visible */
1431 if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1432 ashort = htons(FINDERINFO_INVISIBLE);
1433 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1439 case DIRPBIT_LNAME :
1440 if (dir->d_m_name) /* root of parent can have a null name */
1443 memset(data, 0, sizeof(u_int16_t));
1444 data += sizeof( u_int16_t );
1447 case DIRPBIT_SNAME :
1448 memset(data, 0, sizeof(u_int16_t));
1449 data += sizeof( u_int16_t );
1453 memcpy( data, &dir->d_did, sizeof( aint ));
1454 data += sizeof( aint );
1455 LOG(log_debug, logtype_afpd, "metadata('%s'): DID: %u",
1456 s_path->u_name, ntohl(dir->d_did));
1459 case DIRPBIT_OFFCNT :
1461 /* this needs to handle current directory access rights */
1462 if (diroffcnt(dir, st)) {
1463 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1465 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1466 setdiroffcnt(dir, st, ret);
1467 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1469 ashort = htons( ashort );
1470 memcpy( data, &ashort, sizeof( ashort ));
1471 data += sizeof( ashort );
1475 aint = htonl(st->st_uid);
1476 memcpy( data, &aint, sizeof( aint ));
1477 data += sizeof( aint );
1481 aint = htonl(st->st_gid);
1482 memcpy( data, &aint, sizeof( aint ));
1483 data += sizeof( aint );
1486 case DIRPBIT_ACCESS :
1487 accessmode( upath, &ma, dir , st);
1489 *data++ = ma.ma_user;
1490 *data++ = ma.ma_world;
1491 *data++ = ma.ma_group;
1492 *data++ = ma.ma_owner;
1495 /* Client has requested the ProDOS information block.
1496 Just pass back the same basic block for all
1497 directories. <shirsch@ibm.net> */
1498 case DIRPBIT_PDINFO :
1499 if (afp_version >= 30) { /* UTF8 name */
1500 utf8 = kTextEncodingUTF8;
1501 if (dir->d_m_name) /* root of parent can have a null name */
1504 memset(data, 0, sizeof(u_int16_t));
1505 data += sizeof( u_int16_t );
1507 memcpy(data, &aint, sizeof( aint ));
1508 data += sizeof( aint );
1510 else { /* ProDOS Info Block */
1513 ashort = htons( 0x0200 );
1514 memcpy( data, &ashort, sizeof( ashort ));
1515 data += sizeof( ashort );
1516 memset( data, 0, sizeof( ashort ));
1517 data += sizeof( ashort );
1521 case DIRPBIT_UNIXPR :
1522 aint = htonl(st->st_uid);
1523 memcpy( data, &aint, sizeof( aint ));
1524 data += sizeof( aint );
1525 aint = htonl(st->st_gid);
1526 memcpy( data, &aint, sizeof( aint ));
1527 data += sizeof( aint );
1530 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1531 memcpy( data, &aint, sizeof( aint ));
1532 data += sizeof( aint );
1534 accessmode( upath, &ma, dir , st);
1536 *data++ = ma.ma_user;
1537 *data++ = ma.ma_world;
1538 *data++ = ma.ma_group;
1539 *data++ = ma.ma_owner;
1544 ad_close_metadata( &ad );
1546 return( AFPERR_BITMAP );
1552 ashort = htons( data - buf );
1553 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1554 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, 0);
1556 if ( utf_nameoff ) {
1557 ashort = htons( data - buf );
1558 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1559 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, utf8);
1562 ad_close_metadata( &ad );
1564 *buflen = data - buf;
1568 /* ----------------------------- */
1569 int path_error(struct path *path, int error)
1571 /* - a dir with access error
1572 * - no error it's a file
1575 if (path_isadir(path))
1577 if (path->st_valid && path->st_errno)
1579 return AFPERR_BADTYPE ;
1582 /* ----------------------------- */
1583 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1588 u_int16_t vid, bitmap;
1594 memcpy( &vid, ibuf, sizeof( vid ));
1595 ibuf += sizeof( vid );
1597 if (NULL == ( vol = getvolbyvid( vid )) ) {
1598 return( AFPERR_PARAM );
1601 if (vol->v_flags & AFPVOL_RO)
1602 return AFPERR_VLOCK;
1604 memcpy( &did, ibuf, sizeof( did ));
1605 ibuf += sizeof( int );
1607 if (NULL == ( dir = dirlookup( vol, did )) ) {
1611 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1612 bitmap = ntohs( bitmap );
1613 ibuf += sizeof( bitmap );
1615 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1616 return get_afp_errno(AFPERR_NOOBJ);
1619 if ( *path->m_name != '\0' ) {
1620 rc = path_error(path, AFPERR_NOOBJ);
1621 /* maybe we are trying to set perms back */
1622 if (rc != AFPERR_ACCESS)
1627 * If ibuf is odd, make it even.
1629 if ((u_long)ibuf & 1 ) {
1633 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1634 setvoltime(obj, vol );
1640 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1642 * assume path == '\0' eg. it's a directory in canonical form
1644 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1656 u_int16_t ashort, bshort, oshort;
1658 int change_mdate = 0;
1659 int change_parent_mdate = 0;
1661 u_int16_t bitmap = d_bitmap;
1662 u_char finder_buf[32];
1665 u_int16_t upriv_bit = 0;
1668 upath = path->u_name;
1670 while ( bitmap != 0 ) {
1671 while (( bitmap & 1 ) == 0 ) {
1679 memcpy( &ashort, buf, sizeof( ashort ));
1680 buf += sizeof( ashort );
1682 case DIRPBIT_CDATE :
1684 memcpy(&cdate, buf, sizeof(cdate));
1685 buf += sizeof( cdate );
1687 case DIRPBIT_MDATE :
1688 memcpy(&newdate, buf, sizeof(newdate));
1689 buf += sizeof( newdate );
1691 case DIRPBIT_BDATE :
1693 memcpy(&bdate, buf, sizeof(bdate));
1694 buf += sizeof( bdate );
1696 case DIRPBIT_FINFO :
1698 memcpy( finder_buf, buf, 32 );
1701 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1702 change_parent_mdate = 1;
1703 memcpy( &owner, buf, sizeof(owner));
1704 buf += sizeof( owner );
1707 change_parent_mdate = 1;
1708 memcpy( &group, buf, sizeof( group ));
1709 buf += sizeof( group );
1711 case DIRPBIT_ACCESS :
1713 change_parent_mdate = 1;
1714 ma.ma_user = *buf++;
1715 ma.ma_world = *buf++;
1716 ma.ma_group = *buf++;
1717 ma.ma_owner = *buf++;
1718 mpriv = mtoumode( &ma ) | vol->v_dperm;
1719 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1720 err = set_dir_errors(path, "setdirmode", errno);
1724 /* Ignore what the client thinks we should do to the
1725 ProDOS information block. Skip over the data and
1726 report nothing amiss. <shirsch@ibm.net> */
1727 case DIRPBIT_PDINFO :
1728 if (afp_version < 30) {
1732 err = AFPERR_BITMAP;
1736 case DIRPBIT_UNIXPR :
1737 if (vol_unix_priv(vol)) {
1738 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1739 buf += sizeof( owner );
1740 memcpy( &group, buf, sizeof( group ));
1741 buf += sizeof( group );
1744 change_parent_mdate = 1;
1745 memcpy( &upriv, buf, sizeof( upriv ));
1746 buf += sizeof( upriv );
1747 upriv = ntohl (upriv) | vol->v_dperm;
1748 if (dir_rx_set(upriv)) {
1749 /* maybe we are trying to set perms back */
1750 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1752 err = set_dir_errors(path, "setdirunixmode", errno);
1763 err = AFPERR_BITMAP;
1771 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1773 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1775 * Check to see what we're trying to set. If it's anything
1776 * but ACCESS, UID, or GID, give an error. If it's any of those
1777 * three, we don't need the ad to be open, so just continue.
1779 * note: we also don't need to worry about mdate. also, be quiet
1780 * if we're using the noadouble option.
1782 if (!vol_noadouble(vol) && (d_bitmap &
1783 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1784 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1785 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1786 return AFPERR_ACCESS;
1792 * Check to see if a create was necessary. If it was, we'll want
1793 * to set our name, etc.
1795 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1796 ad_setname(&ad, cfrombstring(curdir->d_m_name));
1802 while ( bitmap != 0 ) {
1803 while (( bitmap & 1 ) == 0 ) {
1811 ad_getattr(&ad, &bshort);
1813 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1814 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1818 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1819 change_parent_mdate = 1;
1820 ad_setattr(&ad, bshort);
1823 case DIRPBIT_CDATE :
1825 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1828 case DIRPBIT_MDATE :
1830 case DIRPBIT_BDATE :
1832 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1835 case DIRPBIT_FINFO :
1837 /* Fixes #2802236 */
1838 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1839 *fflags &= htons(~FINDERINFO_ISHARED);
1841 if ( dir->d_did == DIRDID_ROOT ) {
1843 * Alright, we admit it, this is *really* sick!
1844 * The 4 bytes that we don't copy, when we're dealing
1845 * with the root of a volume, are the directory's
1846 * location information. This eliminates that annoying
1847 * behavior one sees when mounting above another mount
1850 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1851 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1853 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1857 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1858 if ( (dir->d_did == DIRDID_ROOT) &&
1859 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1860 err = set_dir_errors(path, "setdeskowner", errno);
1861 if (isad && err == AFPERR_PARAM) {
1862 err = AFP_OK; /* ???*/
1865 goto setdirparam_done;
1868 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1869 err = set_dir_errors(path, "setdirowner", errno);
1870 goto setdirparam_done;
1874 if (dir->d_did == DIRDID_ROOT)
1875 setdeskowner( -1, ntohl(group) );
1876 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1877 err = set_dir_errors(path, "setdirowner", errno);
1878 goto setdirparam_done;
1881 case DIRPBIT_ACCESS :
1882 if (dir->d_did == DIRDID_ROOT) {
1884 if (!dir_rx_set(mpriv)) {
1885 /* we can't remove read and search for owner on volume root */
1886 err = AFPERR_ACCESS;
1887 goto setdirparam_done;
1891 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1892 err = set_dir_errors(path, "setdirmode", errno);
1893 goto setdirparam_done;
1896 case DIRPBIT_PDINFO :
1897 if (afp_version >= 30) {
1898 err = AFPERR_BITMAP;
1899 goto setdirparam_done;
1902 case DIRPBIT_UNIXPR :
1903 if (vol_unix_priv(vol)) {
1904 if (dir->d_did == DIRDID_ROOT) {
1905 if (!dir_rx_set(upriv)) {
1906 /* we can't remove read and search for owner on volume root */
1907 err = AFPERR_ACCESS;
1908 goto setdirparam_done;
1910 setdeskowner( -1, ntohl(group) );
1911 setdeskmode( upriv );
1913 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1914 err = set_dir_errors(path, "setdirowner", errno);
1915 goto setdirparam_done;
1918 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1919 err = set_dir_errors(path, "setdirunixmode", errno);
1920 goto setdirparam_done;
1924 err = AFPERR_BITMAP;
1925 goto setdirparam_done;
1929 err = AFPERR_BITMAP;
1930 goto setdirparam_done;
1939 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1940 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1944 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1945 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1950 if (path->st_valid && !path->st_errno) {
1951 struct stat *st = &path->st;
1953 if (dir && dir->d_pdid) {
1954 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1958 ad_close_metadata( &ad);
1961 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1962 && gettimeofday(&tv, NULL) == 0) {
1963 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) == 0) {
1964 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1965 /* be careful with bitmap because now dir is null */
1966 bitmap = 1<<DIRPBIT_MDATE;
1967 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1968 /* should we reset curdir ?*/
1975 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1989 memcpy( &vid, ibuf, sizeof( vid ));
1990 ibuf += sizeof( vid );
1991 if (NULL == (vol = getvolbyvid( vid )) ) {
1992 return( AFPERR_PARAM );
1995 memcpy( &did, ibuf, sizeof( did ));
1996 ibuf += sizeof( did );
2000 * if it's CNID 2 our only choice to meet the specs is call sync.
2001 * For any other CNID just sync that dir. To my knowledge the
2002 * intended use of FPSyncDir is to sync the volume so all we're
2003 * ever going to see here is probably CNID 2. Anyway, we' prepared.
2006 if ( ntohl(did) == 2 ) {
2009 if (NULL == ( dir = dirlookup( vol, did )) ) {
2010 return afp_errno; /* was AFPERR_NOOBJ */
2013 if (movecwd( vol, dir ) < 0 )
2014 return ( AFPERR_NOOBJ );
2017 * Assuming only OSens that have dirfd also may require fsyncing directories
2018 * in order to flush metadata e.g. Linux.
2022 if (NULL == ( dp = opendir( "." )) ) {
2025 return( AFPERR_NOOBJ );
2027 return( AFPERR_ACCESS );
2029 return( AFPERR_PARAM );
2033 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2036 if ( fsync ( dfd ) < 0 )
2037 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2038 dir->d_u_name, strerror(errno) );
2039 closedir(dp); /* closes dfd too */
2042 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2045 return( AFPERR_NOOBJ );
2047 return( AFPERR_ACCESS );
2049 return( AFPERR_PARAM );
2053 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2054 vol->ad_path(".", ADFLAGS_DIR) );
2056 if ( fsync(dfd) < 0 )
2057 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2058 vol->ad_path(cfrombstring(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2065 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2071 struct path *s_path;
2079 memcpy( &vid, ibuf, sizeof( vid ));
2080 ibuf += sizeof( vid );
2081 if (NULL == ( vol = getvolbyvid( vid )) ) {
2082 return( AFPERR_PARAM );
2085 if (vol->v_flags & AFPVOL_RO)
2086 return AFPERR_VLOCK;
2088 memcpy( &did, ibuf, sizeof( did ));
2089 ibuf += sizeof( did );
2090 if (NULL == ( dir = dirlookup( vol, did )) ) {
2091 return afp_errno; /* was AFPERR_NOOBJ */
2093 /* for concurrent access we need to be sure we are not in the
2094 * folder we want to create...
2098 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2099 return get_afp_errno(AFPERR_PARAM);
2101 /* cname was able to move curdir to it! */
2102 if (*s_path->m_name == '\0')
2103 return AFPERR_EXIST;
2105 upath = s_path->u_name;
2107 if (AFP_OK != (err = netatalk_mkdir(vol, upath))) {
2111 if (of_stat(s_path) < 0) {
2117 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2121 if ( movecwd( vol, dir ) < 0 ) {
2122 return( AFPERR_PARAM );
2125 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2126 if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2127 if (vol_noadouble(vol))
2128 goto createdir_done;
2129 return( AFPERR_ACCESS );
2131 ad_setname(&ad, s_path->m_name);
2132 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2135 ad_close_metadata( &ad);
2139 /* FIXME: are we really inside the created dir? */
2140 addir_inherit_acl(vol);
2141 #endif /* HAVE_ACLS */
2143 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2144 *rbuflen = sizeof( u_int32_t );
2145 setvoltime(obj, vol );
2150 * dst new unix filename (not a pathname)
2151 * newname new mac name
2153 * dirfd -1 means ignore dirfd (or use AT_FDCWD), otherwise src is relative to dirfd
2155 int renamedir(const struct vol *vol,
2160 struct dir *newparent,
2166 /* existence check moved to afp_moveandrename */
2167 if ( unix_rename(dirfd, src, -1, dst ) < 0 ) {
2170 return( AFPERR_NOOBJ );
2172 return( AFPERR_ACCESS );
2174 return AFPERR_VLOCK;
2176 /* tried to move directory into a subdirectory of itself */
2177 return AFPERR_CANTMOVE;
2179 /* this needs to copy and delete. bleah. that means we have
2180 * to deal with entire directory hierarchies. */
2181 if ((err = copydir(vol, dirfd, src, dst)) < 0) {
2185 if ((err = deletedir(dirfd, src)) < 0)
2189 return( AFPERR_PARAM );
2193 vol->vfs->vfs_renamedir(vol, dirfd, src, dst);
2195 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2197 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2198 ad_setname(&ad, newname);
2200 ad_close_metadata( &ad);
2203 if (dir_modify(vol, dir, curdir->d_did, 0, newname, dst, curdir->d_fullpath) != 0) {
2204 LOG(log_error, logtype_afpd, "renamedir: fatal error from dir_modify: %s -> %s", src, dst);
2211 /* delete an empty directory */
2212 int deletecurdir(struct vol *vol)
2222 if ( dirlookup(vol, curdir->d_pdid) == NULL ) {
2223 return( AFPERR_ACCESS );
2228 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2229 /* we never want to create a resource fork here, we are going to delete it */
2230 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2232 ad_getattr(&ad, &ashort);
2233 ad_close_metadata(&ad);
2234 if ((ashort & htons(ATTRBIT_NODELETE))) {
2235 return AFPERR_OLOCK;
2238 err = vol->vfs->vfs_deletecurdir(vol);
2243 /* now get rid of dangling symlinks */
2244 if ((dp = opendir("."))) {
2245 while ((de = readdir(dp))) {
2246 /* skip this and previous directory */
2247 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2250 /* bail if it's not a symlink */
2251 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2253 return AFPERR_DIRNEMPT;
2256 if ((err = netatalk_unlink(de->d_name))) {
2263 if ( movecwd(vol, dirlookup(vol, curdir->d_pdid)) < 0 ) {
2268 err = netatalk_rmdir_all_errors(-1, cfrombstring(fdir->d_u_name));
2269 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2270 cnid_delete(vol->v_cdb, fdir->d_did);
2271 dir_remove( vol, fdir );
2275 /* inode is used as key for cnid.
2276 * Close the descriptor only after cnid_delete
2284 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2294 sfunc = (unsigned char) *ibuf++;
2298 if (sfunc >= 3 && sfunc <= 6) {
2299 if (afp_version < 30) {
2300 return( AFPERR_PARAM );
2307 case 3 :/* unicode */
2308 memcpy( &id, ibuf, sizeof( id ));
2311 if (( pw = getpwuid( id )) == NULL ) {
2312 return( AFPERR_NOITEM );
2314 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2315 pw->pw_name, -1, &name);
2322 case 4 : /* unicode */
2323 memcpy( &id, ibuf, sizeof( id ));
2326 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2327 return( AFPERR_NOITEM );
2329 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2330 gr->gr_name, -1, &name);
2337 case 5 : /* UUID -> username */
2338 case 6 : /* UUID -> groupname */
2339 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2340 return AFPERR_PARAM;
2341 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2343 len = getnamefromuuid( ibuf, &name, &type);
2344 if (len != 0) /* its a error code, not len */
2345 return AFPERR_NOITEM;
2346 if (type == UUID_USER) {
2347 if (( pw = getpwnam( name )) == NULL )
2348 return( AFPERR_NOITEM );
2349 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2350 id = htonl(UUID_USER);
2351 memcpy( rbuf, &id, sizeof( id ));
2352 id = htonl( pw->pw_uid);
2353 rbuf += sizeof( id );
2354 memcpy( rbuf, &id, sizeof( id ));
2355 rbuf += sizeof( id );
2356 *rbuflen = 2 * sizeof( id );
2357 } else { /* type == UUID_GROUP */
2358 if (( gr = getgrnam( name )) == NULL )
2359 return( AFPERR_NOITEM );
2360 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2361 id = htonl(UUID_GROUP);
2362 memcpy( rbuf, &id, sizeof( id ));
2363 rbuf += sizeof( id );
2364 id = htonl( gr->gr_gid);
2365 memcpy( rbuf, &id, sizeof( id ));
2366 rbuf += sizeof( id );
2367 *rbuflen = 2 * sizeof( id );
2370 #endif /* HAVE_ACLS */
2372 return( AFPERR_PARAM );
2376 len = strlen( name );
2379 u_int16_t tp = htons(len);
2380 memcpy(rbuf, &tp, sizeof(tp));
2389 memcpy( rbuf, name, len );
2397 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2406 sfunc = (unsigned char) *ibuf++;
2408 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2411 case 2 : /* unicode */
2412 if (afp_version < 30) {
2413 return( AFPERR_PARAM );
2415 memcpy(&ulen, ibuf, sizeof(ulen));
2418 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2422 len = (unsigned char) *ibuf++;
2425 case 5 : /* username -> UUID */
2426 case 6 : /* groupname -> UUID */
2427 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2428 return AFPERR_PARAM;
2429 memcpy(&ulen, ibuf, sizeof(ulen));
2433 #endif /* HAVE_ACLS */
2435 return( AFPERR_PARAM );
2441 return AFPERR_PARAM;
2444 case 1 : /* unicode */
2446 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2447 return( AFPERR_NOITEM );
2451 memcpy( rbuf, &id, sizeof( id ));
2452 *rbuflen = sizeof( id );
2455 case 2 : /* unicode */
2457 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2458 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2459 return( AFPERR_NOITEM );
2462 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2464 memcpy( rbuf, &id, sizeof( id ));
2465 *rbuflen = sizeof( id );
2468 case 5 : /* username -> UUID */
2469 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2470 if (0 != getuuidfromname(ibuf, UUID_USER, rbuf))
2471 return AFPERR_NOITEM;
2472 *rbuflen = UUID_BINSIZE;
2474 case 6 : /* groupname -> UUID */
2475 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2476 if (0 != getuuidfromname(ibuf, UUID_GROUP, rbuf))
2477 return AFPERR_NOITEM;
2478 *rbuflen = UUID_BINSIZE;
2480 #endif /* HAVE_ACLS */
2486 /* ------------------------------------
2487 variable DID support
2489 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2500 /* do nothing as dids are static for the life of the process. */
2504 memcpy(&vid, ibuf, sizeof( vid ));
2505 ibuf += sizeof( vid );
2506 if (( vol = getvolbyvid( vid )) == NULL ) {
2507 return( AFPERR_PARAM );
2510 memcpy( &did, ibuf, sizeof( did ));
2511 ibuf += sizeof( did );
2512 if (( dir = dirlookup( vol, did )) == NULL ) {
2513 return( AFPERR_PARAM );
2516 /* dir_remove -- deletedid */
2522 /* did creation gets done automatically
2523 * there's a pb again with case but move it to cname
2525 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2528 struct dir *parentdir;
2536 memcpy(&vid, ibuf, sizeof(vid));
2537 ibuf += sizeof( vid );
2539 if (NULL == ( vol = getvolbyvid( vid )) ) {
2540 return( AFPERR_PARAM );
2543 memcpy(&did, ibuf, sizeof(did));
2544 ibuf += sizeof(did);
2546 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2550 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2551 return get_afp_errno(AFPERR_PARAM);
2554 if ( *path->m_name != '\0' ) {
2555 return path_error(path, AFPERR_NOOBJ);
2558 if ( !path->st_valid && of_stat(path ) < 0 ) {
2559 return( AFPERR_NOOBJ );
2561 if ( path->st_errno ) {
2562 return( AFPERR_NOOBJ );
2565 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2566 *rbuflen = sizeof(curdir->d_did);