Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
super basic 'ls' and 'cat' for reading sqlfs files without mounting them
* both only take a single file arg * 'ls' only shows the file names * both currently only work with unencrypted sqlfs.db files
- Loading branch information
Showing
5 changed files
with
165 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdlib.h> | ||
#include <fcntl.h> | ||
#include <sys/stat.h> | ||
#include <sys/types.h> | ||
|
||
#include "sqlfs.h" | ||
|
||
#define BUF_SIZE 8192 | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int n; | ||
char buf[BUF_SIZE]; | ||
struct stat s; | ||
sqlfs_t *sqlfs = 0; | ||
if (argc != 3) | ||
{ | ||
fprintf(stderr, "Usage: %s sqlfs.db /path/to/file/to/cat\n", argv[0]); | ||
exit(1); | ||
} | ||
const char *db = argv[1]; | ||
const char *file = argv[2]; | ||
|
||
/* first setup the database file */ | ||
if(access(db, R_OK)) | ||
{ | ||
fprintf(stderr, "sqlfs file is not readable! (%s)\n", db); | ||
exit(1); | ||
} | ||
if(stat(db, &s) < 0) | ||
{ | ||
fprintf(stderr, "stat on %s failed!\n", db); | ||
exit(1); | ||
} | ||
if(S_ISREG(s.st_mode)<0) | ||
{ | ||
fprintf(stderr, "Not a regular file: %s\n", db); | ||
exit(1); | ||
} | ||
if (!sqlfs_open(db, &sqlfs)) { | ||
fprintf(stderr, "Failed to open: %s\n", db); | ||
return 1; | ||
} | ||
sqlfs_init(db); | ||
|
||
// TODO memset(0) the key after its passed to sqlfs | ||
|
||
if (!sqlfs_proc_access(sqlfs, file, R_OK)) { | ||
fprintf(stderr, "Cannot access %s in %s\n", file, db); | ||
//return 1; | ||
} | ||
|
||
|
||
/* now read the file from sqlfs */ | ||
struct fuse_file_info ffi; | ||
int wrote = 0; // this is used to track were we are in the read | ||
sqlfs_proc_open(sqlfs, file, &ffi); | ||
while((n = sqlfs_proc_read(sqlfs, file, buf, BUF_SIZE, wrote, &ffi)) > 0) | ||
{ | ||
wrote += write(1, buf, n); // write to stdout | ||
} | ||
|
||
sqlfs_close(sqlfs); | ||
// TODO return proper exit value | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <iostream> | ||
|
||
#include "sqlfs.h" | ||
|
||
typedef std::vector<std::string> DirEntries; | ||
|
||
/* FUSE filler() function for use with the FUSE style readdir() that | ||
* libsqlfs provides. Note: this function only ever expects statp to | ||
* be NULL and off to be 0. buf is DirEntries */ | ||
static int fill_dir(void *buf, const char *name, const struct stat *statp, off_t off) { | ||
DirEntries *entries = (DirEntries*) buf; | ||
if(statp != NULL) | ||
fprintf(stderr, "File.listImpl() fill_dir always expects statp to be NULL"); | ||
if(off != 0) | ||
fprintf(stderr, "File.listImpl() fill_dir always expects off to be 0"); | ||
entries->push_back(name); | ||
// TODO implement returning an error (1) if something bad happened | ||
return 0; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
int n; | ||
struct stat s; | ||
sqlfs_t *sqlfs = 0; | ||
const char *db; | ||
const char *file; | ||
if (argc == 2) | ||
{ | ||
db = argv[1]; | ||
file = "/"; | ||
} | ||
else if (argc != 3) | ||
{ | ||
fprintf(stderr, "Usage: %s sqlfs.db [ /path ]\n", argv[0]); | ||
exit(1); | ||
} | ||
else | ||
{ | ||
db = argv[1]; | ||
file = argv[2]; | ||
} | ||
|
||
/* first setup the database file */ | ||
if(access(db, R_OK)) | ||
{ | ||
fprintf(stderr, "sqlfs file is not readable! (%s)\n", db); | ||
exit(1); | ||
} | ||
if(stat(db, &s) < 0) | ||
{ | ||
fprintf(stderr, "stat on %s failed!\n", db); | ||
exit(1); | ||
} | ||
if(S_ISREG(s.st_mode)<0) | ||
{ | ||
fprintf(stderr, "Not a regular file: %s\n", db); | ||
exit(1); | ||
} | ||
if (!sqlfs_open(db, &sqlfs)) { | ||
fprintf(stderr, "Failed to open: %s\n", db); | ||
return 1; | ||
} | ||
sqlfs_init(db); | ||
// TODO memset(0) the key after its passed to sqlfs | ||
|
||
/* now read the dir entries */ | ||
DirEntries entries; | ||
// using FUSE readdir in old getdir() style which gives us the whole thing at once | ||
int ret = sqlfs_proc_readdir(0, file, (void *)&entries, (fuse_fill_dir_t)fill_dir, 0, NULL); | ||
for(DirEntries::const_iterator i = entries.begin() + 2; i != entries.end(); ++i) { | ||
std::cout << *i << "\n"; // this will print all the contents of *features* | ||
} | ||
|
||
sqlfs_close(sqlfs); | ||
|
||
return abs(ret); // FUSE returns negative error values | ||
} |