1/*-
2 * Copyright (c) 2009-2011 Michihiro NAKAJIMA
3 * Copyright (c) 2003-2006 Tim Kientzle
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer
11 *    in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/*
29 * TODO: A lot of stuff in here isn't actually used by libarchive and
30 * can be trimmed out.  Note that this file is used by libarchive and
31 * libarchive_test but nowhere else.  (But note that it gets compiled
32 * with many different Windows environments, including MinGW, Visual
33 * Studio, and Cygwin.  Significant changes should be tested in all three.)
34 */
35
36/*
37 * TODO: Don't use off_t in here.  Use __int64 instead.  Note that
38 * Visual Studio and the Windows SDK define off_t as 32 bits; Win32's
39 * more modern file handling APIs all use __int64 instead of off_t.
40 */
41
42#ifndef LIBARCHIVE_ARCHIVE_WINDOWS_H_INCLUDED
43#define	LIBARCHIVE_ARCHIVE_WINDOWS_H_INCLUDED
44
45#ifndef __LIBARCHIVE_BUILD
46#error This header is only to be used internally to libarchive.
47#endif
48
49/* Start of configuration for native Win32  */
50#ifndef MINGW_HAS_SECURE_API
51#define MINGW_HAS_SECURE_API 1
52#endif
53
54#include <errno.h>
55#define	set_errno(val)	((errno)=val)
56#include <io.h>
57#include <stdlib.h>   //brings in NULL
58#if defined(HAVE_STDINT_H)
59#include <stdint.h>
60#endif
61#include <stdio.h>
62#include <fcntl.h>
63#include <sys/stat.h>
64#include <process.h>
65#include <direct.h>
66#if defined(__MINGW32__) && defined(HAVE_UNISTD_H)
67/* Prevent build error from a type mismatch of ftruncate().
68 * This unistd.h defines it as ftruncate(int, off_t). */
69#include <unistd.h>
70#endif
71#define NOCRYPT
72#include <windows.h>
73//#define	EFTYPE 7
74
75#if defined(__BORLANDC__)
76#pragma warn -8068	/* Constant out of range in comparison. */
77#pragma warn -8072	/* Suspicious pointer arithmetic. */
78#endif
79
80#ifndef NULL
81#ifdef  __cplusplus
82#define	NULL    0
83#else
84#define	NULL    ((void *)0)
85#endif
86#endif
87
88/* Alias the Windows _function to the POSIX equivalent. */
89#define	close		_close
90#define	fcntl(fd, cmd, flg)	/* No operation. */
91#ifndef fileno
92#define	fileno		_fileno
93#endif
94#ifdef fstat
95#undef fstat
96#endif
97#define	fstat		__la_fstat
98#if !defined(__BORLANDC__)
99#ifdef lseek
100#undef lseek
101#endif
102#define	lseek		_lseeki64
103#else
104#define	lseek		__la_lseek
105#define __LA_LSEEK_NEEDED
106#endif
107#define	lstat		__la_stat
108#define	open		__la_open
109#define	read		__la_read
110#if !defined(__BORLANDC__) && !defined(__WATCOMC__)
111#define setmode		_setmode
112#endif
113#define	la_stat(path,stref)		__la_stat(path,stref)
114#if !defined(__WATCOMC__)
115#if !defined(__BORLANDC__)
116#define	strdup		_strdup
117#endif
118#define	tzset		_tzset
119#if !defined(__BORLANDC__)
120#define	umask		_umask
121#endif
122#endif
123#define	waitpid		__la_waitpid
124#define	write		__la_write
125
126#if !defined(__WATCOMC__)
127
128#ifndef O_RDONLY
129#define	O_RDONLY	_O_RDONLY
130#define	O_WRONLY	_O_WRONLY
131#define	O_TRUNC		_O_TRUNC
132#define	O_CREAT		_O_CREAT
133#define	O_EXCL		_O_EXCL
134#define	O_BINARY	_O_BINARY
135#endif
136
137#ifndef _S_IFIFO
138  #define	_S_IFIFO        0010000   /* pipe */
139#endif
140#ifndef _S_IFCHR
141  #define	_S_IFCHR        0020000   /* character special */
142#endif
143#ifndef _S_IFDIR
144  #define	_S_IFDIR        0040000   /* directory */
145#endif
146#ifndef _S_IFBLK
147  #define	_S_IFBLK        0060000   /* block special */
148#endif
149#ifndef _S_IFLNK
150  #define	_S_IFLNK        0120000   /* symbolic link */
151#endif
152#ifndef _S_IFSOCK
153  #define	_S_IFSOCK       0140000   /* socket */
154#endif
155#ifndef	_S_IFREG
156  #define	_S_IFREG        0100000   /* regular */
157#endif
158#ifndef	_S_IFMT
159  #define	_S_IFMT         0170000   /* file type mask */
160#endif
161
162#ifndef S_IFIFO
163#define	S_IFIFO     _S_IFIFO
164#endif
165//#define	S_IFCHR  _S_IFCHR
166//#define	S_IFDIR  _S_IFDIR
167#ifndef S_IFBLK
168#define	S_IFBLK     _S_IFBLK
169#endif
170#ifndef S_IFLNK
171#define	S_IFLNK     _S_IFLNK
172#endif
173#ifndef S_IFSOCK
174#define	S_IFSOCK    _S_IFSOCK
175#endif
176//#define	S_IFREG  _S_IFREG
177//#define	S_IFMT   _S_IFMT
178
179#ifndef S_ISBLK
180#define	S_ISBLK(m)	(((m) & S_IFMT) == S_IFBLK)	/* block special */
181#define	S_ISFIFO(m)	(((m) & S_IFMT) == S_IFIFO)	/* fifo or socket */
182#define	S_ISCHR(m)	(((m) & S_IFMT) == S_IFCHR)	/* char special */
183#define	S_ISDIR(m)	(((m) & S_IFMT) == S_IFDIR)	/* directory */
184#define	S_ISREG(m)	(((m) & S_IFMT) == S_IFREG)	/* regular file */
185#endif
186#define	S_ISLNK(m)  (((m) & S_IFMT) == S_IFLNK) /* Symbolic link */
187#define	S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) /* Socket */
188
189#define	_S_ISUID        0004000   /* set user id on execution */
190#define	_S_ISGID        0002000   /* set group id on execution */
191#define	_S_ISVTX        0001000   /* save swapped text even after use */
192
193#define	S_ISUID        _S_ISUID
194#define	S_ISGID        _S_ISGID
195#define	S_ISVTX        _S_ISVTX
196
197#define	_S_IRWXU	     (_S_IREAD | _S_IWRITE | _S_IEXEC)
198#define	_S_IXUSR	     _S_IEXEC  /* read permission, user */
199#define	_S_IWUSR	     _S_IWRITE /* write permission, user */
200#define	_S_IRUSR	     _S_IREAD  /* execute/search permission, user */
201#define	_S_IRWXG        (_S_IRWXU >> 3)
202#define	_S_IXGRP        (_S_IXUSR >> 3) /* read permission, group */
203#define	_S_IWGRP        (_S_IWUSR >> 3) /* write permission, group */
204#define	_S_IRGRP        (_S_IRUSR >> 3) /* execute/search permission, group */
205#define	_S_IRWXO        (_S_IRWXG >> 3)
206#define	_S_IXOTH        (_S_IXGRP >> 3) /* read permission, other */
207#define	_S_IWOTH        (_S_IWGRP >> 3) /* write permission, other */
208#define	_S_IROTH        (_S_IRGRP  >> 3) /* execute/search permission, other */
209
210#ifndef S_IRWXU
211#define	S_IRWXU	     _S_IRWXU
212#define	S_IXUSR	     _S_IXUSR
213#define	S_IWUSR	     _S_IWUSR
214#define	S_IRUSR	     _S_IRUSR
215#endif
216#ifndef S_IRWXG
217#define	S_IRWXG        _S_IRWXG
218#define	S_IXGRP        _S_IXGRP
219#define	S_IWGRP        _S_IWGRP
220#endif
221#ifndef S_IRGRP
222#define	S_IRGRP        _S_IRGRP
223#endif
224#ifndef S_IRWXO
225#define	S_IRWXO        _S_IRWXO
226#define	S_IXOTH        _S_IXOTH
227#define	S_IWOTH        _S_IWOTH
228#define	S_IROTH        _S_IROTH
229#endif
230
231#endif
232
233#define	F_DUPFD	  	0	/* Duplicate file descriptor.  */
234#define	F_GETFD		1	/* Get file descriptor flags.  */
235#define	F_SETFD		2	/* Set file descriptor flags.  */
236#define	F_GETFL		3	/* Get file status flags.  */
237#define	F_SETFL		4	/* Set file status flags.  */
238#define	F_GETOWN		5	/* Get owner (receiver of SIGIO).  */
239#define	F_SETOWN		6	/* Set owner (receiver of SIGIO).  */
240#define	F_GETLK		7	/* Get record locking info.  */
241#define	F_SETLK		8	/* Set record locking info (non-blocking).  */
242#define	F_SETLKW		9	/* Set record locking info (blocking).  */
243
244/* XXX missing */
245#define	F_GETLK64	7	/* Get record locking info.  */
246#define	F_SETLK64	8	/* Set record locking info (non-blocking).  */
247#define	F_SETLKW64	9	/* Set record locking info (blocking).  */
248
249/* File descriptor flags used with F_GETFD and F_SETFD.  */
250#define	FD_CLOEXEC	1	/* Close on exec.  */
251
252//NOT SURE IF O_NONBLOCK is OK here but at least the 0x0004 flag is not used by anything else...
253#define	O_NONBLOCK 0x0004 /* Non-blocking I/O.  */
254//#define	O_NDELAY   O_NONBLOCK
255
256/* Symbolic constants for the access() function */
257#if !defined(F_OK)
258    #define	R_OK    4       /*  Test for read permission    */
259    #define	W_OK    2       /*  Test for write permission   */
260    #define	X_OK    1       /*  Test for execute permission */
261    #define	F_OK    0       /*  Test for existence of file  */
262#endif
263
264
265/* Replacement POSIX function */
266extern int	 __la_fstat(int fd, struct stat *st);
267extern int	 __la_lstat(const char *path, struct stat *st);
268#if defined(__LA_LSEEK_NEEDED)
269extern __int64	 __la_lseek(int fd, __int64 offset, int whence);
270#endif
271extern int	 __la_open(const char *path, int flags, ...);
272extern ssize_t	 __la_read(int fd, void *buf, size_t nbytes);
273extern int	 __la_stat(const char *path, struct stat *st);
274extern pid_t	 __la_waitpid(HANDLE child, int *status, int option);
275extern ssize_t	 __la_write(int fd, const void *buf, size_t nbytes);
276
277#define _stat64i32(path, st)	__la_stat(path, st)
278#define _stat64(path, st)	__la_stat(path, st)
279/* for status returned by la_waitpid */
280#define WIFEXITED(sts)		((sts & 0x100) == 0)
281#define WEXITSTATUS(sts)	(sts & 0x0FF)
282
283extern wchar_t *__la_win_permissive_name(const char *name);
284extern wchar_t *__la_win_permissive_name_w(const wchar_t *wname);
285extern void __la_dosmaperr(unsigned long e);
286#define la_dosmaperr(e) __la_dosmaperr(e)
287extern struct archive_entry *__la_win_entry_in_posix_pathseparator(
288    struct archive_entry *);
289
290#if defined(HAVE_WCRTOMB) && defined(__BORLANDC__)
291typedef int mbstate_t;
292size_t wcrtomb(char *, wchar_t, mbstate_t *);
293#endif
294
295#if defined(_MSC_VER) && _MSC_VER < 1300
296WINBASEAPI BOOL WINAPI GetVolumePathNameW(
297       LPCWSTR lpszFileName,
298       LPWSTR lpszVolumePathName,
299       DWORD cchBufferLength
300       );
301# if _WIN32_WINNT < 0x0500 /* windows.h not providing 0x500 API */
302typedef struct _FILE_ALLOCATED_RANGE_BUFFER {
303       LARGE_INTEGER FileOffset;
304       LARGE_INTEGER Length;
305} FILE_ALLOCATED_RANGE_BUFFER, *PFILE_ALLOCATED_RANGE_BUFFER;
306#  define FSCTL_SET_SPARSE \
307     CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 49, METHOD_BUFFERED, FILE_WRITE_DATA)
308#  define FSCTL_QUERY_ALLOCATED_RANGES \
309     CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 51,  METHOD_NEITHER, FILE_READ_DATA)
310# endif
311#endif
312
313#endif /* LIBARCHIVE_ARCHIVE_WINDOWS_H_INCLUDED */
314