Skip to content
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.

Commit

Permalink
cache file pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Shpakovsky committed Sep 26, 2016
1 parent 27e0b7a commit a704236
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
COMPILER = gcc
FILESYSTEM_FILES = catfs.c
OUTPUT = catfs

build: $(FILESYSTEM_FILES)
$(COMPILER) $(FILESYSTEM_FILES) -o ssfs `pkg-config fuse --cflags --libs`
$(COMPILER) $(FILESYSTEM_FILES) -o $(OUTPUT) `pkg-config fuse --cflags --libs`

clean:
rm ssfs
rm $(OUTPUT)
35 changes: 18 additions & 17 deletions catfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ struct entry {

struct entry *entries;
int entries_len;
// fd cache
int fd;
int fd_i=-1;
// fs block size-1
//off_t block_size_1=511;
// properties of the file in fake FS
char *filename;
off_t filelen;
//struct stat stat;
struct stat filestat;

static int do_getattr( const char *path, struct stat *st )
{
Expand Down Expand Up @@ -94,13 +99,14 @@ static int do_read( const char *path, char *buffer, size_t size, off_t offset, s
while(bytes_left>0 && i<entries_len) {
off_t pos_in_file=offset-entries[i].start;
//printf( "entry %d, %d\n", i, pos_in_file);
int fd = open(entries[i].name, O_RDONLY);
// TODO: cache fd
if(fd_i!=i) {
if (fd != -1) close(fd);
fd = open(entries[i].name, O_RDONLY);
fd_i=i;
}
if (fd != -1) {
//printf( "reading %u bytes starting at %u to %u \n", bytes_left, pos_in_file, bytes_written);
pread(fd, buffer+bytes_written, bytes_left, pos_in_file);
close(fd);
//} else {
// NOTE: even if we didn't actually read the file,
// we still consider it "ok" result
// (relevant part in buffer will be zero-filled)
Expand Down Expand Up @@ -141,6 +147,12 @@ int main( int argc, char *argv[] )
line[--len] = '\0';
}
sscanf (line, "%" SCNd64, &(entries[i].size));
// round up to x*512
// http://stackoverflow.com/questions/2022179/c-quick-calculation-of-next-multiple-of-4
//off_t old=entries[i].size;
//entries[i].size = (entries[i].size + block_size_1) & ~block_size_1;
//int blk=entries[i].size/512;
//printf("old [%" PRId64 "], new [%" PRId64 "], blk [%d], new [%d]\n", old, entries[i].size, blk, blk*512);
entries[i].start=filepos;
filepos=entries[i].start+entries[i].size;
char *name = line;
Expand All @@ -151,17 +163,6 @@ int main( int argc, char *argv[] )
i++;
}
filelen=filepos;
fclose(fr); /* close the file prior to exiting the routine */
fclose(fr);
return fuse_main( argc-1, argv+1, &operations, NULL );
/*
off_t value;
sscanf (argv[2], "%" SCNd64, &(value));
int ret=BinarySearch(entries, entries_len, value);
printf("%" PRId64 "\n", value);
printf("%d\n", ret);
i=ret;
printf ("[%s] [%" PRId64 "][%" PRId64 "]\n", entries[i].name, entries[i].size, entries[i].start);
i++;
printf ("[%s] [%" PRId64 "][%" PRId64 "]\n", entries[i].name, entries[i].size, entries[i].start);
*/
}

0 comments on commit a704236

Please sign in to comment.