1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-29 19:30:36 +02:00

Allow mkstemp! to have optional "mode" argument

* m4/mkstemp.m4: Remove.
* lib/mkstemp.c: Remove.
* lib/mkostemp.c: New file.
* m4/mkostemp.m4: New file.
* lib/Makefile.am:
* m4/gnulib-cache.m4:
* m4/gnulib-comp.m4: Remove mkstemp module, replace with mkostemp.

* libguile/fports.h:
* libguile/fports.c (scm_i_mode_to_open_flags): Factor out helper to
  parse mode string to open flags.
  (scm_open_file_with_encoding): Use the new helper.
* libguile/filesys.c:
  (scm_i_mkstemp): Adapt to take optional second argument, being a mode
  string.  Use mkostemp.
  (scm_mkstemp): Backwards compatible shim that calls scm_i_mkstemp.

* doc/ref/posix.texi:
* NEWS: Update.

* module/system/base/compile.scm (call-with-output-file/atomic): Pass
  "wb" as mode, to cause O_BINARY to be added on MinGW.
This commit is contained in:
Andy Wingo 2016-07-16 15:34:41 +02:00
parent e868fae658
commit aae3561584
12 changed files with 149 additions and 147 deletions

14
NEWS
View file

@ -743,6 +743,20 @@ longer installed to the libdir. This change should be transparent to
users, but packagers may be interested.
Changes in 2.0.13 (since 2.0.12):
* Notable changes
* New interfaces
** mkstemp! takes optional "mode" argument
See "File System" in the manual, for more.
* Bug fixes
** Fix optimizer bug when compiling fixpoint operator
** Fix build error on MinGW
** Update `uname' implementation on MinGW
Changes in 2.0.12 (since 2.0.11):

View file

@ -954,7 +954,7 @@ another name if the file exists (error @code{EEXIST}).
@code{mkstemp!} below does that.
@end deffn
@deffn {Scheme Procedure} mkstemp! tmpl
@deffn {Scheme Procedure} mkstemp! tmpl [mode]
@deffnx {C Function} scm_mkstemp (tmpl)
@cindex temporary file
Create a new unique file in the file system and return a new buffered
@ -975,6 +975,10 @@ which is usual for ordinary file creation,
(chmod port (logand #o666 (lognot (umask))))
...)
@end example
The optional @var{mode} argument specifies a mode with which to open the
new file, as a string in the same format that @code{open-file} takes.
It defaults to @code{"w+"}.
@end deffn
@deffn {Scheme Procedure} tmpfile

View file

@ -21,7 +21,7 @@
# the same distribution terms as the rest of that program.
#
# Generated by gnulib-tool.
# Reproduce by: gnulib-tool --import --local-dir=gnulib-local --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=lock --avoid=unistr/base --avoid=unistr/u8-mbtouc --avoid=unistr/u8-mbtouc-unsafe --avoid=unistr/u8-mbtoucr --avoid=unistr/u8-prev --avoid=unistr/u8-uctomb --avoid=unitypes --lgpl=3 --conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept alignof alloca-opt announce-gen autobuild bind byteswap c-strcase canonicalize-lgpl ceil clock-time close connect copysign dirfd duplocale environ extensions flock floor fpieee frexp fstat fsync full-read full-write func gendocs getaddrinfo getlogin getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isfinite isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring link listen localcharset locale log1p lstat maintainer-makefile malloc-gnu malloca mkdir mkstemp nl_langinfo nproc open pipe-posix pipe2 poll putenv readlink recv recvfrom regex rename rmdir select send sendto setenv setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat time times trunc unistd verify vsnprintf warnings wchar
# Reproduce by: gnulib-tool --import --local-dir=gnulib-local --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=lock --avoid=unistr/base --avoid=unistr/u8-mbtouc --avoid=unistr/u8-mbtouc-unsafe --avoid=unistr/u8-mbtoucr --avoid=unistr/u8-prev --avoid=unistr/u8-uctomb --avoid=unitypes --lgpl=3 --conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept alignof alloca-opt announce-gen autobuild bind byteswap c-strcase canonicalize-lgpl ceil clock-time close connect copysign dirfd duplocale environ extensions flock floor fpieee frexp fstat fsync full-read full-write func gendocs getaddrinfo getlogin getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isfinite isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring link listen localcharset locale log1p lstat maintainer-makefile malloc-gnu malloca mkdir mkostemp nl_langinfo nproc open pipe-posix pipe2 poll putenv readlink recv recvfrom regex rename rmdir select send sendto setenv setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat time times trunc unistd verify vsnprintf warnings wchar
AUTOMAKE_OPTIONS = 1.9.6 gnits
@ -1500,14 +1500,14 @@ EXTRA_libgnu_la_SOURCES += mkdir.c
## end gnulib module mkdir
## begin gnulib module mkstemp
## begin gnulib module mkostemp
EXTRA_DIST += mkstemp.c
EXTRA_DIST += mkostemp.c
EXTRA_libgnu_la_SOURCES += mkstemp.c
EXTRA_libgnu_la_SOURCES += mkostemp.c
## end gnulib module mkstemp
## end gnulib module mkostemp
## begin gnulib module mktime

View file

@ -24,7 +24,7 @@
#if !_LIBC
# include "tempname.h"
# define __gen_tempname gen_tempname
# ifndef __GT_FILE
# ifndef __GTFILE
# define __GT_FILE GT_FILE
# endif
#endif
@ -38,13 +38,9 @@
/* Generate a unique temporary file name from XTEMPLATE.
The last six characters of XTEMPLATE must be "XXXXXX";
they are replaced with a string that makes the file name unique.
Then open the file and return a fd.
If you are creating temporary files which will later be removed,
consider using the clean-temp module, which avoids several pitfalls
of using mkstemp directly. */
Then open the file and return a fd. */
int
mkstemp (char *xtemplate)
mkostemp (char *xtemplate, int flags)
{
return __gen_tempname (xtemplate, 0, 0, __GT_FILE);
return __gen_tempname (xtemplate, 0, flags, __GT_FILE);
}

View file

@ -1439,8 +1439,9 @@ SCM_DEFINE (scm_umask, "umask", 0, 1, 0,
}
#undef FUNC_NAME
SCM_DEFINE (scm_mkstemp, "mkstemp!", 1, 0, 0,
(SCM tmpl),
SCM_INTERNAL SCM scm_i_mkstemp (SCM, SCM);
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"
"\n"
@ -1459,18 +1460,38 @@ SCM_DEFINE (scm_mkstemp, "mkstemp!", 1, 0, 0,
"(let ((port (mkstemp! (string-copy \"/tmp/myfile-XXXXXX\"))))\n"
" (chmod port (logand #o666 (lognot (umask))))\n"
" ...)\n"
"@end example")
#define FUNC_NAME s_scm_mkstemp
"@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
{
char *c_tmpl;
long mode_bits;
int rv;
int open_flags, is_binary;
SCM port;
scm_dynwind_begin (0);
c_tmpl = scm_to_locale_string (tmpl);
scm_dynwind_free (c_tmpl);
if (SCM_UNBNDP (mode))
{
/* mkostemp will create a read/write file and add on additional
flags; open_flags just adjoins flags to that set. */
open_flags = 0;
is_binary = 0;
mode_bits = SCM_OPN | SCM_RDNG | SCM_WRTNG;
}
else
{
open_flags = scm_i_mode_to_open_flags (mode, &is_binary, FUNC_NAME);
mode_bits = scm_i_mode_bits (mode);
}
SCM_SYSCALL (rv = mkstemp (c_tmpl));
SCM_SYSCALL (rv = mkostemp (c_tmpl, open_flags));
if (rv == -1)
SCM_SYSERROR;
@ -1479,10 +1500,22 @@ SCM_DEFINE (scm_mkstemp, "mkstemp!", 1, 0, 0,
tmpl, SCM_INUM0);
scm_dynwind_end ();
return scm_fdes_to_port (rv, "w+", tmpl);
port = scm_i_fdes_to_port (rv, mode_bits, tmpl);
if (is_binary)
/* Use the binary-friendly ISO-8859-1 encoding. */
scm_i_set_port_encoding_x (port, NULL);
return port;
}
#undef FUNC_NAME
SCM
scm_mkstemp (SCM tmpl)
{
return scm_i_mkstemp (tmpl, SCM_UNDEFINED);
}
/* Filename manipulation */

View file

@ -152,45 +152,17 @@ fport_canonicalize_filename (SCM filename)
}
}
/* scm_open_file_with_encoding
Return a new port open on a given file.
The mode string must match the pattern: [rwa+]** which
is interpreted in the usual unix way.
Unless binary mode is requested, the character encoding of the new
port is determined as follows: First, if GUESS_ENCODING is true,
'file-encoding' is used to guess the encoding of the file. If
GUESS_ENCODING is false or if 'file-encoding' fails, ENCODING is used
unless it is also false. As a last resort, the default port encoding
is used. It is an error to pass a non-false GUESS_ENCODING or
ENCODING if binary mode is requested.
Return the new port. */
SCM
scm_open_file_with_encoding (SCM filename, SCM mode,
SCM guess_encoding, SCM encoding)
#define FUNC_NAME "open-file"
int
scm_i_mode_to_open_flags (SCM mode, int *is_binary, const char *FUNC_NAME)
{
SCM port;
int fdes, flags = 0, binary = 0;
unsigned int retries;
char *file;
int flags = 0;
const char *md, *ptr;
if (SCM_UNLIKELY (!(scm_is_false (encoding) || scm_is_string (encoding))))
scm_wrong_type_arg_msg (FUNC_NAME, 0, encoding,
"encoding to be string or false");
scm_dynwind_begin (0);
file = scm_to_locale_string (filename);
scm_dynwind_free (file);
if (SCM_UNLIKELY (!scm_i_try_narrow_string (mode)))
scm_out_of_range (FUNC_NAME, mode);
md = scm_i_string_chars (mode);
*is_binary = 0;
switch (*md)
{
@ -215,7 +187,7 @@ scm_open_file_with_encoding (SCM filename, SCM mode,
flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
break;
case 'b':
binary = 1;
*is_binary = 1;
#if defined (O_BINARY)
flags |= O_BINARY;
#endif
@ -229,6 +201,45 @@ scm_open_file_with_encoding (SCM filename, SCM mode,
ptr++;
}
return flags;
}
/* scm_open_file_with_encoding
Return a new port open on a given file.
The mode string must match the pattern: [rwa+]** which
is interpreted in the usual unix way.
Unless binary mode is requested, the character encoding of the new
port is determined as follows: First, if GUESS_ENCODING is true,
'file-encoding' is used to guess the encoding of the file. If
GUESS_ENCODING is false or if 'file-encoding' fails, ENCODING is used
unless it is also false. As a last resort, the default port encoding
is used. It is an error to pass a non-false GUESS_ENCODING or
ENCODING if binary mode is requested.
Return the new port. */
SCM
scm_open_file_with_encoding (SCM filename, SCM mode,
SCM guess_encoding, SCM encoding)
#define FUNC_NAME "open-file"
{
SCM port;
int fdes, flags, binary = 0;
unsigned int retries;
char *file;
if (SCM_UNLIKELY (!(scm_is_false (encoding) || scm_is_string (encoding))))
scm_wrong_type_arg_msg (FUNC_NAME, 0, encoding,
"encoding to be string or false");
scm_dynwind_begin (0);
file = scm_to_locale_string (filename);
scm_dynwind_free (file);
flags = scm_i_mode_to_open_flags (mode, &binary, FUNC_NAME);
for (retries = 0, fdes = -1;
fdes < 0 && retries < 2;
retries++)

View file

@ -53,6 +53,8 @@ SCM_API scm_t_port_type *scm_file_port_type;
SCM_API void scm_evict_ports (int fd);
SCM_INTERNAL int scm_i_mode_to_open_flags (SCM mode, int *is_binary,
const char *FUNC_NAME);
SCM_API SCM scm_open_file_with_encoding (SCM filename, SCM modes,
SCM guess_encoding, SCM encoding);
SCM_API SCM scm_open_file (SCM filename, SCM modes);

View file

@ -27,7 +27,7 @@
# Specification in the form of a command-line invocation:
# gnulib-tool --import --local-dir=gnulib-local --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=lock --avoid=unistr/base --avoid=unistr/u8-mbtouc --avoid=unistr/u8-mbtouc-unsafe --avoid=unistr/u8-mbtoucr --avoid=unistr/u8-prev --avoid=unistr/u8-uctomb --avoid=unitypes --lgpl=3 --conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept alignof alloca-opt announce-gen autobuild bind byteswap c-strcase canonicalize-lgpl ceil clock-time close connect copysign dirfd duplocale environ extensions flock floor fpieee frexp fstat fsync full-read full-write func gendocs getaddrinfo getlogin getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isfinite isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring link listen localcharset locale log1p lstat maintainer-makefile malloc-gnu malloca mkdir mkstemp nl_langinfo nproc open pipe-posix pipe2 poll putenv readlink recv recvfrom regex rename rmdir select send sendto setenv setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat time times trunc unistd verify vsnprintf warnings wchar
# gnulib-tool --import --local-dir=gnulib-local --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=lock --avoid=unistr/base --avoid=unistr/u8-mbtouc --avoid=unistr/u8-mbtouc-unsafe --avoid=unistr/u8-mbtoucr --avoid=unistr/u8-prev --avoid=unistr/u8-uctomb --avoid=unitypes --lgpl=3 --conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept alignof alloca-opt announce-gen autobuild bind byteswap c-strcase canonicalize-lgpl ceil clock-time close connect copysign dirfd duplocale environ extensions flock floor fpieee frexp fstat fsync full-read full-write func gendocs getaddrinfo getlogin getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isfinite isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring link listen localcharset locale log1p lstat maintainer-makefile malloc-gnu malloca mkdir mkostemp nl_langinfo nproc open pipe-posix pipe2 poll putenv readlink recv recvfrom regex rename rmdir select send sendto setenv setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat time times trunc unistd verify vsnprintf warnings wchar
# Specification in the form of a few gnulib-tool.m4 macro invocations:
gl_LOCAL_DIR([gnulib-local])
@ -90,7 +90,7 @@ gl_MODULES([
malloc-gnu
malloca
mkdir
mkstemp
mkostemp
nl_langinfo
nproc
open

View file

@ -151,7 +151,7 @@ AC_DEFUN([gl_EARLY],
# Code from module mbtowc:
# Code from module memchr:
# Code from module mkdir:
# Code from module mkstemp:
# Code from module mkostemp:
# Code from module mktime:
# Code from module mktime-internal:
# Code from module msvc-inval:
@ -515,12 +515,13 @@ AC_DEFUN([gl_INIT],
if test $REPLACE_MKDIR = 1; then
AC_LIBOBJ([mkdir])
fi
gl_FUNC_MKSTEMP
if test $HAVE_MKSTEMP = 0 || test $REPLACE_MKSTEMP = 1; then
AC_LIBOBJ([mkstemp])
gl_PREREQ_MKSTEMP
gl_FUNC_MKOSTEMP
if test $HAVE_MKOSTEMP = 0; then
AC_LIBOBJ([mkostemp])
gl_PREREQ_MKOSTEMP
fi
gl_STDLIB_MODULE_INDICATOR([mkstemp])
gl_MODULE_INDICATOR([mkostemp])
gl_STDLIB_MODULE_INDICATOR([mkostemp])
gl_MULTIARCH
gl_HEADER_NETDB
gl_HEADER_NETINET_IN
@ -1360,7 +1361,7 @@ AC_SUBST([LTALLOCA])
if test $REPLACE_MKDIR = 1; then
func_gl_gnulib_m4code_a691da99c1d83b83238e45f41a696f5c
fi
if test $HAVE_MKSTEMP = 0 || test $REPLACE_MKSTEMP = 1; then
if test $HAVE_MKOSTEMP = 0; then
func_gl_gnulib_m4code_tempname
fi
if test $HAVE_NL_LANGINFO = 0 || test $REPLACE_NL_LANGINFO = 1; then
@ -1786,7 +1787,7 @@ AC_DEFUN([gl_FILE_LIST], [
lib/memchr.c
lib/memchr.valgrind
lib/mkdir.c
lib/mkstemp.c
lib/mkostemp.c
lib/mktime-internal.h
lib/mktime.c
lib/msvc-inval.c
@ -1987,7 +1988,7 @@ AC_DEFUN([gl_FILE_LIST], [
m4/mbtowc.m4
m4/memchr.m4
m4/mkdir.m4
m4/mkstemp.m4
m4/mkostemp.m4
m4/mktime.m4
m4/mmap-anon.m4
m4/mode_t.m4

23
m4/mkostemp.m4 Normal file
View file

@ -0,0 +1,23 @@
# mkostemp.m4 serial 2
dnl Copyright (C) 2009-2016 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
AC_DEFUN([gl_FUNC_MKOSTEMP],
[
AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
dnl Persuade glibc <stdlib.h> to declare mkostemp().
AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
AC_CHECK_FUNCS_ONCE([mkostemp])
if test $ac_cv_func_mkostemp != yes; then
HAVE_MKOSTEMP=0
fi
])
# Prerequisites of lib/mkostemp.c.
AC_DEFUN([gl_PREREQ_MKOSTEMP],
[
])

View file

@ -1,82 +0,0 @@
#serial 23
# Copyright (C) 2001, 2003-2007, 2009-2016 Free Software Foundation, Inc.
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# On some hosts (e.g., HP-UX 10.20, SunOS 4.1.4, Solaris 2.5.1), mkstemp has a
# silly limit that it can create no more than 26 files from a given template.
# Other systems lack mkstemp altogether.
# On OSF1/Tru64 V4.0F, the system-provided mkstemp function can create
# only 32 files per process.
# On some hosts, mkstemp creates files with mode 0666, which is a security
# problem and a violation of POSIX 2008.
# On systems like the above, arrange to use the replacement function.
AC_DEFUN([gl_FUNC_MKSTEMP],
[
AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
AC_CHECK_FUNCS_ONCE([mkstemp])
if test $ac_cv_func_mkstemp = yes; then
AC_CACHE_CHECK([for working mkstemp],
[gl_cv_func_working_mkstemp],
[
mkdir conftest.mkstemp
AC_RUN_IFELSE(
[AC_LANG_PROGRAM(
[AC_INCLUDES_DEFAULT],
[[int result = 0;
int i;
off_t large = (off_t) 4294967295u;
if (large < 0)
large = 2147483647;
umask (0);
for (i = 0; i < 70; i++)
{
char templ[] = "conftest.mkstemp/coXXXXXX";
int (*mkstemp_function) (char *) = mkstemp;
int fd = mkstemp_function (templ);
if (fd < 0)
result |= 1;
else
{
struct stat st;
if (lseek (fd, large, SEEK_SET) != large)
result |= 2;
if (fstat (fd, &st) < 0)
result |= 4;
else if (st.st_mode & 0077)
result |= 8;
if (close (fd))
result |= 16;
}
}
return result;]])],
[gl_cv_func_working_mkstemp=yes],
[gl_cv_func_working_mkstemp=no],
[case "$host_os" in
# Guess yes on glibc systems.
*-gnu*) gl_cv_func_working_mkstemp="guessing yes" ;;
# If we don't know, assume the worst.
*) gl_cv_func_working_mkstemp="guessing no" ;;
esac
])
rm -rf conftest.mkstemp
])
case "$gl_cv_func_working_mkstemp" in
*yes) ;;
*)
REPLACE_MKSTEMP=1
;;
esac
else
HAVE_MKSTEMP=0
fi
])
# Prerequisites of lib/mkstemp.c.
AC_DEFUN([gl_PREREQ_MKSTEMP],
[
])

View file

@ -51,7 +51,7 @@
;; emacs: (put 'call-with-output-file/atomic 'scheme-indent-function 1)
(define* (call-with-output-file/atomic filename proc #:optional reference)
(let* ((template (string-append filename ".XXXXXX"))
(tmp (mkstemp! template)))
(tmp (mkstemp! template "wb")))
(call-once
(lambda ()
(with-throw-handler #t