diff options
author | Andy Wingo <wingo@pobox.com> | 2021-03-10 20:35:58 +0100 |
---|---|---|
committer | Andy Wingo <wingo@pobox.com> | 2021-03-10 20:40:10 +0100 |
commit | 85433fc2b122dc78342c3c83941949d1d9318399 (patch) | |
tree | 2fff776765468df861dc8a8c5e80f551b4905a5f /libguile/filesys.c | |
parent | 89a299102ff3597a48febe1fb6d3097fddcda40e (diff) | |
download | guile-85433fc2b122dc78342c3c83941949d1d9318399.tar.gz |
Add mkstemp; undocument mkstemp!
* doc/ref/posix.texi (File System): Update to document mkstemp only.
* libguile/filesys.c: Make a mkstemp that doesn't modify the input
template. Instead the caller has to get the file name from
port-filename.
(scm_mkstemp): Use the new mkstemp to implement mkstemp!. Can't
deprecate yet though as the replacement hasn't been there for long
enough.
* libguile/posix.c (scm_tempnam): Update to mention mkstemp instead.
* module/system/base/compile.scm (call-with-output-file/atomic): Use
mkstemp.
* test-suite/tests/posix.test:
* test-suite/tests/r6rs-files.test: Use mkstemp.
* NEWS: Update.
Diffstat (limited to 'libguile/filesys.c')
-rw-r--r-- | libguile/filesys.c | 74 |
1 files changed, 54 insertions, 20 deletions
diff --git a/libguile/filesys.c b/libguile/filesys.c index 1b0af6e14..7ca46c8af 100644 --- a/libguile/filesys.c +++ b/libguile/filesys.c @@ -1454,24 +1454,29 @@ SCM_DEFINE (scm_umask, "umask", 0, 1, 0, #undef FUNC_NAME SCM_INTERNAL SCM scm_i_mkstemp (SCM, SCM); -SCM_DEFINE (scm_i_mkstemp, "mkstemp!", 1, 1, 0, +SCM_DEFINE (scm_i_mkstemp, "mkstemp", 1, 1, 0, (SCM tmpl, SCM mode), - "Create a new unique file in the file system and return a new\n" - "buffered port open for reading and writing to the file.\n" + "Create a new unique file in the file system. Return\n" + "a buffered port open for reading and writing to the file.\n" "\n" "@var{tmpl} is a string specifying where the file should be\n" - "created: it must end with @samp{XXXXXX} and those @samp{X}s\n" - "will be changed in the string to return the name of the file.\n" - "(@code{port-filename} on the port also gives the name.)\n" - "\n" - "POSIX doesn't specify the permissions mode of the file, on GNU\n" - "and most systems it's @code{#o600}. An application can use\n" - "@code{chmod} to relax that if desired. For example\n" + "created: it must end with @samp{XXXXXX}. The name of the\n" + "newly created file will be the same as @var{tmpl}, but with\n" + "those @samp{X}s changed, and can be determined by calling\n" + "@code{port-filename} on the returned port.\n" + "\n" + "Note that the newly created file is not deleted automatically\n" + "by Guile; probably the caller should arrange to call\n" + "@code{delete-file} when the file is no longer needed.\n" + "\n" + "POSIX doesn't specify the permissions mode of the file.\n" + "On GNU and most systems it's @code{#o600}. An application can\n" + "use @code{chmod} to relax that if desired. For example\n" "@code{#o666} less @code{umask}, which is usual for ordinary\n" "file creation,\n" "\n" "@example\n" - "(let ((port (mkstemp! (string-copy \"/tmp/myfile-XXXXXX\"))))\n" + "(let ((port (mkstemp \"/tmp/myfile-XXXXXX\")))\n" " (chmod port (logand #o666 (lognot (umask))))\n" " ...)\n" "@end example\n" @@ -1491,10 +1496,6 @@ SCM_DEFINE (scm_i_mkstemp, "mkstemp!", 1, 1, 0, if (!SCM_UNBNDP (mode)) SCM_VALIDATE_STRING (SCM_ARG2, mode); - /* Ensure tmpl is mutable. */ - scm_i_string_start_writing (tmpl); - scm_i_string_stop_writing (); - scm_dynwind_begin (0); c_tmpl = scm_to_locale_string (tmpl); @@ -1523,13 +1524,10 @@ SCM_DEFINE (scm_i_mkstemp, "mkstemp!", 1, 1, 0, if (rv == -1) SCM_SYSERROR; - scm_substring_move_x (scm_from_locale_string (c_tmpl), - SCM_INUM0, scm_string_length (tmpl), - tmpl, SCM_INUM0); - + SCM name = scm_from_locale_string (c_tmpl); scm_dynwind_end (); - port = scm_i_fdes_to_port (rv, mode_bits, tmpl, 0); + port = scm_i_fdes_to_port (rv, mode_bits, name, 0); if (is_binary) /* Use the binary-friendly ISO-8859-1 encoding. */ scm_i_set_port_encoding_x (port, NULL); @@ -1538,6 +1536,42 @@ SCM_DEFINE (scm_i_mkstemp, "mkstemp!", 1, 1, 0, } #undef FUNC_NAME +SCM_INTERNAL SCM scm_i_mkstemp_x (SCM, SCM); +SCM_DEFINE (scm_i_mkstemp_x, "mkstemp!", 1, 1, 0, + (SCM tmpl, SCM mode), + "Create a new unique file in the file system and return a new\n" + "buffered port open for reading and writing to the file.\n" + "\n" + "@var{tmpl} is a string specifying where the file should be\n" + "created: it must end with @samp{XXXXXX} and those @samp{X}s\n" + "will be changed in the string to return the name of the file.\n" + "(@code{port-filename} on the port also gives the name.)\n" + "\n" + "POSIX doesn't specify the permissions mode of the file, on GNU\n" + "and most systems it's @code{#o600}. An application can use\n" + "@code{chmod} to relax that if desired. For example\n" + "@code{#o666} less @code{umask}, which is usual for ordinary\n" + "file creation,\n" + "\n" + "@example\n" + "(let ((port (mkstemp! (string-copy \"/tmp/myfile-XXXXXX\"))))\n" + " (chmod port (logand #o666 (lognot (umask))))\n" + " ...)\n" + "@end example\n" + "\n" + "The optional @var{mode} argument specifies a mode, as a string\n" + "in the same format that @code{open-file} takes. It defaults\n" + "to @code{\"w+\"}.") +#define FUNC_NAME s_scm_i_mkstemp_x +{ + SCM ret = scm_i_mkstemp (tmpl, mode); + scm_substring_move_x (scm_port_filename (ret), + SCM_INUM0, scm_string_length (tmpl), + tmpl, SCM_INUM0); + return ret; +} +#undef FUNC_NAME + SCM scm_mkstemp (SCM tmpl) { |