2 * $Id: directory.c,v 1.131.2.7 2010-02-02 13:16:15 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
17 #include <sys/param.h>
23 #include <atalk/adouble.h>
24 #include <atalk/vfs.h>
25 #include <atalk/afp.h>
26 #include <atalk/util.h>
27 #include <atalk/cnid.h>
28 #include <atalk/logger.h>
29 #include <atalk/uuid.h>
30 #include <atalk/unix.h>
31 #include <atalk/bstrlib.h>
32 #include <atalk/bstradd.h>
34 #include "directory.h"
46 #ifdef HAVE_NFSv4_ACLS
47 extern void addir_inherit_acl(const struct vol *vol);
51 * FIXMEs, loose ends after the dircache rewrite:
52 * o case-insensitivity is gone
53 * o setdirparams doesn't change parent mdate anymore
54 * o catsearch doesn't work, see FIXMEs in catsearch.c
55 * o curdir per volume caching is gone
56 * o directory offspring count calculation probably broken
57 * o doesn't work with CNID backend last and the like.
58 * CNID backend must support persistent CNIDs.
59 * o dirs and open-forks probably broken, eg. cache eviction
64 /*******************************************************************************************
66 ******************************************************************************************/
69 struct dir rootParent = {
70 NULL, NULL, NULL, NULL, /* path, d_m_name, d_u_name, d_m_name_ucs2 */
71 NULL, NULL, 0, 0, /* qidx_node, d_ofork, ctime, d_flags */
72 0, 0, 0, 0 /* pdid, did, offcnt, d_vid */
74 struct dir *curdir = &rootParent;
75 struct path Cur_Path = {
81 0, /* stat is not set */
85 /*******************************************************************************************
87 ******************************************************************************************/
90 /* -------------------------
91 appledouble mkdir afp error code.
93 static int netatalk_mkdir(const char *name)
95 if (ad_mkdir(name, DIRBITS | 0777) < 0) {
98 return( AFPERR_NOOBJ );
100 return( AFPERR_VLOCK );
103 return( AFPERR_ACCESS );
105 return( AFPERR_EXIST );
108 return( AFPERR_DFULL );
110 return( AFPERR_PARAM );
116 /* ------------------- */
117 static int deletedir(char *dir)
119 char path[MAXPATHLEN + 1];
127 if ((len = strlen(dir)) +2 > sizeof(path))
131 if ((dp = opendir(dir)) == NULL)
137 remain = sizeof(path) -len -1;
138 while ((de = readdir(dp)) && err == AFP_OK) {
139 /* skip this and previous directory */
140 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
143 if (strlen(de->d_name) > remain) {
147 strcpy(path + len, de->d_name);
148 if (stat(path, &st)) {
151 if (S_ISDIR(st.st_mode)) {
152 err = deletedir(path);
154 err = netatalk_unlink(path);
159 /* okay. the directory is empty. delete it. note: we already got rid
162 err = netatalk_rmdir(dir);
167 /* do a recursive copy. */
168 static int copydir(const struct vol *vol, char *src, char *dst)
170 char spath[MAXPATHLEN + 1], dpath[MAXPATHLEN + 1];
179 /* doesn't exist or the path is too long. */
180 if (((slen = strlen(src)) > sizeof(spath) - 2) ||
181 ((dlen = strlen(dst)) > sizeof(dpath) - 2) ||
182 ((dp = opendir(src)) == NULL))
185 /* try to create the destination directory */
186 if (AFP_OK != (err = netatalk_mkdir(dst)) ) {
191 /* set things up to copy */
195 srem = sizeof(spath) - slen -1;
200 drem = sizeof(dpath) - dlen -1;
203 while ((de = readdir(dp))) {
204 /* skip this and previous directory */
205 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
208 if (strlen(de->d_name) > srem) {
212 strcpy(spath + slen, de->d_name);
214 if (stat(spath, &st) == 0) {
215 if (strlen(de->d_name) > drem) {
219 strcpy(dpath + dlen, de->d_name);
221 if (S_ISDIR(st.st_mode)) {
222 if (AFP_OK != (err = copydir(vol, spath, dpath)))
224 } else if (AFP_OK != (err = copyfile(vol, vol, spath, dpath, NULL, NULL))) {
228 /* keep the same time stamp. */
229 ut.actime = ut.modtime = st.st_mtime;
235 /* keep the same time stamp. */
236 if (stat(src, &st) == 0) {
237 ut.actime = ut.modtime = st.st_mtime;
246 /* ---------------------
247 * is our cached offspring count valid?
249 static int diroffcnt(struct dir *dir, struct stat *st)
251 return st->st_ctime == dir->ctime;
254 /* --------------------- */
255 static int invisible_dots(const struct vol *vol, const char *name)
257 return vol_inv_dots(vol) && *name == '.' && strcmp(name, ".") && strcmp(name, "..");
260 /* ------------------ */
261 static int set_dir_errors(struct path *path, const char *where, int err)
266 return AFPERR_ACCESS;
270 LOG(log_error, logtype_afpd, "setdirparam(%s): %s: %s", fullpathname(path->u_name), where, strerror(err) );
275 * @brief Convert name in client encoding to server encoding
277 * Convert ret->m_name to ret->u_name from client encoding to server encoding.
278 * This only gets called from cname().
280 * @returns 0 on success, -1 on error
282 * @note If the passed ret->m_name is mangled, we'll demangle it
284 static int cname_mtouname(const struct vol *vol, const struct dir *dir, struct path *ret, int toUTF8)
286 static char temp[ MAXPATHLEN + 1];
290 if (afp_version >= 30) {
292 if (dir->d_did == DIRDID_ROOT_PARENT) {
294 * With uft8 volume name is utf8-mac, but requested path may be a mangled longname. See #2611981.
295 * So we compare it with the longname from the current volume and if they match
296 * we overwrite the requested path with the utf8 volume name so that the following
299 ucs2_to_charset(vol->v_maccharset, vol->v_macname, temp, AFPVOL_MACNAMELEN + 1);
300 if (strcasecmp(ret->m_name, temp) == 0)
301 ucs2_to_charset(CH_UTF8_MAC, vol->v_u8mname, ret->m_name, AFPVOL_U8MNAMELEN);
304 if (mtoUTF8(vol, ret->m_name, strlen(ret->m_name), temp, MAXPATHLEN) == (size_t)-1) {
305 afp_errno = AFPERR_PARAM;
308 strcpy(ret->m_name, temp);
312 /* check for OS X mangled filename :( */
313 t = demangle_osx(vol, ret->m_name, dir->d_did, &fileid);
314 LOG(log_maxdebug, logtype_afpd, "cname_mtouname('%s',did:%u) {demangled:'%s', fileid:%u}",
315 ret->m_name, ntohl(dir->d_did), t, ntohl(fileid));
317 if (t != ret->m_name) {
319 /* duplicate work but we can't reuse all convert_char we did in demangle_osx
320 * flags weren't the same
322 if ( (t = utompath(vol, ret->u_name, fileid, utf8_encoding())) ) {
323 /* at last got our view of mac name */
324 strcpy(ret->m_name, t);
327 } /* afp_version >= 30 */
329 /* If we haven't got it by now, get it */
330 if (ret->u_name == NULL) {
331 if ((ret->u_name = mtoupath(vol, ret->m_name, dir->d_did, utf8_encoding())) == NULL) {
332 afp_errno = AFPERR_PARAM;
341 * @brief Build struct path from struct dir
343 * The final movecwd in cname failed, possibly with EPERM or ENOENT. We:
344 * 1. move cwd into parent dir (we're often already there, but not always)
346 static struct path *path_from_dir(struct vol *vol, struct dir *dir, struct path *ret)
349 * it's tricky: movecwd failed some of dir path are not there anymore.
350 * FIXME: Is it true with other errors?
352 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
358 if (movecwd( vol, dirlookup(vol, dir->d_pdid)) < 0 )
361 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
362 if (dir->d_m_name == dir->d_u_name) {
363 ret->u_name = ret->m_name;
365 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
366 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
372 ret->st_errno = EACCES;
375 LOG(log_debug, logtype_afpd, "cname(AFPERR_ACCESS:'%s') {path-from-dir: curdir:'%s', path:'%s'}",
376 cfrombstring(dir->d_fullpath),
377 cfrombstring(curdir->d_fullpath),
383 if (movecwd(vol, dirlookup(vol, dir->d_pdid)) < 0 )
386 memcpy(ret->m_name, cfrombstring(dir->d_m_name), blength(dir->d_m_name) + 1);
387 if (dir->d_m_name == dir->d_u_name) {
388 ret->u_name = ret->m_name;
390 ret->u_name = ret->m_name + blength(dir->d_m_name) + 1;
391 memcpy(ret->u_name, cfrombstring(dir->d_u_name), blength(dir->d_u_name) + 1);
396 ret->st_errno = ENOENT;
399 dir_remove(vol, dir);
406 /* DEADC0DE: never get here */
411 /*********************************************************************************************
413 ********************************************************************************************/
415 int get_afp_errno(const int param)
417 if (afp_errno != AFPERR_DID1)
423 * @brief Resolve a DID
425 * Resolve a DID, allocate a struct dir for it
426 * 1. Check for special CNIDs 0 (invalid), 1 and 2.
427 * 2. Check if the DID is in the cache.
428 * 3. If it's not in the cache resolve it via the database.
429 * 4. Build complete server-side path to the dir.
430 * 5. Check if it exists and is a directory.
431 * 6. Create the struct dir and populate it.
432 * 7. Add it to the cache.
434 * @param vol (r) pointer to struct vol
435 * @param did (r) DID to resolve
437 * @returns pointer to struct dir
439 * @note FIXME: OSX calls it with bogus id, ie file ID not folder ID,
440 * and we are really bad in this case.
442 struct dir *dirlookup(const struct vol *vol, cnid_t did)
444 static char buffer[12 + MAXPATHLEN + 1];
445 struct bstrList *pathlist = NULL;
446 bstring fullpath = NULL;
448 struct dir *ret = NULL;
452 int buflen = 12 + MAXPATHLEN + 1;
456 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {start}", ntohl(did));
458 /* check for did 0, 1 and 2 */
459 if (did == 0 || vol == NULL) { /* 1 */
460 afp_errno = AFPERR_PARAM;
462 } else if (did == DIRDID_ROOT_PARENT) {
463 rootParent.d_vid = vol->v_vid;
464 return (&rootParent);
465 } else if (did == DIRDID_ROOT) {
469 /* Search the cache */
470 if ((ret = dircache_search_by_did(vol, did)) != NULL) { /* 2 */
471 if (lstat(cfrombstring(ret->d_fullpath), &st) != 0) {
472 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {lstat: %s}", ntohl(did), strerror(errno));
476 /* It's not there anymore, so remove it */
477 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {calling dir_remove()}", ntohl(did));
478 dir_remove(vol, ret);
479 afp_errno = AFPERR_NOOBJ;
490 utf8 = utf8_encoding();
491 maxpath = (utf8) ? MAXPATHLEN - 7 : 255;
493 /* Create list for path elements, request 16 list elements for now*/
494 if ((pathlist = bstListCreateMin(16)) == NULL) { /* 4 */
495 LOG(log_error, logtype_afpd, "dirlookup(did: %u): OOM: %s", ntohl(did), strerror(errno));
499 /* Get it from the database */
501 if ( (upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
502 afp_errno = AFPERR_NOOBJ;
508 /* construct path, copy already found uname to path element list*/
509 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
510 afp_errno = AFPERR_MISC;
515 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {%u, %s}", ntohl(did), ntohl(pdid), upath);
517 /* The stuff that follows is for building the full path to the directory */
519 /* work upwards until we reach volume root */
520 while (cnid != DIRDID_ROOT) {
521 /* construct path, copy already found uname to path element list*/
522 if ((bstrListPush(pathlist, bfromcstr(upath))) != BSTR_OK) { /* 4 */
523 afp_errno = AFPERR_MISC;
529 if ((upath = cnid_resolve(vol->v_cdb, &cnid, buffer, buflen)) == NULL ) { /* 3 */
530 afp_errno = AFPERR_NOOBJ;
536 if ((bstrListPush(pathlist, bfromcstr(vol->v_path))) != BSTR_OK) { /* 4 */
537 afp_errno = AFPERR_MISC;
542 if ((fullpath = bjoinInv(pathlist, bfromcstr("/"))) == NULL) { /* 4 */
543 afp_errno = AFPERR_MISC;
547 /* Finished building the fullpath */
549 /* stat it and check if it's a dir */
550 LOG(log_debug, logtype_afpd, "dirlookup: {stating %s}", cfrombstring(fullpath));
552 if (stat(cfrombstring(fullpath), &st) != 0) { /* 5a */
555 afp_errno = AFPERR_NOOBJ;
559 afp_errno = AFPERR_ACCESS;
563 afp_errno = AFPERR_MISC;
568 if ( ! S_ISDIR(st.st_mode)) { /* 5b */
569 afp_errno = AFPERR_BADTYPE;
575 /* Get macname from unix name */
576 if ( (mpath = utompath(vol, upath, did, utf8)) == NULL ) {
577 afp_errno = AFPERR_NOOBJ;
582 /* Create struct dir */
583 if ((ret = dir_new(mpath, upath, vol, pdid, did, fullpath)) == NULL) { /* 6 */
584 LOG(log_error, logtype_afpd, "dirlookup(did: %u) {%s, %s}: %s", ntohl(did), mpath, upath, strerror(errno));
589 /* Add it to the cache only if it's a dir */
590 if (dircache_add(ret) != 0) { /* 7 */
595 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {end: did:%u, path:'%s'}",
596 ntohl(did), ntohl(pdid), cfrombstring(ret->d_fullpath));
600 bstrListDestroy(pathlist);
603 LOG(log_debug, logtype_afpd, "dirlookup(did: %u) {exit_error: %s}",
604 ntohl(did), AfpErr2name(afp_errno));
615 #define ENUMVETO "./../Network Trash Folder/TheVolumeSettingsFolder/TheFindByContentFolder/:2eDS_Store/Contents/Desktop Folder/Trash/Benutzer/"
617 int caseenumerate(const struct vol *vol, struct path *path, struct dir *dir)
622 static u_int32_t did = 0;
623 static char cname[MAXPATHLEN];
624 static char lname[MAXPATHLEN];
625 ucs2_t u2_path[MAXPATHLEN];
626 ucs2_t u2_dename[MAXPATHLEN];
627 char *tmp, *savepath;
629 if (!(vol->v_flags & AFPVOL_CASEINSEN))
632 if (veto_file(ENUMVETO, path->u_name))
635 savepath = path->u_name;
637 /* very simple cache */
638 if ( dir->d_did == did && strcmp(lname, path->u_name) == 0) {
639 path->u_name = cname;
641 if (of_stat( path ) == 0 ) {
644 /* something changed, we cannot stat ... */
648 if (NULL == ( dp = opendir( "." )) ) {
649 LOG(log_debug, logtype_afpd, "caseenumerate: opendir failed: %s", dir->d_u_name);
654 /* LOG(log_debug, logtype_afpd, "caseenumerate: for %s", path->u_name); */
655 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, path->u_name, -1, u2_path, sizeof(u2_path)) )
656 LOG(log_debug, logtype_afpd, "caseenumerate: conversion failed for %s", path->u_name);
658 /*LOG(log_debug, logtype_afpd, "caseenumerate: dir: %s, path: %s", dir->d_u_name, path->u_name); */
660 for ( de = readdir( dp ); de != NULL; de = readdir( dp )) {
661 if (NULL == check_dirent(vol, de->d_name))
664 if ((size_t) -1 == convert_string(vol->v_volcharset, CH_UCS2, de->d_name, -1, u2_dename, sizeof(u2_dename)) )
667 if (strcasecmp_w( u2_path, u2_dename) == 0) {
669 strlcpy(cname, de->d_name, sizeof(cname));
670 path->u_name = cname;
672 if (of_stat( path ) == 0 ) {
673 LOG(log_debug, logtype_afpd, "caseenumerate: using dir: %s, path: %s", de->d_name, path->u_name);
674 strlcpy(lname, tmp, sizeof(lname));
687 /* invalidate cache */
690 path->u_name = savepath;
692 /* LOG(log_debug, logtype_afpd, "caseenumerate: path on ret: %s", path->u_name); */
698 * @brief Construct struct dir
700 * Construct struct dir from parameters.
702 * @param m_name (r) directory name in UTF8-dec
703 * @param u_name (r) directory name in server side encoding
704 * @param vol (r) pointer to struct vol
705 * @param pdid (r) Parent CNID
706 * @param did (r) CNID
707 * @param fullpath (r) Full unix path to dir
709 * @returns pointer to new struct dir or NULL on error
711 * @note Most of the time mac name and unix name are the same.
713 struct dir *dir_new(const char *m_name,
715 const struct vol *vol,
722 dir = (struct dir *) calloc(1, sizeof( struct dir ));
726 if ((dir->d_m_name = bfromcstr(m_name)) == NULL) {
731 if (convert_string_allocate( (utf8_encoding()) ? CH_UTF8_MAC : vol->v_maccharset,
734 -1, (char **)&dir->d_m_name_ucs2) == (size_t)-1 ) {
735 LOG(log_error, logtype_afpd, "dir_new(did: %u) {%s, %s}: couldn't set UCS2 name", ntohl(did), m_name, u_name);
736 dir->d_m_name_ucs2 = NULL;
739 if (m_name == u_name || !strcmp(m_name, u_name)) {
740 dir->d_u_name = dir->d_m_name;
742 else if ((dir->d_u_name = bfromcstr(u_name)) == NULL) {
743 bdestroy(dir->d_m_name);
750 dir->d_vid = vol->v_vid;
751 dir->d_fullpath = path;
756 * @brief Free a struct dir and all its members
758 * @param (rw) pointer to struct dir
760 void dir_free(struct dir *dir)
762 if (dir->d_u_name != dir->d_m_name) {
763 bdestroy(dir->d_u_name);
765 if (dir->d_m_name_ucs2)
766 free(dir->d_m_name_ucs2);
767 bdestroy(dir->d_m_name);
768 bdestroy(dir->d_fullpath);
773 * @brief Create struct dir from struct path
775 * Create a new struct dir from struct path. Then add it to the cache.
776 * The caller must have assured that the dir is not already in the cache,
778 * 1. Open adouble file, get CNID from it.
779 * 2. Search the database, hinting with the CNID from (1).
780 * 3. Build fullpath and create struct dir.
781 * 4. Add it to the cache.
783 * @param vol (r) pointer to struct vol
784 * @param dir (r) pointer to parrent directory
785 * @param path (rw) pointer to struct path with valid path->u_name
786 * @param len (r) strlen of path->u_name
788 * @returns Pointer to new struct dir or NULL on error.
790 * @note Function also assigns path->m_name from path->u_name.
792 struct dir *dir_add(const struct vol *vol, const struct dir *dir, struct path *path, int len)
795 struct dir *cdir = NULL;
798 struct adouble *adp = NULL;
806 if ((cdir = dircache_search_by_name(vol, dir->d_did, path->u_name, strlen(path->u_name))) != NULL) {
807 /* there's a stray entry in the dircache */
808 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {stray cache entry: did:%u,'%s', removing}",
809 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
810 ntohl(cdir->d_did), cfrombstring(dir->d_fullpath));
811 if (dir_remove(vol, cdir) != 0) {
817 /* get_id needs adp for reading CNID from adouble file */
818 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
819 if ((ad_open_metadata(path->u_name, ADFLAGS_DIR, 0, &ad)) == 0) /* 1 */
823 if ((id = get_id(vol, adp, &path->st, dir->d_did, path->u_name, len)) == 0) { /* 2 */
829 ad_close_metadata(adp);
831 /* Get macname from unixname */
832 if (path->m_name == NULL) {
833 if ((path->m_name = utompath(vol, path->u_name, id, utf8_encoding())) == NULL) {
840 if ( ((fullpath = bstrcpy(dir->d_fullpath)) == NULL) /* 3 */
841 || (bconchar(fullpath, '/') != BSTR_OK)
842 || (bcatcstr(fullpath, path->u_name)) != BSTR_OK) {
843 LOG(log_error, logtype_afpd, "dir_add: fullpath: %s", strerror(errno) );
848 /* Allocate and initialize struct dir */
849 if ((cdir = dir_new( path->m_name, path->u_name, vol, dir->d_did, id, fullpath)) == NULL) { /* 3 */
854 if ((dircache_add(cdir)) != 0) { /* 4 */
855 LOG(log_error, logtype_afpd, "dir_add: fatal dircache error: %s", cfrombstring(fullpath));
861 LOG(log_debug, logtype_afpd, "dir_add('%s/%s'): error: %u",
862 cfrombstring(dir->d_u_name), path->u_name, err);
865 ad_close_metadata(adp);
866 if (!cdir && fullpath)
873 LOG(log_debug, logtype_afpd, "dir_add(did:%u,'%s/%s'): {cached: %u,'%s'}",
874 ntohl(dir->d_did), cfrombstring(dir->d_fullpath), path->u_name,
875 ntohl(cdir->d_did), cfrombstring(cdir->d_fullpath));
882 * @brief Remove a dir from a cache and free it and any ressources with it
884 * @param (r) pointer to struct vol
885 * @param (rw) pointer to struct dir
887 int dir_remove(const struct vol *vol, struct dir *dir)
892 if (dir->d_did == DIRDID_ROOT_PARENT || dir->d_did == DIRDID_ROOT)
896 if (movecwd(vol, vol->v_root) < 0) {
897 LOG(log_error, logtype_afpd, "dir_remove: can't chdir to : %s", vol->v_root);
902 LOG(log_debug, logtype_afpd, "dir_remove(did:%u,'%s'): {removing}",
903 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
905 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
912 * @brief Modify a struct dir, adust cache
914 * Any value that is 0 or NULL is not changed. If new_uname is NULL it is set to new_mname.
915 * If given new_uname == new_mname, new_uname will point to new_mname.
917 * @param vol (r) pointer to struct vol
918 * @param dir (rw) pointer to struct dir
919 * @param pdid (r) new parent DID
920 * @param did (r) new DID
921 * @param new_mname (r) new mac-name
922 * @param new_uname (r) new unix-name
924 int dir_modify(const struct vol *vol,
928 const char *new_mname,
929 const char *new_uname,
930 bstring pdir_fullpath)
934 /* Remove it from the cache */
935 dircache_remove(vol, dir, DIRCACHE | DIDNAME_INDEX | QUEUE_INDEX);
943 /* free uname if it's not the same as mname */
944 if (dir->d_m_name != dir->d_u_name)
945 bdestroy(dir->d_u_name);
947 if (new_uname == NULL)
948 new_uname = new_mname;
950 /* assign new name */
951 if ((bassigncstr(dir->d_m_name, new_mname)) != BSTR_OK) {
952 LOG(log_error, logtype_afpd, "dir_modify: bassigncstr: %s", strerror(errno) );
956 if (new_mname == new_uname || (strcmp(new_mname, new_uname) == 0)) {
957 dir->d_u_name = dir->d_m_name;
959 if ((dir->d_u_name = bfromcstr(new_uname)) == NULL) {
960 LOG(log_error, logtype_afpd, "renamedir: bassigncstr: %s", strerror(errno) );
967 if (bassign(dir->d_fullpath, pdir_fullpath) != BSTR_OK)
969 if (bcatcstr(dir->d_fullpath, "/") != BSTR_OK)
971 if (bcatcstr(dir->d_fullpath, new_uname) != BSTR_OK)
975 if (dir->d_m_name_ucs2)
976 free(dir->d_m_name_ucs2);
977 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))
978 dir->d_m_name_ucs2 = NULL;
980 /* Re-add it to the cache */
981 if ((dircache_add(dir)) != 0) {
990 * @brief Resolve a catalog node name path
992 * If it's a filename:
993 * 1. compute unix name
994 * 2. stat the file, storing struct stat or errno in struct path
995 * 3. cwd (and curdir) is filename parent directory
996 * 4. return path with with filename
999 * 5. search the dircache
1000 * 6. if not in the cache:
1001 * 7. compute unix name
1002 * 8. stat the dir, storing struct stat or errno in struct path
1004 * 10. if chdir failed, return path with dirname
1005 * cwd is dir parent directory
1006 * if chdir succeeded, add to dircache
1007 * return path with "" and "."
1009 * 11. else in the cache:
1010 * 12. stat the dir, storing struct stat or errno in struct path
1012 * 14. if chdir failed
1015 * if not last path part, return NULL
1020 curdir: dir parent directory
1025 struct path *cname(struct vol *vol, struct dir *dir, char **cpath)
1027 static char path[ MAXPATHLEN + 1];
1028 static struct path ret;
1038 LOG(log_debug, logtype_afpd, "came('%s'): {start}", cfrombstring(dir->d_fullpath));
1041 afp_errno = AFPERR_NOOBJ;
1042 memset(&ret, 0, sizeof(ret));
1044 switch (ret.m_type = *data) { /* path type */
1047 len = (unsigned char) *data++;
1049 if (afp_version >= 30) {
1055 if (afp_version >= 30) {
1057 memcpy(&hint, data, sizeof(hint));
1059 data += sizeof(hint);
1061 memcpy(&len16, data, sizeof(len16));
1067 /* else it's an error */
1069 afp_errno = AFPERR_PARAM;
1072 *cpath += len + size;
1077 if (movecwd(vol, dir) < 0 ) {
1078 LOG(log_debug, logtype_afpd, "cname(did:%u): failed to chdir to '%s'",
1079 ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1081 return path_from_dir(vol, dir, &ret);
1089 * 1. single 0 -> delimiter
1090 * 2. additional 0 -> chdir(..)
1092 * a) name is a file, build struct path from it etc., exit while loop
1093 * b) name is a dir, add it to the dircache, chdir to it, continue
1095 if (*data == 0) { /* case 1 or 2 */
1098 while (len > 0 && *data == 0) { /* case 2 */
1099 /* chdir to parrent dir */
1100 if ((dir = dirlookup(vol, dir->d_pdid)) == NULL)
1102 if (movecwd( vol, dir ) < 0 ) {
1103 dir_remove(vol, dir);
1112 /* case 3: copy name from packet buffer to ret.m_name and process it */
1113 for ( p = path; *data != 0 && len > 0; len-- ) {
1115 if (p > &path[ MAXPATHLEN]) {
1116 afp_errno = AFPERR_PARAM;
1120 *p = 0; /* Terminate string */
1123 /* Get u_name from m_name */
1124 if (cname_mtouname(vol, dir, &ret, toUTF8) != 0) {
1125 LOG(log_error, logtype_afpd, "cname('%s'): error from cname_mtouname", path);
1129 LOG(log_maxdebug, logtype_afpd, "came('%s'): {node: '%s}", cfrombstring(dir->d_fullpath), ret.u_name);
1131 /* Prevent access to our special folders like .AppleDouble */
1132 if (check_name(vol, ret.u_name)) {
1133 /* the name is illegal */
1134 LOG(log_info, logtype_afpd, "cname: illegal path: '%s'", ret.u_name);
1135 afp_errno = AFPERR_PARAM;
1139 if (dir->d_did == DIRDID_ROOT_PARENT) {
1141 * Special case: CNID 1
1142 * root parent (did 1) has one child: the volume. Requests for did=1 with
1143 * some <name> must check against the volume name.
1145 if ((strcmp(cfrombstring(vol->v_root->d_m_name), ret.m_name)) == 0)
1151 * CNID != 1, eg. most of the times we take this way.
1152 * Now check if current path-part is a file or dir:
1153 * o if it's dir we have to step into it
1154 * o if it's a file we expect it to be the last part of the requested path
1155 * and thus call continue which should terminate the while loop because
1158 if (of_stat(&ret) != 0) {
1160 * ret.u_name doesn't exist, might be afp_createfile|dir
1161 * that means it should have been the last part
1164 /* it wasn't the last part, so we have a bogus path request */
1165 afp_errno = AFPERR_NOOBJ;
1169 * this will terminate clean in while (1) because len == 0,
1170 * probably afp_createfile|dir
1172 LOG(log_maxdebug, logtype_afpd, "came('%s'): {leave-cnode ENOENT (possile create request): '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1176 switch (ret.st.st_mode & S_IFMT) {
1178 LOG(log_debug, logtype_afpd, "came('%s'): {file: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1180 /* it wasn't the last part, so we have a bogus path request */
1181 afp_errno = AFPERR_PARAM;
1184 continue; /* continues while loop */
1186 LOG(log_debug, logtype_afpd, "came('%s'): {link: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1188 LOG(log_warning, logtype_afpd, "came('%s'): {symlinked dir: '%s'}", cfrombstring(dir->d_fullpath), ret.u_name);
1189 afp_errno = AFPERR_PARAM;
1192 continue; /* continues while loop */
1196 LOG(log_info, logtype_afpd, "cname: special file: '%s'", ret.u_name);
1197 afp_errno = AFPERR_NODIR;
1201 /* Search the cache */
1202 int unamelen = strlen(ret.u_name);
1203 cdir = dircache_search_by_name(vol, dir->d_did, ret.u_name, unamelen);
1205 /* Not in cache, create one */
1206 if ((cdir = dir_add(vol, dir, &ret, unamelen)) == NULL) {
1207 LOG(log_error, logtype_afpd, "cname(did:%u, name:'%s', cwd:'%s'): failed to add dir",
1208 ntohl(dir->d_did), ret.u_name, getcwdpath());
1212 } /* if/else cnid==1 */
1214 /* Now chdir to the evaluated dir */
1215 if (movecwd( vol, cdir ) < 0 ) {
1216 LOG(log_debug, logtype_afpd, "cname(cwd:'%s'): failed to chdir to new subdir '%s': %s",
1217 cfrombstring(curdir->d_fullpath), cfrombstring(cdir->d_fullpath), strerror(errno));
1219 return path_from_dir(vol, cdir, &ret);
1224 ret.m_name[0] = 0; /* so we later know last token was a dir */
1227 if (curdir->d_did == DIRDID_ROOT_PARENT) {
1228 afp_errno = AFPERR_DID1;
1232 if (ret.m_name[0] == 0) {
1233 /* Last part was a dir */
1234 ret.u_name = mtoupath(vol, ret.m_name, 0, 1); /* Force "." into a useable static buffer */
1238 LOG(log_debug, logtype_afpd, "came('%s') {end: curdir:'%s', path:'%s'}",
1239 cfrombstring(dir->d_fullpath),
1240 cfrombstring(curdir->d_fullpath),
1247 * @brief chdir() to dir
1249 * @param vol (r) pointer to struct vol
1250 * @param dir (r) pointer to struct dir
1252 * @returns 0 on success, -1 on error with afp_errno set appropiately
1254 int movecwd(const struct vol *vol, struct dir *dir)
1261 LOG(log_maxdebug, logtype_afpd, "movecwd(curdir:'%s', cwd:'%s')",
1262 cfrombstring(curdir->d_fullpath), getcwdpath());
1266 if (dir->d_did == DIRDID_ROOT_PARENT) {
1267 curdir = &rootParent;
1271 LOG(log_debug, logtype_afpd, "movecwd(did:%u, '%s')", ntohl(dir->d_did), cfrombstring(dir->d_fullpath));
1273 if ( chdir(cfrombstring(dir->d_fullpath)) < 0 ) {
1274 LOG(log_debug, logtype_afpd, "movecwd('%s'): %s", cfrombstring(dir->d_fullpath), strerror(errno));
1278 afp_errno = AFPERR_ACCESS;
1281 afp_errno = AFPERR_NOOBJ;
1291 * We can't use unix file's perm to support Apple's inherited protection modes.
1292 * If we aren't the file's owner we can't change its perms when moving it and smb
1293 * nfs,... don't even try.
1295 #define AFP_CHECK_ACCESS
1297 int check_access(char *path, int mode)
1299 #ifdef AFP_CHECK_ACCESS
1307 accessmode(p, &ma, curdir, NULL);
1308 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1310 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1316 /* --------------------- */
1317 int file_access(struct path *path, int mode)
1321 accessmode(path->u_name, &ma, curdir, &path->st);
1322 if ((mode & OPENACC_WR) && !(ma.ma_user & AR_UWRITE))
1324 if ((mode & OPENACC_RD) && !(ma.ma_user & AR_UREAD))
1330 /* --------------------- */
1331 void setdiroffcnt(struct dir *dir, struct stat *st, u_int32_t count)
1333 dir->offcnt = count;
1334 dir->ctime = st->st_ctime;
1335 dir->d_flags &= ~DIRF_CNID;
1339 /* ---------------------
1340 * is our cached also for reenumerate id?
1342 int dirreenumerate(struct dir *dir, struct stat *st)
1344 return st->st_ctime == dir->ctime && (dir->d_flags & DIRF_CNID);
1347 /* ------------------------------
1349 (name, dir) with curdir:name == dir, from afp_enumerate
1352 int getdirparams(const struct vol *vol,
1353 u_int16_t bitmap, struct path *s_path,
1355 char *buf, size_t *buflen )
1359 char *data, *l_nameoff = NULL, *utf_nameoff = NULL;
1360 int bit = 0, isad = 0;
1366 struct stat *st = &s_path->st;
1367 char *upath = s_path->u_name;
1369 if ((bitmap & ((1 << DIRPBIT_ATTR) |
1370 (1 << DIRPBIT_CDATE) |
1371 (1 << DIRPBIT_MDATE) |
1372 (1 << DIRPBIT_BDATE) |
1373 (1 << DIRPBIT_FINFO)))) {
1375 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1376 if ( !ad_metadata( upath, ADFLAGS_CREATE|ADFLAGS_DIR, &ad) ) {
1378 if (ad.ad_md->adf_flags & O_CREAT) {
1379 /* We just created it */
1380 ad_setname(&ad, s_path->m_name);
1395 while ( bitmap != 0 ) {
1396 while (( bitmap & 1 ) == 0 ) {
1404 ad_getattr(&ad, &ashort);
1405 } else if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1406 ashort = htons(ATTRBIT_INVISIBLE);
1409 ashort |= htons(ATTRBIT_SHARED);
1410 memcpy( data, &ashort, sizeof( ashort ));
1411 data += sizeof( ashort );
1415 memcpy( data, &pdid, sizeof( pdid ));
1416 data += sizeof( pdid );
1419 case DIRPBIT_CDATE :
1420 if (!isad || (ad_getdate(&ad, AD_DATE_CREATE, &aint) < 0))
1421 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1422 memcpy( data, &aint, sizeof( aint ));
1423 data += sizeof( aint );
1426 case DIRPBIT_MDATE :
1427 aint = AD_DATE_FROM_UNIX(st->st_mtime);
1428 memcpy( data, &aint, sizeof( aint ));
1429 data += sizeof( aint );
1432 case DIRPBIT_BDATE :
1433 if (!isad || (ad_getdate(&ad, AD_DATE_BACKUP, &aint) < 0))
1434 aint = AD_DATE_START;
1435 memcpy( data, &aint, sizeof( aint ));
1436 data += sizeof( aint );
1439 case DIRPBIT_FINFO :
1441 memcpy( data, ad_entry( &ad, ADEID_FINDERI ), 32 );
1442 } else { /* no appledouble */
1443 memset( data, 0, 32 );
1444 /* set default view -- this also gets done in ad_open() */
1445 ashort = htons(FINDERINFO_CLOSEDVIEW);
1446 memcpy(data + FINDERINFO_FRVIEWOFF, &ashort, sizeof(ashort));
1448 /* dot files are by default visible */
1449 if (invisible_dots(vol, cfrombstring(dir->d_u_name))) {
1450 ashort = htons(FINDERINFO_INVISIBLE);
1451 memcpy(data + FINDERINFO_FRFLAGOFF, &ashort, sizeof(ashort));
1457 case DIRPBIT_LNAME :
1458 if (dir->d_m_name) /* root of parent can have a null name */
1461 memset(data, 0, sizeof(u_int16_t));
1462 data += sizeof( u_int16_t );
1465 case DIRPBIT_SNAME :
1466 memset(data, 0, sizeof(u_int16_t));
1467 data += sizeof( u_int16_t );
1471 memcpy( data, &dir->d_did, sizeof( aint ));
1472 data += sizeof( aint );
1475 case DIRPBIT_OFFCNT :
1477 /* this needs to handle current directory access rights */
1478 if (diroffcnt(dir, st)) {
1479 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1481 else if ((ret = for_each_dirent(vol, upath, NULL,NULL)) >= 0) {
1482 setdiroffcnt(dir, st, ret);
1483 ashort = (dir->offcnt > 0xffff)?0xffff:dir->offcnt;
1485 ashort = htons( ashort );
1486 memcpy( data, &ashort, sizeof( ashort ));
1487 data += sizeof( ashort );
1491 aint = htonl(st->st_uid);
1492 memcpy( data, &aint, sizeof( aint ));
1493 data += sizeof( aint );
1497 aint = htonl(st->st_gid);
1498 memcpy( data, &aint, sizeof( aint ));
1499 data += sizeof( aint );
1502 case DIRPBIT_ACCESS :
1503 accessmode( upath, &ma, dir , st);
1505 *data++ = ma.ma_user;
1506 *data++ = ma.ma_world;
1507 *data++ = ma.ma_group;
1508 *data++ = ma.ma_owner;
1511 /* Client has requested the ProDOS information block.
1512 Just pass back the same basic block for all
1513 directories. <shirsch@ibm.net> */
1514 case DIRPBIT_PDINFO :
1515 if (afp_version >= 30) { /* UTF8 name */
1516 utf8 = kTextEncodingUTF8;
1517 if (dir->d_m_name) /* root of parent can have a null name */
1520 memset(data, 0, sizeof(u_int16_t));
1521 data += sizeof( u_int16_t );
1523 memcpy(data, &aint, sizeof( aint ));
1524 data += sizeof( aint );
1526 else { /* ProDOS Info Block */
1529 ashort = htons( 0x0200 );
1530 memcpy( data, &ashort, sizeof( ashort ));
1531 data += sizeof( ashort );
1532 memset( data, 0, sizeof( ashort ));
1533 data += sizeof( ashort );
1537 case DIRPBIT_UNIXPR :
1538 aint = htonl(st->st_uid);
1539 memcpy( data, &aint, sizeof( aint ));
1540 data += sizeof( aint );
1541 aint = htonl(st->st_gid);
1542 memcpy( data, &aint, sizeof( aint ));
1543 data += sizeof( aint );
1546 aint = htonl ( aint & ~S_ISGID ); /* Remove SGID, OSX doesn't like it ... */
1547 memcpy( data, &aint, sizeof( aint ));
1548 data += sizeof( aint );
1550 accessmode( upath, &ma, dir , st);
1552 *data++ = ma.ma_user;
1553 *data++ = ma.ma_world;
1554 *data++ = ma.ma_group;
1555 *data++ = ma.ma_owner;
1560 ad_close_metadata( &ad );
1562 return( AFPERR_BITMAP );
1568 ashort = htons( data - buf );
1569 memcpy( l_nameoff, &ashort, sizeof( ashort ));
1570 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, 0);
1572 if ( utf_nameoff ) {
1573 ashort = htons( data - buf );
1574 memcpy( utf_nameoff, &ashort, sizeof( ashort ));
1575 data = set_name(vol, data, pdid, cfrombstring(dir->d_m_name), dir->d_did, utf8);
1578 ad_close_metadata( &ad );
1580 *buflen = data - buf;
1584 /* ----------------------------- */
1585 int path_error(struct path *path, int error)
1587 /* - a dir with access error
1588 * - no error it's a file
1591 if (path_isadir(path))
1593 if (path->st_valid && path->st_errno)
1595 return AFPERR_BADTYPE ;
1598 /* ----------------------------- */
1599 int afp_setdirparams(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
1604 u_int16_t vid, bitmap;
1610 memcpy( &vid, ibuf, sizeof( vid ));
1611 ibuf += sizeof( vid );
1613 if (NULL == ( vol = getvolbyvid( vid )) ) {
1614 return( AFPERR_PARAM );
1617 if (vol->v_flags & AFPVOL_RO)
1618 return AFPERR_VLOCK;
1620 memcpy( &did, ibuf, sizeof( did ));
1621 ibuf += sizeof( int );
1623 if (NULL == ( dir = dirlookup( vol, did )) ) {
1627 memcpy( &bitmap, ibuf, sizeof( bitmap ));
1628 bitmap = ntohs( bitmap );
1629 ibuf += sizeof( bitmap );
1631 if (NULL == ( path = cname( vol, dir, &ibuf )) ) {
1632 return get_afp_errno(AFPERR_NOOBJ);
1635 if ( *path->m_name != '\0' ) {
1636 rc = path_error(path, AFPERR_NOOBJ);
1637 /* maybe we are trying to set perms back */
1638 if (rc != AFPERR_ACCESS)
1643 * If ibuf is odd, make it even.
1645 if ((u_long)ibuf & 1 ) {
1649 if (AFP_OK == ( rc = setdirparams(vol, path, bitmap, ibuf )) ) {
1650 setvoltime(obj, vol );
1656 * cf AFP3.0.pdf page 244 for change_mdate and change_parent_mdate logic
1658 * assume path == '\0' eg. it's a directory in canonical form
1660 int setdirparams(struct vol *vol, struct path *path, u_int16_t d_bitmap, char *buf )
1672 u_int16_t ashort, bshort, oshort;
1674 int change_mdate = 0;
1675 int change_parent_mdate = 0;
1677 u_int16_t bitmap = d_bitmap;
1678 u_char finder_buf[32];
1681 u_int16_t upriv_bit = 0;
1684 upath = path->u_name;
1686 while ( bitmap != 0 ) {
1687 while (( bitmap & 1 ) == 0 ) {
1695 memcpy( &ashort, buf, sizeof( ashort ));
1696 buf += sizeof( ashort );
1698 case DIRPBIT_CDATE :
1700 memcpy(&cdate, buf, sizeof(cdate));
1701 buf += sizeof( cdate );
1703 case DIRPBIT_MDATE :
1704 memcpy(&newdate, buf, sizeof(newdate));
1705 buf += sizeof( newdate );
1707 case DIRPBIT_BDATE :
1709 memcpy(&bdate, buf, sizeof(bdate));
1710 buf += sizeof( bdate );
1712 case DIRPBIT_FINFO :
1714 memcpy( finder_buf, buf, 32 );
1717 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1718 change_parent_mdate = 1;
1719 memcpy( &owner, buf, sizeof(owner));
1720 buf += sizeof( owner );
1723 change_parent_mdate = 1;
1724 memcpy( &group, buf, sizeof( group ));
1725 buf += sizeof( group );
1727 case DIRPBIT_ACCESS :
1729 change_parent_mdate = 1;
1730 ma.ma_user = *buf++;
1731 ma.ma_world = *buf++;
1732 ma.ma_group = *buf++;
1733 ma.ma_owner = *buf++;
1734 mpriv = mtoumode( &ma ) | vol->v_dperm;
1735 if (dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1736 err = set_dir_errors(path, "setdirmode", errno);
1740 /* Ignore what the client thinks we should do to the
1741 ProDOS information block. Skip over the data and
1742 report nothing amiss. <shirsch@ibm.net> */
1743 case DIRPBIT_PDINFO :
1744 if (afp_version < 30) {
1748 err = AFPERR_BITMAP;
1752 case DIRPBIT_UNIXPR :
1753 if (vol_unix_priv(vol)) {
1754 memcpy( &owner, buf, sizeof(owner)); /* FIXME need to change owner too? */
1755 buf += sizeof( owner );
1756 memcpy( &group, buf, sizeof( group ));
1757 buf += sizeof( group );
1760 change_parent_mdate = 1;
1761 memcpy( &upriv, buf, sizeof( upriv ));
1762 buf += sizeof( upriv );
1763 upriv = ntohl (upriv) | vol->v_dperm;
1764 if (dir_rx_set(upriv)) {
1765 /* maybe we are trying to set perms back */
1766 if ( setdirunixmode(vol, upath, upriv) < 0 ) {
1768 err = set_dir_errors(path, "setdirunixmode", errno);
1779 err = AFPERR_BITMAP;
1787 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
1789 if (ad_open_metadata( upath, ADFLAGS_DIR, O_CREAT, &ad) < 0) {
1791 * Check to see what we're trying to set. If it's anything
1792 * but ACCESS, UID, or GID, give an error. If it's any of those
1793 * three, we don't need the ad to be open, so just continue.
1795 * note: we also don't need to worry about mdate. also, be quiet
1796 * if we're using the noadouble option.
1798 if (!vol_noadouble(vol) && (d_bitmap &
1799 ~((1<<DIRPBIT_ACCESS)|(1<<DIRPBIT_UNIXPR)|
1800 (1<<DIRPBIT_UID)|(1<<DIRPBIT_GID)|
1801 (1<<DIRPBIT_MDATE)|(1<<DIRPBIT_PDINFO)))) {
1802 return AFPERR_ACCESS;
1808 * Check to see if a create was necessary. If it was, we'll want
1809 * to set our name, etc.
1811 if ( (ad_get_HF_flags( &ad ) & O_CREAT)) {
1812 ad_setname(&ad, cfrombstring(curdir->d_m_name));
1818 while ( bitmap != 0 ) {
1819 while (( bitmap & 1 ) == 0 ) {
1827 ad_getattr(&ad, &bshort);
1829 if ( ntohs( ashort ) & ATTRBIT_SETCLR ) {
1830 bshort |= htons( ntohs( ashort ) & ~ATTRBIT_SETCLR );
1834 if ((bshort & htons(ATTRBIT_INVISIBLE)) != (oshort & htons(ATTRBIT_INVISIBLE)))
1835 change_parent_mdate = 1;
1836 ad_setattr(&ad, bshort);
1839 case DIRPBIT_CDATE :
1841 ad_setdate(&ad, AD_DATE_CREATE, cdate);
1844 case DIRPBIT_MDATE :
1846 case DIRPBIT_BDATE :
1848 ad_setdate(&ad, AD_DATE_BACKUP, bdate);
1851 case DIRPBIT_FINFO :
1853 /* Fixes #2802236 */
1854 u_int16_t *fflags = (u_int16_t *)(finder_buf + FINDERINFO_FRFLAGOFF);
1855 *fflags &= htons(~FINDERINFO_ISHARED);
1857 if ( dir->d_did == DIRDID_ROOT ) {
1859 * Alright, we admit it, this is *really* sick!
1860 * The 4 bytes that we don't copy, when we're dealing
1861 * with the root of a volume, are the directory's
1862 * location information. This eliminates that annoying
1863 * behavior one sees when mounting above another mount
1866 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 10 );
1867 memcpy( ad_entry( &ad, ADEID_FINDERI ) + 14, finder_buf + 14, 18 );
1869 memcpy( ad_entry( &ad, ADEID_FINDERI ), finder_buf, 32 );
1873 case DIRPBIT_UID : /* What kind of loser mounts as root? */
1874 if ( (dir->d_did == DIRDID_ROOT) &&
1875 (setdeskowner( ntohl(owner), -1 ) < 0)) {
1876 err = set_dir_errors(path, "setdeskowner", errno);
1877 if (isad && err == AFPERR_PARAM) {
1878 err = AFP_OK; /* ???*/
1881 goto setdirparam_done;
1884 if ( setdirowner(vol, upath, ntohl(owner), -1 ) < 0 ) {
1885 err = set_dir_errors(path, "setdirowner", errno);
1886 goto setdirparam_done;
1890 if (dir->d_did == DIRDID_ROOT)
1891 setdeskowner( -1, ntohl(group) );
1892 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1893 err = set_dir_errors(path, "setdirowner", errno);
1894 goto setdirparam_done;
1897 case DIRPBIT_ACCESS :
1898 if (dir->d_did == DIRDID_ROOT) {
1900 if (!dir_rx_set(mpriv)) {
1901 /* we can't remove read and search for owner on volume root */
1902 err = AFPERR_ACCESS;
1903 goto setdirparam_done;
1907 if (!dir_rx_set(mpriv) && setdirmode( vol, upath, mpriv) < 0 ) {
1908 err = set_dir_errors(path, "setdirmode", errno);
1909 goto setdirparam_done;
1912 case DIRPBIT_PDINFO :
1913 if (afp_version >= 30) {
1914 err = AFPERR_BITMAP;
1915 goto setdirparam_done;
1918 case DIRPBIT_UNIXPR :
1919 if (vol_unix_priv(vol)) {
1920 if (dir->d_did == DIRDID_ROOT) {
1921 if (!dir_rx_set(upriv)) {
1922 /* we can't remove read and search for owner on volume root */
1923 err = AFPERR_ACCESS;
1924 goto setdirparam_done;
1926 setdeskowner( -1, ntohl(group) );
1927 setdeskmode( upriv );
1929 if ( setdirowner(vol, upath, -1, ntohl(group) ) < 0 ) {
1930 err = set_dir_errors(path, "setdirowner", errno);
1931 goto setdirparam_done;
1934 if ( upriv_bit && setdirunixmode(vol, upath, upriv) < 0 ) {
1935 err = set_dir_errors(path, "setdirunixmode", errno);
1936 goto setdirparam_done;
1940 err = AFPERR_BITMAP;
1941 goto setdirparam_done;
1945 err = AFPERR_BITMAP;
1946 goto setdirparam_done;
1955 if (change_mdate && newdate == 0 && gettimeofday(&tv, NULL) == 0) {
1956 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1960 ad_setdate(&ad, AD_DATE_MODIFY, newdate);
1961 ut.actime = ut.modtime = AD_DATE_TO_UNIX(newdate);
1966 if (path->st_valid && !path->st_errno) {
1967 struct stat *st = &path->st;
1969 if (dir && dir->d_pdid) {
1970 ad_setid(&ad, st->st_dev, st->st_ino, dir->d_did, dir->d_pdid, vol->v_stamp);
1974 ad_close_metadata( &ad);
1978 if (change_parent_mdate && dir->d_did != DIRDID_ROOT
1979 && gettimeofday(&tv, NULL) == 0) {
1980 if (!movecwd(vol, dir->d_parent)) {
1981 newdate = AD_DATE_FROM_UNIX(tv.tv_sec);
1982 /* be careful with bitmap because now dir is null */
1983 bitmap = 1<<DIRPBIT_MDATE;
1984 setdirparams(vol, &Cur_Path, bitmap, (char *)&newdate);
1985 /* should we reset curdir ?*/
1992 int afp_syncdir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2006 memcpy( &vid, ibuf, sizeof( vid ));
2007 ibuf += sizeof( vid );
2008 if (NULL == (vol = getvolbyvid( vid )) ) {
2009 return( AFPERR_PARAM );
2012 memcpy( &did, ibuf, sizeof( did ));
2013 ibuf += sizeof( did );
2017 * if it's CNID 2 our only choice to meet the specs is call sync.
2018 * For any other CNID just sync that dir. To my knowledge the
2019 * intended use of FPSyncDir is to sync the volume so all we're
2020 * ever going to see here is probably CNID 2. Anyway, we' prepared.
2023 if ( ntohl(did) == 2 ) {
2026 if (NULL == ( dir = dirlookup( vol, did )) ) {
2027 return afp_errno; /* was AFPERR_NOOBJ */
2030 if (movecwd( vol, dir ) < 0 )
2031 return ( AFPERR_NOOBJ );
2034 * Assuming only OSens that have dirfd also may require fsyncing directories
2035 * in order to flush metadata e.g. Linux.
2039 if (NULL == ( dp = opendir( "." )) ) {
2042 return( AFPERR_NOOBJ );
2044 return( AFPERR_ACCESS );
2046 return( AFPERR_PARAM );
2050 LOG(log_debug, logtype_afpd, "afp_syncdir: dir: '%s'", dir->d_u_name);
2053 if ( fsync ( dfd ) < 0 )
2054 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2055 dir->d_u_name, strerror(errno) );
2056 closedir(dp); /* closes dfd too */
2059 if ( -1 == (dfd = open(vol->ad_path(".", ADFLAGS_DIR), O_RDWR))) {
2062 return( AFPERR_NOOBJ );
2064 return( AFPERR_ACCESS );
2066 return( AFPERR_PARAM );
2070 LOG(log_debug, logtype_afpd, "afp_syncdir: ad-file: '%s'",
2071 vol->ad_path(".", ADFLAGS_DIR) );
2073 if ( fsync(dfd) < 0 )
2074 LOG(log_error, logtype_afpd, "afp_syncdir(%s): %s",
2075 vol->ad_path(cfrombstring(dir->d_u_name), ADFLAGS_DIR), strerror(errno) );
2082 int afp_createdir(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2088 struct path *s_path;
2096 memcpy( &vid, ibuf, sizeof( vid ));
2097 ibuf += sizeof( vid );
2098 if (NULL == ( vol = getvolbyvid( vid )) ) {
2099 return( AFPERR_PARAM );
2102 if (vol->v_flags & AFPVOL_RO)
2103 return AFPERR_VLOCK;
2105 memcpy( &did, ibuf, sizeof( did ));
2106 ibuf += sizeof( did );
2107 if (NULL == ( dir = dirlookup( vol, did )) ) {
2108 return afp_errno; /* was AFPERR_NOOBJ */
2110 /* for concurrent access we need to be sure we are not in the
2111 * folder we want to create...
2115 if (NULL == ( s_path = cname( vol, dir, &ibuf )) ) {
2116 return get_afp_errno(AFPERR_PARAM);
2118 /* cname was able to move curdir to it! */
2119 if (*s_path->m_name == '\0')
2120 return AFPERR_EXIST;
2122 upath = s_path->u_name;
2124 if (AFP_OK != (err = netatalk_mkdir( upath))) {
2128 if (of_stat(s_path) < 0) {
2134 if ((dir = dir_add(vol, curdir, s_path, strlen(s_path->u_name))) == NULL) {
2138 if ( movecwd( vol, dir ) < 0 ) {
2139 return( AFPERR_PARAM );
2142 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2143 if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad ) < 0) {
2144 if (vol_noadouble(vol))
2145 goto createdir_done;
2146 return( AFPERR_ACCESS );
2148 ad_setname(&ad, s_path->m_name);
2149 ad_setid( &ad, s_path->st.st_dev, s_path->st.st_ino, dir->d_did, did, vol->v_stamp);
2152 ad_close_metadata( &ad);
2155 #ifdef HAVE_NFSv4_ACLS
2156 /* FIXME: are we really inside the created dir? */
2157 addir_inherit_acl(vol);
2160 memcpy( rbuf, &dir->d_did, sizeof( u_int32_t ));
2161 *rbuflen = sizeof( u_int32_t );
2162 setvoltime(obj, vol );
2167 * dst new unix filename (not a pathname)
2168 * newname new mac name
2171 int renamedir(const struct vol *vol, char *src, char *dst,
2173 struct dir *newparent,
2179 /* existence check moved to afp_moveandrename */
2180 if ( unix_rename( src, dst ) < 0 ) {
2183 return( AFPERR_NOOBJ );
2185 return( AFPERR_ACCESS );
2187 return AFPERR_VLOCK;
2189 /* tried to move directory into a subdirectory of itself */
2190 return AFPERR_CANTMOVE;
2192 /* this needs to copy and delete. bleah. that means we have
2193 * to deal with entire directory hierarchies. */
2194 if ((err = copydir(vol, src, dst)) < 0) {
2198 if ((err = deletedir(src)) < 0)
2202 return( AFPERR_PARAM );
2206 vol->vfs->vfs_renamedir(vol, src, dst);
2208 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2210 if (!ad_open_metadata( dst, ADFLAGS_DIR, 0, &ad)) {
2211 ad_setname(&ad, newname);
2213 ad_close_metadata( &ad);
2216 if (dir_modify(vol, dir, curdir->d_did, 0, newname, dst, curdir->d_fullpath) != 0) {
2217 LOG(log_error, logtype_afpd, "renamedir: fatal error from dir_modify: %s -> %s", src, dst);
2224 /* delete an empty directory */
2225 int deletecurdir(struct vol *vol)
2235 if ( dirlookup(vol, curdir->d_pdid) == NULL ) {
2236 return( AFPERR_ACCESS );
2241 ad_init(&ad, vol->v_adouble, vol->v_ad_options);
2242 /* we never want to create a resource fork here, we are going to delete it */
2243 if ( ad_metadata( ".", ADFLAGS_DIR, &ad) == 0 ) {
2245 ad_getattr(&ad, &ashort);
2246 ad_close( &ad, ADFLAGS_HF );
2247 if ((ashort & htons(ATTRBIT_NODELETE))) {
2248 return AFPERR_OLOCK;
2251 err = vol->vfs->vfs_deletecurdir(vol);
2256 /* now get rid of dangling symlinks */
2257 if ((dp = opendir("."))) {
2258 while ((de = readdir(dp))) {
2259 /* skip this and previous directory */
2260 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
2263 /* bail if it's not a symlink */
2264 if ((lstat(de->d_name, &st) == 0) && !S_ISLNK(st.st_mode)) {
2266 return AFPERR_DIRNEMPT;
2269 if ((err = netatalk_unlink(de->d_name))) {
2276 if ( movecwd(vol, dirlookup(vol, curdir->d_pdid)) < 0 ) {
2281 err = netatalk_rmdir_all_errors(cfrombstring(fdir->d_u_name));
2282 if ( err == AFP_OK || err == AFPERR_NOOBJ) {
2283 cnid_delete(vol->v_cdb, fdir->d_did);
2284 dir_remove( vol, fdir );
2288 /* inode is used as key for cnid.
2289 * Close the descriptor only after cnid_delete
2297 int afp_mapid(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2307 sfunc = (unsigned char) *ibuf++;
2311 if (sfunc >= 3 && sfunc <= 6) {
2312 if (afp_version < 30) {
2313 return( AFPERR_PARAM );
2320 case 3 :/* unicode */
2321 memcpy( &id, ibuf, sizeof( id ));
2324 if (( pw = getpwuid( id )) == NULL ) {
2325 return( AFPERR_NOITEM );
2327 len = convert_string_allocate( obj->options.unixcharset, ((!utf8)?obj->options.maccharset:CH_UTF8_MAC),
2328 pw->pw_name, -1, &name);
2335 case 4 : /* unicode */
2336 memcpy( &id, ibuf, sizeof( id ));
2339 if (NULL == ( gr = (struct group *)getgrgid( id ))) {
2340 return( AFPERR_NOITEM );
2342 len = convert_string_allocate( obj->options.unixcharset, (!utf8)?obj->options.maccharset:CH_UTF8_MAC,
2343 gr->gr_name, -1, &name);
2349 #ifdef HAVE_NFSv4_ACLS
2350 case 5 : /* UUID -> username */
2351 case 6 : /* UUID -> groupname */
2352 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2353 return AFPERR_PARAM;
2354 LOG(log_debug, logtype_afpd, "afp_mapid: valid UUID request");
2356 len = getnamefromuuid( ibuf, &name, &type);
2357 if (len != 0) /* its a error code, not len */
2358 return AFPERR_NOITEM;
2359 if (type == UUID_USER) {
2360 if (( pw = getpwnam( name )) == NULL )
2361 return( AFPERR_NOITEM );
2362 LOG(log_debug, logtype_afpd, "afp_mapid: name:%s -> uid:%d", name, pw->pw_uid);
2363 id = htonl(UUID_USER);
2364 memcpy( rbuf, &id, sizeof( id ));
2365 id = htonl( pw->pw_uid);
2366 rbuf += sizeof( id );
2367 memcpy( rbuf, &id, sizeof( id ));
2368 rbuf += sizeof( id );
2369 *rbuflen = 2 * sizeof( id );
2370 } else { /* type == UUID_GROUP */
2371 if (( gr = getgrnam( name )) == NULL )
2372 return( AFPERR_NOITEM );
2373 LOG(log_debug, logtype_afpd, "afp_mapid: group:%s -> gid:%d", name, gr->gr_gid);
2374 id = htonl(UUID_GROUP);
2375 memcpy( rbuf, &id, sizeof( id ));
2376 rbuf += sizeof( id );
2377 id = htonl( gr->gr_gid);
2378 memcpy( rbuf, &id, sizeof( id ));
2379 rbuf += sizeof( id );
2380 *rbuflen = 2 * sizeof( id );
2385 return( AFPERR_PARAM );
2389 len = strlen( name );
2392 u_int16_t tp = htons(len);
2393 memcpy(rbuf, &tp, sizeof(tp));
2402 memcpy( rbuf, name, len );
2410 int afp_mapname(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2419 sfunc = (unsigned char) *ibuf++;
2421 LOG(log_debug, logtype_afpd, "afp_mapname: sfunc: %d, afp_version: %d", sfunc, afp_version);
2424 case 2 : /* unicode */
2425 if (afp_version < 30) {
2426 return( AFPERR_PARAM );
2428 memcpy(&ulen, ibuf, sizeof(ulen));
2431 LOG(log_debug, logtype_afpd, "afp_mapname: alive");
2435 len = (unsigned char) *ibuf++;
2437 #ifdef HAVE_NFSv4_ACLS
2438 case 5 : /* username -> UUID */
2439 case 6 : /* groupname -> UUID */
2440 if ((afp_version < 32) || !(obj->options.flags & OPTION_UUID ))
2441 return AFPERR_PARAM;
2442 memcpy(&ulen, ibuf, sizeof(ulen));
2448 return( AFPERR_PARAM );
2454 return AFPERR_PARAM;
2457 case 1 : /* unicode */
2459 if (NULL == ( pw = (struct passwd *)getpwnam( ibuf )) ) {
2460 return( AFPERR_NOITEM );
2464 memcpy( rbuf, &id, sizeof( id ));
2465 *rbuflen = sizeof( id );
2468 case 2 : /* unicode */
2470 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s",ibuf);
2471 if (NULL == ( gr = (struct group *)getgrnam( ibuf ))) {
2472 return( AFPERR_NOITEM );
2475 LOG(log_debug, logtype_afpd, "afp_mapname: gettgrnam for name: %s -> id: %d",ibuf, id);
2477 memcpy( rbuf, &id, sizeof( id ));
2478 *rbuflen = sizeof( id );
2480 #ifdef HAVE_NFSv4_ACLS
2481 case 5 : /* username -> UUID */
2482 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2483 if (0 != getuuidfromname(ibuf, UUID_USER, rbuf))
2484 return AFPERR_NOITEM;
2485 *rbuflen = UUID_BINSIZE;
2487 case 6 : /* groupname -> UUID */
2488 LOG(log_debug, logtype_afpd, "afp_mapname: name: %s",ibuf);
2489 if (0 != getuuidfromname(ibuf, UUID_GROUP, rbuf))
2490 return AFPERR_NOITEM;
2491 *rbuflen = UUID_BINSIZE;
2499 /* ------------------------------------
2500 variable DID support
2502 int afp_closedir(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
2513 /* do nothing as dids are static for the life of the process. */
2517 memcpy(&vid, ibuf, sizeof( vid ));
2518 ibuf += sizeof( vid );
2519 if (( vol = getvolbyvid( vid )) == NULL ) {
2520 return( AFPERR_PARAM );
2523 memcpy( &did, ibuf, sizeof( did ));
2524 ibuf += sizeof( did );
2525 if (( dir = dirlookup( vol, did )) == NULL ) {
2526 return( AFPERR_PARAM );
2529 /* dir_remove -- deletedid */
2535 /* did creation gets done automatically
2536 * there's a pb again with case but move it to cname
2538 int afp_opendir(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
2541 struct dir *parentdir;
2549 memcpy(&vid, ibuf, sizeof(vid));
2550 ibuf += sizeof( vid );
2552 if (NULL == ( vol = getvolbyvid( vid )) ) {
2553 return( AFPERR_PARAM );
2556 memcpy(&did, ibuf, sizeof(did));
2557 ibuf += sizeof(did);
2559 if (NULL == ( parentdir = dirlookup( vol, did )) ) {
2563 if (NULL == ( path = cname( vol, parentdir, &ibuf )) ) {
2564 return get_afp_errno(AFPERR_PARAM);
2567 if ( *path->m_name != '\0' ) {
2568 return path_error(path, AFPERR_NOOBJ);
2571 if ( !path->st_valid && of_stat(path ) < 0 ) {
2572 return( AFPERR_NOOBJ );
2574 if ( path->st_errno ) {
2575 return( AFPERR_NOOBJ );
2578 memcpy(rbuf, &curdir->d_did, sizeof(curdir->d_did));
2579 *rbuflen = sizeof(curdir->d_did);