|
10 | 10 | * Win32 (NT, Win2k, XP). replace() doesn't work on Win95/98/Me.
|
11 | 11 | *
|
12 | 12 | * IDENTIFICATION
|
13 |
| - * $PostgreSQL: pgsql/src/port/dirmod.c,v 1.13 2004/08/01 06:19:26 momjian Exp $ |
| 13 | + * $PostgreSQL: pgsql/src/port/dirmod.c,v 1.14 2004/08/07 21:48:09 momjian Exp $ |
14 | 14 | *
|
15 | 15 | *-------------------------------------------------------------------------
|
16 | 16 | */
|
|
33 | 33 |
|
34 | 34 |
|
35 | 35 | #include "miscadmin.h"
|
| 36 | +#include <winioctl.h> |
36 | 37 |
|
37 | 38 | #undef rename
|
38 | 39 | #undef unlink
|
39 | 40 |
|
| 41 | +/* |
| 42 | + * pgrename |
| 43 | + */ |
40 | 44 | int
|
41 | 45 | pgrename(const char *from, const char *to)
|
42 | 46 | {
|
@@ -79,6 +83,9 @@ pgrename(const char *from, const char *to)
|
79 | 83 | }
|
80 | 84 |
|
81 | 85 |
|
| 86 | +/* |
| 87 | + * pgunlink |
| 88 | + */ |
82 | 89 | int
|
83 | 90 | pgunlink(const char *path)
|
84 | 91 | {
|
@@ -110,12 +117,119 @@ pgunlink(const char *path)
|
110 | 117 | return 0;
|
111 | 118 | }
|
112 | 119 |
|
| 120 | + |
| 121 | +/* |
| 122 | + * pgsymlink support: |
| 123 | + * |
| 124 | + * This struct is a replacement for REPARSE_DATA_BUFFER which is defined in VC6 winnt.h |
| 125 | + * but omitted in later SDK functions. |
| 126 | + * We only need the SymbolicLinkReparseBuffer part of the original struct's union. |
| 127 | + */ |
| 128 | +typedef struct |
| 129 | +{ |
| 130 | + DWORD ReparseTag; |
| 131 | + WORD ReparseDataLength; |
| 132 | + WORD Reserved; |
| 133 | + /* SymbolicLinkReparseBuffer */ |
| 134 | + WORD SubstituteNameOffset; |
| 135 | + WORD SubstituteNameLength; |
| 136 | + WORD PrintNameOffset; |
| 137 | + WORD PrintNameLength; |
| 138 | + WCHAR PathBuffer[1]; |
| 139 | +} |
| 140 | +REPARSE_JUNCTION_DATA_BUFFER; |
| 141 | + |
| 142 | +#define REPARSE_JUNCTION_DATA_BUFFER_HEADER_SIZE \ |
| 143 | + FIELD_OFFSET(REPARSE_JUNCTION_DATA_BUFFER, SubstituteNameOffset) |
| 144 | + |
| 145 | + |
| 146 | +/* |
| 147 | + * pgsymlink - uses Win32 junction points |
| 148 | + * |
| 149 | + * For reference: http://www.codeproject.com/w2k/junctionpoints.asp |
| 150 | + */ |
| 151 | +int |
| 152 | +pgsymlink(const char *oldpath, const char *newpath) |
| 153 | +{ |
| 154 | + HANDLE dirhandle; |
| 155 | + DWORD len; |
| 156 | + char *p = nativeTarget; |
| 157 | + char buffer[MAX_PATH*sizeof(WCHAR) + sizeof(REPARSE_JUNCTION_DATA_BUFFER)]; |
| 158 | + char nativeTarget[MAX_PATH]; |
| 159 | + REPARSE_JUNCTION_DATA_BUFFER *reparseBuf = (REPARSE_JUNCTION_DATA_BUFFER*)buffer; |
| 160 | + |
| 161 | + CreateDirectory(newpath, 0); |
| 162 | + dirhandle = CreateFile(newpath, GENERIC_READ | GENERIC_WRITE, |
| 163 | + 0, 0, OPEN_EXISTING, |
| 164 | + FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, 0); |
| 165 | + |
| 166 | + if (dirhandle == INVALID_HANDLE_VALUE) |
| 167 | + return -1; |
| 168 | + |
| 169 | + /* make sure we have an unparsed native win32 path */ |
| 170 | + if (memcmp("\\??\\", oldpath, 4)) |
| 171 | + sprintf(nativeTarget, "\\??\\%s", oldpath); |
| 172 | + else |
| 173 | + strcpy(nativeTarget, oldpath); |
| 174 | + |
| 175 | + while ((p = strchr(p, '/')) != 0) |
| 176 | + *p++ = '\\'; |
| 177 | + |
| 178 | + len = strlen(nativeTarget) * sizeof(WCHAR); |
| 179 | + reparseBuf->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT; |
| 180 | + reparseBuf->ReparseDataLength = len + 12; |
| 181 | + reparseBuf->Reserved = 0; |
| 182 | + reparseBuf->SubstituteNameOffset = 0; |
| 183 | + reparseBuf->SubstituteNameLength = len; |
| 184 | + reparseBuf->PrintNameOffset = len+sizeof(WCHAR); |
| 185 | + reparseBuf->PrintNameLength = 0; |
| 186 | + MultiByteToWideChar(CP_ACP, 0, nativeTarget, -1, |
| 187 | + reparseBuf->PathBuffer, MAX_PATH); |
| 188 | + |
| 189 | + /* |
| 190 | + * FSCTL_SET_REPARSE_POINT is coded differently depending on SDK version; |
| 191 | + * we use our own definition |
| 192 | + */ |
| 193 | + if (!DeviceIoControl(dirhandle, |
| 194 | + CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 41, METHOD_BUFFERED, FILE_ANY_ACCESS), |
| 195 | + reparseBuf, |
| 196 | + reparseBuf->ReparseDataLength + REPARSE_JUNCTION_DATA_BUFFER_HEADER_SIZE, |
| 197 | + 0, 0, &len, 0)) |
| 198 | + { |
| 199 | + LPSTR msg; |
| 200 | + |
| 201 | + errno=0; |
| 202 | + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
| 203 | + NULL, GetLastError(), |
| 204 | + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 205 | + (LPSTR)&msg, 0, NULL ); |
| 206 | + ereport(ERROR, (errcode_for_file_access(), |
| 207 | + errmsg("Error setting junction for %s: %s", nativeTarget, msg))); |
| 208 | + |
| 209 | + LocalFree(msg); |
| 210 | + |
| 211 | + CloseHandle(dirhandle); |
| 212 | + RemoveDirectory(newpath); |
| 213 | + return -1; |
| 214 | + } |
| 215 | + |
| 216 | + CloseHandle(dirhandle); |
| 217 | + |
| 218 | + return 0; |
| 219 | +} |
| 220 | + |
113 | 221 | #endif
|
114 | 222 |
|
| 223 | + |
| 224 | +/* ---------------- |
| 225 | + * rmtree routines |
| 226 | + * ---------------- |
| 227 | + */ |
| 228 | + |
| 229 | + |
| 230 | +/* We undefined these above, so we redefine them */ |
115 | 231 | #if defined(WIN32) || defined(__CYGWIN__)
|
116 |
| -#define rmt_unlink(path) pgunlink(path) |
117 |
| -#else |
118 |
| -#define rmt_unlink(path) unlink(path) |
| 232 | +#define unlink(path) pgunlink(path) |
119 | 233 | #endif
|
120 | 234 |
|
121 | 235 | #ifdef FRONTEND
|
@@ -175,16 +289,15 @@ rmt_cleanup(char ** filenames)
|
175 | 289 | xfree(filenames);
|
176 | 290 | }
|
177 | 291 |
|
178 |
| - |
179 |
| - |
180 | 292 | /*
|
181 |
| - * delete a directory tree recursively |
182 |
| - * assumes path points to a valid directory |
183 |
| - * deletes everything under path |
184 |
| - * if rmtopdir is true deletes the directory too |
| 293 | + * rmtree |
| 294 | + * |
| 295 | + * Delete a directory tree recursively. |
| 296 | + * Assumes path points to a valid directory. |
| 297 | + * Deletes everything under path. |
| 298 | + * If rmtopdir is true deletes the directory too. |
185 | 299 | *
|
186 | 300 | */
|
187 |
| - |
188 | 301 | bool
|
189 | 302 | rmtree(char *path, bool rmtopdir)
|
190 | 303 | {
|
@@ -249,7 +362,7 @@ rmtree(char *path, bool rmtopdir)
|
249 | 362 | }
|
250 | 363 | else
|
251 | 364 | {
|
252 |
| - if (rmt_unlink(filepath) != 0) |
| 365 | + if (unlink(filepath) != 0) |
253 | 366 | {
|
254 | 367 | rmt_cleanup(filenames);
|
255 | 368 | return false;
|
|
0 commit comments