47#include "utilities/qstring.h"
48#include "utilities/qfile.h"
79 memset((
void *) &lock, 0,
sizeof(flock));
80 lock.l_type = F_WRLCK;
81 lock.l_whence = SEEK_SET;
84 int ret = fcntl(fd, F_SETLK, &lock);
103 memset((
void *) &lock, 0,
sizeof(flock));
104 lock.l_type = F_UNLCK;
105 lock.l_whence = SEEK_SET;
108 int ret = fcntl(fd, F_SETLK, &lock);
123 if (access(filepath, F_OK) == 0)
161 if ((fd = open(filepath, O_RDONLY, 0)) < 0)
165 if (fstat(fd, &fs) < 0) {
170 size_t size = fs.st_size;
171 if (nbytes != NULL && *nbytes > 0 && *nbytes < fs.st_size)
174 void *buf = malloc(size + 1);
180 ssize_t count = read(fd, buf, size);
188 ((
char *) buf)[count] =
'\0';
215 size_t memsize = 1024;
218 if (nbytes != NULL && *nbytes > 0) {
226 for (c_count = 0; (c = fgetc(fp)) != EOF;) {
227 if (size > 0 && c_count == size)
231 data = (
char *) malloc(
sizeof(
char) * memsize);
233 DEBUG(
"Memory allocation failed.");
236 }
else if (c_count == memsize - 1) {
240 char *datatmp = (
char *) malloc(
sizeof(
char) * (memsize + 1));
241 if (datatmp == NULL) {
242 DEBUG(
"Memory allocation failed.");
246 memcpy(datatmp, data, c_count);
250 data[c_count++] = (char) c;
253 if (c_count == 0 && c == EOF)
255 data[c_count] =
'\0';
260 return (
void *) data;
283ssize_t
qfile_save(
const char *filepath,
const void *buf,
size_t size,
287 if (append ==
false) {
288 fd = open(filepath, O_CREAT | O_WRONLY | O_TRUNC,
289 (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
291 fd = open(filepath, O_CREAT | O_WRONLY | O_APPEND,
292 (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
297 ssize_t count = write(fd, buf, size);
312bool qfile_mkdir(
const char *dirpath, mode_t mode,
bool recursive) {
313 DEBUG(
"try to create directory %s", dirpath);
314 if (mkdir(dirpath, mode) == 0)
318 if (recursive ==
true && errno == ENOENT) {
321 && mkdir(dirpath, mode) == 0) {
341 int nLen = strlen(path);
342 if (nLen == 0 || nLen >= PATH_MAX)
344 else if (strpbrk(path,
"\\:*?\"<>|") != NULL)
357 char *path = strdup(filepath);
358 char *bname = basename(path);
359 char *filename = strdup(bname);
372 char *path = strdup(filepath);
373 char *dname = dirname(path);
374 char *dir = strdup(dname);
387#define MAX_EXTENSION_LENGTH (8)
389 char *p = strrchr(filename,
'.');
391 if (p != NULL && strlen(p + 1) <= MAX_EXTENSION_LENGTH
392 &&
qstrtest(isalnum, p + 1) ==
true) {
412 if (stat(filepath, &finfo) < 0)
414 return finfo.st_size;
443 if (strstr(path,
"//") != NULL) {
448 if (strstr(path,
"/./") != NULL) {
453 if (strstr(path,
"/../") != NULL) {
454 char *pszTmp = strstr(path,
"/../");
455 if (pszTmp == path) {
457 strcpy(path, pszTmp + 3);
462 strcpy(path, pszNewPrefix);
463 strcat(path, pszTmp + 3);
470 size_t nLen = strlen(path);
472 if (path[nLen - 1] ==
'/') {
473 path[nLen - 1] =
'\0';
480 if (!strcmp(path + (nLen - 2),
"/.")) {
481 path[nLen - 2] =
'\0';
488 if (!strcmp(path + (nLen - 2),
"/.")) {
489 path[nLen - 2] =
'\0';
496 if (!strcmp(path + (nLen - 3),
"/..")) {
497 path[nLen - 3] =
'\0';
499 strcpy(path, pszNewPath);
534 if (path[0] ==
'/') {
537 if (getcwd(buf, bufsize) == NULL)
void * qfile_load(const char *filepath, size_t *nbytes)
Load file into memory.
char * qfile_correct_path(char *path)
Correct path string.
char * qfile_get_name(const char *filepath)
Get filename from filepath.
void * qfile_read(FILE *fp, size_t *nbytes)
Read data from a file stream.
bool qfile_unlock(int fd)
Unlock file which is locked by qfile_lock()
char * qfile_abspath(char *buf, size_t bufsize, const char *path)
Make full absolute system path string.
ssize_t qfile_save(const char *filepath, const void *buf, size_t size, bool append)
Save data into file.
char * qfile_get_ext(const char *filepath)
Get extension from filepath.
bool qfile_mkdir(const char *dirpath, mode_t mode, bool recursive)
Attempts to create a directory recursively.
char * qfile_get_dir(const char *filepath)
Get directory suffix from filepath.
bool qfile_lock(int fd)
Lock file.
bool qfile_exist(const char *filepath)
Check file existence.
bool qfile_check_path(const char *path)
Check path string contains invalid characters.
off_t qfile_get_size(const char *filepath)
Get file size.
char * qstrcpy(char *dst, size_t size, const char *src)
Copy src string to dst.
char * qstrtrim(char *str)
Remove white spaces(including CR, LF) from head and tail of the string.
bool qstrtest(int(*testfunc)(int), const char *str)
Test for an alpha-numeric string.
char * qstrreplace(const char *mode, char *srcstr, const char *tokstr, const char *word)
Replace string or tokens as word from source string with given mode.
char * qstrlower(char *str)
Convert character to lower character.