X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Fpath-util.c;h=69fcb1660a134531830047795a3edd77d7ef6eb2;hb=76cf10dab7a36653a159f0e87c46a13df494474f;hp=fcacf541ed83970be11fc44b842ae380bcb1ed88;hpb=97f2d76d4f4dfab8b0629c09926a05a1e5621125;p=elogind.git diff --git a/src/shared/path-util.c b/src/shared/path-util.c index fcacf541e..69fcb1660 100644 --- a/src/shared/path-util.c +++ b/src/shared/path-util.c @@ -45,18 +45,6 @@ bool is_path(const char *p) { return !!strchr(p, '/'); } -char *path_get_file_name(const char *p) { - char *r; - - assert(p); - - r = strrchr(p, '/'); - if (r) - return r + 1; - - return (char*) p; -} - int path_get_parent(const char *path, char **_r) { const char *e, *a = NULL, *b = NULL, *p; char *r; @@ -165,7 +153,7 @@ char **path_strv_make_absolute_cwd(char **l) { return l; } -char **path_strv_canonicalize(char **l) { +char **path_strv_canonicalize_absolute(char **l, const char *prefix) { char **s; unsigned k = 0; bool enomem = false; @@ -180,13 +168,21 @@ char **path_strv_canonicalize(char **l) { STRV_FOREACH(s, l) { char *t, *u; - t = path_make_absolute_cwd(*s); - free(*s); - *s = NULL; - - if (!t) { - enomem = true; + if (!path_is_absolute(*s)) continue; + + if (prefix) { + t = strappend(prefix, *s); + free(*s); + *s = NULL; + + if (!t) { + enomem = true; + continue; + } + } else { + t = *s; + *s = NULL; } errno = 0; @@ -196,7 +192,7 @@ char **path_strv_canonicalize(char **l) { u = t; else { free(t); - if (errno == ENOMEM || !errno) + if (errno == ENOMEM || errno == 0) enomem = true; continue; @@ -215,11 +211,12 @@ char **path_strv_canonicalize(char **l) { return l; } -char **path_strv_canonicalize_uniq(char **l) { +char **path_strv_canonicalize_absolute_uniq(char **l, const char *prefix) { + if (strv_isempty(l)) return l; - if (!path_strv_canonicalize(l)) + if (!path_strv_canonicalize_absolute(l, prefix)) return NULL; return strv_uniq(l); @@ -327,11 +324,15 @@ bool path_equal(const char *a, const char *b) { } int path_is_mount_point(const char *t, bool allow_symlink) { - char *parent; - int r; - struct file_handle *h; + + union file_handle_union h = { + .handle.handle_bytes = MAX_HANDLE_SZ + }; + int mount_id, mount_id_parent; + char *parent; struct stat a, b; + int r; /* We are not actually interested in the file handles, but * name_to_handle_at() also passes us the mount ID, hence use @@ -340,12 +341,9 @@ int path_is_mount_point(const char *t, bool allow_symlink) { if (path_equal(t, "/")) return 1; - h = alloca(MAX_HANDLE_SZ); - h->handle_bytes = MAX_HANDLE_SZ; - - r = name_to_handle_at(AT_FDCWD, t, h, &mount_id, allow_symlink ? AT_SYMLINK_FOLLOW : 0); + r = name_to_handle_at(AT_FDCWD, t, &h.handle, &mount_id, allow_symlink ? AT_SYMLINK_FOLLOW : 0); if (r < 0) { - if (errno == ENOSYS || errno == ENOTSUP) + if (IN_SET(errno, ENOSYS, EOPNOTSUPP)) /* This kernel or file system does not support * name_to_handle_at(), hence fallback to the * traditional stat() logic */ @@ -361,15 +359,14 @@ int path_is_mount_point(const char *t, bool allow_symlink) { if (r < 0) return r; - h->handle_bytes = MAX_HANDLE_SZ; - r = name_to_handle_at(AT_FDCWD, parent, h, &mount_id_parent, 0); + h.handle.handle_bytes = MAX_HANDLE_SZ; + r = name_to_handle_at(AT_FDCWD, parent, &h.handle, &mount_id_parent, 0); free(parent); - if (r < 0) { /* The parent can't do name_to_handle_at() but the * directory we are interested in can? If so, it must * be a mount point */ - if (errno == ENOTSUP) + if (errno == EOPNOTSUPP) return 1; return -errno; @@ -396,7 +393,6 @@ fallback: r = lstat(parent, &b); free(parent); - if (r < 0) return -errno; @@ -428,17 +424,21 @@ int path_is_os_tree(const char *path) { int find_binary(const char *name, char **filename) { assert(name); + if (strchr(name, '/')) { - char *p; + if (access(name, X_OK) < 0) + return -errno; + + if (filename) { + char *p; - if (path_is_absolute(name)) - p = strdup(name); - else p = path_make_absolute_cwd(name); - if (!p) - return -ENOMEM; + if (!p) + return -ENOMEM; + + *filename = p; + } - *filename = p; return 0; } else { const char *path; @@ -454,18 +454,18 @@ int find_binary(const char *name, char **filename) { path = DEFAULT_PATH; FOREACH_WORD_SEPARATOR(w, l, path, ":", state) { - char *p; + _cleanup_free_ char *p = NULL; if (asprintf(&p, "%.*s/%s", (int) l, w, name) < 0) return -ENOMEM; - if (access(p, X_OK) < 0) { - free(p); + if (access(p, X_OK) < 0) continue; - } - path_kill_slashes(p); - *filename = p; + if (filename) { + *filename = path_kill_slashes(p); + p = NULL; + } return 0; } @@ -474,33 +474,44 @@ int find_binary(const char *name, char **filename) { } } -bool paths_check_timestamp(char **paths, usec_t *timestamp, bool update) -{ - unsigned int i; +bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) { bool changed = false; + const char* const* i; assert(timestamp); if (paths == NULL) - goto out; + return false; - for (i = 0; paths[i]; i++) { + STRV_FOREACH(i, paths) { struct stat stats; + usec_t u; - if (stat(paths[i], &stats) < 0) + if (stat(*i, &stats) < 0) continue; + u = timespec_load(&stats.st_mtim); + /* first check */ - if (*timestamp >= timespec_load(&stats.st_mtim)) + if (*timestamp >= u) continue; - log_debug("timestamp of '%s' changed\n", paths[i]); - changed = true; + log_debug("timestamp of '%s' changed", *i); /* update timestamp */ - if (update) - *timestamp = timespec_load(&stats.st_mtim); + if (update) { + *timestamp = u; + changed = true; + } else + return true; } -out: + return changed; } + +int fsck_exists(const char *fstype) { + const char *checker; + + checker = strappenda("fsck.", fstype); + return find_binary(checker, NULL); +}