2 * Copyright (c) 2009 Michihiro NAKAJIMA
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #if defined(_WIN32) && !defined(__CYGWIN__)
30 #include "cpio_platform.h"
36 #ifdef HAVE_SYS_UTIME_H
37 #include <sys/utime.h>
49 #define EPOC_TIME (116444736000000000ULL)
51 static void cpio_dosmaperr(unsigned long);
54 * Prepend "\\?\" to the path name and convert it to unicode to permit
55 * an extended-length path for a maximum total path length of 32767
57 * see also http://msdn.microsoft.com/en-us/library/aa365247.aspx
60 permissive_name(const char *name
)
64 DWORD l
, len
, slen
, alloclen
;
67 len
= (DWORD
)strlen(name
);
68 wn
= malloc((len
+ 1) * sizeof(wchar_t));
71 l
= MultiByteToWideChar(CP_ACP
, 0, name
, len
, wn
, len
);
78 /* Get a full path names */
79 l
= GetFullPathNameW(wn
, 0, NULL
, NULL
);
84 wnp
= malloc(l
* sizeof(wchar_t));
89 len
= GetFullPathNameW(wn
, l
, wnp
, NULL
);
93 if (wnp
[0] == L
'\\' && wnp
[1] == L
'\\' &&
94 wnp
[2] == L
'?' && wnp
[3] == L
'\\')
95 /* We have already permissive names. */
98 if (wnp
[0] == L
'\\' && wnp
[1] == L
'\\' &&
99 wnp
[2] == L
'.' && wnp
[3] == L
'\\') {
101 if (((wnp
[4] >= L
'a' && wnp
[4] <= L
'z') ||
102 (wnp
[4] >= L
'A' && wnp
[4] <= L
'Z')) &&
103 wnp
[5] == L
':' && wnp
[6] == L
'\\')
104 wnp
[2] = L
'?';/* Not device names. */
109 if (wnp
[0] == L
'\\' && wnp
[1] == L
'\\' && wnp
[2] != L
'\\') {
110 wchar_t *p
= &wnp
[2];
112 /* Skip server-name letters. */
113 while (*p
!= L
'\\' && *p
!= L
'\0')
117 /* Skip share-name letters. */
118 while (*p
!= L
'\\' && *p
!= L
'\0')
120 if (*p
== L
'\\' && p
!= rp
) {
121 /* Now, match patterns such as
122 * "\\server-name\share-name\" */
130 alloclen
= slen
= 4 + (unc
* 4) + len
+ 1;
131 ws
= wsp
= malloc(slen
* sizeof(wchar_t));
137 wcsncpy(wsp
, L
"\\\\?\\", 4);
141 /* append "UNC\" ---> "\\?\UNC\" */
142 wcsncpy(wsp
, L
"UNC\\", 4);
146 wcsncpy(wsp
, wnp
, slen
);
148 ws
[alloclen
- 1] = L
'\0';
153 cpio_CreateFile(const char *path
, DWORD dwDesiredAccess
, DWORD dwShareMode
,
154 LPSECURITY_ATTRIBUTES lpSecurityAttributes
, DWORD dwCreationDisposition
,
155 DWORD dwFlagsAndAttributes
, HANDLE hTemplateFile
)
160 handle
= CreateFileA(path
, dwDesiredAccess
, dwShareMode
,
161 lpSecurityAttributes
, dwCreationDisposition
, dwFlagsAndAttributes
,
163 if (handle
!= INVALID_HANDLE_VALUE
)
165 if (GetLastError() != ERROR_PATH_NOT_FOUND
)
167 wpath
= permissive_name(path
);
170 handle
= CreateFileW(wpath
, dwDesiredAccess
, dwShareMode
,
171 lpSecurityAttributes
, dwCreationDisposition
, dwFlagsAndAttributes
,
177 #define WINTIME(sec, usec) ((Int32x32To64(sec, 10000000) + EPOC_TIME) + (usec * 10))
179 __hutimes(HANDLE handle
, const struct __timeval
*times
)
181 ULARGE_INTEGER wintm
;
182 FILETIME fatime
, fmtime
;
184 wintm
.QuadPart
= WINTIME(times
[0].tv_sec
, times
[0].tv_usec
);
185 fatime
.dwLowDateTime
= wintm
.LowPart
;
186 fatime
.dwHighDateTime
= wintm
.HighPart
;
187 wintm
.QuadPart
= WINTIME(times
[1].tv_sec
, times
[1].tv_usec
);
188 fmtime
.dwLowDateTime
= wintm
.LowPart
;
189 fmtime
.dwHighDateTime
= wintm
.HighPart
;
190 if (SetFileTime(handle
, NULL
, &fatime
, &fmtime
) == 0) {
198 futimes(int fd
, const struct __timeval
*times
)
201 return (__hutimes((HANDLE
)_get_osfhandle(fd
), times
));
205 utimes(const char *name
, const struct __timeval
*times
)
210 handle
= cpio_CreateFile(name
, GENERIC_READ
| GENERIC_WRITE
,
211 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
,
212 FILE_FLAG_BACKUP_SEMANTICS
, NULL
);
213 if (handle
== INVALID_HANDLE_VALUE
) {
214 cpio_dosmaperr(GetLastError());
217 ret
= __hutimes(handle
, times
);
223 * The following function was modified from PostgreSQL sources and is
224 * subject to the copyright below.
226 /*-------------------------------------------------------------------------
229 * Map win32 error codes to errno values
231 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
234 * $PostgreSQL: pgsql/src/port/win32error.c,v 1.4 2008/01/01 19:46:00 momjian Exp $
236 *-------------------------------------------------------------------------
239 PostgreSQL Database Management System
240 (formerly known as Postgres, then as Postgres95)
242 Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
244 Portions Copyright (c) 1994, The Regents of the University of California
246 Permission to use, copy, modify, and distribute this software and its
247 documentation for any purpose, without fee, and without a written agreement
248 is hereby granted, provided that the above copyright notice and this
249 paragraph and the following two paragraphs appear in all copies.
251 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
252 DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
253 LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
254 DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
255 POSSIBILITY OF SUCH DAMAGE.
257 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
258 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
259 AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
260 ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
261 PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
264 static const struct {
269 { ERROR_INVALID_FUNCTION
, EINVAL
},
270 { ERROR_FILE_NOT_FOUND
, ENOENT
},
271 { ERROR_PATH_NOT_FOUND
, ENOENT
},
272 { ERROR_TOO_MANY_OPEN_FILES
, EMFILE
},
273 { ERROR_ACCESS_DENIED
, EACCES
},
274 { ERROR_INVALID_HANDLE
, EBADF
},
275 { ERROR_ARENA_TRASHED
, ENOMEM
},
276 { ERROR_NOT_ENOUGH_MEMORY
, ENOMEM
},
277 { ERROR_INVALID_BLOCK
, ENOMEM
},
278 { ERROR_BAD_ENVIRONMENT
, E2BIG
},
279 { ERROR_BAD_FORMAT
, ENOEXEC
},
280 { ERROR_INVALID_ACCESS
, EINVAL
},
281 { ERROR_INVALID_DATA
, EINVAL
},
282 { ERROR_INVALID_DRIVE
, ENOENT
},
283 { ERROR_CURRENT_DIRECTORY
, EACCES
},
284 { ERROR_NOT_SAME_DEVICE
, EXDEV
},
285 { ERROR_NO_MORE_FILES
, ENOENT
},
286 { ERROR_LOCK_VIOLATION
, EACCES
},
287 { ERROR_SHARING_VIOLATION
, EACCES
},
288 { ERROR_BAD_NETPATH
, ENOENT
},
289 { ERROR_NETWORK_ACCESS_DENIED
, EACCES
},
290 { ERROR_BAD_NET_NAME
, ENOENT
},
291 { ERROR_FILE_EXISTS
, EEXIST
},
292 { ERROR_CANNOT_MAKE
, EACCES
},
293 { ERROR_FAIL_I24
, EACCES
},
294 { ERROR_INVALID_PARAMETER
, EINVAL
},
295 { ERROR_NO_PROC_SLOTS
, EAGAIN
},
296 { ERROR_DRIVE_LOCKED
, EACCES
},
297 { ERROR_BROKEN_PIPE
, EPIPE
},
298 { ERROR_DISK_FULL
, ENOSPC
},
299 { ERROR_INVALID_TARGET_HANDLE
, EBADF
},
300 { ERROR_INVALID_HANDLE
, EINVAL
},
301 { ERROR_WAIT_NO_CHILDREN
, ECHILD
},
302 { ERROR_CHILD_NOT_COMPLETE
, ECHILD
},
303 { ERROR_DIRECT_ACCESS_HANDLE
, EBADF
},
304 { ERROR_NEGATIVE_SEEK
, EINVAL
},
305 { ERROR_SEEK_ON_DEVICE
, EACCES
},
306 { ERROR_DIR_NOT_EMPTY
, ENOTEMPTY
},
307 { ERROR_NOT_LOCKED
, EACCES
},
308 { ERROR_BAD_PATHNAME
, ENOENT
},
309 { ERROR_MAX_THRDS_REACHED
, EAGAIN
},
310 { ERROR_LOCK_FAILED
, EACCES
},
311 { ERROR_ALREADY_EXISTS
, EEXIST
},
312 { ERROR_FILENAME_EXCED_RANGE
, ENOENT
},
313 { ERROR_NESTING_NOT_ALLOWED
, EAGAIN
},
314 { ERROR_NOT_ENOUGH_QUOTA
, ENOMEM
}
318 cpio_dosmaperr(unsigned long e
)
327 for (i
= 0; i
< sizeof(doserrors
); i
++) {
328 if (doserrors
[i
].winerr
== e
) {
329 errno
= doserrors
[i
].doserr
;
334 /* fprintf(stderr, "unrecognized win32 error code: %lu", e); */