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

Commit

Permalink
bugfixes: use absolute paths and -s switch for single-threaded mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Shpakovsky committed Oct 2, 2016
1 parent a704d97 commit 46e3945
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Usage

* To mount, from the same directory:

./catfs {list file} {mount point}
./catfs -s {list file} {mount point}

You will see `{mount point}/archive.cat` file.
It should be identical to the one produced by this command:
Expand Down Expand Up @@ -49,7 +49,7 @@ or:

cd backup/
find . -type f -printf "%s %P\n" >cat.fs
mkdir mount && ~/catfs cat.fs mount
mkdir mount && ~/catfs -s cat.fs mount
par2create -r1 -n1 par2 mount/archive.cat
fusermount -u mount && rmdir mount

Expand All @@ -74,7 +74,7 @@ In case of damage to any file (say, `important/data.txt`),
you can recover it this way:

cd backup/
mkdir mount && ~/catfs cat.fs mount
mkdir mount && ~/catfs -s cat.fs mount
cp mount/archive.cat archive.cat
fusermount -u mount && rmdir mount
par2recover par2 archive.cat
Expand Down
45 changes: 40 additions & 5 deletions catfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <inttypes.h>
#include <linux/limits.h>
#include <errno.h>


struct entry {
char *name;
Expand All @@ -20,10 +22,12 @@ struct entry {
struct entry *entries;
int entries_len;
// fd cache
int fd;
int fd=-1;
int fd_i=-1;
// fs block size-1
//off_t block_size_1=511;
//pwd where the catfs was started from
char *pwd;
// properties of the file in fake FS
char *filename;
off_t filelen;
Expand Down Expand Up @@ -98,15 +102,18 @@ 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);
//printf( "entry %d, %d, %s\n", i, pos_in_file, entries[i].name);
if(fd_i!=i) {
if (fd != -1) close(fd);
//printf("opening\n");
fd = open(entries[i].name, O_RDONLY);
//if (fd==-1) printf("errno=%d\n",errno);
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);
int res=pread(fd, buffer+bytes_written, bytes_left, pos_in_file);
//printf( "read %u bytes\n", res);
// 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 @@ -140,12 +147,34 @@ void usage(char *name) {
,name, "%s");
}


//from rofs-filtered
static char* concat_path(const char* p1, const char* p2) {
size_t p1len = strlen(p1);
size_t p2len = strlen(p2);
// Need room for path separator and null terminator
char *path = malloc(p1len + p2len + 2);
if (!path) return NULL;
strcpy(path, p1);
if ((path[p1len - 1] != '/') && (p2[0] != '/')) {
// Add a "/" if neither p1 nor p2 has it.
strcat(path, "/");
} else if (path[p1len - 1] == '/') {
// If p1 ends in '/', we don't need it from p2
while (p2[0] == '/') p2++;
}
strcat(path, p2);
return path;
}

int main( int argc, char *argv[] ) {
if(argc<2) {
usage(argv[0]);
return;
}
char line[PATH_MAX];
pwd=get_current_dir_name();
//printf("Current working dir: %s\n", pwd);
FILE *fr = fopen (argv[1], "rt");
if(fr == NULL) {
usage(argv[0]);
Expand Down Expand Up @@ -177,8 +206,14 @@ int main( int argc, char *argv[] ) {
char *name = line;
while (*name != ' ') name++;
name++;
entries[i].name = malloc(strlen(name)+1);
strcpy(entries[i].name, name);
if(name[0]=='/') {
entries[i].name = malloc(strlen(name)+1);
strcpy(entries[i].name, name);
//printf("direct copy absolute filename: [%s]\n", entries[i].name);
} else {
entries[i].name = concat_path(pwd, name);
//printf("relative filename: [%s]\n", entries[i].name);
}
i++;
}
filelen=filepos;
Expand Down

0 comments on commit 46e3945

Please sign in to comment.