Skip to content

Commit

Permalink
Native temp file and dir facilities
Browse files Browse the repository at this point in the history
Fixes #934
  • Loading branch information
mfikes committed May 24, 2019
1 parent 68fd50c commit a1b8335
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file. This change
### Fixed
- `tty?` spec refers to `planck.core/IWriter` ([#928](https://github.com/planck-repl/planck/issues/928))

### Added
- Native temp file and dir facilities ([#934](https://github.com/planck-repl/planck/issues/934)

## [2.23.0] - 2019-05-19
### Added
- Add `planck.io/tty?` ([#911](https://github.com/planck-repl/planck/issues/911))
Expand Down
2 changes: 2 additions & 0 deletions planck-c/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ void *do_engine_init(void *data) {

register_global_function(ctx, "PLANCK_FSTAT", function_fstat);

register_global_function(ctx, "PLANCK_MKTEMP", function_mktemp);

register_global_function(ctx, "PLANCK_REQUEST", function_http_request);

register_global_function(ctx, "PLANCK_READ_PASSWORD", function_read_password);
Expand Down
37 changes: 37 additions & 0 deletions planck-c/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,43 @@ JSValueRef function_list_files(JSContextRef ctx, JSObjectRef function, JSObjectR
return JSValueMakeNull(ctx);
}

JSValueRef function_mktemp(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
size_t argc, const JSValueRef args[], JSValueRef *exception) {
if (argc == 1
&& JSValueGetType(ctx, args[0]) == kJSTypeBoolean) {

bool directory = JSValueToBoolean(ctx, args[0]);

char* tmpdir = getenv("TMPDIR");
if (!tmpdir) {
tmpdir = "/tmp";
}

char template[PATH_MAX];
if (str_has_suffix(tmpdir, "/") == 0) {
snprintf(template, PATH_MAX, "%splanck.XXXXXX", tmpdir);
} else {
snprintf(template, PATH_MAX, "%s/planck.XXXXXX", tmpdir);
}

char *temp_name;
if (directory) {
temp_name = mkdtemp(template);
} else {
temp_name = mktemp(template);
}

if (temp_name) {
return c_string_to_value(ctx, temp_name);
} else {
JSValueRef arguments[1];
arguments[0] = c_string_to_value(ctx, strerror(errno));
*exception = JSObjectMakeError(ctx, 1, arguments, NULL);
}

}
return JSValueMakeNull(ctx);
}

JSValueRef function_is_directory(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
size_t argc, const JSValueRef args[], JSValueRef *exception) {
Expand Down
3 changes: 3 additions & 0 deletions planck-c/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ JSValueRef function_copy_file(JSContextRef ctx, JSObjectRef function, JSObjectRe
JSValueRef function_list_files(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc,
const JSValueRef args[], JSValueRef *exception);

JSValueRef function_mktemp(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
size_t argc, const JSValueRef args[], JSValueRef *exception);

JSValueRef function_is_directory(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc,
const JSValueRef args[], JSValueRef *exception);

Expand Down
23 changes: 23 additions & 0 deletions planck-cljs/src/planck/io.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,29 @@
:args (s/cat :dir (s/or :string string? :file file?))
:ret (s/coll-of file?))

(defn temp-file
"Returns a temporary file as a [[File]].
The path name is guaranteed to be unique at the time of the the call, and
the file will not be created on disk."
[]
(as-file (js/PLANCK_MKTEMP false)))

(s/fdef temp-file
:args (s/cat)
:ret file?)

(defn temp-directory
"Returns a temporary directory as a [[File]].
The directory will be created on disk with mode 0700."
[]
(as-file (js/PLANCK_MKTEMP true)))

(s/fdef temp-directory
:args (s/cat)
:ret file?)

(defn resource
"Returns the URI for the named resource, `n`.
Expand Down
7 changes: 7 additions & 0 deletions planck-cljs/test/planck/io_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,10 @@
(is (nil? (io/list-files "/bogus/path")))
(is (seq? (io/list-files "/tmp")))
(is (io/file? (first (io/list-files "/tmp")))))

(deftest temp-file-test
(is (io/file? (io/temp-file))))

(deftest temp-directory-test
(is (io/file? (io/temp-directory)))
(is (io/directory? (io/temp-directory))))

0 comments on commit a1b8335

Please sign in to comment.