+struct file_info {
+ char *path;
+ size_t path_len;
+ size_t path_max_len;
+};
+
+static errcode_t path_append(struct file_info *target, const char *file)
+{
+ if (strlen(file) + target->path_len + 1 > target->path_max_len) {
+ void *p;
+ target->path_max_len *= 2;
+ p = realloc(target->path, target->path_max_len);
+ if (p == NULL)
+ return EXT2_ET_NO_MEMORY;
+ target->path = p;
+ }
+ target->path_len += sprintf(target->path + target->path_len, "/%s",
+ file);
+ return 0;
+}
+
+#ifdef _WIN32
+static int scandir(const char *dir_name, struct dirent ***name_list,
+ int (*filter)(const struct dirent*),
+ int (*compar)(const struct dirent**, const struct dirent**)) {
+ DIR *dir;
+ struct dirent *dent;
+ struct dirent **temp_list = NULL;
+ size_t temp_list_size = 0; // unit: num of dirent
+ size_t num_dent = 0;
+
+ dir = opendir(dir_name);
+ if (dir == NULL) {
+ return -1;
+ }
+
+ while ((dent = readdir(dir))) {
+ if (filter != NULL && !(*filter)(dent))
+ continue;
+
+ // re-allocate the list
+ if (num_dent == temp_list_size) {
+ size_t new_list_size = temp_list_size + 32;
+ struct dirent **new_list = (struct dirent**)realloc(
+ temp_list, new_list_size * sizeof(struct dirent*));
+ if (new_list == NULL) {
+ goto out;
+ }
+ temp_list_size = new_list_size;
+ temp_list = new_list;
+ }
+ // add the copy of dirent to the list
+ temp_list[num_dent] = (struct dirent*)malloc((dent->d_reclen + 3) & ~3);
+ memcpy(temp_list[num_dent], dent, dent->d_reclen);
+ num_dent++;
+ }
+
+ if (compar != NULL) {
+ qsort(temp_list, num_dent, sizeof(struct dirent*),
+ (int (*)(const void*, const void*))compar);
+ }
+
+ // release the temp list
+ *name_list = temp_list;
+ temp_list = NULL;
+
+out:
+ if (temp_list != NULL) {
+ while (num_dent > 0) {
+ free(temp_list[--num_dent]);
+ }
+ free(temp_list);
+ num_dent = -1;
+ }
+ closedir(dir);
+ return num_dent;
+}
+
+static int alphasort(const struct dirent **a, const struct dirent **b) {
+ return strcoll((*a)->d_name, (*b)->d_name);
+}
+#endif
+
+/* Copy files from source_dir to fs in alphabetical order */