core: Track current offset
Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
36
src/main.c
36
src/main.c
@@ -1,6 +1,8 @@
|
||||
#define _DEFAULT_SOURCE
|
||||
#include <sys/stat.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
@@ -20,12 +22,14 @@ static const char *output_name = "out.cav";
|
||||
* @dir: Opened directory
|
||||
* @pass_count: Number of passes made in file
|
||||
* @file_count: Number of files encountered including subdirectories
|
||||
* @cur_off: Current offset tracker
|
||||
*/
|
||||
struct cav_state {
|
||||
int dir_fd;
|
||||
DIR *dir;
|
||||
uint8_t pass_count;
|
||||
size_t file_count;
|
||||
uint32_t cur_off;
|
||||
};
|
||||
|
||||
static void
|
||||
@@ -53,6 +57,30 @@ version(void)
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Obtain file size from path
|
||||
*
|
||||
* @path: Path of file to obtain size from
|
||||
*
|
||||
* Returns number of bytes in file
|
||||
*/
|
||||
static size_t
|
||||
get_file_size(const char *path)
|
||||
{
|
||||
struct stat sb;
|
||||
|
||||
if (path == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (stat(path, &sb) < 0) {
|
||||
perror("stat");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return sb.st_size;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize the cav state machine
|
||||
*
|
||||
@@ -133,7 +161,9 @@ consume_scan(const char *dir_path, struct cav_state *state, DIR *subdir)
|
||||
closedir(subdir);
|
||||
break;
|
||||
default:
|
||||
snprintf(buf, sizeof(buf), "%s/%s", dir_path, dirent->d_name);
|
||||
++state->file_count;
|
||||
state->cur_off += get_file_size(buf);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -155,7 +185,11 @@ consume_pass(struct cav_state *state)
|
||||
case 0:
|
||||
/* Scan pass */
|
||||
consume_scan(input_name, state, NULL);
|
||||
printf("[pass 0] scanned %zu files\n", state->file_count);
|
||||
printf(
|
||||
"[pass 0] scanned %zu files [%d bytes]\n",
|
||||
state->file_count,
|
||||
state->cur_off
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user