This commit is contained in:
TheK0tYaRa 2025-06-15 09:32:47 +03:00
parent 9b877b222a
commit f59216922c
5 changed files with 146 additions and 4 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
./.vscode
./bin

BIN
bin/main

Binary file not shown.

View file

@ -1,5 +1,15 @@
#include "main.h"
//
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdbool.h>
//
char *scanned_path;
//
void check_args(int argc, char *argv[]) {
@ -11,11 +21,130 @@ void check_args(int argc, char *argv[]) {
}
scanned_path=realpath(_scanned_path, NULL);
}
// ulong file_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_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;
// }
void ls(struct path_node *node, char *path) {
// path
if(node->path==NULL) node->path=path;
// temporary size
node->size=0;
// children
int fd;
char buf[BUF_SIZE];
long nread;
struct linux_dirent64 *d;
int bpos;
// Open directory
fd = open(node->path, O_RDONLY | O_DIRECTORY);
if (fd == -1) {
perror("open");
return;
}
while (1) {
// Read directory entries using system call
nread = syscall(SYS_getdents64, fd, buf, BUF_SIZE);
if (nread == -1) {
perror("getdents64");
break;
}
if (nread == 0) // End of directory
break;
// Process each entry in buffer
for (bpos = 0; bpos < nread;) {
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");
}
}
close(fd);
}
int main(int argc, char *argv[]) {
check_args(argc, argv);
printf("scanned path: %s\n",scanned_path);
printf("scanning:\t%s\n",scanned_path);
//
path_node *parent_node=alloc;
struct path_node *parent_node=malloc(sizeof(struct path_node));
ls(parent_node, scanned_path);
// recursive_size(parent_node);
//
// print parent_node verbosely
free(parent_node);
//
return 0;
}

View file

@ -1,6 +1,9 @@
#ifndef __main_h__
#define __main_h__
//
#define BUF_SIZE 4096
//
#include <stdint.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
@ -9,8 +12,16 @@
//
struct path_node {
char *path;
uint size;
struct path_node *next;
ulong size;
struct path_node *children[];
};
// Structure matching the kernel's getdents64 system call
struct linux_dirent64 {
uint64_t d_ino; /* Inode number */
int64_t d_off; /* Offset to next structure */
unsigned short d_reclen; /* Length of this dirent */
unsigned char d_type; /* File type */
char d_name[]; /* Null-terminated filename */
};
//
#endif