From: Frank Lahm Date: Thu, 20 Sep 2012 12:13:31 +0000 (+0200) Subject: Move function cnid_for_path to CNID utility libatalk file X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=netatalk.git;a=commitdiff_plain;h=1e4f4418dc46346bc8a040134c4f9eeffa4898cd Move function cnid_for_path to CNID utility libatalk file --- diff --git a/bin/ad/ad.h b/bin/ad/ad.h index 3195641f..9155900c 100644 --- a/bin/ad/ad.h +++ b/bin/ad/ad.h @@ -69,7 +69,6 @@ extern int ad_find(int argc, char **argv, AFPObj *obj); /* ad_util.c */ extern int openvol(AFPObj *obj, const char *path, afpvol_t *vol); extern void closevol(afpvol_t *vol); -extern cnid_t cnid_for_path(const afpvol_t *vol, const char *path, cnid_t *did); extern cnid_t cnid_for_paths_parent(const afpvol_t *vol, const char *path, cnid_t *did); extern char *utompath(const struct vol *, const char *); extern int convert_dots_encoding(const afpvol_t *svol, const afpvol_t *dvol, char *path, size_t buflen); diff --git a/bin/ad/ad_cp.c b/bin/ad/ad_cp.c index ef995ce8..61b94b5e 100644 --- a/bin/ad/ad_cp.c +++ b/bin/ad/ad_cp.c @@ -531,7 +531,7 @@ static int copy(const char *path, /* Get CNID of Parent and add new childir to CNID database */ ppdid = pdid; - if ((did = cnid_for_path(&dvolume, to.p_path, &pdid)) == CNID_INVALID) { + if ((did = cnid_for_path(dvolume.vol->v_cdb, dvolume.vol->v_path, to.p_path, &pdid)) == CNID_INVALID) { SLOG("Error resolving CNID for %s", to.p_path); badcp = rval = 1; return -1; @@ -599,7 +599,7 @@ static int copy(const char *path, /* Get CNID of Parent and add new childir to CNID database */ pdid = did; cnid_t cnid; - if ((cnid = cnid_for_path(&dvolume, to.p_path, &did)) == CNID_INVALID) { + if ((cnid = cnid_for_path(dvolume.vol->v_cdb, dvolume.vol->v_path, to.p_path, &did)) == CNID_INVALID) { SLOG("Error resolving CNID for %s", to.p_path); badcp = rval = 1; return -1; diff --git a/bin/ad/ad_mv.c b/bin/ad/ad_mv.c index 7f592dd2..e48cae99 100644 --- a/bin/ad/ad_mv.c +++ b/bin/ad/ad_mv.c @@ -301,7 +301,7 @@ static int do_move(const char *from, const char *to) cnid_t cnid = 0; if (!mustcopy) { - if ((cnid = cnid_for_path(&svolume, from, &did)) == CNID_INVALID) { + if ((cnid = cnid_for_path(svolume.vol->v_cdb, svolume.vol->v_path, from, &did)) == CNID_INVALID) { SLOG("Couldn't resolve CNID for %s", from); return -1; } diff --git a/bin/ad/ad_rm.c b/bin/ad/ad_rm.c index 195fee06..b32e659f 100644 --- a/bin/ad/ad_rm.c +++ b/bin/ad/ad_rm.c @@ -211,7 +211,7 @@ static int rm(const char *path, /* Get CNID of Parent and add new childir to CNID database */ pdid = did; - if ((cnid = cnid_for_path(&volume, path, &did)) == CNID_INVALID) { + if ((cnid = cnid_for_path(volume.vol->v_cdb, volume.vol->v_path, path, &did)) == CNID_INVALID) { SLOG("Error resolving CNID for %s", path); return -1; } @@ -247,7 +247,7 @@ static int rm(const char *path, } /* Get CNID of Parent and add new childir to CNID database */ - if ((did = cnid_for_path(&volume, path, &pdid)) == CNID_INVALID) { + if ((did = cnid_for_path(volume.vol->v_cdb, volume.vol->v_path, path, &pdid)) == CNID_INVALID) { SLOG("Error resolving CNID for %s", path); return -1; } @@ -293,7 +293,7 @@ static int rm(const char *path, /* Get CNID of Parent and add new childir to CNID database */ pdid = did; - if ((cnid = cnid_for_path(&volume, path, &did)) == CNID_INVALID) { + if ((cnid = cnid_for_path(volume.vol->v_cdb, volume.vol->v_path, path, &did)) == CNID_INVALID) { SLOG("Error resolving CNID for %s", path); return -1; } diff --git a/bin/ad/ad_util.c b/bin/ad/ad_util.c index 897bdf3b..135bf7ab 100644 --- a/bin/ad/ad_util.c +++ b/bin/ad/ad_util.c @@ -231,73 +231,6 @@ int convert_dots_encoding(const afpvol_t *svol, const afpvol_t *dvol, char *path return 0; } -/*! - * ResolvesCNID of a given paths - * - * path might be: - * (a) relative: - * "dir/subdir" with cwd: "/afp_volume/topdir" - * (b) absolute: - * "/afp_volume/dir/subdir" - * - * path MUST be pointing inside vol, this is usually the case as vol has been build from - * path using loadvolinfo and friends. - * - * @param vol (r) pointer to afpvol_t - * @param path (r) path, see above - * @param did (rw) parent CNID of returned CNID - * - * @returns CNID of path - */ -cnid_t cnid_for_path(const afpvol_t *vol, - const char *path, - cnid_t *did) -{ - EC_INIT; - - cnid_t cnid; - bstring rpath = NULL; - bstring statpath = NULL; - struct bstrList *l = NULL; - struct stat st; - - cnid = htonl(2); - - EC_NULL(rpath = rel_path_in_vol(path, vol->vol->v_path)); - EC_NULL(statpath = bfromcstr(vol->vol->v_path)); - EC_ZERO(bcatcstr(statpath, "/")); - - l = bsplit(rpath, '/'); - for (int i = 0; i < l->qty ; i++) { - *did = cnid; - - EC_ZERO(bconcat(statpath, l->entry[i])); - EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st), - "lstat(rpath: %s, elem: %s): %s: %s", - cfrombstr(rpath), cfrombstr(l->entry[i]), - cfrombstr(statpath), strerror(errno)); - - if ((cnid = cnid_add(vol->vol->v_cdb, - &st, - *did, - cfrombstr(l->entry[i]), - blength(l->entry[i]), - 0)) == CNID_INVALID) { - EC_FAIL; - } - EC_ZERO(bcatcstr(statpath, "/")); - } - -EC_CLEANUP: - bdestroy(rpath); - bstrListDestroy(l); - bdestroy(statpath); - if (ret != 0) - return CNID_INVALID; - - return cnid; -} - /*! * Resolves CNID of a given paths parent directory * diff --git a/include/atalk/util.h b/include/atalk/util.h index c5b23ee6..e5025bef 100644 --- a/include/atalk/util.h +++ b/include/atalk/util.h @@ -18,6 +18,7 @@ #include #include +#include /* exit error codes */ #define EXITERR_CLNT 1 /* client related error */ @@ -188,5 +189,6 @@ extern int run_cmd(const char *cmd, char **cmd_argv); *****************************************************************/ extern bstring rel_path_in_vol(const char *path, const char *volpath); +extern cnid_t cnid_for_path(struct _cnid_db *cdb, const char *volpath, const char *path, cnid_t *did); #endif /* _ATALK_UTIL_H */ diff --git a/libatalk/util/cnid.c b/libatalk/util/cnid.c index 0cc56d49..54dfb55c 100644 --- a/libatalk/util/cnid.c +++ b/libatalk/util/cnid.c @@ -35,7 +35,7 @@ #include #include #include - +#include #include #include #include @@ -134,3 +134,72 @@ EC_CLEANUP: return NULL; return fpath; } + +/*! + * Resolves CNID of a given path + * + * path might be: + * (a) relative: + * "dir/subdir" with cwd: "/afp_volume/topdir" + * (b) absolute: + * "/afp_volume/dir/subdir" + * + * path MUST be pointing inside vol, this is usually the case as vol has been build from + * path using loadvolinfo and friends. + * + * @param cdb (r) CNID db handle + * @param volpath (r) UNIX path of volume + * @param path (r) path, see above + * @param did (w) parent CNID of returned CNID + * + * @returns CNID of path + */ +cnid_t cnid_for_path(struct _cnid_db *cdb, + const char *volpath, + const char *path, + cnid_t *did) +{ + EC_INIT; + + cnid_t cnid; + bstring rpath = NULL; + bstring statpath = NULL; + struct bstrList *l = NULL; + struct stat st; + + cnid = htonl(2); + + EC_NULL(rpath = rel_path_in_vol(path, volpath)); + EC_NULL(statpath = bfromcstr(volpath)); + EC_ZERO(bcatcstr(statpath, "/")); + + l = bsplit(rpath, '/'); + for (int i = 0; i < l->qty ; i++) { + *did = cnid; + + EC_ZERO(bconcat(statpath, l->entry[i])); + EC_ZERO_LOGSTR(lstat(cfrombstr(statpath), &st), + "lstat(rpath: %s, elem: %s): %s: %s", + cfrombstr(rpath), cfrombstr(l->entry[i]), + cfrombstr(statpath), strerror(errno)); + + if ((cnid = cnid_add(cdb, + &st, + *did, + cfrombstr(l->entry[i]), + blength(l->entry[i]), + 0)) == CNID_INVALID) { + EC_FAIL; + } + EC_ZERO(bcatcstr(statpath, "/")); + } + +EC_CLEANUP: + bdestroy(rpath); + bstrListDestroy(l); + bdestroy(statpath); + if (ret != 0) + return CNID_INVALID; + + return cnid; +}