From 0911b4a28215d6ac7aa35d8bfb40733fbb76774e Mon Sep 17 00:00:00 2001 From: John Cupitt Date: Thu, 7 Sep 2017 09:09:19 +0100 Subject: [PATCH] attempt to reset create on Windows When writing a file on Windows, you need to set the create date or the date might be copied over from a previous file of the same name. Quick hack, might be enough to fix this. See https://github.com/jcupitt/libvips/issues/729 --- libvips/iofuncs/util.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/libvips/iofuncs/util.c b/libvips/iofuncs/util.c index 1e08c2b8..a9882485 100644 --- a/libvips/iofuncs/util.c +++ b/libvips/iofuncs/util.c @@ -543,6 +543,29 @@ vips__write( int fd, const void *buf, size_t count ) return( 0 ); } +#ifdef OS_WIN32 +/* Deletes and resets the create date on the named file. This is necessary on + * Windows, or the create date may be copied over from an existing file of the + * same name. + * + * See https://blogs.msdn.microsoft.com/oldnewthing/20050715-14/?p=34923 + */ +int +vips__delete_create( const char *path16 ) +{ + HANDLE handle; + + /* Even though we're only writing, ask for read as well to + * speed up operation over network shares. + */ + handle = CreateFileW( path16, GENERIC_READ | GENERIC_WRITE, + 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); + + if( handle != INVALID_HANDLE_VALUE ) + CloseHandle( handle ); +} +#endif /*OS_WIN32*/ + /* open() with a utf8 filename, setting errno. */ int @@ -566,6 +589,9 @@ vips__open( const char *filename, int flags, ... ) return( -1 ); } + if( mode & O_CREAT ) + vips__delete_create( path16 ); + fd = _wopen( path16, flags, mode ); g_free( path16 ); @@ -606,6 +632,9 @@ vips__fopen( const char *filename, const char *mode ) return( NULL ); } + if( mode[0] == 'w' ) + vips__delete_create( path16 ); + fp = _wfopen( path16, mode16 ); g_free( path16 );