Skip to content

Also record size of each file #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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