Skip to content

Commit

Permalink
In our documentation we claim that the size of each file in bytes
Browse files Browse the repository at this point in the history
is also recorded, something which wasn't true up until now.
This commit aims to correct that.

Signed-off-by: Fotios Valasiadis <[email protected]>
  • Loading branch information
fvalasiad committed Apr 2, 2023
1 parent 6c79eb7 commit 3e30a1e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/record.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ record_hash(char *foutname, char *hash)
record_triple(foutname, "b:hash", hash, true);
}

void
record_size(char *foutname, size_t sz)
{
char size[32];

sprintf(size, "%lu", sz);
record_triple(foutname, "b:size", size, false);
}

void
record_process_create(char *p1outname, char *p2outname)
{
Expand Down
1 change: 1 addition & 0 deletions src/record.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ void record_rename(char *poutname, char *from_foutname, char *to_foutname);
void record_file(char *foutname, char *path, char *abspath);
void record_fileuse(char *poutname, char *foutname, int purpose);
void record_hash(char *foutname, char *hash);
void record_size(char *foutname, size_t sz);
void record_process_create(char *p1outname, char *p2outname);
void record_exec(char *poutname, char *foutname);
36 changes: 30 additions & 6 deletions src/tracer.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ pinfo_new(PROCESS_INFO *self, char ignore_one_sigstop)
}

void
finfo_new(FILE_INFO *self, char *path, char *abspath, char *hash)
finfo_new(FILE_INFO *self, char *path, char *abspath, char *hash, size_t sz)
{
static int fcount = 0;

self->path = path;
self->abspath = abspath;
self->hash = hash;
self->size = sz;
sprintf(self->outname, ":f%d", fcount++);
}

Expand Down Expand Up @@ -264,6 +265,19 @@ find_in_path(char *path)
return ret;
}

static size_t
get_file_size(char *fname)
{
struct stat fstat;

if (stat(fname, &fstat)) {
error(0, errno, "getting `%s' size", fname);
return -1;
}

return fstat.st_size;
}

static void
handle_open(pid_t pid, PROCESS_INFO *pi, int fd, int dirfd, void *path,
int purpose)
Expand All @@ -278,21 +292,23 @@ handle_open(pid_t pid, PROCESS_INFO *pi, int fd, int dirfd, void *path,

if ((purpose & O_ACCMODE) == O_RDONLY) {
char *hash = get_file_hash(abspath);
size_t sz = get_file_size(abspath);

f = find_finfo(abspath, hash);
if (!f) {
f = next_finfo();
finfo_new(f, path, abspath, hash);
finfo_new(f, path, abspath, hash, sz);
record_file(f->outname, path, abspath);
record_hash(f->outname, hash);
record_size(f->outname, sz);
} else {
free(path);
free(abspath);
free(hash);
}
} else {
f = pinfo_next_finfo(pi, fd);
finfo_new(f, path, abspath, NULL);
finfo_new(f, path, abspath, NULL, -1);
record_file(f->outname, path, abspath);
}

Expand All @@ -319,15 +335,17 @@ handle_execve(pid_t pid, PROCESS_INFO *pi, int dirfd, char *path)
}

char *hash = get_file_hash(abspath);
size_t sz = get_file_size(abspath);

FILE_INFO *f;

if (!(f = find_finfo(abspath, hash))) {
f = next_finfo();

finfo_new(f, path, abspath, hash);
finfo_new(f, path, abspath, hash, sz);
record_file(f->outname, path, abspath);
record_hash(f->outname, f->hash);
record_size(f->outname, sz);
} else {
free(abspath);
free(hash);
Expand All @@ -342,14 +360,16 @@ handle_rename_entry(pid_t pid, PROCESS_INFO *pi, int olddirfd, char *oldpath)
{
char *abspath = absolutepath(pid, olddirfd, oldpath);
char *hash = get_file_hash(abspath);
size_t sz = get_file_size(abspath);

FILE_INFO *f = find_finfo(abspath, hash);

if (!f) {
f = next_finfo();
finfo_new(f, oldpath, abspath, hash);
finfo_new(f, oldpath, abspath, hash, sz);
record_file(f->outname, oldpath, abspath);
record_hash(f->outname, f->hash);
record_size(f->outname, sz);
} else {
free(oldpath);
free(abspath);
Expand All @@ -370,9 +390,10 @@ handle_rename_exit(pid_t pid, PROCESS_INFO *pi, int newdirfd, char *newpath)

FILE_INFO *to = next_finfo();

finfo_new(to, newpath, abspath, from->hash);
finfo_new(to, newpath, abspath, from->hash, from->size);
record_file(to->outname, newpath, abspath);
record_hash(to->outname, to->hash);
record_size(to->outname, to->size);

record_rename(pi->outname, from->outname, to->outname);
}
Expand Down Expand Up @@ -479,7 +500,10 @@ handle_syscall_exit(pid_t pid, PROCESS_INFO *pi,

if (f != NULL) {
f->hash = get_file_hash(f->abspath);
f->size = get_file_size(f->abspath);

record_hash(f->outname, f->hash);
record_size(f->outname, f->size);

// Add it to global cache list
*next_finfo() = *f;
Expand Down
1 change: 1 addition & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ typedef struct {
char *path;
char *abspath;
char *hash;
size_t size;
char outname[16];
} FILE_INFO;

Expand Down

0 comments on commit 3e30a1e

Please sign in to comment.