aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuis Hector Chavez <lhchavez@google.com>2017-12-08 16:33:39 +0000
committerandroid-build-merger <android-build-merger@google.com>2017-12-08 16:33:39 +0000
commit32b2554331bd0352e2770271135034a4b20b0406 (patch)
tree782f7332098292e7788d6609945970d9b4f8ed3c
parenta7a2fb83b15174ee7257401164a974dc555dc1d1 (diff)
parentfa8c3fb3d308912de3cffd80c3aea93ce7c91a92 (diff)
downloadsquashfs-tools-32b2554331bd0352e2770271135034a4b20b0406.tar.gz
Merge remote-tracking branch 'aosp/upstream-master' into 'aosp/master'
am: fa8c3fb3d3 Change-Id: I138b144de30c2c6f80d71b17f2653c084394f891
-rw-r--r--squashfs-tools/Makefile20
-rw-r--r--squashfs-tools/action.c11
-rw-r--r--squashfs-tools/compressor.c8
-rw-r--r--squashfs-tools/fnmatch_compat.h32
-rw-r--r--squashfs-tools/gzip_wrapper.c4
-rw-r--r--squashfs-tools/info.c25
-rw-r--r--squashfs-tools/lz4_wrapper.c13
-rw-r--r--squashfs-tools/lzo_wrapper.c4
-rw-r--r--squashfs-tools/mksquashfs.c160
-rw-r--r--squashfs-tools/mksquashfs.h1
-rw-r--r--squashfs-tools/process_fragments.c1
-rw-r--r--squashfs-tools/pseudo.c52
-rw-r--r--squashfs-tools/pseudo.h5
-rw-r--r--squashfs-tools/read_xattrs.c4
-rw-r--r--squashfs-tools/squashfs_fs.h1
-rw-r--r--squashfs-tools/unsquashfs.c32
-rw-r--r--squashfs-tools/unsquashfs.h5
-rw-r--r--squashfs-tools/unsquashfs_info.c25
-rw-r--r--squashfs-tools/unsquashfs_xattr.c5
-rw-r--r--squashfs-tools/xattr.c15
-rw-r--r--squashfs-tools/xz_wrapper.c4
-rw-r--r--squashfs-tools/zstd_wrapper.c254
-rw-r--r--squashfs-tools/zstd_wrapper.h48
23 files changed, 565 insertions, 164 deletions
diff --git a/squashfs-tools/Makefile b/squashfs-tools/Makefile
index 52d2582..22fc559 100644
--- a/squashfs-tools/Makefile
+++ b/squashfs-tools/Makefile
@@ -75,6 +75,18 @@ GZIP_SUPPORT = 1
#LZMA_SUPPORT = 1
#LZMA_DIR = ../../../../LZMA/lzma465
+
+########### Building ZSTD support ############
+#
+# The ZSTD library is supported
+# ZSTD homepage: http://zstd.net
+# ZSTD source repository: https://github.com/facebook/zstd
+#
+# To build using the ZSTD library - install the library and uncomment the
+# ZSTD_SUPPORT line below.
+#
+#ZSTD_SUPPORT = 1
+
######## Specifying default compression ########
#
# The next line specifies which compression algorithm is used by default
@@ -177,6 +189,14 @@ LIBS += -llz4
COMPRESSORS += lz4
endif
+ifeq ($(ZSTD_SUPPORT),1)
+CFLAGS += -DZSTD_SUPPORT
+MKSQUASHFS_OBJS += zstd_wrapper.o
+UNSQUASHFS_OBJS += zstd_wrapper.o
+LIBS += -lzstd
+COMPRESSORS += zstd
+endif
+
ifeq ($(XATTR_SUPPORT),1)
ifeq ($(XATTR_DEFAULT),1)
CFLAGS += -DXATTR_SUPPORT -DXATTR_DEFAULT
diff --git a/squashfs-tools/action.c b/squashfs-tools/action.c
index 7e43f17..4b06ccb 100644
--- a/squashfs-tools/action.c
+++ b/squashfs-tools/action.c
@@ -31,7 +31,6 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
-#include <fnmatch.h>
#include <pwd.h>
#include <grp.h>
#include <sys/wait.h>
@@ -39,14 +38,11 @@
#include <limits.h>
#include <errno.h>
-#ifndef FNM_EXTMATCH /* glibc extension */
- #define FNM_EXTMATCH 0
-#endif
-
#include "squashfs_fs.h"
#include "mksquashfs.h"
#include "action.h"
#include "error.h"
+#include "fnmatch_compat.h"
/*
* code to parse actions
@@ -2288,12 +2284,9 @@ static char *get_start(char *s, int n)
static int subpathname_fn(struct atom *atom, struct action_data *action_data)
{
- char *path = strdup(action_data->subpath);
- int is_match = fnmatch(atom->argv[0], get_start(path,
+ return fnmatch(atom->argv[0], get_start(strdupa(action_data->subpath),
count_components(atom->argv[0])),
FNM_PATHNAME|FNM_PERIOD|FNM_EXTMATCH) == 0;
- free(path);
- return is_match;
}
/*
diff --git a/squashfs-tools/compressor.c b/squashfs-tools/compressor.c
index 525e316..02b5e90 100644
--- a/squashfs-tools/compressor.c
+++ b/squashfs-tools/compressor.c
@@ -65,6 +65,13 @@ static struct compressor xz_comp_ops = {
extern struct compressor xz_comp_ops;
#endif
+#ifndef ZSTD_SUPPORT
+static struct compressor zstd_comp_ops = {
+ ZSTD_COMPRESSION, "zstd"
+};
+#else
+extern struct compressor zstd_comp_ops;
+#endif
static struct compressor unknown_comp_ops = {
0, "unknown"
@@ -77,6 +84,7 @@ struct compressor *compressor[] = {
&lzo_comp_ops,
&lz4_comp_ops,
&xz_comp_ops,
+ &zstd_comp_ops,
&unknown_comp_ops
};
diff --git a/squashfs-tools/fnmatch_compat.h b/squashfs-tools/fnmatch_compat.h
new file mode 100644
index 0000000..7b4afd8
--- /dev/null
+++ b/squashfs-tools/fnmatch_compat.h
@@ -0,0 +1,32 @@
+#ifndef FNMATCH_COMPAT
+#define FNMATCH_COMPAT
+/*
+ * Squashfs
+ *
+ * Copyright (c) 2015
+ * Phillip Lougher <phillip@squashfs.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * fnmatch_compat.h
+ */
+
+#include <fnmatch.h>
+
+#ifndef FNM_EXTMATCH
+#define FNM_EXTMATCH 0
+#endif
+
+#endif
diff --git a/squashfs-tools/gzip_wrapper.c b/squashfs-tools/gzip_wrapper.c
index 076a587..ac32bb9 100644
--- a/squashfs-tools/gzip_wrapper.c
+++ b/squashfs-tools/gzip_wrapper.c
@@ -276,7 +276,7 @@ failed:
}
-void gzip_display_options(void *buffer, int size)
+static void gzip_display_options(void *buffer, int size)
{
struct gzip_comp_opts *comp_opts = buffer;
int i, printed;
@@ -467,7 +467,7 @@ static int gzip_uncompress(void *d, void *s, int size, int outsize, int *error)
}
-void gzip_usage()
+static void gzip_usage()
{
fprintf(stderr, "\t -Xcompression-level <compression-level>\n");
fprintf(stderr, "\t\t<compression-level> should be 1 .. 9 (default "
diff --git a/squashfs-tools/info.c b/squashfs-tools/info.c
index 60a3f5a..7968c77 100644
--- a/squashfs-tools/info.c
+++ b/squashfs-tools/info.c
@@ -134,22 +134,31 @@ void dump_state()
void *info_thrd(void *arg)
{
sigset_t sigmask;
- int sig, err, waiting = 0;
+ struct timespec timespec = { .tv_sec = 1, .tv_nsec = 0 };
+ int sig, waiting = 0;
sigemptyset(&sigmask);
sigaddset(&sigmask, SIGQUIT);
sigaddset(&sigmask, SIGHUP);
- sigaddset(&sigmask, SIGALRM);
while(1) {
- err = sigwait(&sigmask, &sig);
+ if(waiting)
+ sig = sigtimedwait(&sigmask, NULL, &timespec);
+ else
+ sig = sigwaitinfo(&sigmask, NULL);
- if(err == -1) {
+ if(sig == -1) {
switch(errno) {
+ case EAGAIN:
+ /* interval timed out */
+ waiting = 0;
+ /* FALLTHROUGH */
case EINTR:
+ /* if waiting, the wait will be longer, but
+ that's OK */
continue;
default:
- BAD_ERROR("sigwait failed "
+ BAD_ERROR("sigtimedwait/sigwaitinfo failed "
"because %s\n", strerror(errno));
}
}
@@ -160,12 +169,8 @@ void *info_thrd(void *arg)
/* set one second interval period, if ^\ received
within then, dump queue and cache status */
waiting = 1;
- alarm(1);
- } else if (sig == SIGQUIT) {
+ } else
dump_state();
- } else if (sig == SIGALRM) {
- waiting = 0;
- }
}
}
diff --git a/squashfs-tools/lz4_wrapper.c b/squashfs-tools/lz4_wrapper.c
index 3c24971..71b21a8 100644
--- a/squashfs-tools/lz4_wrapper.c
+++ b/squashfs-tools/lz4_wrapper.c
@@ -184,7 +184,7 @@ failed:
}
-void lz4_display_options(void *buffer, int size)
+static void lz4_display_options(void *buffer, int size)
{
struct lz4_comp_opts *comp_opts = buffer;
@@ -225,11 +225,20 @@ static int lz4_compress(void *strm, void *dest, void *src, int size,
{
int res;
+/* ANDROID CHANGES START*/
+#ifdef ANDROID
if(hc)
res = LZ4_compress_HC(src, dest, size, block_size,
LZ4HC_CLEVEL_DEFAULT);
else
res = LZ4_compress_default(src, dest, size, block_size);
+#else
+ if(hc)
+ res = LZ4_compressHC_limitedOutput(src, dest, size, block_size);
+ else
+ res = LZ4_compress_limitedOutput(src, dest, size, block_size);
+#endif
+/* ANDROID CHANGES END */
if(res == 0) {
/*
@@ -262,7 +271,7 @@ static int lz4_uncompress(void *dest, void *src, int size, int outsize,
}
-void lz4_usage()
+static void lz4_usage()
{
fprintf(stderr, "\t -Xhc\n");
fprintf(stderr, "\t\tCompress using LZ4 High Compression\n");
diff --git a/squashfs-tools/lzo_wrapper.c b/squashfs-tools/lzo_wrapper.c
index 8c9bf95..f367fad 100644
--- a/squashfs-tools/lzo_wrapper.c
+++ b/squashfs-tools/lzo_wrapper.c
@@ -263,7 +263,7 @@ failed:
}
-void lzo_display_options(void *buffer, int size)
+static void lzo_display_options(void *buffer, int size)
{
struct lzo_comp_opts *comp_opts = buffer;
@@ -391,7 +391,7 @@ static int lzo_uncompress(void *dest, void *src, int size, int outsize,
}
-void lzo_usage()
+static void lzo_usage()
{
int i;
diff --git a/squashfs-tools/mksquashfs.c b/squashfs-tools/mksquashfs.c
index 60be156..cbfc731 100644
--- a/squashfs-tools/mksquashfs.c
+++ b/squashfs-tools/mksquashfs.c
@@ -3,7 +3,7 @@
* filesystem.
*
* Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
- * 2012, 2013, 2014
+ * 2012, 2013, 2014, 2017
* Phillip Lougher <phillip@squashfs.org.uk>
*
* This program is free software; you can redistribute it and/or
@@ -46,15 +46,10 @@
#include <sys/mman.h>
#include <pthread.h>
#include <regex.h>
-#include <fnmatch.h>
#include <sys/wait.h>
#include <limits.h>
#include <ctype.h>
-#ifndef FNM_EXTMATCH /* glibc extension */
- #define FNM_EXTMATCH 0
-#endif
-
#ifndef linux
#define __BYTE_ORDER BYTE_ORDER
#define __BIG_ENDIAN BIG_ENDIAN
@@ -97,7 +92,10 @@ FILE *block_map_file = NULL;
#endif
/* ANDROID CHANGES END */
+#include "fnmatch_compat.h"
+
int delete = FALSE;
+int quiet = FALSE;
int fd;
struct squashfs_super_block sBlk;
@@ -118,6 +116,7 @@ int old_exclude = TRUE;
int use_regex = FALSE;
int nopad = FALSE;
int exit_on_error = FALSE;
+static off_t squashfs_start_offset = 0;
long long global_uid = -1, global_gid = -1;
@@ -460,8 +459,7 @@ int mangle2(void *strm, char *d, char *s, int size,
"code %d\n", comp->name, error);
}
- if(c_byte == 0 || c_byte >= size ||
- (c_byte > (size * ((100.0 - compress_thresh_per) / 100.0)))) {
+ if(c_byte == 0 || c_byte >= size) {
memcpy(d, s, size);
return size | (data_block ? SQUASHFS_COMPRESSED_BIT_BLOCK :
SQUASHFS_COMPRESSED_BIT);
@@ -561,9 +559,9 @@ int read_fs_bytes(int fd, long long byte, int bytes, void *buff)
pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
pthread_mutex_lock(&pos_mutex);
- if(lseek(fd, off, SEEK_SET) == -1) {
+ if(lseek(fd, off+squashfs_start_offset, SEEK_SET) == -1) {
ERROR("read_fs_bytes: Lseek on destination failed because %s, "
- "offset=0x%llx\n", strerror(errno), off);
+ "offset=0x%llx\n", strerror(errno), off+squashfs_start_offset);
res = 0;
} else if(read_bytes(fd, buff, bytes) < bytes) {
ERROR("Read on destination failed\n");
@@ -602,10 +600,10 @@ void write_destination(int fd, long long byte, int bytes, void *buff)
pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
pthread_mutex_lock(&pos_mutex);
- if(lseek(fd, off, SEEK_SET) == -1) {
+ if(lseek(fd, off+squashfs_start_offset, SEEK_SET) == -1) {
ERROR("write_destination: Lseek on destination "
"failed because %s, offset=0x%llx\n", strerror(errno),
- off);
+ off+squashfs_start_offset);
BAD_ERROR("Probably out of space on output %s\n",
block_device ? "block device" : "filesystem");
}
@@ -873,13 +871,13 @@ char *subpathname(struct dir_ent *dir_ent)
}
-static inline unsigned int get_inode_no(struct inode_info *inode)
+inline unsigned int get_inode_no(struct inode_info *inode)
{
return inode->inode_number;
}
-static inline unsigned int get_parent_no(struct dir_info *dir)
+inline unsigned int get_parent_no(struct dir_info *dir)
{
return dir->depth ? get_inode_no(dir->dir_ent->inode) : inode_no;
}
@@ -1001,10 +999,8 @@ int create_inode(squashfs_inode *i_no, struct dir_info *dir_info,
"%d, fragment %d, offset %d, size %d\n", byte_size,
start_block, offset, fragment->index, fragment->offset,
fragment->size);
- for(i = 0; i < offset; i++) {
+ for(i = 0; i < offset; i++)
TRACE("Block %d, size %d\n", i, block_list[i]);
- total_size += SQUASHFS_COMPRESSED_SIZE_BLOCK(block_list[i]);
- }
/* ANDROID CHANGES START*/
#ifdef ANDROID
sub_path = subpathname(dir_ent);
@@ -1041,10 +1037,8 @@ int create_inode(squashfs_inode *i_no, struct dir_info *dir_info,
"blocks %d, fragment %d, offset %d, size %d, nlink %d"
"\n", byte_size, start_block, offset, fragment->index,
fragment->offset, fragment->size, nlink);
- for(i = 0; i < offset; i++) {
+ for(i = 0; i < offset; i++)
TRACE("Block %d, size %d\n", i, block_list[i]);
- total_size += SQUASHFS_COMPRESSED_SIZE_BLOCK(block_list[i]);
- }
/* ANDROID CHANGES START*/
#ifdef ANDROID
sub_path = subpathname(dir_ent);
@@ -1526,6 +1520,7 @@ again:
cache_block_put(compressed_buffer);
finished:
+ do { } while (0);
pthread_cleanup_pop(0);
return buffer;
@@ -2142,7 +2137,7 @@ struct file_info *duplicate(long long file_size, long long bytes,
}
-static inline int is_fragment(struct inode_info *inode)
+inline int is_fragment(struct inode_info *inode)
{
off_t file_size = inode->buf.st_size;
@@ -2430,9 +2425,9 @@ void *writer(void *arg)
pthread_cleanup_push((void *) pthread_mutex_unlock, &pos_mutex);
pthread_mutex_lock(&pos_mutex);
- if(lseek(fd, off, SEEK_SET) == -1) {
+ if(lseek(fd, off+squashfs_start_offset, SEEK_SET) == -1) {
ERROR("writer: Lseek on destination failed because "
- "%s, offset=0x%llx\n", strerror(errno), off);
+ "%s, offset=0x%llx\n", strerror(errno), off+squashfs_start_offset);
BAD_ERROR("Probably out of space on output "
"%s\n", block_device ? "block device" :
"filesystem");
@@ -3121,19 +3116,19 @@ struct inode_info *lookup_inode3(struct stat *buf, int pseudo, int id,
}
-static inline struct inode_info *lookup_inode2(struct stat *buf, int pseudo, int id)
+struct inode_info *lookup_inode2(struct stat *buf, int pseudo, int id)
{
return lookup_inode3(buf, pseudo, id, NULL, 0);
}
-static inline struct inode_info *lookup_inode(struct stat *buf)
+inline struct inode_info *lookup_inode(struct stat *buf)
{
return lookup_inode2(buf, 0, 0);
}
-static inline void alloc_inode_no(struct inode_info *inode, unsigned int use_this)
+inline void alloc_inode_no(struct inode_info *inode, unsigned int use_this)
{
if (inode->inode_number == 0) {
inode->inode_number = use_this ? : inode_no ++;
@@ -3144,7 +3139,7 @@ static inline void alloc_inode_no(struct inode_info *inode, unsigned int use_thi
}
-static inline struct dir_ent *create_dir_entry(char *name, char *source_name,
+inline struct dir_ent *create_dir_entry(char *name, char *source_name,
char *nonstandard_pathname, struct dir_info *dir)
{
struct dir_ent *dir_ent = malloc(sizeof(struct dir_ent));
@@ -3167,8 +3162,15 @@ static inline struct dir_ent *create_dir_entry(char *name, char *source_name,
}
+/* ANDROID CHANGES START*/
+#ifdef ANDROID
static inline void add_dir_entry(struct dir_ent *dir_ent, struct dir_info *sub_dir,
struct inode_info *inode_info)
+#else
+inline void add_dir_entry(struct dir_ent *dir_ent, struct dir_info *sub_dir,
+ struct inode_info *inode_info)
+#endif
+/* ANDROID CHANGES END */
{
struct dir_info *dir = dir_ent->our_dir;
@@ -3203,7 +3205,8 @@ static inline void add_dir_entry(struct dir_ent *dir_ent, struct dir_info *sub_d
dir->count++;
}
-static inline void add_dir_entry2(char *name, char *source_name,
+
+inline void add_dir_entry2(char *name, char *source_name,
char *nonstandard_pathname, struct dir_info *sub_dir,
struct inode_info *inode_info, struct dir_info *dir)
{
@@ -3215,7 +3218,7 @@ static inline void add_dir_entry2(char *name, char *source_name,
}
-static inline void free_dir_entry(struct dir_ent *dir_ent)
+inline void free_dir_entry(struct dir_ent *dir_ent)
{
if(dir_ent->name)
free(dir_ent->name);
@@ -3236,7 +3239,7 @@ static inline void free_dir_entry(struct dir_ent *dir_ent)
}
-static inline void add_excluded(struct dir_info *dir)
+inline void add_excluded(struct dir_info *dir)
{
dir->excluded ++;
}
@@ -3757,6 +3760,12 @@ void dir_scan2(struct dir_info *dir, struct pseudo *pseudo)
pseudo_ent->pathname, NULL,
lookup_inode2(&buf, PSEUDO_FILE_PROCESS,
pseudo_ent->dev->pseudo_id), dir);
+ } else if(pseudo_ent->dev->type == 's') {
+ add_dir_entry2(pseudo_ent->name, NULL,
+ pseudo_ent->pathname, NULL,
+ lookup_inode3(&buf, PSEUDO_FILE_OTHER, 0,
+ pseudo_ent->dev->symlink,
+ strlen(pseudo_ent->dev->symlink) + 1), dir);
} else {
add_dir_entry2(pseudo_ent->name, NULL,
pseudo_ent->pathname, NULL,
@@ -4368,7 +4377,6 @@ void initialise_threads(int readq, int fragq, int bwriteq, int fwriteq,
sigemptyset(&sigmask);
sigaddset(&sigmask, SIGQUIT);
sigaddset(&sigmask, SIGHUP);
- sigaddset(&sigmask, SIGALRM);
if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1)
BAD_ERROR("Failed to set signal mask in intialise_threads\n");
@@ -4448,7 +4456,8 @@ void initialise_threads(int readq, int fragq, int bwriteq, int fwriteq,
main_thread = pthread_self();
- printf("Parallel mksquashfs: Using %d processor%s\n", processors,
+ if(!quiet)
+ printf("Parallel mksquashfs: Using %d processor%s\n", processors,
processors == 1 ? "" : "s");
/* Restore the signal mask for the main thread */
@@ -5119,6 +5128,9 @@ void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad)
total_bytes += total_inode_bytes + total_directory_bytes +
sizeof(struct squashfs_super_block) + total_xattr_bytes;
+ if(quiet)
+ return;
+
printf("\n%sSquashfs %d.%d filesystem, %s compressed, data block size"
" %d\n", exportable ? "Exportable " : "", SQUASHFS_MAJOR,
SQUASHFS_MINOR, comp->name, block_size);
@@ -5184,9 +5196,6 @@ void write_filesystem_tables(struct squashfs_super_block *sBlk, int nopad)
group->gr_name, id_table[i]->id);
}
}
-
- printf("Number of whitelisted (uncompressed) files %d\n",
- whitelisted_count);
}
@@ -5299,47 +5308,20 @@ int parse_num(char *arg, int *res)
int get_physical_memory()
{
- int phys_mem;
-#ifndef linux
- #ifdef HW_MEMSIZE
- #define SYSCTL_PHYSMEM HW_MEMSIZE
- #elif defined(HW_PHYSMEM64)
- #define SYSCTL_PHYSMEM HW_PHYSMEM64
- #else
- #define SYSCTL_PHYSMEM HW_PHYSMEM
- #endif
-
- int mib[2];
- uint64_t sysctl_physmem = 0;
- size_t sysctl_len = sizeof(sysctl_physmem);
-
- mib[0] = CTL_HW;
- mib[1] = SYSCTL_PHYSMEM;
-
- if(sysctl(mib, 2, &sysctl_physmem, &sysctl_len, NULL, 0) == 0) {
- /* some systems use 32-bit values, work with what we're given */
- if (sysctl_len == 4)
- sysctl_physmem = *(uint32_t*)&sysctl_physmem;
- phys_mem = sysctl_physmem >> 20;
- } else {
- ERROR_START("Failed to get amount of available "
- "memory.");
- ERROR_EXIT(" Defaulting to least viable amount\n");
- phys_mem = SQUASHFS_LOWMEM;
- }
- #undef SYSCTL_PHYSMEM
-#else
- /* Long longs are used here because with PAE, a 32-bit
- machine can have more than 4GB of physical memory */
-
+ /*
+ * Long longs are used here because with PAE, a 32-bit
+ * machine can have more than 4GB of physical memory
+ *
+ * sysconf(_SC_PHYS_PAGES) relies on /proc being mounted.
+ * If it isn't fail.
+ */
long long num_pages = sysconf(_SC_PHYS_PAGES);
long long page_size = sysconf(_SC_PAGESIZE);
- phys_mem = num_pages * page_size >> 20;
+ int phys_mem = num_pages * page_size >> 20;
+
if(num_pages == -1 || page_size == -1)
return 0;
-#endif
-
if(phys_mem < SQUASHFS_LOWMEM)
BAD_ERROR("Mksquashfs requires more physical memory than is "
"available!\n");
@@ -5442,8 +5424,8 @@ void calculate_queue_sizes(int mem, int *readq, int *fragq, int *bwriteq,
#define VERSION() \
- printf("mksquashfs version 4.3-git (2014/09/12)\n");\
- printf("copyright (C) 2014 Phillip Lougher "\
+ printf("mksquashfs version 4.3-git (2017/07/18)\n");\
+ printf("copyright (C) 2017 Phillip Lougher "\
"<phillip@squashfs.org.uk>\n\n"); \
printf("This program is free software; you can redistribute it and/or"\
"\n");\
@@ -5698,6 +5680,15 @@ print_compressor_options:
force_progress = TRUE;
else if(strcmp(argv[i], "-no-exports") == 0)
exportable = FALSE;
+ else if(strcmp(argv[i], "-offset") == 0 ||
+ strcmp(argv[i], "-o") ==0) {
+ if(++i == argc) {
+ ERROR("%s: %s offset missing argument\n", argv[0],
+ argv[i - 1]);
+ exit(1);
+ }
+ squashfs_start_offset = (off_t)atol(argv[i]);
+ }
else if(strcmp(argv[i], "-processors") == 0) {
if((++i == argc) || !parse_num(argv[i], &processors)) {
ERROR("%s: -processors missing or invalid "
@@ -5931,8 +5922,12 @@ print_compressor_options:
else if(strcmp(argv[i], "-noappend") == 0)
delete = TRUE;
+ else if(strcmp(argv[i], "-quiet") == 0)
+ quiet = TRUE;
+
else if(strcmp(argv[i], "-keep-as-directory") == 0)
keep_as_directory = TRUE;
+
/* ANDROID CHANGES START*/
#ifdef ANDROID
else if(strcmp(argv[i], "-android-fs-config") == 0)
@@ -5975,7 +5970,6 @@ print_compressor_options:
}
#endif
/* ANDROID CHANGES END */
-
else if(strcmp(argv[i], "-exit-on-error") == 0)
exit_on_error = TRUE;
@@ -6062,6 +6056,14 @@ printOptions:
"definition\n");
ERROR("-pf <pseudo-file>\tAdd list of pseudo file "
"definitions\n");
+ ERROR("\t\t\tPseudo definitions should be of the "
+ "format\n");
+ ERROR("\t\t\t\tfilename d mode uid gid\n");
+ ERROR("\t\t\t\tfilename m mode uid gid\n");
+ ERROR("\t\t\t\tfilename b mode uid gid major minor\n");
+ ERROR("\t\t\t\tfilename c mode uid gid major minor\n");
+ ERROR("\t\t\t\tfilename f mode uid gid command\n");
+ ERROR("\t\t\t\tfilename s mode uid gid symlink\n");
ERROR("-sort <sort_file>\tsort files according to "
"priorities in <sort_file>. One\n");
ERROR("\t\t\tfile or dir with priority per line. "
@@ -6093,6 +6095,7 @@ printOptions:
"using recovery file <name>\n");
ERROR("-no-recovery\t\tdon't generate a recovery "
"file\n");
+ ERROR("-quiet\t\t\tno verbose output\n");
ERROR("-info\t\t\tprint files written to filesystem\n");
ERROR("-no-progress\t\tdon't display the progress "
"bar\n");
@@ -6109,6 +6112,9 @@ printOptions:
ERROR("\nMiscellaneous options:\n");
ERROR("-root-owned\t\talternative name for -all-root"
"\n");
+ ERROR("-o <offset>\t\tSkip <offset> bytes at the "
+ "beginning of the file.\n\t\t\t"
+ "Default 0 bytes\n");
ERROR("-noInodeCompression\talternative name for -noI"
"\n");
ERROR("-noDataCompression\talternative name for -noD"
@@ -6301,8 +6307,10 @@ printOptions:
void *comp_data = compressor_dump_options(comp, block_size,
&size);
- printf("Creating %d.%d filesystem on %s, block size %d.\n",
- SQUASHFS_MAJOR, SQUASHFS_MINOR, argv[source + 1], block_size);
+ if(!quiet)
+ printf("Creating %d.%d filesystem on %s, block size %d.\n",
+ SQUASHFS_MAJOR, SQUASHFS_MINOR,
+ argv[source + 1], block_size);
/*
* store any compressor specific options after the superblock,
diff --git a/squashfs-tools/mksquashfs.h b/squashfs-tools/mksquashfs.h
index bfbf0bf..0a091cb 100644
--- a/squashfs-tools/mksquashfs.h
+++ b/squashfs-tools/mksquashfs.h
@@ -29,7 +29,6 @@
#include <stdint.h>
#endif
/* ANDROID CHANGES END */
-#include <pthread.h>
struct dir_info {
char *pathname;
diff --git a/squashfs-tools/process_fragments.c b/squashfs-tools/process_fragments.c
index bba6f5a..971dc11 100644
--- a/squashfs-tools/process_fragments.c
+++ b/squashfs-tools/process_fragments.c
@@ -210,6 +210,7 @@ again:
cache_block_put(compressed_buffer);
finished:
+ do { } while (0);
pthread_cleanup_pop(0);
return buffer;
diff --git a/squashfs-tools/pseudo.c b/squashfs-tools/pseudo.c
index e12d399..cb74cf6 100644
--- a/squashfs-tools/pseudo.c
+++ b/squashfs-tools/pseudo.c
@@ -2,7 +2,7 @@
* Create a squashfs filesystem. This is a highly compressed read only
* filesystem.
*
- * Copyright (c) 2009, 2010, 2012, 2014
+ * Copyright (c) 2009, 2010, 2012, 2014, 2017
* Phillip Lougher <phillip@squashfs.org.uk>
*
* This program is free software; you can redistribute it and/or
@@ -30,9 +30,9 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
-#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
+#include <sys/stat.h>
#include <ctype.h>
#include "pseudo.h"
@@ -274,6 +274,7 @@ struct pseudo_dev *get_pseudo_file(int pseudo_id)
int read_pseudo_def(char *def)
{
int n, bytes;
+ int quoted = 0;
unsigned int major = 0, minor = 0, mode;
char type, *ptr;
char suid[100], sgid[100]; /* overflow safe */
@@ -284,13 +285,22 @@ int read_pseudo_def(char *def)
/*
* Scan for filename, don't use sscanf() and "%s" because
- * that can't handle filenames with spaces
+ * that can't handle filenames with spaces.
+ *
+ * Filenames with spaces should either escape (backslash) the
+ * space or use double quotes.
*/
filename = malloc(strlen(def) + 1);
if(filename == NULL)
MEM_ERROR();
- for(name = filename; !isspace(*def) && *def != '\0';) {
+ for(name = filename; (quoted || !isspace(*def)) && *def != '\0';) {
+ if(*def == '"') {
+ quoted = !quoted;
+ def ++;
+ continue;
+ }
+
if(*def == '\\') {
def ++;
if (*def == '\0')
@@ -317,12 +327,11 @@ int read_pseudo_def(char *def)
case -1:
/* FALLTHROUGH */
case 0:
- ERROR("Read filename, but failed to read or match "
- "type\n");
- break;
+ /* FALLTHROUGH */
case 1:
- ERROR("Read filename and type, but failed to read or "
- "match octal mode\n");
+ ERROR("Couldn't parse filename, type or octal mode\n");
+ ERROR("If the filename has spaces, either quote it, or "
+ "backslash the spaces\n");
break;
case 2:
ERROR("Read filename, type and mode, but failed to "
@@ -388,6 +397,20 @@ int read_pseudo_def(char *def)
goto error;
}
break;
+ case 's':
+ if(def[0] == '\0') {
+ ERROR("Not enough arguments in symlink pseudo "
+ "definition \"%s\"\n", orig_def);
+ ERROR("Expected symlink\n");
+ goto error;
+ }
+
+ if(strlen(def) > 65535) {
+ ERROR("Symlink pseudo definition %s is greater than 65535"
+ " bytes!\n", def);
+ goto error;
+ }
+ break;
default:
ERROR("Unsupported type %c\n", type);
goto error;
@@ -444,6 +467,10 @@ int read_pseudo_def(char *def)
case 'f':
mode |= S_IFREG;
break;
+ case 's':
+ /* permissions on symlinks are always rwxrwxrwx */
+ mode = 0777 | S_IFLNK;
+ break;
}
dev = malloc(sizeof(struct pseudo_dev));
@@ -460,6 +487,8 @@ int read_pseudo_def(char *def)
dev->command = strdup(def);
add_pseudo_file(dev);
}
+ if(type == 's')
+ dev->symlink = strdup(def);
pseudo = add_pseudo(pseudo, dev, filename, filename);
@@ -467,12 +496,13 @@ int read_pseudo_def(char *def)
return TRUE;
error:
- ERROR("Pseudo definitions should be of format\n");
+ ERROR("Pseudo definitions should be of the format\n");
ERROR("\tfilename d mode uid gid\n");
ERROR("\tfilename m mode uid gid\n");
ERROR("\tfilename b mode uid gid major minor\n");
ERROR("\tfilename c mode uid gid major minor\n");
- ERROR("\tfilename f mode uid command\n");
+ ERROR("\tfilename f mode uid gid command\n");
+ ERROR("\tfilename s mode uid gid symlink\n");
free(filename);
return FALSE;
}
diff --git a/squashfs-tools/pseudo.h b/squashfs-tools/pseudo.h
index c0d9f6f..ef693c3 100644
--- a/squashfs-tools/pseudo.h
+++ b/squashfs-tools/pseudo.h
@@ -31,7 +31,10 @@ struct pseudo_dev {
unsigned int major;
unsigned int minor;
int pseudo_id;
- char *command;
+ union {
+ char *command;
+ char *symlink;
+ };
};
struct pseudo_entry {
diff --git a/squashfs-tools/read_xattrs.c b/squashfs-tools/read_xattrs.c
index 837d3fb..42106f5 100644
--- a/squashfs-tools/read_xattrs.c
+++ b/squashfs-tools/read_xattrs.c
@@ -39,13 +39,13 @@
#include <endian.h>
#endif
-#include <stdlib.h>
-
#include "squashfs_fs.h"
#include "squashfs_swap.h"
#include "xattr.h"
#include "error.h"
+#include <stdlib.h>
+
extern int read_fs_bytes(int, long long, int, void *);
extern int read_block(int, long long, long long *, int, void *);
diff --git a/squashfs-tools/squashfs_fs.h b/squashfs-tools/squashfs_fs.h
index 791fe12..afca918 100644
--- a/squashfs-tools/squashfs_fs.h
+++ b/squashfs-tools/squashfs_fs.h
@@ -277,6 +277,7 @@ typedef long long squashfs_inode;
#define LZO_COMPRESSION 3
#define XZ_COMPRESSION 4
#define LZ4_COMPRESSION 5
+#define ZSTD_COMPRESSION 6
struct squashfs_super_block {
unsigned int s_magic;
diff --git a/squashfs-tools/unsquashfs.c b/squashfs-tools/unsquashfs.c
index 0dbc4c2..c5e0876 100644
--- a/squashfs-tools/unsquashfs.c
+++ b/squashfs-tools/unsquashfs.c
@@ -30,13 +30,9 @@
#include "xattr.h"
#include "unsquashfs_info.h"
#include "stdarg.h"
+#include "fnmatch_compat.h"
-#ifndef linux
-#include <sys/sysctl.h>
-#else
#include <sys/sysinfo.h>
-#endif
-
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
@@ -47,6 +43,7 @@ struct cache *fragment_cache, *data_cache;
struct queue *to_reader, *to_inflate, *to_writer, *from_writer;
pthread_t *thread, *inflator_thread;
pthread_mutex_t fragment_mutex;
+static off_t squashfs_start_offset = 0;
/* user options that control parallelisation */
int processors = -1;
@@ -636,7 +633,7 @@ int read_fs_bytes(int fd, long long byte, int bytes, void *buff)
TRACE("read_bytes: reading from position 0x%llx, bytes %d\n", byte,
bytes);
- if(lseek(fd, off, SEEK_SET) == -1) {
+ if(lseek(fd, off + squashfs_start_offset, SEEK_SET) == -1) {
ERROR("Lseek failed because %s\n", strerror(errno));
return FALSE;
}
@@ -1122,7 +1119,11 @@ int create_inode(char *pathname, struct inode *i)
case SQUASHFS_CHRDEV_TYPE:
case SQUASHFS_LBLKDEV_TYPE:
case SQUASHFS_LCHRDEV_TYPE: {
- int chrdev = i->type == SQUASHFS_CHRDEV_TYPE;
+ int chrdev = 0;
+ if ( i->type == SQUASHFS_CHRDEV_TYPE ||
+ i->type == SQUASHFS_LCHRDEV_TYPE)
+ chrdev = 1;
+
TRACE("create_inode: dev, rdev 0x%llx\n", i->data);
if(root_process) {
@@ -1645,9 +1646,9 @@ void squashfs_stat(char *source)
printf("Creation or last append time %s", mkfs_str ? mkfs_str :
"failed to get time\n");
- printf("Filesystem size %.2f Kbytes (%.2f Mbytes)\n",
- sBlk.s.bytes_used / 1024.0, sBlk.s.bytes_used /
- (1024.0 * 1024.0));
+ printf("Filesystem size %llu bytes (%.2f Kbytes / %.2f Mbytes)\n",
+ sBlk.s.bytes_used, sBlk.s.bytes_used / 1024.0,
+ sBlk.s.bytes_used / (1024.0 * 1024.0));
if(sBlk.s.s_major == 4) {
printf("Compression %s\n", comp->name);
@@ -2179,7 +2180,6 @@ void initialise_threads(int fragment_buffer_size, int data_buffer_size)
sigemptyset(&sigmask);
sigaddset(&sigmask, SIGQUIT);
sigaddset(&sigmask, SIGHUP);
- sigaddset(&sigmask, SIGALRM);
if(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == -1)
EXIT_UNSQUASH("Failed to set signal mask in initialise_threads"
"\n");
@@ -2550,6 +2550,14 @@ int main(int argc, char *argv[])
exit(1);
}
dest = argv[i];
+ } else if (strcmp(argv[i], "-offset") == 0 ||
+ strcmp(argv[i], "-o") == 0) {
+ if(++i == argc) {
+ fprintf(stderr, "%s: -offset missing argument\n",
+ argv[0]);
+ exit(1);
+ }
+ squashfs_start_offset = (off_t)atol(argv[i]);
} else if(strcmp(argv[i], "-processors") == 0 ||
strcmp(argv[i], "-p") == 0) {
if((++i == argc) ||
@@ -2642,6 +2650,8 @@ options:
"copyright information\n");
ERROR("\t-d[est] <pathname>\tunsquash to <pathname>, "
"default \"squashfs-root\"\n");
+ ERROR("\t-o[ffset] <bytes>\tskip <bytes> at start of input file, "
+ "default \"0\"\n");
ERROR("\t-n[o-progress]\t\tdon't display the progress "
"bar\n");
ERROR("\t-no[-xattrs]\t\tdon't extract xattrs in file system"
diff --git a/squashfs-tools/unsquashfs.h b/squashfs-tools/unsquashfs.h
index 4836b8d..0edbd25 100644
--- a/squashfs-tools/unsquashfs.h
+++ b/squashfs-tools/unsquashfs.h
@@ -40,17 +40,12 @@
#include <grp.h>
#include <time.h>
#include <regex.h>
-#include <fnmatch.h>
#include <signal.h>
#include <pthread.h>
#include <math.h>
#include <sys/ioctl.h>
#include <sys/time.h>
-#ifndef FNM_EXTMATCH /* glibc extension */
- #define FNM_EXTMATCH 0
-#endif
-
#ifndef linux
#define __BYTE_ORDER BYTE_ORDER
#define __BIG_ENDIAN BIG_ENDIAN
diff --git a/squashfs-tools/unsquashfs_info.c b/squashfs-tools/unsquashfs_info.c
index 7d4f7af..c8e2b9b 100644
--- a/squashfs-tools/unsquashfs_info.c
+++ b/squashfs-tools/unsquashfs_info.c
@@ -97,22 +97,31 @@ void dump_state()
void *info_thrd(void *arg)
{
sigset_t sigmask;
- int sig, err, waiting = 0;
+ struct timespec timespec = { .tv_sec = 1, .tv_nsec = 0 };
+ int sig, waiting = 0;
sigemptyset(&sigmask);
sigaddset(&sigmask, SIGQUIT);
sigaddset(&sigmask, SIGHUP);
- sigaddset(&sigmask, SIGALRM);
while(1) {
- err = sigwait(&sigmask, &sig);
+ if(waiting)
+ sig = sigtimedwait(&sigmask, NULL, &timespec);
+ else
+ sig = sigwaitinfo(&sigmask, NULL);
- if(err == -1) {
+ if(sig == -1) {
switch(errno) {
+ case EAGAIN:
+ /* interval timed out */
+ waiting = 0;
+ /* FALLTHROUGH */
case EINTR:
+ /* if waiting, the wait will be longer, but
+ that's OK */
continue;
default:
- BAD_ERROR("sigwait failed "
+ BAD_ERROR("sigtimedwait/sigwaitinfo failed "
"because %s\n", strerror(errno));
}
}
@@ -124,12 +133,8 @@ void *info_thrd(void *arg)
/* set one second interval period, if ^\ received
within then, dump queue and cache status */
waiting = 1;
- alarm(1);
- } else if (sig == SIGQUIT) {
+ } else
dump_state();
- } else if (sig == SIGALRM) {
- waiting = 0;
- }
}
}
diff --git a/squashfs-tools/unsquashfs_xattr.c b/squashfs-tools/unsquashfs_xattr.c
index 13f0e35..59f4aae 100644
--- a/squashfs-tools/unsquashfs_xattr.c
+++ b/squashfs-tools/unsquashfs_xattr.c
@@ -27,11 +27,6 @@
#include <sys/xattr.h>
-#ifdef XATTR_NOFOLLOW /* Apple's xattrs */
- #define lsetxattr(path_, name_, val_, sz_, flags_) \
- setxattr(path_, name_, val_, sz_, 0, flags_ | XATTR_NOFOLLOW)
-#endif
-
#define NOSPACE_MAX 10
extern int root_process;
diff --git a/squashfs-tools/xattr.c b/squashfs-tools/xattr.c
index 6cecb74..c34b2a7 100644
--- a/squashfs-tools/xattr.c
+++ b/squashfs-tools/xattr.c
@@ -22,14 +22,6 @@
* xattr.c
*/
-#ifndef linux
-#define __BYTE_ORDER BYTE_ORDER
-#define __BIG_ENDIAN BIG_ENDIAN
-#define __LITTLE_ENDIAN LITTLE_ENDIAN
-#else
-#include <endian.h>
-#endif
-
#define TRUE 1
#define FALSE 0
@@ -44,13 +36,6 @@
#include <stdlib.h>
#include <sys/xattr.h>
-#ifdef XATTR_NOFOLLOW /* Apple's xattrs */
- #define llistxattr(path_, buf_, sz_) \
- listxattr(path_, buf_, sz_, XATTR_NOFOLLOW)
- #define lgetxattr(path_, name_, val_, sz_) \
- getxattr(path_, name_, val_, sz_, 0, XATTR_NOFOLLOW)
-#endif
-
#include "squashfs_fs.h"
#include "squashfs_swap.h"
#include "mksquashfs.h"
diff --git a/squashfs-tools/xz_wrapper.c b/squashfs-tools/xz_wrapper.c
index e77ec59..0977f74 100644
--- a/squashfs-tools/xz_wrapper.c
+++ b/squashfs-tools/xz_wrapper.c
@@ -322,7 +322,7 @@ failed:
}
-void xz_display_options(void *buffer, int size)
+static void xz_display_options(void *buffer, int size)
{
struct comp_opts *comp_opts = buffer;
int dictionary_size, flags, printed;
@@ -503,7 +503,7 @@ static int xz_uncompress(void *dest, void *src, int size, int outsize,
}
-void xz_usage()
+static void xz_usage()
{
fprintf(stderr, "\t -Xbcj filter1,filter2,...,filterN\n");
fprintf(stderr, "\t\tCompress using filter1,filter2,...,filterN in");
diff --git a/squashfs-tools/zstd_wrapper.c b/squashfs-tools/zstd_wrapper.c
new file mode 100644
index 0000000..dcab75a
--- /dev/null
+++ b/squashfs-tools/zstd_wrapper.c
@@ -0,0 +1,254 @@
+/*
+ * Copyright (c) 2017
+ * Phillip Lougher <phillip@squashfs.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * zstd_wrapper.c
+ *
+ * Support for ZSTD compression http://zstd.net
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <zstd.h>
+#include <zstd_errors.h>
+
+#include "squashfs_fs.h"
+#include "zstd_wrapper.h"
+#include "compressor.h"
+
+static int compression_level = ZSTD_DEFAULT_COMPRESSION_LEVEL;
+
+/*
+ * This function is called by the options parsing code in mksquashfs.c
+ * to parse any -X compressor option.
+ *
+ * This function returns:
+ * >=0 (number of additional args parsed) on success
+ * -1 if the option was unrecognised, or
+ * -2 if the option was recognised, but otherwise bad in
+ * some way (e.g. invalid parameter)
+ *
+ * Note: this function sets internal compressor state, but does not
+ * pass back the results of the parsing other than success/failure.
+ * The zstd_dump_options() function is called later to get the options in
+ * a format suitable for writing to the filesystem.
+ */
+static int zstd_options(char *argv[], int argc)
+{
+ if (strcmp(argv[0], "-Xcompression-level") == 0) {
+ if (argc < 2) {
+ fprintf(stderr, "zstd: -Xcompression-level missing "
+ "compression level\n");
+ fprintf(stderr, "zstd: -Xcompression-level it should "
+ "be 1 <= n <= %d\n", ZSTD_maxCLevel());
+ goto failed;
+ }
+
+ compression_level = atoi(argv[1]);
+ if (compression_level < 1 ||
+ compression_level > ZSTD_maxCLevel()) {
+ fprintf(stderr, "zstd: -Xcompression-level invalid, it "
+ "should be 1 <= n <= %d\n", ZSTD_maxCLevel());
+ goto failed;
+ }
+
+ return 1;
+ }
+
+ return -1;
+failed:
+ return -2;
+}
+
+/*
+ * This function is called by mksquashfs to dump the parsed
+ * compressor options in a format suitable for writing to the
+ * compressor options field in the filesystem (stored immediately
+ * after the superblock).
+ *
+ * This function returns a pointer to the compression options structure
+ * to be stored (and the size), or NULL if there are no compression
+ * options.
+ */
+static void *zstd_dump_options(int block_size, int *size)
+{
+ static struct zstd_comp_opts comp_opts;
+
+ /* don't return anything if the options are all default */
+ if (compression_level == ZSTD_DEFAULT_COMPRESSION_LEVEL)
+ return NULL;
+
+ comp_opts.compression_level = compression_level;
+
+ SQUASHFS_INSWAP_COMP_OPTS(&comp_opts);
+
+ *size = sizeof(comp_opts);
+ return &comp_opts;
+}
+
+/*
+ * This function is a helper specifically for the append mode of
+ * mksquashfs. Its purpose is to set the internal compressor state
+ * to the stored compressor options in the passed compressor options
+ * structure.
+ *
+ * In effect this function sets up the compressor options
+ * to the same state they were when the filesystem was originally
+ * generated, this is to ensure on appending, the compressor uses
+ * the same compression options that were used to generate the
+ * original filesystem.
+ *
+ * Note, even if there are no compressor options, this function is still
+ * called with an empty compressor structure (size == 0), to explicitly
+ * set the default options, this is to ensure any user supplied
+ * -X options on the appending mksquashfs command line are over-ridden.
+ *
+ * This function returns 0 on sucessful extraction of options, and -1 on error.
+ */
+static int zstd_extract_options(int block_size, void *buffer, int size)
+{
+ struct zstd_comp_opts *comp_opts = buffer;
+
+ if (size == 0) {
+ /* Set default values */
+ compression_level = ZSTD_DEFAULT_COMPRESSION_LEVEL;
+ return 0;
+ }
+
+ /* we expect a comp_opts structure of sufficient size to be present */
+ if (size < sizeof(*comp_opts))
+ goto failed;
+
+ SQUASHFS_INSWAP_COMP_OPTS(comp_opts);
+
+ if (comp_opts->compression_level < 1 ||
+ comp_opts->compression_level > ZSTD_maxCLevel()) {
+ fprintf(stderr, "zstd: bad compression level in compression "
+ "options structure\n");
+ goto failed;
+ }
+
+ compression_level = comp_opts->compression_level;
+
+ return 0;
+
+failed:
+ fprintf(stderr, "zstd: error reading stored compressor options from "
+ "filesystem!\n");
+
+ return -1;
+}
+
+static void zstd_display_options(void *buffer, int size)
+{
+ struct zstd_comp_opts *comp_opts = buffer;
+
+ /* we expect a comp_opts structure of sufficient size to be present */
+ if (size < sizeof(*comp_opts))
+ goto failed;
+
+ SQUASHFS_INSWAP_COMP_OPTS(comp_opts);
+
+ if (comp_opts->compression_level < 1 ||
+ comp_opts->compression_level > ZSTD_maxCLevel()) {
+ fprintf(stderr, "zstd: bad compression level in compression "
+ "options structure\n");
+ goto failed;
+ }
+
+ printf("\tcompression-level %d\n", comp_opts->compression_level);
+
+ return;
+
+failed:
+ fprintf(stderr, "zstd: error reading stored compressor options from "
+ "filesystem!\n");
+}
+
+/*
+ * This function is called by mksquashfs to initialise the
+ * compressor, before compress() is called.
+ *
+ * This function returns 0 on success, and -1 on error.
+ */
+static int zstd_init(void **strm, int block_size, int datablock)
+{
+ ZSTD_CCtx *cctx = ZSTD_createCCtx();
+
+ if (!cctx) {
+ fprintf(stderr, "zstd: failed to allocate compression "
+ "context!\n");
+ return -1;
+ }
+
+ *strm = cctx;
+ return 0;
+}
+
+static int zstd_compress(void *strm, void *dest, void *src, int size,
+ int block_size, int *error)
+{
+ const size_t res = ZSTD_compressCCtx((ZSTD_CCtx*)strm, dest, block_size,
+ src, size, compression_level);
+
+ if (ZSTD_isError(res)) {
+ /* FIXME:
+ * zstd does not expose stable error codes. The error enum may
+ * change between versions. Until upstream zstd stablizes the
+ * error codes, we have no way of knowing why the error occurs.
+ * zstd shouldn't fail to compress any input unless there isn't
+ * enough output space. We assume that is the cause and return
+ * the special error code for not enough output space.
+ */
+ return 0;
+ }
+
+ return (int)res;
+}
+
+static int zstd_uncompress(void *dest, void *src, int size, int outsize,
+ int *error)
+{
+ const size_t res = ZSTD_decompress(dest, outsize, src, size);
+
+ if (ZSTD_isError(res)) {
+ fprintf(stderr, "\t%d %d\n", outsize, size);
+
+ *error = (int)ZSTD_getErrorCode(res);
+ return -1;
+ }
+
+ return (int)res;
+}
+
+static void zstd_usage(void)
+{
+ fprintf(stderr, "\t -Xcompression-level <compression-level>\n");
+ fprintf(stderr, "\t\t<compression-level> should be 1 .. %d (default "
+ "%d)\n", ZSTD_maxCLevel(), ZSTD_DEFAULT_COMPRESSION_LEVEL);
+}
+
+struct compressor zstd_comp_ops = {
+ .init = zstd_init,
+ .compress = zstd_compress,
+ .uncompress = zstd_uncompress,
+ .options = zstd_options,
+ .dump_options = zstd_dump_options,
+ .extract_options = zstd_extract_options,
+ .display_options = zstd_display_options,
+ .usage = zstd_usage,
+ .id = ZSTD_COMPRESSION,
+ .name = "zstd",
+ .supported = 1
+};
diff --git a/squashfs-tools/zstd_wrapper.h b/squashfs-tools/zstd_wrapper.h
new file mode 100644
index 0000000..4fbef0a
--- /dev/null
+++ b/squashfs-tools/zstd_wrapper.h
@@ -0,0 +1,48 @@
+#ifndef ZSTD_WRAPPER_H
+#define ZSTD_WRAPPER_H
+/*
+ * Squashfs
+ *
+ * Copyright (c) 2017
+ * Phillip Lougher <phillip@squashfs.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * zstd_wrapper.h
+ *
+ */
+
+#ifndef linux
+#define __BYTE_ORDER BYTE_ORDER
+#define __BIG_ENDIAN BIG_ENDIAN
+#define __LITTLE_ENDIAN LITTLE_ENDIAN
+#else
+#include <endian.h>
+#endif
+
+#if __BYTE_ORDER == __BIG_ENDIAN
+extern unsigned int inswap_le16(unsigned short);
+extern unsigned int inswap_le32(unsigned int);
+
+#define SQUASHFS_INSWAP_COMP_OPTS(s) { \
+ (s)->compression_level = inswap_le32((s)->compression_level); \
+}
+#else
+#define SQUASHFS_INSWAP_COMP_OPTS(s)
+#endif
+
+/* Default compression */
+#define ZSTD_DEFAULT_COMPRESSION_LEVEL 15
+
+struct zstd_comp_opts {
+ int compression_level;
+};
+#endif