squashfs-tools in Windows using Cygwin

2007-12-14
2013-05-29
  • Rajiv Narayan
    Rajiv Narayan
    2007-12-14

    I've compiled squashfs-tools in Windows using Cygwin. Had to make a
    few changes to get it to compile properly.  I also had to disable a
    few features:

    -Disabled setting of directory permissions while unsquashing
    -Disabled FNM_EXTMATCH flag for -wildcard matching in v3.3

    Attached are a couple of patches against the v3.2 and v3.3 squashfs
    source.  Thought someone might find them useful.

    Disclaimer: Though I was able to compress and expand files in a few
    test cases and the squash filesystems seem to mount correctly under linux,
    I don't think the squashfs API has been tested extensively
    on Windows platforms, so use at your own risk.

    Usage:

    Build Requirements: cygwin, make, gcc, zlib, patchutils

    Download and expand squashfs source files.
    Save patches to squashfs3.x_cygwin.patch

    Apply the patch using:
    cd squashfs3.x/
    patch -p1 < ../squashfs3.x_cygwin.patch

    Build using:
    cd squashfs-tools
    make

    The executables mksquashfs.exe and squashfs.exe require cygwin1.dll in
    order to function outside the cygwin environment.

    Note: cygwin1.dll is GPL'd so cannot be redistributed unless its
    source is also made available.  Using mingw would eliminate the
    dependency on cygwin1.dll. However I haven't had much luck getting
    things to compile using either the -mno-cygwin flag in cygwin or msys.

    ======== v 3.2 patch starts ======

    diff -ruN squashfs3.2/squashfs-tools/Makefile squashfs3.2_cygwin/squashfs-tools/Makefile
    --- squashfs3.2/squashfs-tools/Makefile    2007-01-05 18:35:00.000000000 -0500
    +++ squashfs3.2_cygwin/squashfs-tools/Makefile    2007-12-13 23:58:42.750000000 -0500
    @@ -1,6 +1,6 @@
    INCLUDEDIR = .

    -CFLAGS := -I$(INCLUDEDIR) -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -O2
    +CFLAGS := -I$(INCLUDEDIR) -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -O2 -mwin32

    all: mksquashfs unsquashfs

    @@ -17,3 +17,6 @@
         $(CC) unsquashfs.o -lz -o $@

    unsquashfs.o: unsquashfs.c squashfs_fs.h read_fs.h global.h
    +
    +clean:
    +    rm -f *.o mksquashfs.exe unsquashfs.exe
    \ No newline at end of file
    diff -ruN squashfs3.2/squashfs-tools/mksquashfs.c squashfs3.2_cygwin/squashfs-tools/mksquashfs.c
    --- squashfs3.2/squashfs-tools/mksquashfs.c    2007-01-01 08:07:34.000000000 -0500
    +++ squashfs3.2_cygwin/squashfs-tools/mksquashfs.c    2007-12-13 23:58:42.765625000 -0500
    @@ -46,10 +46,18 @@
    #include <math.h>

    #ifndef linux
    +/*To computes num cpu's in win32*/
    +#ifdef _WIN32
    +/* Fixes ERROR redinition warning */
    +#define NOGDI
    +#include <windows.h>
    +#else
    #define __BYTE_ORDER BYTE_ORDER
    #define __BIG_ENDIAN BIG_ENDIAN
    #define __LITTLE_ENDIAN LITTLE_ENDIAN
    #include <sys/sysctl.h>
    +#endif
    +
    #else
    #include <endian.h>
    #include <sys/sysinfo.h>
    @@ -2679,6 +2687,11 @@

         if(processors == -1) {
    #ifndef linux
    +#ifdef __CYGWIN__
    +        SYSTEM_INFO si;
    +        GetSystemInfo(&si);
    +        processors = si.dwNumberOfProcessors;
    +#else
             int mib[2];
             size_t len = sizeof(processors);

    @@ -2693,6 +2706,7 @@
                 ERROR("Failed to get number of available processors.  Defaulting to 1\n");
                 processors = 1;
             }
    +#endif
    #else
             processors = get_nprocs();
    #endif
    diff -ruN squashfs3.2/squashfs-tools/read_fs.c squashfs3.2_cygwin/squashfs-tools/read_fs.c
    --- squashfs3.2/squashfs-tools/read_fs.c    2007-01-01 08:08:26.000000000 -0500
    +++ squashfs3.2_cygwin/squashfs-tools/read_fs.c    2007-12-13 23:58:42.781250000 -0500
    @@ -36,9 +36,11 @@
    #include <sys/mman.h>

    #ifndef linux
    +#ifndef _WIN32
    #define __BYTE_ORDER BYTE_ORDER
    #define __BIG_ENDIAN BIG_ENDIAN
    #define __LITTLE_ENDIAN LITTLE_ENDIAN
    +#endif
    #else
    #include <endian.h>
    #endif
    diff -ruN squashfs3.2/squashfs-tools/unsquashfs.c squashfs3.2_cygwin/squashfs-tools/unsquashfs.c
    --- squashfs3.2/squashfs-tools/unsquashfs.c    2007-01-01 08:07:46.000000000 -0500
    +++ squashfs3.2_cygwin/squashfs-tools/unsquashfs.c    2007-12-13 23:58:42.796875000 -0500
    @@ -36,9 +36,11 @@
    #include <utime.h>

    #ifndef linux
    +#ifndef _WIN32
    #define __BYTE_ORDER BYTE_ORDER
    #define __BIG_ENDIAN BIG_ENDIAN
    #define __LITTLE_ENDIAN LITTLE_ENDIAN
    +#endif
    #else
    #include <endian.h>
    #endif
    @@ -1187,7 +1189,7 @@
             return FALSE;
         }

    -    if(!lsonly && mkdir(parent_name, (mode_t) dir->mode) == -1 && (!force || errno != EEXIST)) {
    +    if(!lsonly && mkdir(parent_name, 0777) == -1 && (!force || errno != EEXIST)) {
             ERROR("dir_scan: failed to open directory %s, because %s\n", parent_name, strerror(errno));
             return FALSE;
         }

    ====== v 3.2 patch ends ====

    ====== v 3.3 patch starts ====
    diff -ruN squashfs3.3/squashfs-tools/Makefile squashfs3.3_cygwin/squashfs-tools/Makefile
    --- squashfs3.3/squashfs-tools/Makefile    2007-10-24 20:47:08.000000000 -0400
    +++ squashfs3.3_cygwin/squashfs-tools/Makefile    2007-12-13 23:16:06.906250000 -0500
    @@ -1,6 +1,6 @@
    INCLUDEDIR = .

    -CFLAGS := -I$(INCLUDEDIR) -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE -O2
    +CFLAGS := -I$(INCLUDEDIR) -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE -O2 -mwin32

    all: mksquashfs unsquashfs

    @@ -17,3 +17,6 @@
         $(CC) unsquashfs.o -lz -o $@

    unsquashfs.o: unsquashfs.c squashfs_fs.h read_fs.h global.h
    +
    +clean:
    +    rm -f *.o mksquashfs.exe unsquashfs.exe
    \ No newline at end of file
    diff -ruN squashfs3.3/squashfs-tools/mksquashfs.c squashfs3.3_cygwin/squashfs-tools/mksquashfs.c
    --- squashfs3.3/squashfs-tools/mksquashfs.c    2007-11-01 02:48:14.000000000 -0400
    +++ squashfs3.3_cygwin/squashfs-tools/mksquashfs.c    2007-12-13 23:43:23.609375000 -0500
    @@ -48,10 +48,18 @@
    #include <fnmatch.h>

    #ifndef linux
    +/*To computes num cpu's in win32*/
    +#ifdef _WIN32
    +/* Fixes ERROR redinition warning */
    +#define NOGDI
    +#include <windows.h>
    +#else
    #define __BYTE_ORDER BYTE_ORDER
    #define __BIG_ENDIAN BIG_ENDIAN
    #define __LITTLE_ENDIAN LITTLE_ENDIAN
    #include <sys/sysctl.h>
    +#endif
    +
    #else
    #include <endian.h>
    #include <sys/sysinfo.h>
    @@ -2830,6 +2838,11 @@

         if(processors == -1) {
    #ifndef linux
    +#ifdef __CYGWIN__
    +        SYSTEM_INFO si;
    +        GetSystemInfo(&si);
    +        processors = si.dwNumberOfProcessors;
    +#else
             int mib[2];
             size_t len = sizeof(processors);

    @@ -2844,6 +2857,7 @@
                 ERROR("Failed to get number of available processors.  Defaulting to 1\n");
                 processors = 1;
             }
    +#endif
    #else
             processors = get_nprocs();
    #endif
    @@ -3097,9 +3111,10 @@
             struct pathname *path = paths->path[n];

             for(i = 0; i < path->names; i++) {
    +            /*Disabled FNM_EXTMATCH ksh style wildcard matching (unavailable in cygwin) */
                 int match = use_regex ?
                     regexec(path->name[i].preg, name, (size_t) 0, NULL, 0) == 0 :
    -                fnmatch(path->name[i].name, name, FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == 0;
    +                fnmatch(path->name[i].name, name, FNM_PATHNAME|FNM_PERIOD) == 0;

                 if(match && path->name[i].paths == NULL) {
                     /* match on a leaf component, any subdirectories in the
    diff -ruN squashfs3.3/squashfs-tools/read_fs.c squashfs3.3_cygwin/squashfs-tools/read_fs.c
    --- squashfs3.3/squashfs-tools/read_fs.c    2007-08-18 23:30:10.000000000 -0400
    +++ squashfs3.3_cygwin/squashfs-tools/read_fs.c    2007-12-13 23:10:34.656250000 -0500
    @@ -36,9 +36,11 @@
    #include <sys/mman.h>

    #ifndef linux
    +#ifndef _WIN32
    #define __BYTE_ORDER BYTE_ORDER
    #define __BIG_ENDIAN BIG_ENDIAN
    #define __LITTLE_ENDIAN LITTLE_ENDIAN
    +#endif
    #else
    #include <endian.h>
    #endif
    diff -ruN squashfs3.3/squashfs-tools/unsquashfs.c squashfs3.3_cygwin/squashfs-tools/unsquashfs.c
    --- squashfs3.3/squashfs-tools/unsquashfs.c    2007-11-01 02:53:46.000000000 -0400
    +++ squashfs3.3_cygwin/squashfs-tools/unsquashfs.c    2007-12-13 23:43:21.390625000 -0500
    @@ -42,9 +42,11 @@
    #include <fnmatch.h>

    #ifndef linux
    +#ifndef _WIN32
    #define __BYTE_ORDER BYTE_ORDER
    #define __BIG_ENDIAN BIG_ENDIAN
    #define __LITTLE_ENDIAN LITTLE_ENDIAN
    +#endif
    #else
    #include <endian.h>
    #endif
    @@ -1636,9 +1638,10 @@
         for(n = 0; n < paths->count; n++) {
             struct pathname *path = paths->path[n];
             for(i = 0; i < path->names; i++) {
    +            /*Disabled FNM_EXTMATCH ksh style wildcard matching (unavailable in cygwin) */
                 int match = use_regex ?
                     regexec(path->name[i].preg, name, (size_t) 0, NULL, 0) == 0 :
    -                fnmatch(path->name[i].name, name, FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == 0;
    +                fnmatch(path->name[i].name, name, FNM_PATHNAME|FNM_PERIOD) == 0;
                 if(match && path->name[i].paths == NULL)
                     /* match on a leaf component, any subdirectories will
                      * implicitly match, therefore return an empty new search set */
    @@ -1683,7 +1686,7 @@
             return FALSE;
         }

    -    if(!lsonly && mkdir(parent_name, (mode_t) dir->mode) == -1 && (!force || errno != EEXIST)) {
    +    if(!lsonly && mkdir(parent_name, 0777) == -1 && (!force || errno != EEXIST)) {
             ERROR("dir_scan: failed to open directory %s, because %s\n", parent_name, strerror(errno));
             return FALSE;
         }

    ====== v 3.2 patch ends ====