Skip to content

Commit 02ef1d7

Browse files
committed
Initial Windows support (madler#62), still need to update CMake
1 parent b11156c commit 02ef1d7

File tree

456 files changed

+87991
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

456 files changed

+87991
-12
lines changed

src/pigz.c

100644100755
Lines changed: 109 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,14 @@
321321
buffers to about the same number.
322322
*/
323323

324+
#if defined(_MSC_VER)
325+
//#include <cstdint>
326+
#include <BaseTsd.h>
327+
//#include <winbase.h>
328+
typedef unsigned short uint_least16_t;
329+
typedef SSIZE_T ssize_t;
330+
#endif
331+
324332
// Use large file functions if available.
325333
#define _FILE_OFFSET_BITS 64
326334

@@ -341,13 +349,19 @@
341349
#include <sys/types.h> // ssize_t
342350
#include <sys/stat.h> // chmod(), stat(), fstat(), lstat(), struct stat,
343351
// S_IFDIR, S_IFLNK, S_IFMT, S_IFREG
344-
#include <sys/time.h> // utimes(), gettimeofday(), struct timeval
345-
#include <unistd.h> // unlink(), _exit(), read(), write(), close(),
346-
// lseek(), isatty(), chown(), fsync()
347-
#include <fcntl.h> // open(), O_CREAT, O_EXCL, O_RDONLY, O_TRUNC,
348-
// O_WRONLY, fcntl(), F_FULLFSYNC
352+
#ifndef _MSC_VER
353+
#include <sys/time.h> // utimes(), gettimeofday(), struct timeval
354+
#include <unistd.h> // unlink(), _exit(), read(), write(), close(),
355+
// lseek(), isatty(), chown(), fsync()
356+
357+
#else
358+
#include <winsock2.h>
359+
#include <pthread.h>
360+
#endif
349361
#include <dirent.h> // opendir(), readdir(), closedir(), DIR,
350362
// struct dirent
363+
#include <fcntl.h> // open(), O_CREAT, O_EXCL, O_RDONLY, O_TRUNC,
364+
// O_WRONLY, fcntl(), F_FULLFSYNC
351365
#include <limits.h> // UINT_MAX, INT_MAX
352366
#if __STDC_VERSION__-0 >= 199901L || __GNUC__-0 >= 3
353367
# include <inttypes.h> // intmax_t, uintmax_t
@@ -380,6 +394,13 @@
380394
# define S_IFLNK 0
381395
#endif
382396

397+
#ifdef _MSC_VER
398+
# define chown(p,o,g) 0
399+
# define utimes(p,t) 0
400+
# define lstat(p,s) stat(p,s)
401+
# define _exit(s) exit(s)
402+
#endif
403+
383404
#ifdef __MINGW32__
384405
# define chown(p,o,g) 0
385406
# define utimes(p,t) 0
@@ -404,6 +425,8 @@
404425
#endif
405426

406427
#ifndef NOZOPFLI
428+
429+
407430
# include "zopfli/src/zopfli/deflate.h" // ZopfliDeflatePart(),
408431
// ZopfliInitOptions(),
409432
// ZopfliOptions
@@ -661,15 +684,17 @@ local void zlib_free(voidpf opaque, voidpf address) {
661684

662685
#define REALLOC(p, s) realloc_track(&mem_track, p, s)
663686
#define FREE(p) free_track(&mem_track, p)
664-
#define OPAQUE (&mem_track)
687+
#define OPAQUEz (&mem_track)
688+
689+
665690
#define ZALLOC zlib_alloc
666691
#define ZFREE zlib_free
667692

668693
#else // !PIGZ_DEBUG
669694

670695
#define REALLOC realloc
671696
#define FREE free
672-
#define OPAQUE Z_NULL
697+
#define OPAQUEz Z_NULL
673698
#define ZALLOC Z_NULL
674699
#define ZFREE Z_NULL
675700

@@ -1633,7 +1658,7 @@ local void compress_thread(void *dummy) {
16331658
// initialize the deflate stream for this thread
16341659
strm.zfree = ZFREE;
16351660
strm.zalloc = ZALLOC;
1636-
strm.opaque = OPAQUE;
1661+
strm.opaque = OPAQUEz;
16371662
ret = deflateInit2(&strm, 6, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
16381663
if (ret == Z_MEM_ERROR)
16391664
throw(ENOMEM, "not enough memory");
@@ -2221,7 +2246,7 @@ local void single_compress(int reset) {
22212246
strm = alloc(NULL, sizeof(z_stream));
22222247
strm->zfree = ZFREE;
22232248
strm->zalloc = ZALLOC;
2224-
strm->opaque = OPAQUE;
2249+
strm->opaque = OPAQUEz;
22252250
ret = deflateInit2(strm, 6, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
22262251
if (ret == Z_MEM_ERROR)
22272252
throw(ENOMEM, "not enough memory");
@@ -3257,7 +3282,7 @@ local void infchk(void) {
32573282
g.out_check = CHECK(0L, Z_NULL, 0);
32583283
strm.zalloc = ZALLOC;
32593284
strm.zfree = ZFREE;
3260-
strm.opaque = OPAQUE;
3285+
strm.opaque = OPAQUEz;
32613286
ret = inflateBackInit(&strm, 15, out_buf);
32623287
if (ret == Z_MEM_ERROR)
32633288
throw(ENOMEM, "not enough memory");
@@ -3626,6 +3651,72 @@ local void touch(char *path, time_t t) {
36263651
(void)utimes(path, times);
36273652
}
36283653

3654+
3655+
3656+
#ifdef _MSC_VER
3657+
/* http://ab-initio.mit.edu/octave-Faddeeva/gnulib/lib/fsync.c
3658+
Emulate fsync on platforms that lack it, primarily Windows and
3659+
cross-compilers like MinGW.
3660+
3661+
This is derived from sqlite3 sources.
3662+
http://www.sqlite.org/cvstrac/rlog?f=sqlite/src/os_win.c
3663+
http://www.sqlite.org/copyright.html
3664+
3665+
Written by Richard W.M. Jones <rjones.at.redhat.com>
3666+
3667+
Copyright (C) 2008-2012 Free Software Foundation, Inc.
3668+
3669+
This library is free software; you can redistribute it and/or
3670+
modify it under the terms of the GNU Lesser General Public
3671+
License as published by the Free Software Foundation; either
3672+
version 2.1 of the License, or (at your option) any later version.
3673+
3674+
This library is distributed in the hope that it will be useful,
3675+
but WITHOUT ANY WARRANTY; without even the implied warranty of
3676+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3677+
Lesser General Public License for more details.
3678+
3679+
You should have received a copy of the GNU General Public License
3680+
along with this program. If not, see <http://www.gnu.org/licenses/>. */
3681+
int fsync (int fd) {
3682+
HANDLE h = (HANDLE) _get_osfhandle (fd);
3683+
DWORD err;
3684+
3685+
if (h == INVALID_HANDLE_VALUE)
3686+
{
3687+
errno = EBADF;
3688+
return -1;
3689+
}
3690+
3691+
if (!FlushFileBuffers (h))
3692+
{
3693+
/* Translate some Windows errors into rough approximations of Unix
3694+
* errors. MSDN is useless as usual - in this case it doesn't
3695+
* document the full range of errors.
3696+
*/
3697+
err = GetLastError ();
3698+
switch (err)
3699+
{
3700+
case ERROR_ACCESS_DENIED:
3701+
/* For a read-only handle, fsync should succeed, even though we have
3702+
no way to sync the access-time changes. */
3703+
return 0;
3704+
3705+
/* eg. Trying to fsync a tty. */
3706+
case ERROR_INVALID_HANDLE:
3707+
errno = EINVAL;
3708+
break;
3709+
3710+
default:
3711+
errno = EIO;
3712+
}
3713+
return -1;
3714+
}
3715+
3716+
return 0;
3717+
}
3718+
#endif /* !Windows */
3719+
36293720
// Request that all data buffered by the operating system for g.outd be written
36303721
// to the permanent storage device. If fsync(fd) is used (POSIX), then all of
36313722
// the data is sent to the device, but will likely be buffered in volatile
@@ -3640,10 +3731,16 @@ local void touch(char *path, time_t t) {
36403731
local void out_push(void) {
36413732
if (g.outd == -1)
36423733
return;
3643-
#ifdef F_FULLSYNC
3644-
int ret = fcntl(g.outd, F_FULLSYNC);
3734+
#ifdef _MSC_VER
3735+
//int ret = fcntl(g.outd, F_FULLFSYNC);
3736+
int ret = fsync(g.outd);
36453737
#else
3738+
3739+
#ifdef F_FULLSYNC
3740+
int ret = fcntl(g.outd, F_FULLSYNC);
3741+
#else
36463742
int ret = fsync(g.outd);
3743+
#endif
36473744
#endif
36483745
if (ret == -1)
36493746
throw(errno, "sync error on %s (%s)", g.outf, strerror(errno));

0 commit comments

Comments
 (0)