Improve error messages in os.c and jpm

In os/* functions, show failed path name. In jpm, indicate
a permission issue if we can't stat the file.
This commit is contained in:
Calvin Rose 2019-12-12 03:20:20 -06:00
parent 2d5af32660
commit b8e02afd1a
2 changed files with 36 additions and 23 deletions

View File

@ -211,12 +211,15 @@
(defn rm
"Remove a directory and all sub directories."
[path]
(if (= (os/stat path :mode) :directory)
(do
(each subpath (os/dir path)
(rm (string path sep subpath)))
(os/rmdir path))
(os/rm path)))
(try
(if (= (os/stat path :mode) :directory)
(do
(each subpath (os/dir path)
(rm (string path sep subpath)))
(os/rmdir path))
(os/rm path))
([err f] (unless (string/has-prefix? "No such file or directory" err)
(propagate err f)))))
(defn copy
"Copy a file or directory recursively from one location to another."
@ -228,6 +231,17 @@
(shell "xcopy" src (if isdir (string dest "\\" end) dest) "/y" "/s" "/e" "/i"))
(shell "cp" "-rf" src dest)))
(defn mkdir
"Create a directory if it doesn't exist. If it does exist, do nothing.
If we can't create it, give a friendly error. Return true if created, false if
existing. Throw an error if we can't create it."
[dir]
(if (os/mkdir dir)
true
(if (os/stat dir :mode)
false
(error (string "Could not create " dir " - this could be a permission issue.")))))
#
# C Compilation
#
@ -552,9 +566,7 @@ int main(int argc, const char **argv) {
(def path ((string/split "\n" line) 0))
(def path ((string/split "\r" path) 0))
(print "removing " path)
(try (rm path) ([err]
(unless (= err "No such file or directory")
(error err)))))
(rm path))
(:close f)
(print "removing " manifest)
(rm manifest)
@ -575,17 +587,17 @@ int main(int argc, const char **argv) {
(defn install-git
"Install a bundle from git. If the bundle is already installed, the bundle
is reinistalled (but not rebuilt if artifacts are cached)."
is reinistalled (but not rebuilt if artifacts are cached)."
[repotab &opt recurse]
(def repo (if (string? repotab) repotab (repotab :repo)))
(def tag (unless (string? repotab) (repotab :tag)))
# prevent infinite recursion (very unlikely, but consider
# prevent infinite recursion (very unlikely, but consider
# 'my-package "my-package" in the package listing)
(when (> (or recurse 0) 100)
(error "too many references resolving package url"))
# Handle short names
(unless (string/find ":" repo)
(def pkgs
(def pkgs
(try (require "pkgs")
([err f]
(install-git (dyn :pkglist default-pkglist))
@ -597,13 +609,14 @@ int main(int argc, const char **argv) {
(error (string "expected string or table for repository, got " next-repo)))
(break (install-git next-repo (if recurse (inc recurse) 0))))
(def cache (find-cache))
(os/mkdir cache)
(mkdir cache)
(def id (filepath-replace repo))
(def module-dir (string cache sep id))
(var fresh false)
(when (os/mkdir module-dir)
(set fresh true)
(os/execute ["git" "clone" repo module-dir] :p))
(when (mkdir module-dir)
(set fresh true)
(print "cloning repository " repo " to " module-dir)
(os/execute ["git" "clone" repo module-dir] :p))
(def olddir (os/cwd))
(try
(with-dyns [:rules @{}
@ -632,7 +645,7 @@ int main(int argc, const char **argv) {
(def path (string destdir sep name))
(array/push (dyn :installed-files) path)
(add-body "install"
(os/mkdir destdir)
(mkdir destdir)
(copy src destdir)))
#
@ -773,12 +786,12 @@ int main(int argc, const char **argv) {
(setdyn :manifest-dir manifests)
(setdyn :installed-files installed-files)
(rule "./build" [] (os/mkdir "build"))
(rule "./build" [] (mkdir "build"))
(phony "build" ["./build"])
(phony "manifest" []
(print "generating " manifest "...")
(os/mkdir manifests)
(mkdir manifests)
(spit manifest (string (string/join installed-files "\n") "\n")))
(phony "install" ["uninstall" "build" "manifest"]
(when (dyn :test)

View File

@ -612,7 +612,7 @@ static Janet os_link(int32_t argc, Janet *argv) {
const char *oldpath = janet_getcstring(argv, 0);
const char *newpath = janet_getcstring(argv, 1);
int res = ((argc == 3 && janet_getboolean(argv, 2)) ? symlink : link)(oldpath, newpath);
if (res == -1) janet_panic(strerror(errno));
if (-1 == res) janet_panicf("%s: %s -> %s", strerror(errno), oldpath, newpath);
return janet_wrap_integer(res);
#endif
}
@ -636,7 +636,7 @@ static Janet os_rmdir(int32_t argc, Janet *argv) {
#else
int res = rmdir(path);
#endif
if (res == -1) janet_panic(strerror(errno));
if (-1 == res) janet_panicf("%s: %s", strerror(errno), path);
return janet_wrap_nil();
}
@ -648,7 +648,7 @@ static Janet os_cd(int32_t argc, Janet *argv) {
#else
int res = chdir(path);
#endif
if (res == -1) janet_panic(strerror(errno));
if (-1 == res) janet_panicf("%s: %s", strerror(errno), path);
return janet_wrap_nil();
}
@ -676,7 +676,7 @@ static Janet os_remove(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
const char *path = janet_getcstring(argv, 0);
int status = remove(path);
if (-1 == status) janet_panic(strerror(errno));
if (-1 == status) janet_panicf("%s: %s", strerror(errno), path);
return janet_wrap_nil();
}