321
321
buffers to about the same number.
322
322
*/
323
323
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
+
324
332
// Use large file functions if available.
325
333
#define _FILE_OFFSET_BITS 64
326
334
341
349
#include <sys/types.h> // ssize_t
342
350
#include <sys/stat.h> // chmod(), stat(), fstat(), lstat(), struct stat,
343
351
// 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
349
361
#include <dirent.h> // opendir(), readdir(), closedir(), DIR,
350
362
// struct dirent
363
+ #include <fcntl.h> // open(), O_CREAT, O_EXCL, O_RDONLY, O_TRUNC,
364
+ // O_WRONLY, fcntl(), F_FULLFSYNC
351
365
#include <limits.h> // UINT_MAX, INT_MAX
352
366
#if __STDC_VERSION__ - 0 >= 199901L || __GNUC__ - 0 >= 3
353
367
# include <inttypes.h> // intmax_t, uintmax_t
380
394
# define S_IFLNK 0
381
395
#endif
382
396
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
+
383
404
#ifdef __MINGW32__
384
405
# define chown (p ,o ,g ) 0
385
406
# define utimes (p ,t ) 0
404
425
#endif
405
426
406
427
#ifndef NOZOPFLI
428
+
429
+
407
430
# include "zopfli/src/zopfli/deflate.h" // ZopfliDeflatePart(),
408
431
// ZopfliInitOptions(),
409
432
// ZopfliOptions
@@ -661,15 +684,17 @@ local void zlib_free(voidpf opaque, voidpf address) {
661
684
662
685
#define REALLOC (p , s ) realloc_track(&mem_track, p, s)
663
686
#define FREE (p ) free_track(&mem_track, p)
664
- #define OPAQUE (&mem_track)
687
+ #define OPAQUEz (&mem_track)
688
+
689
+
665
690
#define ZALLOC zlib_alloc
666
691
#define ZFREE zlib_free
667
692
668
693
#else // !PIGZ_DEBUG
669
694
670
695
#define REALLOC realloc
671
696
#define FREE free
672
- #define OPAQUE Z_NULL
697
+ #define OPAQUEz Z_NULL
673
698
#define ZALLOC Z_NULL
674
699
#define ZFREE Z_NULL
675
700
@@ -1633,7 +1658,7 @@ local void compress_thread(void *dummy) {
1633
1658
// initialize the deflate stream for this thread
1634
1659
strm .zfree = ZFREE ;
1635
1660
strm .zalloc = ZALLOC ;
1636
- strm .opaque = OPAQUE ;
1661
+ strm .opaque = OPAQUEz ;
1637
1662
ret = deflateInit2 (& strm , 6 , Z_DEFLATED , -15 , 8 , Z_DEFAULT_STRATEGY );
1638
1663
if (ret == Z_MEM_ERROR )
1639
1664
throw (ENOMEM , "not enough memory" );
@@ -2221,7 +2246,7 @@ local void single_compress(int reset) {
2221
2246
strm = alloc (NULL , sizeof (z_stream ));
2222
2247
strm -> zfree = ZFREE ;
2223
2248
strm -> zalloc = ZALLOC ;
2224
- strm -> opaque = OPAQUE ;
2249
+ strm -> opaque = OPAQUEz ;
2225
2250
ret = deflateInit2 (strm , 6 , Z_DEFLATED , -15 , 8 , Z_DEFAULT_STRATEGY );
2226
2251
if (ret == Z_MEM_ERROR )
2227
2252
throw (ENOMEM , "not enough memory" );
@@ -3257,7 +3282,7 @@ local void infchk(void) {
3257
3282
g .out_check = CHECK (0L , Z_NULL , 0 );
3258
3283
strm .zalloc = ZALLOC ;
3259
3284
strm .zfree = ZFREE ;
3260
- strm .opaque = OPAQUE ;
3285
+ strm .opaque = OPAQUEz ;
3261
3286
ret = inflateBackInit (& strm , 15 , out_buf );
3262
3287
if (ret == Z_MEM_ERROR )
3263
3288
throw (ENOMEM , "not enough memory" );
@@ -3626,6 +3651,72 @@ local void touch(char *path, time_t t) {
3626
3651
(void )utimes (path , times );
3627
3652
}
3628
3653
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
+
3629
3720
// Request that all data buffered by the operating system for g.outd be written
3630
3721
// to the permanent storage device. If fsync(fd) is used (POSIX), then all of
3631
3722
// 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) {
3640
3731
local void out_push (void ) {
3641
3732
if (g .outd == -1 )
3642
3733
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 );
3645
3737
#else
3738
+
3739
+ #ifdef F_FULLSYNC
3740
+ int ret = fcntl (g .outd , F_FULLSYNC );
3741
+ #else
3646
3742
int ret = fsync (g .outd );
3743
+ #endif
3647
3744
#endif
3648
3745
if (ret == -1 )
3649
3746
throw (errno , "sync error on %s (%s)" , g .outf , strerror (errno ));
0 commit comments