Skip to content

Commit

Permalink
Reduces count of open and close calls on fd
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorWeders committed Nov 12, 2024
1 parent 99f6c4e commit 0c6f56f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 16 deletions.
3 changes: 2 additions & 1 deletion include/filetypes/checktypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ struct laser_magicnumber

int laser_checktype(const char *filename,
const struct laser_magicnumber formats[]);

int laser_checktype_ex(int fd, const char *filename,
const struct laser_magicnumber formats[]);
extern const struct laser_magicnumber laser_archiveformats[];
extern const struct laser_magicnumber laser_mediaformats[];
extern const struct laser_magicnumber laser_documentformats[];
Expand Down
76 changes: 65 additions & 11 deletions src/filetypes/checktype.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@
#include <string.h>
#include <unistd.h>

int laser_checktype(const char *filename,
const struct laser_magicnumber formats[])
enum check_type_return
{
Success = 0,
NotOpened,
CannotRead,
Unknown
};
static int laser_checktype_fd(int fd, const struct laser_magicnumber formats[])
{
int fd = open(filename, O_RDONLY);
if (fd == -1)
{
perror("open");
return 0;
return NotOpened;
}

// should be updated if there is a magic number larger than 8 bits
unsigned char buffer[8];

ssize_t bytesRead = read(fd, buffer, sizeof(buffer));
close(fd);

if (bytesRead < 0)
{
fprintf(stderr, "laser: cannot read %s", filename);
return 0;
return CannotRead;
}

size_t i = 0;
Expand All @@ -33,9 +34,62 @@ int laser_checktype(const char *filename,
{
if (bytesRead >= formats[i].magic_size &&
memcmp(buffer, formats[i].magic, formats[i].magic_size) == 0)
return 1;
return Success;
i++;
}

return 0;
return Unknown;
}

int laser_checktype(const char *filename,
const struct laser_magicnumber formats[])
{
int fd = open(filename, O_RDONLY);
if (fd == -1)
{
perror("open");
return 0;
}
int rv = laser_checktype_fd(fd, formats);
// should be updated if there is a magic number larger than 8 bits
switch (rv)
{
case NotOpened:
perror("open");
break;
case CannotRead:
fprintf(stderr, "laser: cannot read %s", filename);
break;
//Fallthrough
default:
break;
}
close(fd);
return rv == Success;
}

int laser_checktype_ex(int fd, const char *filename,
const struct laser_magicnumber formats[])
{

if (fd == -1)
{
perror("open");
return 0;
}
int rv = laser_checktype_fd(fd, formats);
// should be updated if there is a magic number larger than 8 bits
switch (rv)
{
case NotOpened:
perror("open");
break;
case CannotRead:
fprintf(stderr, "laser: cannot read %s", filename);
break;
//Fallthrough
default:
break;
}
return rv == Success;
}
11 changes: 7 additions & 4 deletions src/laser.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "filetypes/checktypes.h"
#include "git/lgit.h"
#include "utils.h"

#include <fcntl.h>
#define BRANCH_SIZE 8

char *strip_parent_dir(const char *full_path, const char *parent_dir)
Expand Down Expand Up @@ -142,6 +142,8 @@ void laser_process_entries(laser_opts opts, int depth, int max_depth,
}
else
{
int fd = open(full_path, O_RDONLY);

if (S_ISLNK(file_stat.st_mode) && opts.show_symlinks)
{
char symlink_target[PATH_MAX];
Expand All @@ -163,17 +165,17 @@ void laser_process_entries(laser_opts opts, int depth, int max_depth,
LASER_COLORS[LASER_COLOR_EXEC].value, indent,
depth, is_last);

else if (laser_checktype(full_path, laser_archiveformats))
else if (laser_checktype_ex(fd, full_path, laser_archiveformats))
laser_print_entry(entries[i]->d_name,
LASER_COLORS[LASER_COLOR_ARCHIVE].value,
indent, depth, is_last);

else if (laser_checktype(full_path, laser_mediaformats))
else if (laser_checktype_ex(fd, full_path, laser_mediaformats))
laser_print_entry(entries[i]->d_name,
LASER_COLORS[LASER_COLOR_MEDIA].value, indent,
depth, is_last);

else if (laser_checktype(full_path, laser_documentformats))
else if (laser_checktype_ex(fd, full_path, laser_documentformats))
laser_print_entry(entries[i]->d_name,
LASER_COLORS[LASER_COLOR_DOCUMENT].value,
indent, depth, is_last);
Expand All @@ -187,6 +189,7 @@ void laser_process_entries(laser_opts opts, int depth, int max_depth,
laser_print_entry(entries[i]->d_name,
LASER_COLORS[LASER_COLOR_FILE].value, indent,
depth, is_last);
close(fd);
}

free(entries[i]);
Expand Down

0 comments on commit 0c6f56f

Please sign in to comment.