576 lines
17 KiB
C
576 lines
17 KiB
C
#include "scanner.h"
|
|
#include "array_list.h"
|
|
#include "chunk.h"
|
|
#include "file.h"
|
|
#include "queue.h"
|
|
#include "utils.h"
|
|
#include <dirent.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <threads.h>
|
|
#include <unistd.h>
|
|
|
|
typedef struct {
|
|
char* path;
|
|
int depth;
|
|
} DirEntry;
|
|
|
|
static void dir_entry_destroy(void* item) {
|
|
if (item) {
|
|
DirEntry* de = (DirEntry*)item;
|
|
free(de->path);
|
|
free(de);
|
|
}
|
|
}
|
|
|
|
static DirEntry* dir_entry_create(const char* path, int depth) {
|
|
DirEntry* de = malloc(sizeof(DirEntry));
|
|
if (de) {
|
|
de->path = str_dup(path);
|
|
de->depth = depth;
|
|
}
|
|
return de;
|
|
}
|
|
|
|
DirectoryScanner* directory_scanner_create(const char* root_directory, bool use_metadata,
|
|
unsigned long long chunk_size, char** exclude_patterns,
|
|
int exclude_count, char** include_patterns,
|
|
int include_count, unsigned long long max_size,
|
|
unsigned long long min_size, int max_depth,
|
|
bool follow_symlinks, bool copy_links, bool safe_links,
|
|
bool copy_unsafe_links) {
|
|
DirectoryScanner* scanner = malloc(sizeof(DirectoryScanner));
|
|
if (scanner == NULL)
|
|
return NULL;
|
|
scanner->directories = queue_create(100, dir_entry_destroy);
|
|
scanner->current_dir = NULL;
|
|
scanner->current_path = NULL;
|
|
scanner->use_metadata = use_metadata;
|
|
scanner->chunk_size = chunk_size > 0 ? chunk_size : DESIRED_CHUNK_SIZE;
|
|
scanner->exclude_patterns = exclude_patterns;
|
|
scanner->exclude_count = exclude_count;
|
|
scanner->include_patterns = include_patterns;
|
|
scanner->include_count = include_count;
|
|
scanner->max_size = max_size;
|
|
scanner->min_size = min_size;
|
|
scanner->max_depth = max_depth;
|
|
scanner->current_depth = 0;
|
|
scanner->follow_symlinks = follow_symlinks;
|
|
scanner->copy_links = copy_links;
|
|
scanner->safe_links = safe_links;
|
|
scanner->copy_unsafe_links = copy_unsafe_links;
|
|
queue_enqueue(scanner->directories, dir_entry_create(root_directory, 0));
|
|
return scanner;
|
|
}
|
|
|
|
void directory_scanner_destroy(DirectoryScanner* scanner) {
|
|
if (scanner == NULL)
|
|
return;
|
|
if (scanner->current_dir) {
|
|
closedir(scanner->current_dir);
|
|
scanner->current_dir = NULL;
|
|
}
|
|
free(scanner->current_path);
|
|
queue_destroy(scanner->directories);
|
|
free(scanner);
|
|
}
|
|
|
|
static Chunk* chunk_data_to_chunk(ArrayList* chunk_data) {
|
|
void** chunk_items = array_list_to_array(chunk_data);
|
|
Chunk* chunk = chunk_create((File**)chunk_items, chunk_data->size);
|
|
free(chunk_items);
|
|
chunk_data->item_destroyer = NULL;
|
|
array_list_delete(chunk_data);
|
|
return chunk;
|
|
}
|
|
|
|
static int open_next_directory(DirectoryScanner* scanner) {
|
|
if (scanner->current_dir) {
|
|
closedir(scanner->current_dir);
|
|
scanner->current_dir = NULL;
|
|
}
|
|
free(scanner->current_path);
|
|
|
|
if (queue_is_empty(scanner->directories))
|
|
return 0;
|
|
|
|
DirEntry* de = (DirEntry*)queue_dequeue(scanner->directories);
|
|
scanner->current_path = de->path;
|
|
scanner->current_depth = de->depth;
|
|
free(de);
|
|
scanner->current_dir = opendir(scanner->current_path);
|
|
if (scanner->current_dir == NULL) {
|
|
perror("Could not open directory");
|
|
free(scanner->current_path);
|
|
scanner->current_path = NULL;
|
|
return -1;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
Chunk* directory_scanner_next(DirectoryScanner* scanner) {
|
|
ArrayList* chunk_data = array_list_create(file_destroy);
|
|
unsigned long long chunk_data_size = 0;
|
|
|
|
while (1) {
|
|
if (scanner->current_dir == NULL) {
|
|
int ret = open_next_directory(scanner);
|
|
if (ret == 0)
|
|
break;
|
|
if (ret < 0)
|
|
continue;
|
|
}
|
|
|
|
struct dirent* entry = readdir(scanner->current_dir);
|
|
if (entry == NULL) {
|
|
closedir(scanner->current_dir);
|
|
scanner->current_dir = NULL;
|
|
free(scanner->current_path);
|
|
scanner->current_path = NULL;
|
|
continue;
|
|
}
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
continue;
|
|
|
|
char* cur_path = path_cat(scanner->current_path, entry->d_name);
|
|
struct stat stats;
|
|
struct stat lstats;
|
|
bool is_symlink = false;
|
|
if (lstat(cur_path, &lstats) != 0) {
|
|
free(cur_path);
|
|
continue;
|
|
}
|
|
is_symlink = S_ISLNK(lstats.st_mode);
|
|
|
|
if (is_symlink && !scanner->follow_symlinks && !scanner->copy_links && !scanner->safe_links &&
|
|
!scanner->copy_unsafe_links) {
|
|
free(cur_path);
|
|
continue;
|
|
}
|
|
|
|
if (is_symlink && scanner->safe_links) {
|
|
char link_target[4096];
|
|
ssize_t len = readlink(cur_path, link_target, sizeof(link_target) - 1);
|
|
if (len < 0) {
|
|
free(cur_path);
|
|
continue;
|
|
}
|
|
link_target[len] = '\0';
|
|
if (link_target[0] == '/') {
|
|
free(cur_path);
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if (is_symlink && scanner->copy_unsafe_links && !scanner->copy_links) {
|
|
char link_target[4096];
|
|
ssize_t len = readlink(cur_path, link_target, sizeof(link_target) - 1);
|
|
if (len < 0) {
|
|
free(cur_path);
|
|
continue;
|
|
}
|
|
link_target[len] = '\0';
|
|
bool unsafe = (link_target[0] == '/');
|
|
if (!unsafe) {
|
|
free(cur_path);
|
|
continue;
|
|
}
|
|
}
|
|
|
|
bool use_lstat = is_symlink && scanner->follow_symlinks && !scanner->copy_links;
|
|
if (use_lstat) {
|
|
stats = lstats;
|
|
} else {
|
|
if (stat(cur_path, &stats) != 0) {
|
|
free(cur_path);
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if (S_ISDIR(stats.st_mode)) {
|
|
int next_depth = scanner->current_depth + 1;
|
|
if (scanner->max_depth <= 0 || next_depth < scanner->max_depth) {
|
|
DirEntry* de = dir_entry_create(cur_path, next_depth);
|
|
if (!queue_enqueue(scanner->directories, de))
|
|
dir_entry_destroy(de);
|
|
}
|
|
free(cur_path);
|
|
} else {
|
|
if (scanner->max_depth > 0 && scanner->current_depth + 1 > scanner->max_depth) {
|
|
free(cur_path);
|
|
continue;
|
|
}
|
|
bool excluded = false;
|
|
for (int i = 0; i < scanner->exclude_count; i++) {
|
|
if (glob_match(scanner->exclude_patterns[i], entry->d_name)) {
|
|
excluded = true;
|
|
break;
|
|
}
|
|
}
|
|
if (excluded) {
|
|
free(cur_path);
|
|
continue;
|
|
}
|
|
|
|
if (scanner->include_count > 0) {
|
|
bool included = false;
|
|
for (int i = 0; i < scanner->include_count; i++) {
|
|
if (glob_match(scanner->include_patterns[i], entry->d_name)) {
|
|
included = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!included) {
|
|
free(cur_path);
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if ((scanner->max_size > 0 && (unsigned long long)stats.st_size > scanner->max_size) ||
|
|
(scanner->min_size > 0 && (unsigned long long)stats.st_size < scanner->min_size)) {
|
|
free(cur_path);
|
|
continue;
|
|
}
|
|
|
|
File* file = file_create(cur_path);
|
|
if (file == NULL) {
|
|
free(cur_path);
|
|
continue;
|
|
}
|
|
file->data->size = stats.st_size;
|
|
if (scanner->use_metadata)
|
|
file->metadata = file_metadata_create(&stats);
|
|
array_list_add(chunk_data, file);
|
|
chunk_data_size += file->data->size;
|
|
if (chunk_data_size > scanner->chunk_size) {
|
|
free(cur_path);
|
|
return chunk_data_to_chunk(chunk_data);
|
|
}
|
|
free(cur_path);
|
|
}
|
|
}
|
|
|
|
if (chunk_data->size > 0)
|
|
return chunk_data_to_chunk(chunk_data);
|
|
array_list_delete(chunk_data);
|
|
return NULL;
|
|
}
|
|
|
|
typedef struct {
|
|
ParallelScanner* ps;
|
|
char** dirs;
|
|
int dir_count;
|
|
bool use_metadata;
|
|
unsigned long long chunk_size;
|
|
char** exclude_patterns;
|
|
int exclude_count;
|
|
char** include_patterns;
|
|
int include_count;
|
|
unsigned long long max_size;
|
|
unsigned long long min_size;
|
|
int max_depth;
|
|
bool follow_symlinks;
|
|
bool copy_links;
|
|
bool safe_links;
|
|
bool copy_unsafe_links;
|
|
} ParallelWorkerArg;
|
|
|
|
static int parallel_worker_thread(void* arg) {
|
|
ParallelWorkerArg* wa = (ParallelWorkerArg*)arg;
|
|
for (int i = 0; i < wa->dir_count; i++) {
|
|
DirectoryScanner* ds = directory_scanner_create(
|
|
wa->dirs[i], wa->use_metadata, wa->chunk_size, wa->exclude_patterns, wa->exclude_count,
|
|
wa->include_patterns, wa->include_count, wa->max_size, wa->min_size, wa->max_depth,
|
|
wa->follow_symlinks, wa->copy_links, wa->safe_links, wa->copy_unsafe_links);
|
|
Chunk* chunk;
|
|
while ((chunk = directory_scanner_next(ds)) != NULL) {
|
|
queue_enqueue_multithreaded(wa->ps->result_queue, chunk, &wa->ps->result_mutex,
|
|
&wa->ps->result_not_empty, &wa->ps->result_not_full);
|
|
}
|
|
directory_scanner_destroy(ds);
|
|
free(wa->dirs[i]);
|
|
}
|
|
ParallelScanner* ps = wa->ps;
|
|
free(wa->dirs);
|
|
free(wa);
|
|
mtx_lock(&ps->result_mutex);
|
|
ps->completed++;
|
|
if (ps->completed >= ps->num_threads) {
|
|
ps->done = true;
|
|
cnd_signal(&ps->result_not_empty);
|
|
}
|
|
mtx_unlock(&ps->result_mutex);
|
|
return thrd_success;
|
|
}
|
|
|
|
ParallelScanner* parallel_scanner_create(char* root_directory, bool use_metadata,
|
|
unsigned long long chunk_size, char** exclude_patterns,
|
|
int exclude_count, char** include_patterns,
|
|
int include_count, unsigned long long max_size,
|
|
unsigned long long min_size, int max_depth,
|
|
int num_threads, bool follow_symlinks, bool copy_links,
|
|
bool safe_links, bool copy_unsafe_links) {
|
|
ParallelScanner* ps = calloc(1, sizeof(ParallelScanner));
|
|
if (!ps)
|
|
return NULL;
|
|
ps->result_queue = queue_create(100, chunk_destroy);
|
|
if (!ps->result_queue) {
|
|
free(ps);
|
|
return NULL;
|
|
}
|
|
if (mtx_init(&ps->result_mutex, mtx_plain) != thrd_success ||
|
|
cnd_init(&ps->result_not_empty) != thrd_success ||
|
|
cnd_init(&ps->result_not_full) != thrd_success) {
|
|
queue_destroy(ps->result_queue);
|
|
free(ps);
|
|
return NULL;
|
|
}
|
|
|
|
DIR* dir = opendir(root_directory);
|
|
if (!dir) {
|
|
perror("Could not open root directory for parallel scan");
|
|
parallel_scanner_destroy(ps);
|
|
return NULL;
|
|
}
|
|
|
|
ArrayList* root_files = array_list_create(file_destroy);
|
|
ArrayList* subdirs = array_list_create(free);
|
|
struct dirent* entry;
|
|
while ((entry = readdir(dir)) != NULL) {
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
continue;
|
|
char* cur_path = path_cat(root_directory, entry->d_name);
|
|
if (!cur_path)
|
|
continue;
|
|
struct stat lstats;
|
|
if (lstat(cur_path, &lstats) != 0) {
|
|
free(cur_path);
|
|
continue;
|
|
}
|
|
bool is_symlink = S_ISLNK(lstats.st_mode);
|
|
|
|
// Skip symlinks unless the user explicitly enabled following/copying them.
|
|
if (is_symlink && !follow_symlinks && !copy_links && !safe_links &&
|
|
!copy_unsafe_links) {
|
|
free(cur_path);
|
|
continue;
|
|
}
|
|
|
|
// --safe-links: reject symlinks pointing outside the source tree.
|
|
if (is_symlink && safe_links) {
|
|
char link_target[4096];
|
|
ssize_t len = readlink(cur_path, link_target, sizeof(link_target) - 1);
|
|
if (len < 0) {
|
|
free(cur_path);
|
|
continue;
|
|
}
|
|
link_target[len] = ' |