1 /*-
2  * Copyright (c) 2009 Michihiro NAKAJIMA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24  *
25  * $FreeBSD$
26  */
27 
28 #if defined(_WIN32) && !defined(__CYGWIN__)
29 
30 #include "cpio_platform.h"
31 #include <ctype.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <io.h>
35 #include <stddef.h>
36 #ifdef HAVE_SYS_UTIME_H
37 #include <sys/utime.h>
38 #endif
39 #include <sys/stat.h>
40 #include <process.h>
41 #include <stdlib.h>
42 #include <wchar.h>
43 #include <windows.h>
44 #include <sddl.h>
45 
46 #include "cpio.h"
47 #include "err.h"
48 
49 #define EPOC_TIME	(116444736000000000ULL)
50 
51 static void cpio_dosmaperr(unsigned long);
52 
53 /*
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
56  * characters.
57  * see also http://msdn.microsoft.com/en-us/library/aa365247.aspx
58  */
59 static wchar_t *
permissive_name(const char * name)60 permissive_name(const char *name)
61 {
62 	wchar_t *wn, *wnp;
63 	wchar_t *ws, *wsp;
64 	DWORD l, len, slen, alloclen;
65 	int unc;
66 
67 	len = (DWORD)strlen(name);
68 	wn = malloc((len + 1) * sizeof(wchar_t));
69 	if (wn == NULL)
70 		return (NULL);
71 	l = MultiByteToWideChar(CP_ACP, 0, name, len, wn, len);
72 	if (l == 0) {
73 		free(wn);
74 		return (NULL);
75 	}
76 	wn[l] = L'\0';
77 
78 	/* Get a full path names */
79 	l = GetFullPathNameW(wn, 0, NULL, NULL);
80 	if (l == 0) {
81 		free(wn);
82 		return (NULL);
83 	}
84 	wnp = malloc(l * sizeof(wchar_t));
85 	if (wnp == NULL) {
86 		free(wn);
87 		return (NULL);
88 	}
89 	len = GetFullPathNameW(wn, l, wnp, NULL);
90 	free(wn);
91 	wn = wnp;
92 
93 	if (wnp[0] == L'\\' && wnp[1] == L'\\' &&
94 	    wnp[2] == L'?' && wnp[3] == L'\\')
95 		/* We have already permissive names. */
96 		return (wn);
97 
98 	if (wnp[0] == L'\\' && wnp[1] == L'\\' &&
99 		wnp[2] == L'.' && wnp[3] == L'\\') {
100 		/* Device names */
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. */
105 		return (wn);
106 	}
107 
108 	unc = 0;
109 	if (wnp[0] == L'\\' && wnp[1] == L'\\' && wnp[2] != L'\\') {
110 		wchar_t *p = &wnp[2];
111 
112 		/* Skip server-name letters. */
113 		while (*p != L'\\' && *p != L'\0')
114 			++p;
115 		if (*p == L'\\') {
116 			wchar_t *rp = ++p;
117 			/* Skip share-name letters. */
118 			while (*p != L'\\' && *p != L'\0')
119 				++p;
120 			if (*p == L'\\' && p != rp) {
121 				/* Now, match patterns such as
122 				 * "\\server-name\share-name\" */
123 				wnp += 2;
124 				len -= 2;
125 				unc = 1;
126 			}
127 		}
128 	}
129 
130 	alloclen = slen = 4 + (unc * 4) + len + 1;
131 	ws = wsp = malloc(slen * sizeof(wchar_t));
132 	if (ws == NULL) {
133 		free(wn);
134 		return (NULL);
135 	}
136 	/* prepend "\\?\" */
137 	wcsncpy(wsp, L"\\\\?\\", 4);
138 	wsp += 4;
139 	slen -= 4;
140 	if (unc) {
141 		/* append "UNC\" ---> "\\?\UNC\" */
142 		wcsncpy(wsp, L"UNC\\", 4);
143 		wsp += 4;
144 		slen -= 4;
145 	}
146 	wcsncpy(wsp, wnp, slen);
147 	free(wn);
148 	ws[alloclen - 1] = L'\0';
149 	return (ws);
150 }
151 
152 static HANDLE
cpio_CreateFile(const char * path,DWORD dwDesiredAccess,DWORD dwShareMode,LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD dwCreationDisposition,DWORD dwFlagsAndAttributes,HANDLE hTemplateFile)153 cpio_CreateFile(const char *path, DWORD dwDesiredAccess, DWORD dwShareMode,
154     LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
155     DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
156 {
157 	wchar_t *wpath;
158 	HANDLE handle;
159 
160 	handle = CreateFileA(path, dwDesiredAccess, dwShareMode,
161 	    lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
162 	    hTemplateFile);
163 	if (handle != INVALID_HANDLE_VALUE)
164 		return (handle);
165 	if (GetLastError() != ERROR_PATH_NOT_FOUND)
166 		return (handle);
167 	wpath = permissive_name(path);
168 	if (wpath == NULL)
169 		return (handle);
170 	handle = CreateFileW(wpath, dwDesiredAccess, dwShareMode,
171 	    lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
172 	    hTemplateFile);
173 	free(wpath);
174 	return (handle);
175 }
176 
177 #define WINTIME(sec, usec)	((Int32x32To64(sec, 10000000) + EPOC_TIME) + (usec * 10))
178 static int
__hutimes(HANDLE handle,const struct __timeval * times)179 __hutimes(HANDLE handle, const struct __timeval *times)
180 {
181 	ULARGE_INTEGER wintm;
182 	FILETIME fatime, fmtime;
183 
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) {
191 		errno = EINVAL;
192 		return (-1);
193 	}
194 	return (0);
195 }
196 
197 int
futimes(int fd,const struct __timeval * times)198 futimes(int fd, const struct __timeval *times)
199 {
200 
201 	return (__hutimes((HANDLE)_get_osfhandle(fd), times));
202 }
203 
204 int
utimes(const char * name,const struct __timeval * times)205 utimes(const char *name, const struct __timeval *times)
206 {
207 	int ret;
208 	HANDLE handle;
209 
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());
215 		return (-1);
216 	}
217 	ret = __hutimes(handle, times);
218 	CloseHandle(handle);
219 	return (ret);
220 }
221 
222 /*
223  * The following function was modified from PostgreSQL sources and is
224  * subject to the copyright below.
225  */
226 /*-------------------------------------------------------------------------
227  *
228  * win32error.c
229  *	  Map win32 error codes to errno values
230  *
231  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
232  *
233  * IDENTIFICATION
234  *	  $PostgreSQL: pgsql/src/port/win32error.c,v 1.4 2008/01/01 19:46:00 momjian Exp $
235  *
236  *-------------------------------------------------------------------------
237  */
238 /*
239 PostgreSQL Database Management System
240 (formerly known as Postgres, then as Postgres95)
241 
242 Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
243 
244 Portions Copyright (c) 1994, The Regents of the University of California
245 
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.
250 
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.
256 
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.
262 */
263 
264 static const struct {
265 	DWORD		winerr;
266 	int		doserr;
267 } doserrors[] =
268 {
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	}
315 };
316 
317 static void
cpio_dosmaperr(unsigned long e)318 cpio_dosmaperr(unsigned long e)
319 {
320 	int			i;
321 
322 	if (e == 0)	{
323 		errno = 0;
324 		return;
325 	}
326 
327 	for (i = 0; i < (int)sizeof(doserrors); i++) {
328 		if (doserrors[i].winerr == e) {
329 			errno = doserrors[i].doserr;
330 			return;
331 		}
332 	}
333 
334 	/* fprintf(stderr, "unrecognized win32 error code: %lu", e); */
335 	errno = EINVAL;
336 	return;
337 }
338 #endif
339