From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Mike Gran via "Developers list for Guile, the GNU extensibility library" Newsgroups: gmane.lisp.guile.devel Subject: [PATCH] New procedure mkdtemp to create unique directory names Date: Wed, 30 Dec 2020 00:45:55 -0800 Message-ID: <20201230084538.GA825094@spikycactus.com> References: <20201230015005.335417-1-rlb@defaultvalue.org> Reply-To: Mike Gran Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19326"; mail-complaints-to="usenet@ciao.gmane.io" Cc: guile-devel@gnu.org To: Rob Browning Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Wed Dec 30 09:46:54 2020 Return-path: Envelope-to: guile-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kuX88-0004m1-PW for guile-devel@m.gmane-mx.org; Wed, 30 Dec 2020 09:46:52 +0100 Original-Received: from localhost ([::1]:49094 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kuX87-00031n-Jy for guile-devel@m.gmane-mx.org; Wed, 30 Dec 2020 03:46:51 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48408) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kuX7Z-00031d-UU for guile-devel@gnu.org; Wed, 30 Dec 2020 03:46:17 -0500 Original-Received: from sonic315-22.consmr.mail.ne1.yahoo.com ([66.163.190.148]:36060) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kuX7U-0005eO-8j for guile-devel@gnu.org; Wed, 30 Dec 2020 03:46:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1609317968; bh=jDcuL/mdiM3EaANx92BfHuGEcYaY5aRy+Vp0vjM/yG8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From:Subject; b=f7brYyWyz3ab7rlwQ1WFw+dr4vwkHElWJxRy9bL6SHuuxHUVrkVuXmqgKtPKEf6l/1VFwjgSdNeEFMF0e8nAfrBSXcTGoDEppE+hhEpOdadXxC9DvvEk/ZUNYF9AGSHw/U5Td/CX4HnYCaDtqav/O02x9UnjBUERbWIOYbTqkpZoIa1jWf6pnjbmbCR62P83584RT/v+6ZoSQ0fTrG7YEqXl6OILTbET6FNzIYUj5p/bD/A4kMACs8G37qp6mZNH7HesJE6GMaTt0xq7igy+OF+8TNgqM3xeLf5n/WHXF8RMD4LWM/dVJGopVqUeFP8flHF8cmH+zaD2BZHA7dnEng== X-SONIC-DKIM-SIGN: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1609317968; bh=80uaoNMLUoyIqZ7DH2svusysjGJcvZQYin4xD0ZAHjS=; h=Date:From:To:Subject:From:Subject; b=ik8ZieWY/+Y98Qrsd6Oo7ZBHAoPKCKoRCm40l5jsi5sojg4JVbQzctMpiJ6ARfMhg6w/3EguEF76DQBCucrLIBgbvh8nVQdHY+t9dH+FX2t43O5JUeDMGOLja52FuesNg5MTAEMSjbckdNEOxAIYDN1Z/baAoyg6cUg1saed2pX+2FRgZzHb/FAFR8BRO9WA0oXwQAoHNfstI3tpXzTt9Yp6o8IVlSI9t3/zbwOxeq537EidzZ/O03sXkUPNGYSHaHQOV8JoS+Ehne2OOUMGv/Zs1+6oYWidLFPcENoP3INtQPVOBWQ8mMw8wM3lvXwfc78tWE73U4PSzrOefeEJvQ== X-YMail-OSG: kNH58L4VM1kNl2Frno.yUrJIsaCxLxvDLa46cSDBqDi6U2v4zm09DrKnXrVO6.u XLyYDPKpXBHe.JaGpCFVhT33oReECaG5d7pBJMavUB11.YuG8O9gxjsmyG4mo0L7kMMaVvj1B4TS bX2K9t2b5QuzXRSLK3as8KMXQzR3.z5v6vW9Ni2sLGuPMrYurfDn4.heINY7wwj4okA8kkFiNF4A jsXKgb8jD7az46TSfm6jvyYVaXZ7QUArEHX3ErGJ0W5eu8SF6fMs07tFRcqRsutnqvMcyHDmBxvV PGtonqEGR3XNlElZOMCoTPR8FAUliU_i8Jh3XWfu5TakT8GeGTqos_3aWi8m2bHds8orTnnouWx_ lDzEAHzA2OYVB9.jxd2nDIeHp5gp5mrhyVXJtagYyZ3IXsHmQuNX9IwC7qQL7i6Hve9.qddkhv1P uqlgTEvvd50P5XKC9SphCnnARKnn9Ya80VDy2geTXMxB7Z8gUZN63uE1RwvdnDZLWbc2zI97IlBL rxrHFkG3YJIYyHqGdCf1eJ8GC1FHR7YKH_L5241Jf7aLOVGNLofLwZmkVdTwyNbEstyFnlff2FtZ bnGULrUHI8g5uP7v0OcbDPV..BOZNCrTOx9o8lO6XWiDdLwvsUlHWgkFheqSV7CUE3b.CCMhffed RkNaw_SBDaucMO0TfwsgzHaOMCodk98G_bAKAj__4_QCDyB4nGEXKO6TgpQ4yT0H9Y_xw7gXNKC_ IxEoqrYVI9L84xpSSiBQkxLjAMgeZVoll_MJxWj.T4ZgGnLfizOGlZbs0R88YCiIp5MwbD9EaCB1 GA5uGTPcSAbkWsfvz3imoRZ1mgqA.FtPMs0LcMuDZU Original-Received: from sonic.gate.mail.ne1.yahoo.com by sonic315.consmr.mail.ne1.yahoo.com with HTTP; Wed, 30 Dec 2020 08:46:08 +0000 Original-Received: by smtp422.mail.gq1.yahoo.com (VZM Hermes SMTP Server) with ESMTPA ID 169609f585ba9734cf318c061c99ca78; Wed, 30 Dec 2020 08:46:06 +0000 (UTC) Content-Disposition: inline In-Reply-To: <20201230015005.335417-1-rlb@defaultvalue.org> X-Mailer: WebService/1.1.17278 mail.backend.jedi.jws.acl:role.jedi.acl.token.atz.jws.hermes.yahoo Apache-HttpAsyncClient/4.1.4 (Java/11.0.8) Received-SPF: pass client-ip=66.163.190.148; envelope-from=spk121@yahoo.com; helo=sonic315-22.consmr.mail.ne1.yahoo.com X-Spam_score_int: -4 X-Spam_score: -0.5 X-Spam_bar: / X-Spam_report: (-0.5 / 5.0 requ) BAYES_00=-1.9, DKIM_ADSP_CUSTOM_MED=0.001, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, NML_ADSP_CUSTOM_MED=0.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Original-Sender: "guile-devel" Xref: news.gmane.io gmane.lisp.guile.devel:20627 Archived-At: * 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 */ + /* 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