Skip to content

Commit

Permalink
Merge pull request #46 from yhoogstrate/using_old_c_style_realpath_an…
Browse files Browse the repository at this point in the history
…d_basename

Using old c style realpath and basename
  • Loading branch information
yhoogstrate authored Oct 31, 2019
2 parents 04eaa80 + 8657e8f commit a3e818d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
4 changes: 4 additions & 0 deletions include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ std::string std_string_nullbyte_safe(char *, size_t, size_t);
std::string std_string_nullbyte_safe(char *, size_t);

bool is_fasta_file(char *filename);

std::string basename_cpp(std::string);
std::string realpath_cpp(std::string);

3 changes: 2 additions & 1 deletion src/fastafs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ void fastafs::load(std::string afilename)
if(file.is_open()) {
// if a user can't compile this line, please replace it with C's
// 'realpath' function and delete/free afterwards and send a PR
this->filename = std::filesystem::canonical(afilename);// this path must be absolute because if stuff gets send to FUSE, paths are relative to the FUSE process and probably systemd initialization
//this->filename = std::filesystem::canonical(afilename);// this path must be absolute because if stuff gets send to FUSE, paths are relative to the FUSE process and probably systemd initialization
this->filename = realpath_cpp(afilename);

size = file.tellg();

Expand Down
6 changes: 4 additions & 2 deletions src/fuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ fuse_instance *parse_args(int argc, char **argv, char **argv_fuse)
std::string name; // prefix name of mounted fasta dict 2bit and fai files
if(fname.size() == 0) { // invalid mount argument, don't bind fastafs object
fname = std::string(argv[mount_target_arg]);
name = std::filesystem::path(fname).filename();
//name = std::filesystem::path(fname).filename();
name = basename_cpp(fname);

// remove .fastafs suffix
size_t lastindex = name.find_last_of(".");
Expand All @@ -492,7 +493,8 @@ fuse_instance *parse_args(int argc, char **argv, char **argv_fuse)
fi->f->load(fname);
fi->cache = fi->f->init_ffs2f(fi->padding, true);// allow mixed case
} else {
std::string basename = std::filesystem::path(std::string(argv[mount_target_arg])).filename();
std::string basename = basename_cpp(std::string(argv[mount_target_arg]));
//std::string basename = std::filesystem::path(std::string(argv[mount_target_arg])).filename();

fi->u2b = new ucsc2bit(basename);// useses basename as prefix for filenames to mount: hg19.2bit -> hg19.2bit.fa
fi->u2b->load(std::string(argv[mount_target_arg]));
Expand Down
8 changes: 7 additions & 1 deletion src/lsfastafs.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <filesystem>
#include <iostream>
#include <string.h>
#include <sys/xattr.h>

#include <unordered_map>
#include <vector>

#include "utils.hpp"
#include "config.hpp"


Expand Down Expand Up @@ -57,7 +59,11 @@ std::unordered_multimap<std::string, std::pair<std::string, std::string> > get_f
strstr(mount_dir, arg) != NULL)) {

std::string fn = std::string(mount_dir);
std::string basename = std::filesystem::path(fn).filename();

std::string basename = basename_cpp(fn);
//std::string basename = std::filesystem::path(fn).filename();
//std::cout << "basename: " << basename << "\n";

std::string dict_fn = std::string(mount_dir) + "/" + basename + ".dict";

if(getxattr(mount_dir, FASTAFS_FILE_XATTR_NAME.c_str(), xattr_fastafs_file, 255) != -1
Expand Down
3 changes: 2 additions & 1 deletion src/ucsc2bit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ void ucsc2bit::load(std::string afilename)
if(file.is_open()) {
// if a user can't compile this line, please replace it with C's
// 'realpath' function and delete/free afterwards and send a PR
this->filename = std::filesystem::canonical(afilename);// this path must be absolute because if stuff gets send to FUSE, paths are relative to the FUSE process and probably systemd initialization
//this->filename = std::filesystem::canonical(afilename);// this path must be absolute because if stuff gets send to FUSE, paths are relative to the FUSE process and probably systemd initialization
this->filename = realpath_cpp(afilename);

if(file.tellg() < 16) {
file.close();
Expand Down
31 changes: 31 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

#include <libgen.h>
#include <string.h>
#include <iostream>

#include <openssl/sha.h>
Expand Down Expand Up @@ -170,3 +172,32 @@ bool is_fasta_file(char *filename)
return false;
}


// https://www.systutorials.com/241216/how-to-get-the-directory-path-and-file-name-from-a-absolute-path-in-c-on-linux/
// https://stackoverflow.com/questions/38456127/what-is-the-value-of-cplusplus-for-c17 - THEN use std::filesystem::path(filename).filename();
std::string basename_cpp(std::string fn) {
char* ts = strdup(fn.c_str());

//char* dir = dirname(ts1);
char* filename = basename(ts);
//std::string filenamepp = std::string(filename);

//printf("basename: [%s]\n", filename);
//std::cout << "basenamepp: |" << filenamepp << "|\n";

return std::string(filename);
}


// https://www.linuxquestions.org/questions/programming-9/how-to-get-the-full-path-of-a-file-in-c-841046/
// https://stackoverflow.com/questions/38456127/what-is-the-value-of-cplusplus-for-c17 - THEN use std::filesystem::canonical(filename)
std::string realpath_cpp(std::string fn) {
//std::string out = "asd";
char *path = realpath(fn.c_str(), NULL);
//printf("realpath: [%s]\n", path);

//std::string realpathpp = std::string(path);
//std::cout << "realpath: |" << realpathpp << "|\n";

return std::string(path);
}

0 comments on commit a3e818d

Please sign in to comment.