+ struct stat st;
+ if (tstat(filename, &st) == 0)
+ return st.st_size;
+ else
+ return (off_t)-1;
+}
+
+tchar pat_ntfs_log[] = T("/$ntfs.log");
+tchar pat_hiberfil_sys[] = T("/hiberfil.sys");
+tchar pat_pagefile_sys[] = T("/pagefile.sys");
+tchar pat_system_volume_information[] = T("/System Volume Information");
+tchar pat_recycler[] = T("/RECYCLER");
+tchar pat_windows_csc[] = T("/Windows/CSC");
+
+tchar *default_pats[] = {
+ pat_ntfs_log,
+ pat_hiberfil_sys,
+ pat_pagefile_sys,
+ pat_system_volume_information,
+ pat_recycler,
+ pat_windows_csc,
+};
+
+static struct wimlib_capture_config default_capture_config = {
+ .exclusion_pats = {
+ .num_pats = sizeof(default_pats) / sizeof(*default_pats),
+ .pats = default_pats,
+ },
+};
+
+enum {
+ PARSE_STRING_SUCCESS = 0,
+ PARSE_STRING_FAILURE = 1,
+ PARSE_STRING_NONE = 2,
+};
+
+/*
+ * Parses a string token from an array of characters.
+ *
+ * Tokens are either whitespace-delimited, or double or single-quoted.
+ *
+ * @line_p: Pointer to the pointer to the line of data. Will be updated
+ * to point past the string token iff the return value is
+ * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
+ * be '\0'.
+ *
+ * @len_p: @len_p initially stores the length of the line of data, which may
+ * be 0, and it will be updated to the number of bytes remaining in
+ * the line iff the return value is PARSE_STRING_SUCCESS.
+ *
+ * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
+ * parsed string token will be returned here.
+ *
+ * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
+ * PARSE_STRING_FAILURE if the data was invalid due to a missing
+ * closing quote; or PARSE_STRING_NONE if the line ended before the
+ * beginning of a string token was found.
+ */
+static int
+parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
+{
+ size_t len = *len_p;
+ tchar *line = *line_p;
+ tchar *fn;
+ tchar quote_char;
+
+ /* Skip leading whitespace */
+ for (;;) {
+ if (len == 0)
+ return PARSE_STRING_NONE;
+ if (!istspace(*line) && *line != T('\0'))
+ break;
+ line++;
+ len--;
+ }
+ quote_char = *line;
+ if (quote_char == T('"') || quote_char == T('\'')) {
+ /* Quoted string */
+ line++;
+ len--;
+ fn = line;
+ line = tmemchr(line, quote_char, len);
+ if (!line) {
+ imagex_error(T("Missing closing quote: %"TS), fn - 1);
+ return PARSE_STRING_FAILURE;
+ }
+ } else {
+ /* Unquoted string. Go until whitespace. Line is terminated
+ * by '\0', so no need to check 'len'. */
+ fn = line;
+ do {
+ line++;
+ } while (!istspace(*line) && *line != T('\0'));
+ }
+ *line = T('\0');
+ len -= line - fn;
+ *len_p = len;
+ *line_p = line;
+ *fn_ret = fn;
+ return PARSE_STRING_SUCCESS;
+}
+
+/* Parses a line of data (not an empty line or comment) in the source list file
+ * format. (See the man page for 'wimlib-imagex capture' for details on this
+ * format and the meaning.)
+ *
+ * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
+ * len == 0. The data in @line will be modified by this function call.
+ *
+ * @len: Length of the line of data.
+ *
+ * @source: On success, the capture source and target described by the line is
+ * written into this destination. Note that it will contain pointers
+ * to data in the @line array.
+ *
+ * Returns true if the line was valid; false otherwise. */
+static bool
+parse_source_list_line(tchar *line, size_t len,
+ struct wimlib_capture_source *source)
+{
+ /* SOURCE [DEST] */
+ int ret;
+ ret = parse_string(&line, &len, &source->fs_source_path);
+ if (ret != PARSE_STRING_SUCCESS)
+ return false;
+ ret = parse_string(&line, &len, &source->wim_target_path);
+ if (ret == PARSE_STRING_NONE)
+ source->wim_target_path = source->fs_source_path;
+ return ret != PARSE_STRING_FAILURE;
+}
+
+/* Returns %true if the given line of length @len > 0 is a comment or empty line
+ * in the source list file format. */
+static bool
+is_comment_line(const tchar *line, size_t len)
+{
+ for (;;) {
+ if (*line == T('#'))
+ return true;
+ if (!istspace(*line) && *line != T('\0'))
+ return false;
+ ++line;
+ --len;
+ if (len == 0)
+ return true;
+ }
+}
+
+static ssize_t
+text_file_count_lines(tchar **contents_p, size_t *nchars_p)
+{
+ ssize_t nlines = 0;
+ tchar *contents = *contents_p;
+ size_t nchars = *nchars_p;
+ size_t i;
+
+ for (i = 0; i < nchars; i++)
+ if (contents[i] == T('\n'))
+ nlines++;
+
+ /* Handle last line not terminated by a newline */
+ if (nchars != 0 && contents[nchars - 1] != T('\n')) {
+ contents = realloc(contents, (nchars + 1) * sizeof(tchar));
+ if (!contents) {
+ imagex_error(T("Out of memory!"));
+ return -1;
+ }
+ contents[nchars] = T('\n');
+ *contents_p = contents;
+ nchars++;
+ nlines++;
+ }
+ *nchars_p = nchars;
+ return nlines;
+}
+
+/* Parses a file in the source list format. (See the man page for
+ * 'wimlib-imagex capture' for details on this format and the meaning.)
+ *
+ * @source_list_contents: Contents of the source list file. Note that this
+ * buffer will be modified to save memory allocations,
+ * and cannot be freed until the returned array of
+ * wimlib_capture_source's has also been freed.
+ *
+ * @source_list_nbytes: Number of bytes of data in the @source_list_contents
+ * buffer.
+ *
+ * @nsources_ret: On success, the length of the returned array is
+ * returned here.
+ *
+ * Returns: An array of `struct wimlib_capture_source's that can be passed to
+ * the wimlib_add_image_multisource() function to specify how a WIM image is to
+ * be created. */
+static struct wimlib_capture_source *
+parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
+ size_t *nsources_ret)
+{
+ ssize_t nlines;
+ tchar *p;
+ struct wimlib_capture_source *sources;
+ size_t i, j;
+
+ nlines = text_file_count_lines(source_list_contents_p,
+ &source_list_nchars);
+ if (nlines < 0)
+ return NULL;
+
+ /* Always allocate at least 1 slot, just in case the implementation of
+ * calloc() returns NULL if 0 bytes are requested. */
+ sources = calloc(nlines ?: 1, sizeof(*sources));
+ if (!sources) {
+ imagex_error(T("out of memory"));
+ return NULL;
+ }
+ p = *source_list_contents_p;
+ j = 0;
+ for (i = 0; i < nlines; i++) {
+ /* XXX: Could use rawmemchr() here instead, but it may not be
+ * available on all platforms. */
+ tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
+ size_t len = endp - p + 1;
+ *endp = T('\0');
+ if (!is_comment_line(p, len)) {
+ if (!parse_source_list_line(p, len, &sources[j++])) {
+ free(sources);
+ return NULL;
+ }
+ }
+ p = endp + 1;
+
+ }
+ *nsources_ret = j;
+ return sources;
+}
+
+
+enum capture_config_section {
+ CAPTURE_CONFIG_NO_SECTION,
+ CAPTURE_CONFIG_EXCLUSION_SECTION,
+ CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
+ CAPTURE_CONFIG_IGNORE_SECTION,
+};
+
+enum {
+ CAPTURE_CONFIG_INVALID_SECTION,
+ CAPTURE_CONFIG_CHANGED_SECTION,
+ CAPTURE_CONFIG_SAME_SECTION,
+};
+
+static int
+check_config_section(tchar *line, size_t len,
+ enum capture_config_section *cur_section)
+{
+ while (istspace(*line))
+ line++;
+
+ if (*line != T('['))
+ return CAPTURE_CONFIG_SAME_SECTION;
+
+ line++;
+ tchar *endbrace = tstrrchr(line, T(']'));
+ if (!endbrace)
+ return CAPTURE_CONFIG_SAME_SECTION;
+
+ if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
+ *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
+ } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
+ *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
+ } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
+ *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
+ tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
+ "of capture config file\n"),
+ stderr);
+ } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
+ *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
+ tfputs(T("WARNING: Ignoring [AlignmentList] section "
+ "of capture config file\n"),
+ stderr);
+ } else {
+ imagex_error(T("Invalid capture config file section \"%"TS"\""),
+ line - 1);
+ return CAPTURE_CONFIG_INVALID_SECTION;
+ }
+ return CAPTURE_CONFIG_CHANGED_SECTION;
+}
+
+
+static bool
+pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
+ tchar *pat)
+{
+ if (pat_list->num_pats == pat_list->num_allocated_pats) {
+ tchar **pats;
+ size_t num_allocated_pats = pat_list->num_pats + 8;
+
+ pats = realloc(pat_list->pats,
+ num_allocated_pats * sizeof(pat_list->pats[0]));
+ if (!pats) {
+ imagex_error(T("Out of memory!"));
+ return false;
+ }
+ pat_list->pats = pats;
+ pat_list->num_allocated_pats = num_allocated_pats;
+ }
+ pat_list->pats[pat_list->num_pats++] = pat;
+ return true;
+}
+
+static bool
+parse_capture_config_line(tchar *line, size_t len,
+ enum capture_config_section *cur_section,
+ struct wimlib_capture_config *config)
+{
+ tchar *filename;
+ int ret;
+
+ ret = check_config_section(line, len, cur_section);
+ if (ret == CAPTURE_CONFIG_INVALID_SECTION)
+ return false;
+ if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
+ return true;
+
+ switch (*cur_section) {
+ case CAPTURE_CONFIG_NO_SECTION:
+ imagex_error(T("Line \"%"TS"\" is not in a section "
+ "(such as [ExclusionList]"), line);
+ return false;
+ case CAPTURE_CONFIG_EXCLUSION_SECTION:
+ if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
+ return false;
+ return pattern_list_add_pattern(&config->exclusion_pats,
+ filename);
+ case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
+ if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
+ return false;
+ return pattern_list_add_pattern(&config->exclusion_exception_pats,
+ filename);
+ case CAPTURE_CONFIG_IGNORE_SECTION:
+ return true;
+ }
+ return false;