unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [PATCH 1/1] Support mkdtemp via mkdtemp! and scm_mkdtemp
@ 2020-12-30  1:50 Rob Browning
  2020-12-30  8:41 ` Mike Gran
  2020-12-30  8:45 ` [PATCH] New procedure mkdtemp to create unique directory names Mike Gran via Developers list for Guile, the GNU extensibility library
  0 siblings, 2 replies; 7+ messages in thread
From: Rob Browning @ 2020-12-30  1:50 UTC (permalink / raw)
  To: guile-devel

* doc/ref/posix.texi: Document mkdtemp! and scm_mkdtemp.
* libguile/filesys.c: (mkdtemp!, scm_mkdtemp): New functions.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
---

 Proposed for 3.0 and/or 2.2 (current patch is against 3.0).

 doc/ref/posix.texi | 14 ++++++++++++++
 libguile/filesys.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++
 libguile/posix.h   |  1 +
 3 files changed, 63 insertions(+)

diff --git a/doc/ref/posix.texi b/doc/ref/posix.texi
index f34c5222d..52c71d193 100644
--- a/doc/ref/posix.texi
+++ b/doc/ref/posix.texi
@@ -1020,6 +1020,20 @@ The file is automatically deleted when the port is closed
 or the program terminates.
 @end deffn
 
+@deffn {Scheme Procedure} mkdtemp! tmpl
+@deffnx {C Function} scm_mkdtemp (tmpl)
+@cindex temporary file
+Create a new unique directory in the file system and return
+its path.
+
+@var{tmpl} is a string specifying where the file should be created: it
+must end with @samp{XXXXXX} and those @samp{X}s will be changed in the
+string to reflect the name of the directory created.
+
+The directory mode will be @code{#o700}, as adjusted by the current
+@code{umask}.
+@end deffn
+
 @deffn {Scheme Procedure} dirname filename
 @deffnx {C Function} scm_dirname (filename)
 Return the directory name component of the file name
diff --git a/libguile/filesys.c b/libguile/filesys.c
index 39bfd38cc..d3643bdbf 100644
--- a/libguile/filesys.c
+++ b/libguile/filesys.c
@@ -1544,6 +1544,54 @@ scm_mkstemp (SCM tmpl)
   return scm_i_mkstemp (tmpl, SCM_UNDEFINED);
 }
 
+SCM_INTERNAL SCM scm_i_mkdtemp (SCM);
+SCM_DEFINE (scm_i_mkdtemp, "mkdtemp!", 1, 0, 0,
+	    (SCM tmpl),
+	    "Create a new unique directory in the file system and return\n"
+	    "its path.\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 reflect the name of the\n"
+            "directory created.\n"
+	    "\n"
+	    "The directory mode will be code{#o700}, as adjusted by the\n"
+            "current @code{umask}.")
+#define FUNC_NAME s_scm_i_mkdtemp
+{
+  char *c_tmpl;
+  char *rv;
+
+  SCM_VALIDATE_STRING (SCM_ARG1, tmpl);
+
+  /* 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);
+  scm_dynwind_free (c_tmpl);
+  SCM_SYSCALL (rv = mkdtemp (c_tmpl));
+  if (rv == NULL)
+    SCM_SYSERROR;
+
+  scm_substring_move_x (scm_from_locale_string (c_tmpl),
+			SCM_INUM0, scm_string_length (tmpl),
+			tmpl, SCM_INUM0);
+
+  scm_dynwind_end ();
+
+  return tmpl;
+}
+#undef FUNC_NAME
+
+SCM
+scm_mkdtemp (SCM tmpl)
+{
+  return scm_i_mkdtemp (tmpl);
+}
+
 \f
 /* Filename manipulation */
 
diff --git a/libguile/posix.h b/libguile/posix.h
index 1d2e1835e..1ac076a83 100644
--- a/libguile/posix.h
+++ b/libguile/posix.h
@@ -66,6 +66,7 @@ SCM_API SCM scm_uname (void);
 SCM_API SCM scm_environ (SCM env);
 SCM_API SCM scm_tmpnam (void);
 SCM_API SCM scm_mkstemp (SCM tmpl);
+SCM_API SCM scm_mkdtemp (SCM tmpl);
 SCM_API SCM scm_tmpfile (void);
 SCM_API SCM scm_open_pipe (SCM pipestr, SCM modes);
 SCM_API SCM scm_close_pipe (SCM port);
-- 
2.29.2




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] Support mkdtemp via mkdtemp! and scm_mkdtemp
  2020-12-30  1:50 [PATCH 1/1] Support mkdtemp via mkdtemp! and scm_mkdtemp Rob Browning
@ 2020-12-30  8:41 ` Mike Gran
  2020-12-30 23:05   ` Rob Browning
  2020-12-30  8:45 ` [PATCH] New procedure mkdtemp to create unique directory names Mike Gran via Developers list for Guile, the GNU extensibility library
  1 sibling, 1 reply; 7+ messages in thread
From: Mike Gran @ 2020-12-30  8:41 UTC (permalink / raw)
  To: Rob Browning; +Cc: guile-devel

On Tue, Dec 29, 2020 at 07:50:05PM -0600, Rob Browning wrote:
> * doc/ref/posix.texi: Document mkdtemp! and scm_mkdtemp.
> * libguile/filesys.c: (mkdtemp!, scm_mkdtemp): New functions.
> 
> Signed-off-by: Rob Browning <rlb@defaultvalue.org>

Hi Rob,

Since mkdtemp already returns a string of the new directory name,
it might be more scheme-like to not modify the input string, and instead
just return the new directory name.

Also, I don't know how universal mkdtemp is. It does exist in Linux and
OpenBSD, but, I'm not sure about all platforms. So a configure check
might not be amiss.

I'll send a reworked patch for you to consider.

Regards,
Mike Gran



^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] New procedure mkdtemp to create unique directory names
  2020-12-30  1:50 [PATCH 1/1] Support mkdtemp via mkdtemp! and scm_mkdtemp Rob Browning
  2020-12-30  8:41 ` Mike Gran
@ 2020-12-30  8:45 ` Mike Gran via Developers list for Guile, the GNU extensibility library
  1 sibling, 0 replies; 7+ messages in thread
From: Mike Gran via Developers list for Guile, the GNU extensibility library @ 2020-12-30  8:45 UTC (permalink / raw)
  To: Rob Browning; +Cc: guile-devel

* configure.ac (AC_CHECK_FUNCS): search mkdtemp
* doc/ref/posix.texi: document mkdtemp
* libguile/filesys.c (scm_mkdtemp) [HAVE_MKDTEMP]: new procedure
* libguile/filesys.h: declaration of scm_mkdtemp
* test-suite/tests/filesys.test: new tests 'mkdtemp: number arg',
    'mkdtemp: directory name template' and 'mkdtemp: directory created'
---
 configure.ac                  | 20 ++++++++++----------
 doc/ref/posix.texi            | 15 +++++++++++++++
 libguile/filesys.c            | 34 ++++++++++++++++++++++++++++++++++
 libguile/filesys.h            |  1 +
 test-suite/tests/filesys.test | 31 +++++++++++++++++++++++++++++++
 5 files changed, 91 insertions(+), 10 deletions(-)

diff --git a/configure.ac b/configure.ac
index 3e96094f6..743a4c7e7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -484,16 +484,16 @@ AC_CHECK_HEADERS([assert.h crt_externs.h])
 #   sched_getaffinity, sched_setaffinity - GNU extensions (glibc)
 #   sendfile - non-POSIX, found in glibc
 #
-AC_CHECK_FUNCS([DINFINITY DQNAN cexp chsize clog clog10 ctermid		\
-  fesetround ftime ftruncate fchown fchmod getcwd geteuid getsid	\
-  gettimeofday getuid getgid gmtime_r ioctl lstat mkdir mknod nice	\
-  readlink rename rmdir setegid seteuid		                        \
-  setlocale setuid setgid setpgid setsid sigaction siginterrupt stat64	\
-  strptime symlink sync sysconf tcgetpgrp tcsetpgrp uname waitpid	\
-  strdup system usleep atexit on_exit chown link fcntl ttyname getpwent	\
-  getgrent kill getppid getpgrp fork setitimer getitimer strchr strcmp	\
-  index bcopy memcpy rindex truncate isblank _NSGetEnviron		\
-  strcoll strcoll_l strtod_l strtol_l newlocale uselocale utimensat	\
+AC_CHECK_FUNCS([DINFINITY DQNAN cexp chsize clog clog10 ctermid         \
+  fesetround ftime ftruncate fchown fchmod getcwd geteuid getsid        \
+  gettimeofday getuid getgid gmtime_r ioctl lstat mkdir mkdtemp mknod   \
+  nice readlink rename rmdir setegid seteuid                            \
+  setlocale setuid setgid setpgid setsid sigaction siginterrupt stat64  \
+  strptime symlink sync sysconf tcgetpgrp tcsetpgrp uname waitpid       \
+  strdup system usleep atexit on_exit chown link fcntl ttyname getpwent \
+  getgrent kill getppid getpgrp fork setitimer getitimer strchr strcmp  \
+  index bcopy memcpy rindex truncate isblank _NSGetEnviron              \
+  strcoll strcoll_l strtod_l strtol_l newlocale uselocale utimensat     \
   sched_getaffinity sched_setaffinity sendfile])
 
 # The newlib C library uses _NL_ prefixed locale langinfo constants.
diff --git a/doc/ref/posix.texi b/doc/ref/posix.texi
index f34c5222d..9cb0be038 100644
--- a/doc/ref/posix.texi
+++ b/doc/ref/posix.texi
@@ -1020,6 +1020,21 @@ The file is automatically deleted when the port is closed
 or the program terminates.
 @end deffn
 
+@deffn {Scheme Procedure} mkdtemp tmpl
+@deffnx {C Function} scm_mkdtemp (tmpl)
+@cindex temporary file
+Create a new unique directory in the file system and return
+its path.
+
+@var{tmpl} is a string specifying where the file should be created: it
+must end with @samp{XXXXXX}.  The return value is a string in which
+those @samp{X}s will be changed to reflect the name of the directory
+created.
+
+The directory mode will be @code{#o700}, as adjusted by the current
+@code{umask}.
+@end deffn
+
 @deffn {Scheme Procedure} dirname filename
 @deffnx {C Function} scm_dirname (filename)
 Return the directory name component of the file name
diff --git a/libguile/filesys.c b/libguile/filesys.c
index 39bfd38cc..50f76c5a1 100644
--- a/libguile/filesys.c
+++ b/libguile/filesys.c
@@ -1544,6 +1544,40 @@ scm_mkstemp (SCM tmpl)
   return scm_i_mkstemp (tmpl, SCM_UNDEFINED);
 }
 
+#if HAVE_MKDTEMP
+SCM_DEFINE (scm_mkdtemp, "mkdtemp", 1, 0, 0,
+	    (SCM tmpl),
+	    "Create a new unique directory in the file system and return\n"
+	    "its path.\n"
+	    "\n"
+	    "@var{tmpl} is a string specifying where the file should be\n"
+	    "created: it must end with @samp{XXXXXX}.  The return value is\n"
+            "a string that is the name of the directory created.\n"
+	    "\n"
+	    "The directory mode will be code{#o700}, as adjusted by the\n"
+            "current @code{umask}.")
+#define FUNC_NAME s_scm_mkdtemp
+{
+  char *c_tmpl;
+  char *rv;
+
+  SCM_VALIDATE_STRING (SCM_ARG1, tmpl);
+
+  scm_dynwind_begin (0);
+
+  c_tmpl = scm_to_locale_string (tmpl);
+  scm_dynwind_free (c_tmpl);
+  SCM_SYSCALL (rv = mkdtemp (c_tmpl));
+  if (rv == NULL)
+    SCM_SYSERROR;
+
+  scm_dynwind_end ();
+
+  return scm_from_locale_string (c_tmpl);
+}
+#undef FUNC_NAME
+#endif /* HAVE_MKDTEMP */
+
 \f
 /* Filename manipulation */
 
diff --git a/libguile/filesys.h b/libguile/filesys.h
index f870ee434..ec4a74a48 100644
--- a/libguile/filesys.h
+++ b/libguile/filesys.h
@@ -65,6 +65,7 @@ SCM_API SCM scm_symlink (SCM oldpath, SCM newpath);
 SCM_API SCM scm_readlink (SCM path);
 SCM_API SCM scm_lstat (SCM str);
 SCM_API SCM scm_copy_file (SCM oldfile, SCM newfile);
+SCM_API SCM scm_mkdtemp (SCM tmpl);
 SCM_API SCM scm_dirname (SCM filename);
 SCM_API SCM scm_basename (SCM filename, SCM suffix);
 SCM_API SCM scm_canonicalize_path (SCM path);
diff --git a/test-suite/tests/filesys.test b/test-suite/tests/filesys.test
index 9ec9f6172..f90ecd8a8 100644
--- a/test-suite/tests/filesys.test
+++ b/test-suite/tests/filesys.test
@@ -231,3 +231,34 @@
 (delete-file (test-file))
 (when (file-exists? (test-symlink))
   (delete-file (test-symlink)))
+
+
+(with-test-prefix "mkdtemp"
+
+  (pass-if-exception "number arg" exception:wrong-type-arg
+    (if (not (defined? 'mkdtemp))
+        (throw 'unresolved)
+        (mkdtemp 123)))
+
+  (pass-if "directory name template"
+    (if (not (defined? 'mkdtemp))
+        (throw 'unresolved)
+        (let* ((template "T-XXXXXX")
+               (str      (mkdtemp template))
+               (result   (and
+                          (string? str)
+                          (not (string=? str template))
+                          (string-contains str "T-")
+                          (= (string-length str 8)))))
+          (false-if-exception (rmdir str))
+          result)))
+
+  (pass-if "directory created"
+    (if (not (defined? 'mkdtemp))
+        (throw 'unresolved)
+        (let* ((template "T-XXXXXX")
+               (str      (mkdtemp template))
+               (_stat    (stat str))
+               (result   (eqv? 'directory (stat:type _stat))))
+          (false-if-exception (rmdir str))
+          result))))
-- 
2.29.2





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] Support mkdtemp via mkdtemp! and scm_mkdtemp
  2020-12-30  8:41 ` Mike Gran
@ 2020-12-30 23:05   ` Rob Browning
  2021-01-05  0:42     ` Rob Browning
  0 siblings, 1 reply; 7+ messages in thread
From: Rob Browning @ 2020-12-30 23:05 UTC (permalink / raw)
  To: Mike Gran; +Cc: guile-devel

Mike Gran <spk121@yahoo.com> writes:

> Since mkdtemp already returns a string of the new directory name,
> it might be more scheme-like to not modify the input string, and instead
> just return the new directory name.

Perhaps, though I was just matching the existing semantics of guile
mkstemp, i.e. figured maybe they should behave the same.

> Also, I don't know how universal mkdtemp is. It does exist in Linux and
> OpenBSD, but, I'm not sure about all platforms. So a configure check
> might not be amiss.

Yeah I wondered, though it is at least POSIX:

  https://pubs.opengroup.org/onlinepubs/9699919799/functions/mkdtemp.html

Not sure what our current bar is for checks.

Thanks
-- 
Rob Browning
rlb @defaultvalue.org and @debian.org
GPG as of 2011-07-10 E6A9 DA3C C9FD 1FF8 C676 D2C4 C0F0 39E9 ED1B 597A
GPG as of 2002-11-03 14DD 432F AE39 534D B592 F9A0 25C8 D377 8C7E 73A4



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] Support mkdtemp via mkdtemp! and scm_mkdtemp
  2020-12-30 23:05   ` Rob Browning
@ 2021-01-05  0:42     ` Rob Browning
  2021-01-11 14:04       ` Mike Gran
  0 siblings, 1 reply; 7+ messages in thread
From: Rob Browning @ 2021-01-05  0:42 UTC (permalink / raw)
  To: Mike Gran; +Cc: guile-devel

Rob Browning <rlb@defaultvalue.org> writes:

> Mike Gran <spk121@yahoo.com> writes:
>
>> Since mkdtemp already returns a string of the new directory name,
>> it might be more scheme-like to not modify the input string, and instead
>> just return the new directory name.
>
> Perhaps, though I was just matching the existing semantics of guile
> mkstemp, i.e. figured maybe they should behave the same.

...mkstemp! I meant, and I think I might still somewhat favor keeping
the correspondence between a mkstemp! and mkdtemp! that have the same
semantics, and more or less directly match their POSIX functions.

Though if we thought it was worth it, I suppose we could also provide
trivial mkstemp and mkdtemp wrappers (without the exclamation points),
that do the extra copy for you.

-- 
Rob Browning
rlb @defaultvalue.org and @debian.org
GPG as of 2011-07-10 E6A9 DA3C C9FD 1FF8 C676 D2C4 C0F0 39E9 ED1B 597A
GPG as of 2002-11-03 14DD 432F AE39 534D B592 F9A0 25C8 D377 8C7E 73A4



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] Support mkdtemp via mkdtemp! and scm_mkdtemp
  2021-01-05  0:42     ` Rob Browning
@ 2021-01-11 14:04       ` Mike Gran
  2021-01-13  0:34         ` Rob Browning
  0 siblings, 1 reply; 7+ messages in thread
From: Mike Gran @ 2021-01-11 14:04 UTC (permalink / raw)
  To: Rob Browning; +Cc: guile-devel

On Mon, Jan 04, 2021 at 06:42:07PM -0600, Rob Browning wrote:
> Rob Browning <rlb@defaultvalue.org> writes:
> 
> > Mike Gran <spk121@yahoo.com> writes:
> >
> >> Since mkdtemp already returns a string of the new directory name,
> >> it might be more scheme-like to not modify the input string, and instead
> >> just return the new directory name.
> >
> > Perhaps, though I was just matching the existing semantics of guile
> > mkstemp, i.e. figured maybe they should behave the same.
> 
> ...mkstemp! I meant, and I think I might still somewhat favor keeping
> the correspondence between a mkstemp! and mkdtemp! that have the same
> semantics, and more or less directly match their POSIX functions.

OK, fair enough. I'll push a patch on mkdtemp! in a day or two if
you're good with the idea.  (I asked on IRC.) But, one thing I'd like
to change, if you don't mind, is the split into scm_i_mkdtemp and
scm_mkdtemp because there is no internal client of scm_i_mkdtemp.

Regards,
Mike Gran




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] Support mkdtemp via mkdtemp! and scm_mkdtemp
  2021-01-11 14:04       ` Mike Gran
@ 2021-01-13  0:34         ` Rob Browning
  0 siblings, 0 replies; 7+ messages in thread
From: Rob Browning @ 2021-01-13  0:34 UTC (permalink / raw)
  To: Mike Gran; +Cc: guile-devel

Mike Gran <spk121@yahoo.com> writes:

> OK, fair enough. I'll push a patch on mkdtemp! in a day or two if
> you're good with the idea.  (I asked on IRC.) But, one thing I'd like
> to change, if you don't mind, is the split into scm_i_mkdtemp and
> scm_mkdtemp because there is no internal client of scm_i_mkdtemp.

Oh, offhand, that seems fine to me -- I don't know that I had any reason
for that other than that I just copied and adapted what had been done
for mkstemp.

Thanks
-- 
Rob Browning
rlb @defaultvalue.org and @debian.org
GPG as of 2011-07-10 E6A9 DA3C C9FD 1FF8 C676 D2C4 C0F0 39E9 ED1B 597A
GPG as of 2002-11-03 14DD 432F AE39 534D B592 F9A0 25C8 D377 8C7E 73A4



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-01-13  0:34 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-30  1:50 [PATCH 1/1] Support mkdtemp via mkdtemp! and scm_mkdtemp Rob Browning
2020-12-30  8:41 ` Mike Gran
2020-12-30 23:05   ` Rob Browning
2021-01-05  0:42     ` Rob Browning
2021-01-11 14:04       ` Mike Gran
2021-01-13  0:34         ` Rob Browning
2020-12-30  8:45 ` [PATCH] New procedure mkdtemp to create unique directory names Mike Gran via Developers list for Guile, the GNU extensibility library

unofficial mirror of guile-devel@gnu.org 

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://yhetil.org/guile-devel/0 guile-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 guile-devel guile-devel/ https://yhetil.org/guile-devel \
		guile-devel@gnu.org
	public-inbox-index guile-devel

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.yhetil.org/yhetil.lisp.guile.devel
	nntp://news.gmane.io/gmane.lisp.guile.devel


AGPL code for this site: git clone http://ou63pmih66umazou.onion/public-inbox.git