/*
* Create a squashfs filesystem. This is a highly compressed read only
* filesystem.
*
* Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
* 2012, 2013, 2014, 2017, 2019, 2021, 2022, 2023
* 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.
*
* mksquashfs.c
*/
#define FALSE 0
#define TRUE 1
#define MAX_LINE 16384
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <pthread.h>
#include <regex.h>
#include <sys/wait.h>
#include <limits.h>
#include <ctype.h>
#ifdef __linux__
#include <sys/sysinfo.h>
#include <sys/sysmacros.h>
#include <sched.h>
#else
#include <sys/sysctl.h>
#endif
#include "squashfs_fs.h"
#include "squashfs_swap.h"
#include "mksquashfs.h"
#include "sort.h"
#include "pseudo.h"
#include "compressor.h"
#include "xattr.h"
#include "action.h"
#include "mksquashfs_error.h"
#include "progressbar.h"
#include "info.h"
#include "caches-queues-lists.h"
#include "read_fs.h"
#include "restore.h"
#include "process_fragments.h"
#include "fnmatch_compat.h"
#include "tar.h"
#include "merge_sort.h"
/* Compression options */
int noF = FALSE;
int noI = FALSE;
int noId = FALSE;
int noD = FALSE;
int noX = FALSE;
/* block size used to build filesystem */
int block_size = SQUASHFS_FILE_SIZE;
int block_log;
/* Fragment options, are fragments in filesystem and are they used for tailends? */
int no_fragments = FALSE;
int always_use_fragments = FALSE;
/* Are duplicates detected in fileystem ? */
int duplicate_checking = TRUE;
/* Are filesystems exportable via NFS? */
int exportable = TRUE;
/* Are sparse files detected and stored? */
int sparse_files = TRUE;
/* Options which override root inode settings */
int root_mode_opt = FALSE;
mode_t root_mode;
int root_uid_opt = FALSE;
unsigned int root_uid;
int root_gid_opt = FALSE;
unsigned int root_gid;
unsigned int root_time;
int root_time_opt = FALSE;
/* Values that override uids and gids for all files and directories */
int global_uid_opt = FALSE;
unsigned int global_uid;
int global_gid_opt = FALSE;
unsigned int global_gid;
/* Do pseudo uids and guids override -all-root, -force-uid and -force-gid? */
int pseudo_override = FALSE;
/* Time value over-ride options */
unsigned int mkfs_time;
int mkfs_time_opt = FALSE;
unsigned int all_time;
int all_time_opt = FALSE;
int clamping = TRUE;
/* Is max depth option in effect, and max depth to descend into directories */
int max_depth_opt = FALSE;
unsigned int max_depth;
/* how should Mksquashfs treat the source files? */
int tarstyle = FALSE;
int keep_as_directory = FALSE;
/* should Mksquashfs read files from stdin, like cpio? */
int cpiostyle = FALSE;
char filename_terminator = '\n';
/* Should Mksquashfs detect hardlinked files? */
int no_hardlinks = FALSE;
/* Should Mksquashfs cross filesystem boundaries? */
int one_file_system = FALSE;
int one_file_system_x = FALSE;
dev_t *source_dev;
dev_t cur_dev;
/* Is Mksquashfs processing a tarfile? */
int tarfile = FALSE;
/* Is Mksquashfs reading a pseudo file from stdin? */
int pseudo_stdin = FALSE;
/* Is Mksquashfs storing Xattrs, or excluding/including xattrs using regexs? */
int no_xattrs = XATTR_DEF;
unsigned int xattr_bytes = 0, total_xattr_bytes = 0;
regex_t *xattr_exclude_preg = NULL;
regex_t *xattr_include_preg = NULL;
/* Does Mksquashfs print a summary and other information when running? */
int quiet = FALSE;
/* Does Mksquashfs display filenames as they are archived? */
int silent = TRUE;
/* Is Mksquashfs using the older non-wildcard exclude code? */
int old_exclude = TRUE;
/* Is Mksquashfs using regexs in exclude file matching (default wildcards)? */
int use_regex = FALSE;
/* Will Mksquashfs pad the filesystem to a multiple of 4 Kbytes? */
int nopad = FALSE;
/* Should Mksquashfs treat normally ignored errors as fatal? */
int exit_on_error = FALSE;
/* Is filesystem stored at an offset from the start of the block device/file? */
long long start_offset = 0;
/* File count statistics used to print summary and fill in superblock */
unsigned int file_count = 0, sym_count = 0, dev_count = 0, dir_count = 0,
fifo_count = 0, sock_count = 0, id_count = 0;
long long hardlnk_count = 0;
/* superblock attributes */
struct squashfs_super_block sBlk;
/* write position within data section */
long long bytes = 0, total_bytes = 0;
/* in memory directory table - possibly compressed */
char *directory_table = NULL;
long long directory_bytes = 0, directory_size = 0, total_directory_bytes = 0;
/* cached directory table */
char *directory_data_cache = NULL;
unsigned int directory_cache_bytes = 0, directory_cache_size = 0;
/* in memory inode table - possibly compressed */
char *inode_table = NULL;
long long inode_bytes = 0, inode_size = 0, total_inode_bytes = 0;
/* cached inode table */
char *data_cache = NULL;
unsigned int cache_bytes = 0, cache_size = 0, inode_count = 0;
/* inode lookup table */
squashfs_inode *inode_lookup_table = NULL;
struct inode_info *inode_info[INODE_HASH_SIZE];
/* hash tables used to do fast duplicate searches in duplicate check */
struct file_info **dupl_frag;
struct file_info **dupl_block;
unsigned int dup_files = 0;
int exclude = 0;
struct exclude_info *exclude_paths = NULL;
struct path_entry {
char *name;
regex_t *preg;
struct pathname *paths;
};
struct pathnames *paths = NULL;
struct pathname *path = NULL;
struct pathname *stickypath = NULL;
unsigned int fragments = 0;
struct squashfs_fragment_entry *fragment_table = NULL;
int fragments_outstanding = 0;
int fragments_locked = FALSE;
/* current inode number for directories and non directories */
unsigned int inode_no = 1;
unsigned int root_inode_number = 0;
/* list of source dirs/files */
int source = 0;
char **source_path;
int option_offset;
/* flag whether destination file is a block device */
int block_device = FALSE;
/* flag indicating whether files are sorted using sort list(s) */
int sorted = FALSE;
/* save destination file name for deleting on error */
char *destination_file = NULL;
struct id *id_hash_table[ID_ENTRIES];
struct id *id_table[SQUASHFS_IDS], *sid_table[SQUASHFS_IDS];
unsigned int uid_count = 0, guid_count = 0;
unsigned int sid_count = 0, suid_count = 0, sguid_count = 0;
/* caches used to store buffers being worked on, and queues
* used to send buffers between threads */
struct cache *reader_buffer, *fragment_buffer, *reserve_cache;
struct cache *bwriter_buffer, *fwriter_buffer;
struct queue *to_reader, *to_deflate, *to_writer, *from_writer,
*to_frag, *locked_fragment, *to_process_frag;
struct seq_queue *to_main;
/* pthread threads and mutexes */
pthread_t reader_thread, writer_thread, main_thread;
pthread_t *deflator_thread, *frag_deflator_thread, *frag_thread;
pthread_t *restore_thread = NULL;
pthread_mutex_t fragment_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t pos_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t dup_mutex = PTHREAD_MUTEX_INITIALIZER;
/* reproducible image queues and threads */
struct seq_queue *to_order;
pthread_t order_thread;
pthread_cond_t fragment_waiting = PTHREAD_COND_INITIALIZER;
int