Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b8fd675

Browse files
committed
Allow unlink/rename of files open by another process on Win32, using a
special Win32 open flag FILE_SHARE_DELETE. Claudio Natoli
1 parent d6bc594 commit b8fd675

File tree

6 files changed

+110
-6
lines changed

6 files changed

+110
-6
lines changed

configure

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12079,6 +12079,7 @@ esac
1207912079
case $host_os in mingw*)
1208012080
LIBOBJS="$LIBOBJS copydir.$ac_objext"
1208112081
LIBOBJS="$LIBOBJS gettimeofday.$ac_objext"
12082+
LIBOBJS="$LIBOBJS open.$ac_objext"
1208212083
LIBOBJS="$LIBOBJS pipe.$ac_objext"
1208312084
LIBOBJS="$LIBOBJS rand.$ac_objext" ;;
1208412085
esac

configure.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dnl Process this file with autoconf to produce a configure script.
2-
dnl $PostgreSQL: pgsql/configure.in,v 1.321 2004/03/20 16:11:22 momjian Exp $
2+
dnl $PostgreSQL: pgsql/configure.in,v 1.322 2004/03/24 03:54:16 momjian Exp $
33
dnl
44
dnl Developers, please strive to achieve this order:
55
dnl
@@ -907,6 +907,7 @@ esac
907907
case $host_os in mingw*)
908908
AC_LIBOBJ(copydir)
909909
AC_LIBOBJ(gettimeofday)
910+
AC_LIBOBJ(open)
910911
AC_LIBOBJ(pipe)
911912
AC_LIBOBJ(rand) ;;
912913
esac

src/include/port.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/port.h,v 1.22 2004/03/10 21:12:46 momjian Exp $
9+
* $PostgreSQL: pgsql/src/include/port.h,v 1.23 2004/03/24 03:54:16 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -50,6 +50,11 @@ extern int pgunlink(const char *path);
5050
#endif
5151

5252
#ifdef WIN32
53+
54+
/* open() replacement to allow delete of held files */
55+
extern int win32_open(const char*,int,...);
56+
#define open(a,b,...) win32_open(a,b,##__VA_ARGS__)
57+
5358
extern int copydir(char *fromdir, char *todir);
5459

5560
/* Missing rand functions */

src/interfaces/libpq/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# Copyright (c) 1994, Regents of the University of California
66
#
7-
# $PostgreSQL: pgsql/src/interfaces/libpq/Makefile,v 1.99 2004/03/12 04:33:41 momjian Exp $
7+
# $PostgreSQL: pgsql/src/interfaces/libpq/Makefile,v 1.100 2004/03/24 03:54:16 momjian Exp $
88
#
99
#-------------------------------------------------------------------------
1010

@@ -23,7 +23,7 @@ override CPPFLAGS := -I$(srcdir) $(CPPFLAGS) $(THREAD_CPPFLAGS) -DFRONTEND -DSYS
2323
OBJS= fe-auth.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o \
2424
fe-protocol2.o fe-protocol3.o pqexpbuffer.o pqsignal.o fe-secure.o \
2525
dllist.o md5.o ip.o wchar.o encnames.o \
26-
$(filter crypt.o getaddrinfo.o inet_aton.o noblock.o snprintf.o strerror.o path.o thread.o, $(LIBOBJS))
26+
$(filter crypt.o getaddrinfo.o inet_aton.o noblock.o snprintf.o strerror.o open.o path.o thread.o, $(LIBOBJS))
2727
ifeq ($(PORTNAME), win32)
2828
OBJS+=win32.o
2929
endif
@@ -52,7 +52,7 @@ backend_src = $(top_srcdir)/src/backend
5252
# For port modules, this only happens if configure decides the module
5353
# is needed (see filter hack in OBJS, above).
5454

55-
crypt.c getaddrinfo.c inet_aton.c noblock.c snprintf.c strerror.c path.c thread.c: % : $(top_srcdir)/src/port/%
55+
crypt.c getaddrinfo.c inet_aton.c noblock.c snprintf.c strerror.c open.c path.c thread.c: % : $(top_srcdir)/src/port/%
5656
rm -f $@ && $(LN_S) $< .
5757

5858
md5.c ip.c: % : $(backend_src)/libpq/%

src/interfaces/libpq/win32.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#define _strnicmp(a,b,c) strnicmp(a,b,c)
1717
#define _errno errno
1818
#else
19-
#define open(a,b,c) _open(a,b,c)
19+
/* open provided elsewhere */
2020
#define close(a) _close(a)
2121
#define read(a,b,c) _read(a,b,c)
2222
#define write(a,b,c) _write(a,b,c)

src/port/open.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* open.c
4+
* Win32 open() replacement
5+
*
6+
*
7+
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
8+
*
9+
* $PostgreSQL: pgsql/src/port/open.c,v 1.1 2004/03/24 03:54:16 momjian Exp $
10+
*
11+
*-------------------------------------------------------------------------
12+
*/
13+
14+
#ifdef WIN32
15+
16+
#include <windows.h>
17+
#include <fcntl.h>
18+
#include <errno.h>
19+
#include <assert.h>
20+
21+
int openFlagsToCreateFileFlags(int openFlags)
22+
{
23+
switch (openFlags & (O_CREAT|O_TRUNC|O_EXCL))
24+
{
25+
case 0:
26+
case O_EXCL: return OPEN_EXISTING;
27+
28+
case O_CREAT: return OPEN_ALWAYS;
29+
30+
case O_TRUNC:
31+
case O_TRUNC|O_EXCL: return TRUNCATE_EXISTING;
32+
33+
case O_CREAT|O_TRUNC: return CREATE_ALWAYS;
34+
35+
case O_CREAT|O_EXCL:
36+
case O_CREAT|O_TRUNC|O_EXCL: return CREATE_NEW;
37+
}
38+
39+
/* will never get here */
40+
return 0;
41+
}
42+
43+
/*
44+
* - file attribute setting, based on fileMode?
45+
* - handle other flags? (eg FILE_FLAG_NO_BUFFERING/FILE_FLAG_WRITE_THROUGH)
46+
*/
47+
int win32_open(const char* fileName, int fileFlags, ...)
48+
{
49+
int fd;
50+
HANDLE h;
51+
SECURITY_ATTRIBUTES sa;
52+
53+
/* Check that we can handle the request */
54+
assert((fileFlags & ((O_RDONLY|O_WRONLY|O_RDWR) | O_APPEND |
55+
(O_RANDOM|O_SEQUENTIAL|O_TEMPORARY) |
56+
_O_SHORT_LIVED |
57+
(O_CREAT|O_TRUNC|O_EXCL) | (O_TEXT|O_BINARY))) == fileFlags);
58+
59+
sa.nLength=sizeof(sa);
60+
sa.bInheritHandle=TRUE;
61+
sa.lpSecurityDescriptor=NULL;
62+
63+
if ((h = CreateFile(fileName,
64+
/* cannot use O_RDONLY, as it == 0 */
65+
(fileFlags & O_RDWR) ? (GENERIC_WRITE | GENERIC_READ) :
66+
((fileFlags & O_WRONLY) ? GENERIC_WRITE : GENERIC_READ),
67+
(FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE),
68+
&sa,
69+
openFlagsToCreateFileFlags(fileFlags),
70+
FILE_ATTRIBUTE_NORMAL |
71+
((fileFlags & O_RANDOM) ? FILE_FLAG_RANDOM_ACCESS : 0) |
72+
((fileFlags & O_SEQUENTIAL) ? FILE_FLAG_SEQUENTIAL_SCAN : 0) |
73+
((fileFlags & _O_SHORT_LIVED) ? FILE_ATTRIBUTE_TEMPORARY : 0) |
74+
((fileFlags & O_TEMPORARY) ? FILE_FLAG_DELETE_ON_CLOSE : 0),
75+
NULL)) == INVALID_HANDLE_VALUE)
76+
{
77+
switch (GetLastError())
78+
{
79+
/* EMFILE, ENFILE should not occur from CreateFile. */
80+
case ERROR_PATH_NOT_FOUND:
81+
case ERROR_FILE_NOT_FOUND: errno = ENOENT; break;
82+
case ERROR_FILE_EXISTS: errno = EEXIST; break;
83+
case ERROR_ACCESS_DENIED: errno = EACCES; break;
84+
default:
85+
errno = EINVAL;
86+
}
87+
return -1;
88+
}
89+
90+
/* _open_osfhandle will, on error, set errno accordingly */
91+
if ((fd = _open_osfhandle((long)h,fileFlags&O_APPEND)) < 0 ||
92+
(fileFlags&(O_TEXT|O_BINARY) && (_setmode(fd,fileFlags&(O_TEXT|O_BINARY)) < 0)))
93+
CloseHandle(h); /* will not affect errno */
94+
return fd;
95+
}
96+
97+
#endif

0 commit comments

Comments
 (0)