use O_TMPFILE, if available

If O_TNMPFILE is available, use it. This is a linux extension that
creates an unlinked file, so it'll be closed by the system when the last
associated fd is closed.

see https://github.com/jcupitt/libvips/pull/930
This commit is contained in:
John Cupitt 2018-04-09 13:27:57 +01:00
parent 1e647a2af4
commit 06802e4110
2 changed files with 33 additions and 6 deletions

View File

@ -12,6 +12,7 @@
- drop incompatible ICC profiles before save
- better hasalpha rules
- create funcs always make MULTIBAND (ie. no alpha)
- use O_TMPFILE, if available [Alexander--]
12/3/18 started 8.6.4
- better fitting of fonts with overhanging edges, thanks Adrià

View File

@ -21,6 +21,8 @@
* - use expat for xml read, printf for xml write
* 16/8/17
* - validate strs as being utf-8 before we write
* 9/4/18 Alexander--
* - use O_TMPFILE, if available
*/
/*
@ -55,6 +57,10 @@
#define DEBUG
*/
/* Enable linux extensions like O_TMPFILE, if available.
*/
#define _GNU_SOURCE
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
@ -69,12 +75,12 @@
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_SYS_FILE_H
#include <sys/file.h>
#endif /*HAVE_SYS_FILE_H*/
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /*HAVE_UNISTD_H*/
@ -181,8 +187,26 @@ vips__open_image_write( const char *filename, gboolean temp )
flags = MODE_WRITE;
#ifdef O_TMPFILE
/* Linux-only extension creates an unlinked file. CREAT and TRUNC must
* be clear. The filename arg to open() must name a directory.
*/
if( temp ) {
char *dirname;
flags |= O_TMPFILE;
flags &= ~O_CREAT;
flags &= ~O_TRUNC;
dirname = g_path_get_dirname( filename );
fd = vips_tracked_open( dirname, flags, 0666 );
g_free( dirname );
}
else
fd = vips_tracked_open( filename, flags, 0666 );
#else /*!O_TMPFILE*/
#ifdef _O_TEMPORARY
/* On Windows, setting O_TEMP gets the file automatically
/* On Windows, setting _O_TEMPORARY gets the file automatically
* deleted on process exit, even if the processes crashes. See
* vips_image_rewind() for what we do to help on *nix.
*/
@ -190,10 +214,12 @@ vips__open_image_write( const char *filename, gboolean temp )
flags |= _O_TEMPORARY;
#endif /*_O_TEMPORARY*/
if( (fd = vips_tracked_open( filename, flags, 0666 )) < 0 ) {
fd = vips_tracked_open( filename, flags, 0666 );
#endif /*O_TMPFILE*/
if( fd < 0 ) {
vips_error_system( errno, "VipsImage",
_( "unable to write to \"%s\"" ),
filename );
_( "unable to write to \"%s\"" ), filename );
return( -1 );
}