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';
216 size_t memsize = 1024;
219 if (nbytes != NULL && *nbytes > 0) {
227 for (c_count = 0; (c = fgetc(fp)) != EOF;) {
228 if (size > 0 && c_count == size)
232 data = (
char *) malloc(
sizeof(
char) * memsize);
234 DEBUG(
"Memory allocation failed.");
237 }
else if (c_count == memsize - 1) {
241 char *datatmp = (
char *) malloc(
sizeof(
char) * (memsize + 1));
242 if (datatmp == NULL) {
243 DEBUG(
"Memory allocation failed.");
247 memcpy(datatmp, data, c_count);
251 data[c_count++] = (char) c;
254 if (c_count == 0 && c == EOF)
256 data[c_count] =
'\0';
261 return (
void *) data;
284ssize_t
qfile_save(
const char *filepath,
const void *buf,
size_t size,
288 if (append ==
false) {
289 fd = open(filepath, O_CREAT | O_WRONLY | O_TRUNC,
290 (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
292 fd = open(filepath, O_CREAT | O_WRONLY | O_APPEND,
293 (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
298 ssize_t count = write(fd, buf, size);
313bool qfile_mkdir(
const char *dirpath, mode_t mode,
bool recursive) {
314 DEBUG(
"try to create directory %s", dirpath);
315 if (mkdir(dirpath, mode) == 0)
319 if (recursive ==
true && errno == ENOENT) {
322 && mkdir(dirpath, mode) == 0) {
342 int nLen = strlen(path);
343 if (nLen == 0 || nLen >= PATH_MAX)
345 else if (strpbrk(path,
"\\:*?\"<>|") != NULL)
358 char *path = strdup(filepath);
359 char *bname = basename(path);
360 char *filename = strdup(bname);
373 char *path = strdup(filepath);
374 char *dname = dirname(path);
375 char *dir = strdup(dname);
388#define MAX_EXTENSION_LENGTH (8)
390 char *p = strrchr(filename,
'.');
392 if (p != NULL && strlen(p + 1) <= MAX_EXTENSION_LENGTH
393 &&
qstrtest(isalnum, p + 1) ==
true) {
413 if (stat(filepath, &finfo) < 0)
415 return finfo.st_size;
444 if (strstr(path,
"//") != NULL) {
449 if (strstr(path,
"/./") != NULL) {
454 if (strstr(path,
"/../") != NULL) {
455 char *pszTmp = strstr(path,
"/../");
456 if (pszTmp == path) {
458 strcpy(path, pszTmp + 3);
463 strcpy(path, pszNewPrefix);
464 strcat(path, pszTmp + 3);
471 size_t nLen = strlen(path);
473 if (path[nLen - 1] ==
'/') {
474 path[nLen - 1] =
'\0';
481 if (!strcmp(path + (nLen - 2),
"/.")) {
482 path[nLen - 2] =
'\0';
489 if (!strcmp(path + (nLen - 2),
"/.")) {
490 path[nLen - 2] =
'\0';
497 if (!strcmp(path + (nLen - 3),
"/..")) {
498 path[nLen - 3] =
'\0';
500 strcpy(path, pszNewPath);
535 if (path[0] ==
'/') {
538 if (getcwd(buf, bufsize) == NULL)
void * qfile_load(const char *filepath, size_t *nbytes)
Load a file into memory.
char * qfile_correct_path(char *path)
Correct path string.
char * qfile_get_name(const char *filepath)
Get the file name from a path.
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 the file extension from a path.
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 the directory part of a path.
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 whitespace, including CR and LF, from both ends of a string.
bool qstrtest(int(*testfunc)(int), const char *str)
Test whether a string matches a character rule.
char * qstrreplace(const char *mode, char *srcstr, const char *tokstr, const char *word)
Replace tokens or strings in a source string using the given mode.
char * qstrlower(char *str)
Convert character to lower character.