single layer
This commit is contained in:
parent
f59216922c
commit
f1652cd9f5
4 changed files with 133 additions and 63 deletions
BIN
bin/main
BIN
bin/main
Binary file not shown.
100
src/main.c
100
src/main.c
|
|
@ -21,46 +21,18 @@ void check_args(int argc, char *argv[]) {
|
|||
}
|
||||
scanned_path=realpath(_scanned_path, NULL);
|
||||
}
|
||||
// ulong file_size(char *path) {
|
||||
// struct stat file_stat;
|
||||
// Helper to create child paths safely
|
||||
char* make_child_path(const char* parent, const char* child) {
|
||||
size_t parent_len = strlen(parent);
|
||||
size_t child_len = strlen(child);
|
||||
size_t total = parent_len + child_len + 2; // +2 for '/' and null term
|
||||
|
||||
// if (stat(path, &file_stat) == -1) {
|
||||
// perror("stat failed");
|
||||
// return -1; // Return error
|
||||
// }
|
||||
char* path = malloc(total);
|
||||
if (!path) return NULL;
|
||||
|
||||
// if (!S_ISREG(file_stat->st_mode)) {
|
||||
// fputs("Not a regular file\n", stderr);
|
||||
// return -1; // Not a regular file
|
||||
// }
|
||||
|
||||
// return file_stat->st_size; // Return size in bytes
|
||||
// }
|
||||
// bool is_dir(char *path) {
|
||||
// struct stat file_stat;
|
||||
|
||||
// if (stat(path, &file_stat) == -1) {
|
||||
// perror("stat failed");
|
||||
// return false; // Return error
|
||||
// }
|
||||
|
||||
// return S_ISDIR(file_stat->st_mode);
|
||||
// }
|
||||
// ulong cluster_size(char *path) {
|
||||
// struct stat file_stat;
|
||||
|
||||
// if (stat(path, &file_stat) == -1) {
|
||||
// perror("stat failed");
|
||||
// return -1; // Return error
|
||||
// }
|
||||
|
||||
// if (!S_ISREG(file_stat->st_mode)) {
|
||||
// fputs("Not a regular file\n", stderr);
|
||||
// return -1; // Not a regular file
|
||||
// }
|
||||
|
||||
// return file_stat->st_blocks * 512;
|
||||
// }
|
||||
snprintf(path, total, "%s/%s", parent, child);
|
||||
return path;
|
||||
}
|
||||
void ls(struct path_node *node, char *path) {
|
||||
// path
|
||||
if(node->path==NULL) node->path=path;
|
||||
|
|
@ -100,36 +72,38 @@ void ls(struct path_node *node, char *path) {
|
|||
if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
|
||||
continue;
|
||||
|
||||
// Print filename (no path, no metadata)
|
||||
//
|
||||
char *child_path=node->path;
|
||||
strcat(child_path, "/");
|
||||
strcat(child_path, d->d_name);
|
||||
|
||||
struct stat *file_stat=malloc(sizeof(struct stat));
|
||||
// bool is_dir;
|
||||
if (stat(child_path, file_stat) == -1) {
|
||||
perror("stat failed");
|
||||
// Create child path safely
|
||||
char* child_path = make_child_path(node->path, d->d_name);
|
||||
if (!child_path) {
|
||||
perror("malloc failed");
|
||||
continue;
|
||||
}
|
||||
|
||||
struct stat file_stat;
|
||||
if (lstat(child_path, &file_stat) == -1) { // Use lstat to avoid following symlinks
|
||||
perror("stat failed");
|
||||
free(child_path);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Determine file type
|
||||
const char* type = "OTHER";
|
||||
|
||||
long path_size=0;
|
||||
switch (file_stat->st_mode & S_IFMT) {
|
||||
case S_IFREG:
|
||||
path_size=file_stat->st_size;
|
||||
printf("FILE\t%ld\t%s\n", path_size, child_path);
|
||||
break;
|
||||
case S_IFDIR:
|
||||
path_size=file_stat->st_blocks*512;
|
||||
printf("DIR\t%ld\t%s/\n", path_size, child_path);
|
||||
break;
|
||||
default:
|
||||
printf("UNKNOWN\t%ld\t%s\n", path_size, child_path);
|
||||
break;
|
||||
|
||||
if (S_ISREG(file_stat.st_mode)) {
|
||||
type = "FILE";
|
||||
path_size = file_stat.st_size;
|
||||
}
|
||||
else if (S_ISDIR(file_stat.st_mode)) {
|
||||
type = "DIR";
|
||||
path_size = file_stat.st_blocks * 512;
|
||||
}
|
||||
// Add more types (LNK, FIFO, etc) later?
|
||||
|
||||
// store to struct
|
||||
printf("%s\t%ld\t%s\n", type, path_size, d->d_name);
|
||||
free(child_path);
|
||||
// free(file_stat);
|
||||
// free(d);
|
||||
printf("next...\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
57
src/tmp1.c
Normal file
57
src/tmp1.c
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
// Helper to create child paths safely
|
||||
char* make_child_path(const char* parent, const char* child) {
|
||||
size_t parent_len = strlen(parent);
|
||||
size_t child_len = strlen(child);
|
||||
size_t total = parent_len + child_len + 2; // +2 for '/' and null term
|
||||
|
||||
char* path = malloc(total);
|
||||
if (!path) return NULL;
|
||||
|
||||
snprintf(path, total, "%s/%s", parent, child);
|
||||
return path;
|
||||
}
|
||||
|
||||
// Inside your directory processing loop:
|
||||
d = (struct linux_dirent64*)(buf + bpos);
|
||||
bpos += d->d_reclen;
|
||||
|
||||
// Skip . and ..
|
||||
if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
|
||||
continue;
|
||||
|
||||
// Create child path safely
|
||||
char* child_path = make_child_path(node->path, d->d_name);
|
||||
if (!child_path) {
|
||||
perror("malloc failed");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Use stack variable for stat (no allocation)
|
||||
struct stat file_stat;
|
||||
if (lstat(child_path, &file_stat) == -1) { // Use lstat to avoid following symlinks
|
||||
perror("stat failed");
|
||||
free(child_path);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Determine file type
|
||||
const char* type = "OTHER";
|
||||
long path_size = 0;
|
||||
|
||||
if (S_ISREG(file_stat.st_mode)) {
|
||||
type = "FILE";
|
||||
path_size = file_stat.st_size;
|
||||
}
|
||||
else if (S_ISDIR(file_stat.st_mode)) {
|
||||
type = "DIR";
|
||||
// For directory metadata size only (not contents!)
|
||||
path_size = file_stat.st_blocks * 512;
|
||||
}
|
||||
// Add more types (LNK, FIFO, etc) as needed
|
||||
|
||||
printf("%s\t%ld\t%s\n", type, path_size, d->d_name);
|
||||
|
||||
// Cleanup
|
||||
free(child_path);
|
||||
*/
|
||||
39
src/tmp2.c
Normal file
39
src/tmp2.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
d = (struct linux_dirent64 *)(buf + bpos);
|
||||
bpos += d->d_reclen;
|
||||
|
||||
// Skip . and .. entries
|
||||
if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
|
||||
continue;
|
||||
|
||||
// Print filename (no path, no metadata)
|
||||
//
|
||||
char *child_path=node->path;
|
||||
strcat(child_path, "/");
|
||||
strcat(child_path, d->d_name);
|
||||
|
||||
struct stat *file_stat=malloc(sizeof(struct stat));
|
||||
// bool is_dir;
|
||||
if (stat(child_path, file_stat) == -1) {
|
||||
perror("stat failed");
|
||||
}
|
||||
|
||||
long path_size = 0;
|
||||
switch (file_stat->st_mode & S_IFMT) {
|
||||
case S_IFREG:
|
||||
path_size=file_stat->st_size;
|
||||
printf("FILE\t%ld\t%s\n", path_size, child_path);
|
||||
break;
|
||||
case S_IFDIR:
|
||||
path_size=file_stat->st_blocks*512;
|
||||
printf("DIR\t%ld\t%s/\n", path_size, child_path);
|
||||
break;
|
||||
default:
|
||||
printf("UNKNOWN\t%ld\t%s\n", path_size, child_path);
|
||||
break;
|
||||
}
|
||||
free(child_path);
|
||||
// free(file_stat);
|
||||
// free(d);
|
||||
printf("next...\n");
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue