|
|
@@ -1,29 +1,94 @@ |
|
|
|
#include <fcntl.h> |
|
|
|
#include <unistd.h> |
|
|
|
#include <stdio.h> |
|
|
|
#include <stddef.h> |
|
|
|
#include <string.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <dirent.h> |
|
|
|
#include <sys/stat.h> |
|
|
|
#include <sys/types.h> |
|
|
|
#include <errno.h> |
|
|
|
|
|
|
|
#include "cms_util.h" |
|
|
|
|
|
|
|
int cms_util_file_exists(char* fname) |
|
|
|
{ |
|
|
|
FILE* f; |
|
|
|
if (f = fopen(fname, "r")) |
|
|
|
{ |
|
|
|
fclose(f); |
|
|
|
int f = open(fname, O_RDONLY); |
|
|
|
|
|
|
|
if (errno == ENOENT) |
|
|
|
return 0; |
|
|
|
else |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
|
close(f); |
|
|
|
} |
|
|
|
|
|
|
|
void cms_util_file_create(char* fname) |
|
|
|
{ |
|
|
|
FILE* f = fopen(fname, "ab+"); |
|
|
|
fclose(f); |
|
|
|
int f = open(fname, O_CREAT); |
|
|
|
close(f); |
|
|
|
} |
|
|
|
|
|
|
|
int cms_util_file_copy(char* fname1, char* fname2) |
|
|
|
{ |
|
|
|
int f1 = open(fname1, O_RDONLY); |
|
|
|
int f2 = open(fname2, O_WRONLY); |
|
|
|
|
|
|
|
struct stat* s = malloc(sizeof(struct stat)); |
|
|
|
if (s == NULL) |
|
|
|
return 1; |
|
|
|
|
|
|
|
fstat(f1, s); |
|
|
|
|
|
|
|
void* buf = malloc(s->st_size); |
|
|
|
if (buf == NULL) |
|
|
|
return 1; |
|
|
|
|
|
|
|
read(f1, buf, s->st_size); |
|
|
|
write(f2, buf, s->st_size); |
|
|
|
|
|
|
|
close(f1); |
|
|
|
close(f2); |
|
|
|
|
|
|
|
free(s); |
|
|
|
free(buf); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int cms_util_dir_copy_recursive(char* dir1, char* dir2) |
|
|
|
{ |
|
|
|
mkdir(dir2, 0777); |
|
|
|
DIR* dp = opendir(dir1); |
|
|
|
|
|
|
|
struct dirent* ep; |
|
|
|
|
|
|
|
if (dp == NULL) |
|
|
|
return 1; |
|
|
|
|
|
|
|
while (ep = readdir(dp)) |
|
|
|
{ |
|
|
|
char* path1 = cms_util_path_join(dir1, ep->d_name); |
|
|
|
char* path2 = cms_util_path_join(dir2, ep->d_name); |
|
|
|
struct stat* s = malloc(sizeof(struct stat)); |
|
|
|
stat(path1, s); |
|
|
|
|
|
|
|
if (S_ISDIR(s->st_mode)) |
|
|
|
{ |
|
|
|
cms_util_dir_copy_recursive(path1, path2); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
cms_util_file_copy(path1, path2); |
|
|
|
} |
|
|
|
|
|
|
|
free(s); |
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
char* cms_util_join_paths(char* str1, char* str2) |
|
|
|
char* cms_util_path_join(char* str1, char* str2) |
|
|
|
{ |
|
|
|
size_t len1 = strlen(str1); |
|
|
|
size_t len2 = strlen(str2); |