1
0
mirror of https://github.com/janet-lang/janet synced 2025-02-20 10:30:02 +00:00

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 (defn rm
"Remove a directory and all sub directories." "Remove a directory and all sub directories."
[path] [path]
(if (= (os/stat path :mode) :directory) (try
(do (if (= (os/stat path :mode) :directory)
(each subpath (os/dir path) (do
(rm (string path sep subpath))) (each subpath (os/dir path)
(os/rmdir path)) (rm (string path sep subpath)))
(os/rm path))) (os/rmdir path))
(os/rm path))
([err f] (unless (string/has-prefix? "No such file or directory" err)
(propagate err f)))))
(defn copy (defn copy
"Copy a file or directory recursively from one location to another." "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 "xcopy" src (if isdir (string dest "\\" end) dest) "/y" "/s" "/e" "/i"))
(shell "cp" "-rf" src dest))) (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 # C Compilation
# #
@ -552,9 +566,7 @@ int main(int argc, const char **argv) {
(def path ((string/split "\n" line) 0)) (def path ((string/split "\n" line) 0))
(def path ((string/split "\r" path) 0)) (def path ((string/split "\r" path) 0))
(print "removing " path) (print "removing " path)
(try (rm path) ([err] (rm path))
(unless (= err "No such file or directory")
(error err)))))
(:close f) (:close f)
(print "removing " manifest) (print "removing " manifest)
(rm manifest) (rm manifest)
@ -575,17 +587,17 @@ int main(int argc, const char **argv) {
(defn install-git (defn install-git
"Install a bundle from git. If the bundle is already installed, the bundle "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] [repotab &opt recurse]
(def repo (if (string? repotab) repotab (repotab :repo))) (def repo (if (string? repotab) repotab (repotab :repo)))
(def tag (unless (string? repotab) (repotab :tag))) (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) # 'my-package "my-package" in the package listing)
(when (> (or recurse 0) 100) (when (> (or recurse 0) 100)
(error "too many references resolving package url")) (error "too many references resolving package url"))
# Handle short names # Handle short names
(unless (string/find ":" repo) (unless (string/find ":" repo)
(def pkgs (def pkgs
(try (require "pkgs") (try (require "pkgs")
([err f] ([err f]
(install-git (dyn :pkglist default-pkglist)) (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))) (error (string "expected string or table for repository, got " next-repo)))
(break (install-git next-repo (if recurse (inc recurse) 0)))) (break (install-git next-repo (if recurse (inc recurse) 0))))
(def cache (find-cache)) (def cache (find-cache))
(os/mkdir cache) (mkdir cache)
(def id (filepath-replace repo)) (def id (filepath-replace repo))
(def module-dir (string cache sep id)) (def module-dir (string cache sep id))
(var fresh false) (var fresh false)
(when (os/mkdir module-dir) (when (mkdir module-dir)
(set fresh true) (set fresh true)
(os/execute ["git" "clone" repo module-dir] :p)) (print "cloning repository " repo " to " module-dir)
(os/execute ["git" "clone" repo module-dir] :p))
(def olddir (os/cwd)) (def olddir (os/cwd))
(try (try
(with-dyns [:rules @{} (with-dyns [:rules @{}
@ -632,7 +645,7 @@ int main(int argc, const char **argv) {
(def path (string destdir sep name)) (def path (string destdir sep name))
(array/push (dyn :installed-files) path) (array/push (dyn :installed-files) path)
(add-body "install" (add-body "install"
(os/mkdir destdir) (mkdir destdir)
(copy src destdir))) (copy src destdir)))
# #
@ -773,12 +786,12 @@ int main(int argc, const char **argv) {
(setdyn :manifest-dir manifests) (setdyn :manifest-dir manifests)
(setdyn :installed-files installed-files) (setdyn :installed-files installed-files)
(rule "./build" [] (os/mkdir "build")) (rule "./build" [] (mkdir "build"))
(phony "build" ["./build"]) (phony "build" ["./build"])
(phony "manifest" [] (phony "manifest" []
(print "generating " manifest "...") (print "generating " manifest "...")
(os/mkdir manifests) (mkdir manifests)
(spit manifest (string (string/join installed-files "\n") "\n"))) (spit manifest (string (string/join installed-files "\n") "\n")))
(phony "install" ["uninstall" "build" "manifest"] (phony "install" ["uninstall" "build" "manifest"]
(when (dyn :test) (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 *oldpath = janet_getcstring(argv, 0);
const char *newpath = janet_getcstring(argv, 1); const char *newpath = janet_getcstring(argv, 1);
int res = ((argc == 3 && janet_getboolean(argv, 2)) ? symlink : link)(oldpath, newpath); 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); return janet_wrap_integer(res);
#endif #endif
} }
@ -636,7 +636,7 @@ static Janet os_rmdir(int32_t argc, Janet *argv) {
#else #else
int res = rmdir(path); int res = rmdir(path);
#endif #endif
if (res == -1) janet_panic(strerror(errno)); if (-1 == res) janet_panicf("%s: %s", strerror(errno), path);
return janet_wrap_nil(); return janet_wrap_nil();
} }
@ -648,7 +648,7 @@ static Janet os_cd(int32_t argc, Janet *argv) {
#else #else
int res = chdir(path); int res = chdir(path);
#endif #endif
if (res == -1) janet_panic(strerror(errno)); if (-1 == res) janet_panicf("%s: %s", strerror(errno), path);
return janet_wrap_nil(); return janet_wrap_nil();
} }
@ -676,7 +676,7 @@ static Janet os_remove(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1); janet_fixarity(argc, 1);
const char *path = janet_getcstring(argv, 0); const char *path = janet_getcstring(argv, 0);
int status = remove(path); 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(); return janet_wrap_nil();
} }