Skip to content

Commit

Permalink
use runtime path for all file operations (OSPi/Bo/Linux)
Browse files Browse the repository at this point in the history
  • Loading branch information
rayshobby committed Sep 15, 2015
1 parent 7cb9528 commit 560db55
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
12 changes: 6 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,15 +921,15 @@ void write_log(byte type, ulong curr_time) {
}
#else // prepare log folder for RPI/BBB
struct stat st;
if(stat(LOG_PREFIX, &st)) {
if(mkdir(LOG_PREFIX, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH)) {
if(stat(get_filename_fullpath(LOG_PREFIX), &st)) {
if(mkdir(get_filename_fullpath(LOG_PREFIX), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH)) {
return;
}
}
FILE *file;
file = fopen(tmp_buffer, "rb+");
file = fopen(get_filename_fullpath(tmp_buffer), "rb+");
if(!file) {
file = fopen(tmp_buffer, "wb");
file = fopen(get_filename_fullpath(tmp_buffer), "wb");
if (!file) return;
}
fseek(file, 0, SEEK_END);
Expand Down Expand Up @@ -1009,11 +1009,11 @@ void delete_log(char *name) {
#else // delete_log implementation for RPI/BBB
if (strncmp(name, "all", 3) == 0) {
// delete the log folder
rmdir(LOG_PREFIX);
rmdir(get_filename_fullpath(LOG_PREFIX));
return;
} else {
make_logfile_name(name);
remove(tmp_buffer);
remove(get_filename_fullpath(tmp_buffer));
}
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ byte server_json_log(char *p) {
file.open(tmp_buffer, O_READ);
#else // prepare to open log file for RPI/BBB
FILE *file;
file = fopen(tmp_buffer, "rb");
file = fopen(get_filename_fullpath(tmp_buffer), "rb");
if(!file) continue;
#endif // prepare to open log file

Expand Down
50 changes: 31 additions & 19 deletions utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void remove_file(const char *name) {

#else // RPI/BBB/LINUX
void nvm_read_block(void *dst, const void *src, int len) {
FILE *fp = fopen(NVM_FILENAME, "rb");
FILE *fp = fopen(get_filename_fullpath(NVM_FILENAME), "rb");
if(fp) {
fseek(fp, (unsigned int)src, SEEK_SET);
fread(dst, 1, len, fp);
Expand All @@ -88,9 +88,9 @@ void nvm_read_block(void *dst, const void *src, int len) {
}

void nvm_write_block(const void *src, void *dst, int len) {
FILE *fp = fopen(NVM_FILENAME, "rb+");
FILE *fp = fopen(get_filename_fullpath(NVM_FILENAME), "rb+");
if(!fp) {
fp = fopen(NVM_FILENAME, "wb");
fp = fopen(get_filename_fullpath(NVM_FILENAME), "wb");
}
if(fp) {
fseek(fp, (unsigned int)dst, SEEK_SET);
Expand All @@ -102,7 +102,7 @@ void nvm_write_block(const void *src, void *dst, int len) {
}

byte nvm_read_byte(const byte *p) {
FILE *fp = fopen(NVM_FILENAME, "rb");
FILE *fp = fopen(get_filename_fullpath(NVM_FILENAME), "rb");
byte v = 0;
if(fp) {
fseek(fp, (unsigned int)p, SEEK_SET);
Expand All @@ -115,9 +115,9 @@ byte nvm_read_byte(const byte *p) {
}

void nvm_write_byte(const byte *p, byte v) {
FILE *fp = fopen(NVM_FILENAME, "rb+");
FILE *fp = fopen(get_filename_fullpath(NVM_FILENAME), "rb+");
if(!fp) {
fp = fopen(NVM_FILENAME, "wb");
fp = fopen(get_filename_fullpath(NVM_FILENAME), "wb");
}
if(fp) {
fseek(fp, (unsigned int)p, SEEK_SET);
Expand All @@ -131,11 +131,11 @@ void nvm_write_byte(const byte *p, byte v) {
void write_to_file(const char *name, const char *data, int size, int pos, bool trunc) {
FILE *file;
if(trunc) {
file = fopen(name, "wb");
file = fopen(get_filename_fullpath(name), "wb");
} else {
file = fopen(name, "r+b");
file = fopen(get_filename_fullpath(name), "r+b");
if(!file) {
file = fopen(name, "wb");
file = fopen(get_filename_fullpath(name), "wb");
}
}

Expand All @@ -149,7 +149,7 @@ void write_to_file(const char *name, const char *data, int size, int pos, bool t
bool read_from_file(const char *name, char *data, int maxsize, int pos) {

FILE *file;
file = fopen(name, "rb");
file = fopen(get_filename_fullpath(name), "rb");
if(!file) {
data[0] = 0;
return true;
Expand All @@ -172,23 +172,35 @@ bool read_from_file(const char *name, char *data, int maxsize, int pos) {
}

void remove_file(const char *name) {
remove(name);
remove(get_filename_fullpath(name));
}

char* get_runtime_path() {
static char path[PATH_MAX];
if(readlink("/proc/self/exe", path, PATH_MAX ) <= 0) {
return NULL;
}
char* path_end = strrchr(path, '/');
if(path_end == NULL) {
return NULL;
static byte query = 1;

if(query) {
if(readlink("/proc/self/exe", path, PATH_MAX ) <= 0) {
return NULL;
}
char* path_end = strrchr(path, '/');
if(path_end == NULL) {
return NULL;
}
path_end++;
*path_end=0;
query = 0;
}
path_end++;
*path_end=0;
return path;
}

char* get_filename_fullpath(const char *filename) {
static char fullpath[PATH_MAX];
strcpy(fullpath, get_runtime_path());
strcat(fullpath, filename);
return fullpath;
}

#if defined(OSPI)
unsigned int detect_rpi_rev() {
FILE * filp;
Expand Down
1 change: 1 addition & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void remove_file(const char *name);
byte nvm_read_byte(const byte *p);
void nvm_write_byte(const byte *p, byte v);
char* get_runtime_path();
char* get_filename_fullpath(const char *filename);
void delay(ulong ms);
void delayMicroseconds(ulong us);
void delayMicrosecondsHard(ulong us);
Expand Down

0 comments on commit 560db55

Please sign in to comment.