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>
31 #include <atalk/errchk.h>
33 #include "directory.h"
46 * FIXMEs, loose ends after the dircache rewrite:
47 * o merge dircache_search_by_name and dir_add ??
48 * o case-insensitivity is gone from cname
52 /*******************************************************************************************
54 ******************************************************************************************/
57 /* As long as directory.c hasn't got its own init call, this get initialized in dircache_init */
58 struct dir rootParent = {
59 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
60 NULL, 0, 0, /* qidx_node, ctime, d_flags */
61 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
63 struct dir *curdir = &rootParent;
64 struct path Cur_Path = {
69 NULL,/* struct dir * */
70 0, /* stat is not set */
76 * dir_remove queues struct dirs to be freed here. We can't just delete them immeidately
77 * eg in dircache_search_by_id, because a caller somewhere up the stack might be
80 * - we mark it as invalid by setting d_did to CNID_INVALID (ie 0)
81 * - queue it in "invalid_dircache_entries" queue
82 * - which is finally freed at the end of every AFP func in afp_dsi.c.
84 q_t *invalid_dircache_entries;
87 /*******************************************************************************************
89 ******************************************************************************************/
92 /* -------------------------
93 appledouble mkdir afp error code.
95 static int netatalk_mkdir(const struct vol *vol, const char *name)
100 if (vol->v_flags & AFPVOL_UNIX_PRIV) {
101 if (lstat(".", &st) < 0)
103 int mode = (DIRBITS & (~S_ISGID & st.st_mode)) | (0777 & ~vol->v_umask);
104 LOG(log_maxdebug, logtype_afpd, "netatalk_mkdir(\"%s\") {parent mode: %04o, vol umask: %04o}",
105 name, st.st_mode, vol->v_umask);
107 ret = mkdir(name, mode);
109 ret = ad_mkdir(name, DIRBITS | 0777);
115 return( AFPERR_NOOBJ );
117 return( AFPERR_VLOCK );
120 return( AFPERR_ACCESS );
122 return( AFPERR_EXIST );
125 return( AFPERR_DFULL );
127 return( AFPERR_PARAM );
133 /* ------------------- */
134 static int deletedir(int dirfd, char *dir)
136 char path[MAXPATHLEN + 1];
144 if ((len = strlen(dir)) +2 > sizeof(path))
148 if ((dp = opendirat(dirfd, dir)) == NULL)
154 remain = sizeof(path) -len -1;
155 while ((de = readdir(dp)) && err == AFP_OK) {
156 /* skip this and previous directory */
157 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
160 if (strlen(de->d_name) > remain) {
164 strcpy(path + len, de->d_name);
165 if (lstatat(dirfd, path, &st)) {
168 if (S_ISDIR(st.st_mode)) {
169 err = deletedir(dirfd, path);
171 err = netatalk_unlinkat(dirfd, path);
176 /* okay. the directory is empty. delete it. note: we already got rid
179 err = netatalk_rmdir(dirfd, dir);
184 /* do a recursive copy. */
185 static int copydir(const struct vol *vol, int dirfd, char *src, char *dst)
187 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
196 /* doesn't exist or the path is too long. */
197 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
198 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
199 ((dp = opendirat(dirfd, src)) == NULL))
202 /* try to create the destination directory */
203 if (AFP_OK != (err = netatalk_mkdir(vol, dst)) ) {
208 /* set things up to copy */
212 srem = sizeof(spath) - slen -1;
217 drem = sizeof(dpath) - dlen -1;
220 while ((de = readdir(dp))) {
221 /* skip this and previous directory */
222 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
225 if (strlen(de->d_name) > srem) {
229 strcpy(spath + slen, de->d_name);
231 if (lstatat(dirfd, spath, &st) == 0) {
232 if (strlen(de->d_name) > drem) {
236 strcpy(dpath + dlen, de->d_name);
238 if (S_ISDIR(st.st_mode)) {
239 if (AFP_OK != (err = copydir(vol, dirfd, spath, dpath)))
241 } else if (AFP_OK != (err = copyfile(vol, vol, dirfd, spath, dpath, NULL, NULL))) {
245 /* keep the same time stamp. */
246 ut.actime = ut.modtime = st.st_mtime;
252 /* keep the same time stamp. */
253 if (lstatat(dirfd, src, &st) == 0) {
254 ut.actime = ut.modtime = st.st_mtime;
263 /* ---------------------
264 * is our cached offspring count valid?
266 static int diroffcnt(struct dir *dir, struct stat *st)
268 return st->st_ctime == dir->ctime;
271 /* --------------------- */
272 static int invisible_dots(const struct vol *vol, const char *name)
274 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
277 /* ------------------ */
278 static int set_dir_errors(struct path *path, const char *where, int err)
283 return AFPERR_ACCESS;
287 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
292 * @brief Convert name in client encoding to server encoding
294 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
295 * This only gets called from cname().
297 * @returns 0 on success, -1 on error
299 * @note If the passed ret->m_name is mangled, we'll demangle it
301 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
303 static char temp[ MAXPATHLEN + 1];
307 if (afp_version >= 30) {
309 if (dir->d_did == DIRDID_ROOT_PARENT) {
311 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
312 * So we compare it with the longname from the current volume and if they match
313 * we overwrite the requested path with the utf8 volume name so that the following
316 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
317 if (strcasecmp(ret->m_name, temp) == 0)
318 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
321 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
322 afp_errno = AFPERR_PARAM;
325 strcpy(ret->m_name, temp);
329 /* check for OS X mangled filename :( */
330 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
331 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
332 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
334 if (t != ret->m_name) {
336 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
337 * flags weren't the same
339 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
340 /* at last got our view of mac name */
341 strcpy(ret->m_name, t);
344 } /* afp_version >= 30 */
346 /* If we haven't got it by now, get it */
347 if (ret->u_name == NULL) {
348 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
349 afp_errno = AFPERR_PARAM;
358 * @brief Build struct path from struct dir
360 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
361 * 1. move cwd into parent dir (we're often already there, but not always)
362 * 2. set struct path to the dirname
364 * AFPERR_ACCESS: the dir is there, we just cant chdir into it
365 * AFPERR_NOOBJ: the dir was there when we stated it in cname, so we have a race
366 * 4. indicate there's no dir for this path
369 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
371 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
377 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
380 memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1); /* 3 */
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, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
390 LOG(log_debug, logtype_afpd, "cname('%s') {path-from-dir: AFPERR_ACCESS. curdir:'%s', path:'%s'}",
391 cfrombstr(dir->d_fullpath),
392 cfrombstr(curdir->d_fullpath),
398 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 ) /* 1 */
401 memcpy(ret->m_name, cfrombstr(dir->d_m_name), blength(dir->d_m_name) + 1);
402 if (dir->d_m_name == dir->d_u_name) {
403 ret->u_name = ret->m_name;
405 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
406 memcpy(ret->u_name, cfrombstr(dir->d_u_name), blength(dir->d_u_name) + 1);
409 ret->d_dir = NULL; /* 4 */
410 dir_remove(vol, dir); /* 5 */
417 /* DEADC0DE: never get here */
422 /*********************************************************************************************
424 ********************************************************************************************/
426 int get_afp_errno(const int param)
428 if (afp_errno != AFPERR_DID1)
434 * Resolve struct dir for an absolute path
436 * Given a path like "/Volumes/volume/dir/subdir" in a volume "/Volumes/volume" return
437 * a pointer to struct dir of "subdir".
438 * 1. Remove volue path from absolute path
440 * 3. loop through all elements of the remaining path from 1.
441 * 4. we only allow dirs
443 * 6. if not found in the dircache query the CNID database for the DID
444 * 7. and use dirlookup to resolve the DID to a it's struct dir *
446 * @param vol (r) volume the path is in, must be known
447 * @param path (r) absoule path
449 * @returns pointer to struct dir or NULL on error
451 struct dir *dirlookup_bypath(const struct vol *vol, const char *path)
455 struct dir *dir = NULL;
457 bstring rpath = NULL;
458 bstring statpath = NULL;
459 struct bstrList *l = NULL;
465 EC_NULL(rpath = rel_path_in_vol(path, vol->v_path)); /* 1. */
466 EC_NULL(statpath = bfromcstr(vol->v_path)); /* 2. */
468 l = bsplit(rpath, '/');
469 for (int i = 0; i < l->qty ; i++) { /* 3. */
471 EC_ZERO(bconcat(statpath, l->entry[i]));
472 EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st),
473 "lstat(rpath: %s, elem: %s): %s: %s",
474 cfrombstr(rpath), cfrombstr(l->entry[i]),
475 cfrombstr(statpath), strerror(errno));
477 if (!(S_ISDIR(st.st_mode))) /* 4. */
480 if ((dir = dircache_search_by_name(vol, /* 5. */
482 cfrombstr(l->entry[i]),
483 blength(l->entry[i]),
484 st.st_ctime)) == NULL) {
485 if ((cnid = cnid_add(vol->v_cdb, /* 6. */
488 cfrombstr(l->entry[i]),
489 blength(l->entry[i]),
490 0)) == CNID_INVALID) {
494 if ((dir = dirlookup(vol, cnid)) == NULL) /* 7. */
498 EC_ZERO(bcatcstr(statpath, "/"));
512 * @brief Resolve a DID
514 * Resolve a DID, allocate a struct dir for it
515 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
516 * 2a. Check if the DID is in the cache.
517 * 2b. Check if it's really a dir (d_fullpath != NULL) because we cache files too.
518 * 3. If it's not in the cache resolve it via the database.
519 * 4. Build complete server-side path to the dir.
520 * 5. Check if it exists and is a directory.
521 * 6. Create the struct dir and populate it.
522 * 7. Add it to the cache.
524 * @param vol (r) pointer to struct vol
525 * @param did (r) DID to resolve
527 * @returns pointer to struct dir
529 struct dir *dirlookup(const struct vol *vol, cnid_t did)
531 static char buffer[12 + MAXPATHLEN + 1];
533 struct dir *ret = NULL, *pdir;
534 bstring fullpath = NULL;
535 char *upath = NULL, *mpath;
538 int buflen = 12 + MAXPATHLEN + 1;
542 LOG(log_debug, logtype_afpd, "dirlookup(did: %u)", ntohl(did));
544 /* check for did 0, 1 and 2 */
545 if (did == 0 || vol == NULL) { /* 1 */
546 afp_errno = AFPERR_PARAM;
549 } else if (did == DIRDID_ROOT_PARENT) {
550 rootParent.d_vid = vol->v_vid;
553 } else if (did == DIRDID_ROOT) {
558 /* Search the cache */
559 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2a */
560 if (ret->d_fullpath == NULL) { /* 2b */
561 afp_errno = AFPERR_BADTYPE;
565 if (lstat(cfrombstr(ret->d_fullpath), &st) != 0) {
566 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {lstat: %s}", ntohl(did), strerror(errno));
570 /* It's not there anymore, so remove it */
571 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {calling dir_remove()}", ntohl(did));
572 dir_remove(vol, ret);
573 afp_errno = AFPERR_NOOBJ;
588 utf8 = utf8_encoding();
589 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
591 /* Get it from the database */
593 if ((upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL) {
594 afp_errno = AFPERR_NOOBJ;
598 if ((upath = strdup(upath)) == NULL) { /* 3 */
599 afp_errno = AFPERR_NOOBJ;
606 * Recurse up the tree, terminates in dirlookup when either
607 * - DIRDID_ROOT is hit
608 * - a cached entry is found
610 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {recursion for did: %u}", ntohl(pdid));
611 if ((pdir = dirlookup(vol, pdid)) == NULL) {
616 /* build the fullpath */
617 if ((fullpath = bstrcpy(pdir->d_fullpath)) == NULL
618 || bconchar(fullpath, '/') != BSTR_OK
619 || bcatcstr(fullpath, upath) != BSTR_OK) {
624 /* stat it and check if it's a dir */
625 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstr(fullpath));
627 if (stat(cfrombstr(fullpath), &st) != 0) { /* 5a */
630 afp_errno = AFPERR_NOOBJ;
634 afp_errno = AFPERR_ACCESS;
638 afp_errno = AFPERR_MISC;
643 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
644 afp_errno = AFPERR_BADTYPE;
650 /* Get macname from unix name */
651 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
652 afp_errno = AFPERR_NOOBJ;
657 /* Create struct dir */
658 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath, st.st_ctime)) == NULL) { /* 6 */
659 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
664 /* Add it to the cache only if it's a dir */
665 if (dircache_add(vol, ret) != 0) { /* 7 */
671 if (upath) free(upath);
673 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
674 ntohl(did), AfpErr2name(afp_errno));
683 LOG(log_debug, logtype_afpd, "dirlookup(did: %u): pdid: %u, \"%s\"",
684 ntohl(ret->d_did), ntohl(ret->d_pdid), cfrombstr(ret->d_fullpath));
689 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
691 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
696 static u_int32_t did = 0;
697 static char cname[MAXPATHLEN];
698 static char lname[MAXPATHLEN];
699 ucs2_t u2_path[MAXPATHLEN];
700 ucs2_t u2_dename[MAXPATHLEN];
701 char *tmp, *savepath;
703 if (!(vol->v_flags & AFPVOL_CASEINSEN))
706 if (veto_file(ENUMVETO, path->u_name))
709 savepath = path->u_name;
711 /* very simple cache */
712 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
713 path->u_name = cname;
715 if (of_stat( path ) == 0 ) {
718 /* something changed, we cannot stat ... */
722 if (NULL == ( dp = opendir( "." )) ) {
723 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
728 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
729 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
730 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
732 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
734 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
735 if (NULL == check_dirent(vol, de->d_name))
738 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
741 if (strcasecmp_w( u2_path, u2_dename) == 0) {
743 strlcpy(cname, de->d_name, sizeof(cname));
744 path->u_name = cname;
746 if (of_stat( path ) == 0 ) {
747 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
748 strlcpy(lname, tmp, sizeof(lname));
761 /* invalidate cache */
764 path->u_name = savepath;
766 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
772 * @brief Construct struct dir
774 * Construct struct dir from parameters.
776 * @param m_name (r) directory name in UTF8-dec
777 * @param u_name (r) directory name in server side encoding
778 * @param vol (r) pointer to struct vol
779 * @param pdid (r) Parent CNID
780 * @param did (r) CNID
781 * @param path (r) Full unix path to dir or NULL for files
782 * @param ctime (r) st_ctime from stat
784 * @returns pointer to new struct dir or NULL on error
786 * @note Most of the time mac name and unix name are the same.
788 struct dir *dir_new(const char *m_name,
790 const struct vol *vol,
798 dir = (struct dir *) calloc(1, sizeof( struct dir ));
802 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
807 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
810 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
811 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
812 dir->d_m_name_ucs2 = NULL;
815 if (m_name == u_name || !strcmp(m_name, u_name)) {
816 dir->d_u_name = dir->d_m_name;
818 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
819 bdestroy(dir->d_m_name);
826 dir->d_vid = vol->v_vid;
827 dir->d_fullpath = path;
828 dir->ctime_dircache = ctime;
833 * @brief Free a struct dir and all its members
835 * @param (rw) pointer to struct dir
837 void dir_free(struct dir *dir)
839 if (dir->d_u_name != dir->d_m_name) {
840 bdestroy(dir->d_u_name);
842 if (dir->d_m_name_ucs2)
843 free(dir->d_m_name_ucs2);
844 bdestroy(dir->d_m_name);
845 bdestroy(dir->d_fullpath);
850 * @brief Create struct dir from struct path
852 * Create a new struct dir from struct path. Then add it to the cache.
854 * 1. Open adouble file, get CNID from it.
855 * 2. Search the database, hinting with the CNID from (1).
856 * 3. Build fullpath and create struct dir.
857 * 4. Add it to the cache.
859 * @param vol (r) pointer to struct vol, possibly modified in callee
860 * @param dir (r) pointer to parrent directory
861 * @param path (rw) pointer to struct path with valid path->u_name
862 * @param len (r) strlen of path->u_name
864 * @returns Pointer to new struct dir or NULL on error.
866 * @note Function also assigns path->m_name from path->u_name.
868 struct dir *dir_add(struct vol *vol, const struct dir *dir, struct path *path, int len)
871 struct dir *cdir = NULL;
874 struct adouble *adp = NULL;
882 if ((cdir = dircache_search_by_name(vol, dir, path->u_name, strlen(path->u_name), path->st.st_ctime)) != NULL) {
883 /* there's a stray entry in the dircache */
884 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
885 ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
886 ntohl(cdir->d_did), cfrombstr(dir->d_fullpath));
887 if (dir_remove(vol, cdir) != 0) {
889 AFP_PANIC("dir_add");
893 /* get_id needs adp for reading CNID from adouble file */
894 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
895 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
899 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
905 ad_close_metadata(adp);
907 /* Get macname from unixname */
908 if (path->m_name == NULL) {
909 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
910 LOG(log_error, logtype_afpd, "dir_add(\"%s\"): can't assign macname", path->u_name);
917 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
918 || (bconchar(fullpath, '/') != BSTR_OK)
919 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
920 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
925 /* Allocate and initialize struct dir */
926 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath, path->st.st_ctime)) == NULL) { /* 3 */
931 if ((dircache_add(vol, cdir)) != 0) { /* 4 */
932 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstr(fullpath));
938 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
939 cfrombstr(dir->d_u_name), path->u_name, err);
942 ad_close_metadata(adp);
943 if (!cdir && fullpath)
950 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
951 ntohl(dir->d_did), cfrombstr(dir->d_fullpath), path->u_name,
952 ntohl(cdir->d_did), cfrombstr(cdir->d_fullpath));
959 * Free the queue with invalid struct dirs
961 * This gets called at the end of every AFP func.
963 void dir_free_invalid_q(void)
966 while (dir = (struct dir *)dequeue(invalid_dircache_entries))
971 * @brief Remove a dir from a cache and queue it for freeing
973 * 1. Check if the dir is locked or has opened forks
974 * 2. Remove it from the cache
975 * 3. Queue it for removal
976 * 4. If it's a request to remove curdir, mark curdir as invalid
977 * 5. Mark it as invalid
979 * @param (r) pointer to struct vol
980 * @param (rw) pointer to struct dir
982 int dir_remove(const struct vol *vol, struct dir *dir)
987 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
990 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
991 ntohl(dir->d_did), cfrombstr(dir->d_u_name));
993 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX); /* 2 */
994 enqueue(invalid_dircache_entries, dir); /* 3 */
996 if (curdir == dir) /* 4 */
999 dir->d_did = CNID_INVALID; /* 5 */
1006 * @brief Modify a struct dir, adjust cache
1008 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
1009 * If given new_uname == new_mname, new_uname will point to new_mname.
1011 * @param vol (r) pointer to struct vol
1012 * @param dir (rw) pointer to struct dir
1013 * @param pdid (r) new parent DID
1014 * @param did (r) new DID
1015 * @param new_mname (r) new mac-name
1016 * @param new_uname (r) new unix-name
1017 * @param pdir_fullpath (r) new fullpath of parent dir
1019 int dir_modify(const struct vol *vol,
1023 const char *new_mname,
1024 const char *new_uname,
1025 bstring pdir_fullpath)
1029 /* Remove it from the cache */
1030 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
1038 /* free uname if it's not the same as mname */
1039 if (dir->d_m_name != dir->d_u_name)
1040 bdestroy(dir->d_u_name);
1042 if (new_uname == NULL)
1043 new_uname = new_mname;
1045 /* assign new name */
1046 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
1047 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
1051 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
1052 dir->d_u_name = dir->d_m_name;
1054 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
1055 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
1061 if (pdir_fullpath) {
1062 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
1064 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
1066 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
1070 if (dir->d_m_name_ucs2)
1071 free(dir->d_m_name_ucs2);
1072 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))
1073 dir->d_m_name_ucs2 = NULL;
1075 /* Re-add it to the cache */
1076 if ((dircache_add(vol, dir)) != 0) {
1078 AFP_PANIC("dir_modify");
1086 * @brief Resolve a catalog node name path
1088 * 1. Evaluate path type
1089 * 2. Move to start dir, if we cant, it might eg because of EACCES, build
1090 * path from dirname, so eg getdirparams has sth it can chew on. curdir
1091 * is dir parent then. All this is done in path_from_dir().
1092 * 3. Parse next cnode name in path, cases:
1093 * 4. single "\0" -> do nothing
1094 * 5. two or more consecutive "\0" -> chdir("..") one or more times
1095 * 6. cnode name -> copy it to path.m_name
1096 * 7. Get unix name from mac name
1097 * 8. Special handling of request with did 1
1098 * 9. stat the cnode name
1099 * 10. If it's not there, it's probably an afp_createfile|dir,
1100 * return with curdir = dir parent, struct path = dirname
1101 * 11. If it's there and it's a file, it must should be the last element of the requested
1102 * path. Return with curdir = cnode name parent dir, struct path = filename
1103 * 12. Treat symlinks like files, dont follow them
1104 * 13. If it's a dir:
1105 * 14. Search the dircache for it
1106 * 15. If it's not in the cache, create a struct dir for it and add it to the cache
1107 * 16. chdir into the dir and
1108 * 17. set m_name to the mac equivalent of "."
1111 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1113 static char path[ MAXPATHLEN + 1];
1114 static struct path ret;
1124 LOG(log_maxdebug, logtype_afpd, "came('%s'): {start}", cfrombstr(dir->d_fullpath));
1127 afp_errno = AFPERR_NOOBJ;
1128 memset(&ret, 0, sizeof(ret));
1130 switch (ret.m_type = *data) { /* 1 */
1133 len = (unsigned char) *data++;
1135 if (afp_version >= 30) {
1141 if (afp_version >= 30) {
1143 memcpy(&hint, data, sizeof(hint));
1145 data += sizeof(hint);
1147 memcpy(&len16, data, sizeof(len16));
1153 /* else it's an error */
1155 afp_errno = AFPERR_PARAM;
1158 *cpath += len + size;
1163 if (movecwd(vol, dir) < 0 ) {
1164 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1165 ntohl(dir->d_did), cfrombstr(dir->d_fullpath));
1167 return path_from_dir(vol, dir, &ret);
1172 while (len) { /* 3 */
1173 if (*data == 0) { /* 4 or 5 */
1176 while (len > 0 && *data == 0) { /* 5 */
1177 /* chdir to parrent dir */
1178 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1180 if (movecwd( vol, dir ) < 0 ) {
1181 dir_remove(vol, dir);
1191 for ( p = path; *data != 0 && len > 0; len-- ) {
1193 if (p > &path[UTF8FILELEN_EARLY]) { /* FIXME safeguard, limit of early Mac OS X */
1194 afp_errno = AFPERR_PARAM;
1198 *p = 0; /* Terminate string */
1201 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) { /* 7 */
1202 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1206 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstr(dir->d_fullpath), ret.u_name);
1208 /* Prevent access to our special folders like .AppleDouble */
1209 if (check_name(vol, ret.u_name)) {
1210 /* the name is illegal */
1211 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1212 afp_errno = AFPERR_PARAM;
1216 if (dir->d_did == DIRDID_ROOT_PARENT) { /* 8 */
1218 * Special case: CNID 1
1219 * root parent (did 1) has one child: the volume. Requests for did=1 with
1220 * some <name> must check against the volume name.
1222 if ((strcmp(cfrombstr(vol->v_root->d_m_name), ret.m_name)) == 0)
1228 * CNID != 1, eg. most of the times we take this way.
1229 * Now check if current path-part is a file or dir:
1230 * o if it's dir we have to step into it
1231 * o if it's a file we expect it to be the last part of the requested path
1232 * and thus call continue which should terminate the while loop because
1235 if (of_stat(&ret) != 0) { /* 9 */
1237 * ret.u_name doesn't exist, might be afp_createfile|dir
1238 * that means it should have been the last part
1241 /* it wasn't the last part, so we have a bogus path request */
1242 afp_errno = AFPERR_NOOBJ;
1246 * this will terminate clean in while (1) because len == 0,
1247 * probably afp_createfile|dir
1249 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}",
1250 cfrombstr(dir->d_fullpath), ret.u_name);
1254 switch (ret.st.st_mode & S_IFMT) {
1255 case S_IFREG: /* 11 */
1256 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}",
1257 cfrombstr(dir->d_fullpath), ret.u_name);
1259 /* it wasn't the last part, so we have a bogus path request */
1260 afp_errno = AFPERR_PARAM;
1263 continue; /* continues while loop */
1264 case S_IFLNK: /* 12 */
1265 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}",
1266 cfrombstr(dir->d_fullpath), ret.u_name);
1268 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}",
1269 cfrombstr(dir->d_fullpath), ret.u_name);
1270 afp_errno = AFPERR_PARAM;
1273 continue; /* continues while loop */
1274 case S_IFDIR: /* 13 */
1277 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1278 afp_errno = AFPERR_NODIR;
1282 /* Search the cache */
1283 int unamelen = strlen(ret.u_name);
1284 cdir = dircache_search_by_name(vol, dir, ret.u_name, unamelen, ret.st.st_ctime); /* 14 */
1286 /* Not in cache, create one */
1287 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) { /* 15 */
1288 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1289 ntohl(dir->d_did), ret.u_name, getcwdpath());
1293 } /* if/else cnid==1 */
1295 /* Now chdir to the evaluated dir */
1296 if (movecwd( vol, cdir ) < 0 ) { /* 16 */
1297 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1298 cfrombstr(curdir->d_fullpath), cfrombstr(cdir->d_fullpath), strerror(errno));
1300 return path_from_dir(vol, cdir, &ret);
1305 ret.m_name[0] = 0; /* 17, so we later know last token was a dir */
1308 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1309 afp_errno = AFPERR_DID1;
1313 if (ret.m_name[0] == 0) {
1314 /* Last part was a dir */
1315 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1319 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1320 cfrombstr(dir->d_fullpath),
1321 cfrombstr(curdir->d_fullpath),
1328 * @brief chdir() to dir
1330 * @param vol (r) pointer to struct vol
1331 * @param dir (r) pointer to struct dir
1333 * @returns 0 on success, -1 on error with afp_errno set appropiately
1335 int movecwd(const struct vol *vol, struct dir *dir)
1342 LOG(log_maxdebug, logtype_afpd, "movecwd: from: curdir:\"%s\", cwd:\"%s\"",
1343 curdir ? cfrombstr(curdir->d_fullpath) : "INVALID", getcwdpath());
1345 if (dir->d_did == DIRDID_ROOT_PARENT) {
1346 curdir = &rootParent;
1350 LOG(log_debug, logtype_afpd, "movecwd(to: did: %u, \"%s\")",
1351 ntohl(dir->d_did), cfrombstr(dir->d_fullpath));
1353 if ((ret = lchdir(cfrombstr(dir->d_fullpath))) != 0 ) {
1354 LOG(log_debug, logtype_afpd, "movecwd(\"%s\"): ret: %u, %s",
1355 cfrombstr(dir->d_fullpath), ret, strerror(errno));
1357 /* p is a symlink or getcwd failed */
1358 afp_errno = AFPERR_BADTYPE;
1360 if (chdir(vol->v_path ) < 0) {
1361 LOG(log_error, logtype_afpd, "can't chdir back'%s': %s", vol->v_path, strerror(errno));
1362 /* XXX what do we do here? */
1364 curdir = vol->v_root;
1371 afp_errno = AFPERR_ACCESS;
1374 afp_errno = AFPERR_NOOBJ;
1384 * We can't use unix file's perm to support Apple's inherited protection modes.
1385 * If we aren't the file's owner we can't change its perms when moving it and smb
1386 * nfs,... don't even try.
1388 #define AFP_CHECK_ACCESS
1390 int check_access(char *path, int mode)
1392 #ifdef AFP_CHECK_ACCESS
1400 accessmode(p, &ma, curdir, NULL);
1401 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1403 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1409 /* --------------------- */
1410 int file_access(struct path *path, int mode)
1414 accessmode(path->u_name, &ma, curdir, &path->st);
1416 LOG(log_debug, logtype_afpd, "file_access(\"%s\"): mapped user mode: 0x%02x",
1417 path->u_name, ma.ma_user);
1419 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE)) {
1420 LOG(log_debug, logtype_afpd, "file_access(\"%s\"): write access denied", path->u_name);
1423 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD)) {
1424 LOG(log_debug, logtype_afpd, "file_access(\"%s\"): read access denied", path->u_name);
1431 /* --------------------- */
1432 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1434 dir->offcnt = count;
1435 dir->ctime = st->st_ctime;
1436 dir->d_flags &= ~DIRF_CNID;
1440 /* ---------------------
1441 * is our cached also for reenumerate id?
1443 int dirreenumerate(struct dir *dir, struct stat *st)
1445 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1448 /* ------------------------------
1450 (name, dir) with curdir:name == dir, from afp_enumerate
1453 int getdirparams(const struct vol *vol,
1454 u_int16_t bitmap, struct path *s_path,
1456 char *buf, size_t *buflen )
1460 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1461 int bit = 0, isad = 0;
1467 struct stat *st = &s_path->st;
1468 char *upath = s_path->u_name;
1470 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1471 (1 << DIRPBIT_CDATE) |
1472 (1 << DIRPBIT_MDATE) |
1473 (1 << DIRPBIT_BDATE) |
1474 (1 << DIRPBIT_FINFO)))) {
1476 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1477 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1479 if (ad.ad_md->adf_flags & O_CREAT) {
1480 /* We just created it */
1481 ad_setname(&ad, s_path->m_name);
1496 while ( bitmap != 0 ) {
1497 while (( bitmap & 1 ) == 0 ) {
1505 ad_getattr(&ad, &ashort);
1506 } else if (invisible_dots(vol, cfrombstr(dir->d_u_name))) {
1507 ashort = htons(ATTRBIT_INVISIBLE);
1510 ashort |= htons(ATTRBIT_SHARED);
1511 memcpy( data, &ashort, sizeof( ashort ));
1512 data += sizeof( ashort );
1516 memcpy( data, &pdid, sizeof( pdid ));
1517 data += sizeof( pdid );
1518 LOG(log_debug, logtype_afpd, "metadata('%s'): Parent DID: %u",
1519 s_path->u_name, ntohl(pdid));
1522 case DIRPBIT_CDATE :
1523 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1524 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1525 memcpy( data, &aint, sizeof( aint ));
1526 data += sizeof( aint );
1529 case DIRPBIT_MDATE :
1530 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1531 memcpy( data, &aint, sizeof( aint ));
1532 data += sizeof( aint );
1535 case DIRPBIT_BDATE :
1536 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1537 aint = AD_DATE_START;
1538 memcpy( data, &aint, sizeof( aint ));
1539 data += sizeof( aint );
1542 case DIRPBIT_FINFO :
1544 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1545 } else { /* no appledouble */
1546 memset( data, 0, 32 );
1547 /* set default view -- this also gets done in ad_open() */
1548 ashort = htons(FINDERINFO_CLOSEDVIEW);
1549 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1551 /* dot files are by default visible */
1552 if (invisible_dots(vol, cfrombstr(dir->d_u_name))) {
1553 ashort = htons(FINDERINFO_INVISIBLE);
1554 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1560 case DIRPBIT_LNAME :
1561 if (dir->d_m_name) /* root of parent can have a null name */
1564 memset(data, 0, sizeof(u_int16_t));
1565 data += sizeof( u_int16_t );
1568 case DIRPBIT_SNAME :
1569 memset(data, 0, sizeof(u_int16_t));
1570 data += sizeof( u_int16_t );
1574 memcpy( data, &dir->d_did, sizeof( aint ));
1575 data += sizeof( aint );
1576 LOG(log_debug, logtype_afpd, "metadata('%s'): DID: %u",
1577 s_path->u_name, ntohl(dir->d_did));
1580 case DIRPBIT_OFFCNT :
1582 /* this needs to handle current directory access rights */
1583 if (diroffcnt(dir, st)) {
1584 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1586 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1587 setdiroffcnt(dir, st, ret);
1588 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1590 ashort = htons( ashort );
1591 memcpy( data, &ashort, sizeof( ashort ));
1592 data += sizeof( ashort );
1596 aint = htonl(st->st_uid);
1597 memcpy( data, &aint, sizeof( aint ));
1598 data += sizeof( aint );
1602 aint = htonl(st->st_gid);
1603 memcpy( data, &aint, sizeof( aint ));
1604 data += sizeof( aint );
1607 case DIRPBIT_ACCESS :
1608 accessmode( upath, &ma, dir , st);
1610 *data++ = ma.ma_user;
1611 *data++ = ma.ma_world;
1612 *data++ = ma.ma_group;
1613 *data++ = ma.ma_owner;
1616 /* Client has requested the ProDOS information block.
1617 Just pass back the same basic block for all
1618 directories. <shirsch@ibm.net> */
1619 case DIRPBIT_PDINFO :
1620 if (afp_version >= 30) { /* UTF8 name */
1621 utf8 = kTextEncodingUTF8;
1622 if (dir->d_m_name) /* root of parent can have a null name */
1625 memset(data, 0, sizeof(u_int16_t));
1626 data += sizeof( u_int16_t );
1628 memcpy(data, &aint, sizeof( aint ));
1629 data += sizeof( aint );
1631 else { /* ProDOS Info Block */
1634 ashort = htons( 0x0200 );
1635 memcpy( data, &ashort, sizeof( ashort ));
1636 data += sizeof( ashort );
1637 memset( data, 0, sizeof( ashort ));
1638 data += sizeof( ashort );
1642 case DIRPBIT_UNIXPR :
1643 aint = htonl(st->st_uid);
1644 memcpy( data, &aint, sizeof( aint ));
1645 data += sizeof( aint );
1646 aint = htonl(st->st_gid);
1647 memcpy( data, &aint, sizeof( aint ));
1648 data += sizeof( aint );
1651 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1652 memcpy( data, &aint, sizeof( aint ));
1653 data += sizeof( aint );
1655 accessmode( upath, &ma, dir , st);
1657 *data++ = ma.ma_user;
1658 *data++ = ma.ma_world;
1659 *data++ = ma.ma_group;
1660 *data++ = ma.ma_owner;
1665 ad_close_metadata( &ad );
1667 return( AFPERR_BITMAP );
1673 ashort = htons( data - buf );
1674 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1675 data = set_name(vol, data, pdid, cfrombstr(dir->d_m_name), dir->d_did, 0);
1677 if ( utf_nameoff ) {
1678 ashort = htons( data - buf );
1679 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1680 data = set_name(vol, data, pdid, cfrombstr(dir->d_m_name), dir->d_did, utf8);
1683 ad_close_metadata( &ad );
1685 *buflen = data - buf;
1689 /* ----------------------------- */
1690 int path_error(struct path *path, int error)
1692 /* - a dir with access error
1693 * - no error it's a file
1696 if (path_isadir(path))
1698 if (path->st_valid && path->st_errno)
1700 return AFPERR_BADTYPE ;
1703 /* ----------------------------- */
1704 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1709 u_int16_t vid, bitmap;
1715 memcpy( &vid, ibuf, sizeof( vid ));
1716 ibuf += sizeof( vid );
1718 if (NULL == ( vol = getvolbyvid( vid )) ) {
1719 return( AFPERR_PARAM );
1722 if (vol->v_flags & AFPVOL_RO)
1723 return AFPERR_VLOCK;
1725 memcpy( &did, ibuf, sizeof( did ));
1726 ibuf += sizeof( int );
1728 if (NULL == ( dir = dirlookup( vol, did )) ) {
1732 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1733 bitmap = ntohs( bitmap );
1734 ibuf += sizeof( bitmap );
1736 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1737 return get_afp_errno(AFPERR_NOOBJ);
1740 if ( *path->m_name != '\0' ) {
1741 rc = path_error(path, AFPERR_NOOBJ);
1742 /* maybe we are trying to set perms back */
1743 if (rc != AFPERR_ACCESS)
1748 * If ibuf is odd, make it even.
1750 if ((u_long)ibuf & 1 ) {
1754 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1755 setvoltime(obj, vol );
1761 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1763 * assume path == '\0' eg. it's a directory in canonical form
1765 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1777 u_int16_t ashort, bshort, oshort;
1779 int change_mdate = 0;
1780 int change_parent_mdate = 0;
1782 u_int16_t bitmap = d_bitmap;
1783 u_char finder_buf[32];
1786 u_int16_t upriv_bit = 0;
1789 upath = path->u_name;
1791 while ( bitmap != 0 ) {
1792 while (( bitmap & 1 ) == 0 ) {
1800 memcpy( &ashort, buf, sizeof( ashort ));
1801 buf += sizeof( ashort );
1803 case DIRPBIT_CDATE :
1805 memcpy(&cdate, buf, sizeof(cdate));
1806 buf += sizeof( cdate );
1808 case DIRPBIT_MDATE :
1809 memcpy(&newdate, buf, sizeof(newdate));
1810 buf += sizeof( newdate );
1812 case DIRPBIT_BDATE :
1814 memcpy(&bdate, buf, sizeof(bdate));
1815 buf += sizeof( bdate );
1817 case DIRPBIT_FINFO :
1819 memcpy( finder_buf, buf, 32 );
1822 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1823 change_parent_mdate = 1;
1824 memcpy( &owner, buf, sizeof(owner));
1825 buf += sizeof( owner );
1828 change_parent_mdate = 1;
1829 memcpy( &group, buf, sizeof( group ));
1830 buf += sizeof( group );
1832 case DIRPBIT_ACCESS :
1834 change_parent_mdate = 1;
1835 ma.ma_user = *buf++;
1836 ma.ma_world = *buf++;
1837 ma.ma_group = *buf++;
1838 ma.ma_owner = *buf++;
1839 mpriv = mtoumode( &ma ) | vol->v_dperm;
1840 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1841 err = set_dir_errors(path, "setdirmode", errno);
1845 /* Ignore what the client thinks we should do to the
1846 ProDOS information block. Skip over the data and
1847 report nothing amiss. <shirsch@ibm.net> */
1848 case DIRPBIT_PDINFO :
1849 if (afp_version < 30) {
1853 err = AFPERR_BITMAP;
1857 case DIRPBIT_UNIXPR :
1858 if (vol_unix_priv(vol)) {
1859 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1860 buf += sizeof( owner );
1861 memcpy( &group, buf, sizeof( group ));
1862 buf += sizeof( group );
1865 change_parent_mdate = 1;
1866 memcpy( &upriv, buf, sizeof( upriv ));
1867 buf += sizeof( upriv );
1868 upriv = ntohl (upriv) | vol->v_dperm;
1869 if (dir_rx_set(upriv)) {
1870 /* maybe we are trying to set perms back */
1871 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1873 err = set_dir_errors(path, "setdirunixmode", errno);
1884 err = AFPERR_BITMAP;
1892 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1894 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1896 * Check to see what we're trying to set. If it's anything
1897 * but ACCESS, UID, or GID, give an error. If it's any of those
1898 * three, we don't need the ad to be open, so just continue.
1900 * note: we also don't need to worry about mdate. also, be quiet
1901 * if we're using the noadouble option.
1903 if (!vol_noadouble(vol) && (d_bitmap &
1904 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1905 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1906 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1907 return AFPERR_ACCESS;
1913 * Check to see if a create was necessary. If it was, we'll want
1914 * to set our name, etc.
1916 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1917 ad_setname(&ad, cfrombstr(curdir->d_m_name));
1923 while ( bitmap != 0 ) {
1924 while (( bitmap & 1 ) == 0 ) {
1932 ad_getattr(&ad, &bshort);
1934 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1935 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1939 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1940 change_parent_mdate = 1;
1941 ad_setattr(&ad, bshort);
1944 case DIRPBIT_CDATE :
1946 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1949 case DIRPBIT_MDATE :
1951 case DIRPBIT_BDATE :
1953 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1956 case DIRPBIT_FINFO :
1958 /* Fixes #2802236 */
1959 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1960 *fflags &= htons(~FINDERINFO_ISHARED);
1962 if ( dir->d_did == DIRDID_ROOT ) {
1964 * Alright, we admit it, this is *really* sick!
1965 * The 4 bytes that we don't copy, when we're dealing
1966 * with the root of a volume, are the directory's
1967 * location information. This eliminates that annoying
1968 * behavior one sees when mounting above another mount
1971 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1972 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1974 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1978 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1979 if ( (dir->d_did == DIRDID_ROOT) &&
1980 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1981 err = set_dir_errors(path, "setdeskowner", errno);
1982 if (isad && err == AFPERR_PARAM) {
1983 err = AFP_OK; /* ???*/
1986 goto setdirparam_done;
1989 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1990 err = set_dir_errors(path, "setdirowner", errno);
1991 goto setdirparam_done;
1995 if (dir->d_did == DIRDID_ROOT)
1996 setdeskowner( -1, ntohl(group) );
1997 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1998 err = set_dir_errors(path, "setdirowner", errno);
1999 goto setdirparam_done;
2002 case DIRPBIT_ACCESS :
2003 if (dir->d_did == DIRDID_ROOT) {
2005 if (!dir_rx_set(mpriv)) {
2006 /* we can't remove read and search for owner on volume root */
2007 err = AFPERR_ACCESS;
2008 goto setdirparam_done;
2012 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
2013 err = set_dir_errors(path, "setdirmode", errno);
2014 goto setdirparam_done;
2017 case DIRPBIT_PDINFO :
2018 if (afp_version >= 30) {
2019 err = AFPERR_BITMAP;
2020 goto setdirparam_done;
2023 case DIRPBIT_UNIXPR :
2024 if (vol_unix_priv(vol)) {
2025 if (dir->d_did == DIRDID_ROOT) {
2026 if (!dir_rx_set(upriv)) {
2027 /* we can't remove read and search for owner on volume root */
2028 err = AFPERR_ACCESS;
2029 goto setdirparam_done;
2031 setdeskowner( -1, ntohl(group) );
2032 setdeskmode( upriv );
2034 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
2035 err = set_dir_errors(path, "setdirowner", errno);
2036 goto setdirparam_done;
2039 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
2040 err = set_dir_errors(path, "setdirunixmode", errno);
2041 goto setdirparam_done;
2045 err = AFPERR_BITMAP;
2046 goto setdirparam_done;
2050 err = AFPERR_BITMAP;
2051 goto setdirparam_done;
2060 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
2061 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2065 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
2066 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
2071 if (path->st_valid && !path->st_errno) {
2072 struct stat *st = &path->st;
2074 if (dir && dir->d_pdid) {
2075 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
2079 ad_close_metadata( &ad);
2082 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
2083 && gettimeofday(&tv, NULL) == 0) {
2084 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) == 0) {
2085 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
2086 /* be careful with bitmap because now dir is null */
2087 bitmap = 1<<DIRPBIT_MDATE;
2088 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
2089 /* should we reset curdir ?*/
2096 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2110 memcpy( &vid, ibuf, sizeof( vid ));
2111 ibuf += sizeof( vid );
2112 if (NULL == (vol = getvolbyvid( vid )) ) {
2113 return( AFPERR_PARAM );
2116 memcpy( &did, ibuf, sizeof( did ));
2117 ibuf += sizeof( did );
2121 * if it's CNID 2 our only choice to meet the specs is call sync.
2122 * For any other CNID just sync that dir. To my knowledge the
2123 * intended use of FPSyncDir is to sync the volume so all we're
2124 * ever going to see here is probably CNID 2. Anyway, we' prepared.
2127 if ( ntohl(did) == 2 ) {
2130 if (NULL == ( dir = dirlookup( vol, did )) ) {
2131 return afp_errno; /* was AFPERR_NOOBJ */
2134 if (movecwd( vol, dir ) < 0 )
2135 return ( AFPERR_NOOBJ );
2138 * Assuming only OSens that have dirfd also may require fsyncing directories
2139 * in order to flush metadata e.g. Linux.
2143 if (NULL == ( dp = opendir( "." )) ) {
2146 return( AFPERR_NOOBJ );
2148 return( AFPERR_ACCESS );
2150 return( AFPERR_PARAM );
2154 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2157 if ( fsync ( dfd ) < 0 )
2158 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2159 dir->d_u_name, strerror(errno) );
2160 closedir(dp); /* closes dfd too */
2163 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2166 return( AFPERR_NOOBJ );
2168 return( AFPERR_ACCESS );
2170 return( AFPERR_PARAM );
2174 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2175 vol->ad_path(".", ADFLAGS_DIR) );
2177 if ( fsync(dfd) < 0 )
2178 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2179 vol->ad_path(cfrombstr(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2186 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2192 struct path *s_path;
2200 memcpy( &vid, ibuf, sizeof( vid ));
2201 ibuf += sizeof( vid );
2202 if (NULL == ( vol = getvolbyvid( vid )) ) {
2203 return( AFPERR_PARAM );
2206 if (vol->v_flags & AFPVOL_RO)
2207 return AFPERR_VLOCK;
2209 memcpy( &did, ibuf, sizeof( did ));
2210 ibuf += sizeof( did );
2211 if (NULL == ( dir = dirlookup( vol, did )) ) {
2212 return afp_errno; /* was AFPERR_NOOBJ */
2214 /* for concurrent access we need to be sure we are not in the
2215 * folder we want to create...
2219 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2220 return get_afp_errno(AFPERR_PARAM);
2222 /* cname was able to move curdir to it! */
2223 if (*s_path->m_name == '\0')
2224 return AFPERR_EXIST;
2226 upath = s_path->u_name;
2228 if (AFP_OK != (err = netatalk_mkdir(vol, upath))) {
2232 if (of_stat(s_path) < 0) {
2238 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2242 if ( movecwd( vol, dir ) < 0 ) {
2243 return( AFPERR_PARAM );
2246 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2247 if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2248 if (vol_noadouble(vol))
2249 goto createdir_done;
2250 return( AFPERR_ACCESS );
2252 ad_setname(&ad, s_path->m_name);
2253 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2256 ad_close_metadata( &ad);
2259 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2260 *rbuflen = sizeof( u_int32_t );
2261 setvoltime(obj, vol );
2266 * dst new unix filename (not a pathname)
2267 * newname new mac name
2269 * dirfd -1 means ignore dirfd (or use AT_FDCWD), otherwise src is relative to dirfd
2271 int renamedir(const struct vol *vol,
2276 struct dir *newparent,
2282 /* existence check moved to afp_moveandrename */
2283 if ( unix_rename(dirfd, src, -1, dst ) < 0 ) {
2286 return( AFPERR_NOOBJ );
2288 return( AFPERR_ACCESS );
2290 return AFPERR_VLOCK;
2292 /* tried to move directory into a subdirectory of itself */
2293 return AFPERR_CANTMOVE;
2295 /* this needs to copy and delete. bleah. that means we have
2296 * to deal with entire directory hierarchies. */
2297 if ((err = copydir(vol, dirfd, src, dst)) < 0) {
2301 if ((err = deletedir(dirfd, src)) < 0)
2305 return( AFPERR_PARAM );
2309 vol->vfs->vfs_renamedir(vol, dirfd, src, dst);
2311 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2313 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2314 ad_setname(&ad, newname);
2316 ad_close_metadata( &ad);
2322 /* delete an empty directory */
2323 int deletecurdir(struct vol *vol)
2327 struct dir *fdir, *pdir;
2333 if ((pdir = dirlookup(vol, curdir->d_pdid)) == NULL) {
2334 return( AFPERR_ACCESS );
2339 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2340 /* we never want to create a resource fork here, we are going to delete it */
2341 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2343 ad_getattr(&ad, &ashort);
2344 ad_close_metadata(&ad);
2345 if ((ashort & htons(ATTRBIT_NODELETE))) {
2346 return AFPERR_OLOCK;
2349 err = vol->vfs->vfs_deletecurdir(vol);
2351 LOG(log_error, logtype_afpd, "deletecurdir: error deleting .AppleDouble in \"%s\"",
2352 curdir->d_fullpath);
2356 /* now get rid of dangling symlinks */
2357 if ((dp = opendir("."))) {
2358 while ((de = readdir(dp))) {
2359 /* skip this and previous directory */
2360 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2363 /* bail if it's not a symlink */
2364 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2365 LOG(log_error, logtype_afpd, "deletecurdir(\"%s\"): not empty",
2366 curdir->d_fullpath);
2368 return AFPERR_DIRNEMPT;
2371 if ((err = netatalk_unlink(de->d_name))) {
2378 if (movecwd(vol, pdir) < 0) {
2383 LOG(log_debug, logtype_afpd, "deletecurdir: moved to \"%s\"",
2384 cfrombstr(curdir->d_fullpath));
2386 err = netatalk_rmdir_all_errors(-1, cfrombstr(fdir->d_u_name));
2387 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2388 cnid_delete(vol->v_cdb, fdir->d_did);
2389 dir_remove( vol, fdir );
2391 LOG(log_error, logtype_afpd, "deletecurdir(\"%s\"): netatalk_rmdir_all_errors error",
2392 curdir->d_fullpath);
2397 /* inode is used as key for cnid.
2398 * Close the descriptor only after cnid_delete
2406 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2416 sfunc = (unsigned char) *ibuf++;
2419 if (sfunc >= 3 && sfunc <= 6) {
2420 if (afp_version < 30) {
2421 return( AFPERR_PARAM );
2428 case 3 :/* unicode */
2429 memcpy( &id, ibuf, sizeof( id ));
2432 if (( pw = getpwuid( id )) == NULL ) {
2433 return( AFPERR_NOITEM );
2435 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2436 pw->pw_name, -1, &name);
2443 case 4 : /* unicode */
2444 memcpy( &id, ibuf, sizeof( id ));
2447 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2448 return( AFPERR_NOITEM );
2450 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2451 gr->gr_name, -1, &name);
2458 case 5 : /* UUID -> username */
2459 case 6 : /* UUID -> groupname */
2460 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2461 return AFPERR_PARAM;
2462 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2464 len = getnamefromuuid((unsigned char*) ibuf, &name, &type);
2465 if (len != 0) /* its a error code, not len */
2466 return AFPERR_NOITEM;
2469 if (( pw = getpwnam( name )) == NULL )
2470 return( AFPERR_NOITEM );
2471 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2472 id = htonl(UUID_USER);
2473 memcpy( rbuf, &id, sizeof( id ));
2474 id = htonl( pw->pw_uid);
2475 rbuf += sizeof( id );
2476 memcpy( rbuf, &id, sizeof( id ));
2477 rbuf += sizeof( id );
2478 *rbuflen = 2 * sizeof( id );
2481 if (( gr = getgrnam( name )) == NULL )
2482 return( AFPERR_NOITEM );
2483 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2484 id = htonl(UUID_GROUP);
2485 memcpy( rbuf, &id, sizeof( id ));
2486 rbuf += sizeof( id );
2487 id = htonl( gr->gr_gid);
2488 memcpy( rbuf, &id, sizeof( id ));
2489 rbuf += sizeof( id );
2490 *rbuflen = 2 * sizeof( id );
2494 return (AFPERR_NOITEM);
2501 return( AFPERR_PARAM );
2505 len = strlen( name );
2508 u_int16_t tp = htons(len);
2509 memcpy(rbuf, &tp, sizeof(tp));
2518 memcpy( rbuf, name, len );
2526 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2535 sfunc = (unsigned char) *ibuf++;
2537 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2540 case 2 : /* unicode */
2541 if (afp_version < 30) {
2542 return( AFPERR_PARAM );
2544 memcpy(&ulen, ibuf, sizeof(ulen));
2547 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2551 len = (unsigned char) *ibuf++;
2553 case 5 : /* username -> UUID */
2554 case 6 : /* groupname -> UUID */
2555 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2556 return AFPERR_PARAM;
2557 memcpy(&ulen, ibuf, sizeof(ulen));
2562 return( AFPERR_PARAM );
2568 return AFPERR_PARAM;
2571 case 1 : /* unicode */
2573 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2574 return( AFPERR_NOITEM );
2578 memcpy( rbuf, &id, sizeof( id ));
2579 *rbuflen = sizeof( id );
2582 case 2 : /* unicode */
2584 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2585 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2586 return( AFPERR_NOITEM );
2589 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2591 memcpy( rbuf, &id, sizeof( id ));
2592 *rbuflen = sizeof( id );
2594 case 5 : /* username -> UUID */
2595 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2596 if (0 != getuuidfromname(ibuf, UUID_USER, (unsigned char *)rbuf))
2597 return AFPERR_NOITEM;
2598 *rbuflen = UUID_BINSIZE;
2600 case 6 : /* groupname -> UUID */
2601 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2602 if (0 != getuuidfromname(ibuf, UUID_GROUP, (unsigned char *)rbuf))
2603 return AFPERR_NOITEM;
2604 *rbuflen = UUID_BINSIZE;
2611 /* ------------------------------------
2612 variable DID support
2614 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2625 /* do nothing as dids are static for the life of the process. */
2629 memcpy(&vid, ibuf, sizeof( vid ));
2630 ibuf += sizeof( vid );
2631 if (( vol = getvolbyvid( vid )) == NULL ) {
2632 return( AFPERR_PARAM );
2635 memcpy( &did, ibuf, sizeof( did ));
2636 ibuf += sizeof( did );
2637 if (( dir = dirlookup( vol, did )) == NULL ) {
2638 return( AFPERR_PARAM );
2641 /* dir_remove -- deletedid */
2647 /* did creation gets done automatically
2648 * there's a pb again with case but move it to cname
2650 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2653 struct dir *parentdir;
2661 memcpy(&vid, ibuf, sizeof(vid));
2662 ibuf += sizeof( vid );
2664 if (NULL == ( vol = getvolbyvid( vid )) ) {
2665 return( AFPERR_PARAM );
2668 memcpy(&did, ibuf, sizeof(did));
2669 ibuf += sizeof(did);
2671 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2675 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2676 return get_afp_errno(AFPERR_PARAM);
2679 if ( *path->m_name != '\0' ) {
2680 return path_error(path, AFPERR_NOOBJ);
2683 if ( !path->st_valid && of_stat(path ) < 0 ) {
2684 return( AFPERR_NOOBJ );
2686 if ( path->st_errno ) {
2687 return( AFPERR_NOOBJ );
2690 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2691 *rbuflen = sizeof(curdir->d_did);