This commit is contained in:
135
posix/Dir.c
Normal file
135
posix/Dir.c
Normal file
@@ -0,0 +1,135 @@
|
||||
#include "../CgeFs.h"
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
typedef struct CgeFsDir {
|
||||
DIR *dir;
|
||||
char path[PATH_MAX];
|
||||
size_t size;
|
||||
} CgeFsDir;
|
||||
|
||||
static int mapErrno(int value) {
|
||||
switch (value) {
|
||||
case ENOENT:
|
||||
return CGE_FS_ENOENT;
|
||||
|
||||
case EACCES:
|
||||
return CGE_FS_EACCES;
|
||||
|
||||
case ENOTDIR:
|
||||
return CGE_FS_ENOTDIR;
|
||||
|
||||
default:
|
||||
return CGE_FS_EUNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
CgeFsDir *CgeFsOpen(const char *path, int *result) {
|
||||
CgeFsDir *d;
|
||||
DIR *dir;
|
||||
size_t size;
|
||||
|
||||
dir = opendir(path);
|
||||
if (!dir) {
|
||||
if (result)
|
||||
*result = mapErrno(errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
d = malloc(sizeof(*d));
|
||||
if (!d) {
|
||||
closedir(dir);
|
||||
if (result)
|
||||
*result = CGE_FS_EUNKNOWN;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
d->dir = dir;
|
||||
size = strlen(path);
|
||||
if (size < PATH_MAX) {
|
||||
memcpy(d->path, path, size);
|
||||
if (size > 0 && d->path[size - 1] != '/') {
|
||||
d->path[size] = '/';
|
||||
d->path[size + 1] = 0;
|
||||
d->size = size + 1;
|
||||
} else {
|
||||
memcpy(d->path, path, size);
|
||||
d->path[size] = 0;
|
||||
d->size = size;
|
||||
}
|
||||
} else {
|
||||
d->path[0] = 0;
|
||||
d->size = 0;
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
int CgeFsNext(CgeFsDir *d, CgeFsInfo *info, int *result) {
|
||||
struct dirent *ent;
|
||||
struct stat st;
|
||||
char path[PATH_MAX];
|
||||
|
||||
while ((ent = readdir(d->dir)) != NULL) {
|
||||
if (!strcmp(ent->d_name, "."))
|
||||
continue;
|
||||
if (!strcmp(ent->d_name, ".."))
|
||||
continue;
|
||||
|
||||
if (d->size > 0 && d->size < PATH_MAX - 2) {
|
||||
strncpy(path, d->path, d->size);
|
||||
path[d->size] = '\0';
|
||||
strncat(path, ent->d_name, PATH_MAX - strlen(path) - 1);
|
||||
} else {
|
||||
strncpy(path, ent->d_name, PATH_MAX - 1);
|
||||
path[PATH_MAX - 1] = '\0';
|
||||
}
|
||||
|
||||
if (stat(path, &st) != 0)
|
||||
continue;
|
||||
|
||||
strncpy(info->name, ent->d_name, CGE_FS_NAME_MAX - 1);
|
||||
info->name[CGE_FS_NAME_MAX - 1] = '\0';
|
||||
info->type = S_ISDIR(st.st_mode) ? CGE_FS_DIR : CGE_FS_FILE;
|
||||
info->size = (uint64_t)st.st_size;
|
||||
info->mtime = (uint64_t)st.st_mtime;
|
||||
|
||||
if (result)
|
||||
*result = CGE_FS_EOK;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void CgeFsClose(CgeFsDir *d) {
|
||||
if (!d)
|
||||
return;
|
||||
|
||||
closedir(d->dir);
|
||||
free(d);
|
||||
}
|
||||
|
||||
int CgeFsCreateDir(const char *path, int *result) {
|
||||
if (mkdir(path, 0777) != 0) {
|
||||
if (result)
|
||||
*result = mapErrno(errno);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CgeFsRemoveDir(const char *path, int *result) {
|
||||
if (rmdir(path) != 0) {
|
||||
if (result)
|
||||
*result = mapErrno(errno);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
114
posix/Stat.c
Normal file
114
posix/Stat.c
Normal file
@@ -0,0 +1,114 @@
|
||||
#include "../CgeFs.h"
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static int mapErrno(int err) {
|
||||
switch (err) {
|
||||
case ENOENT:
|
||||
return CGE_FS_ENOENT;
|
||||
|
||||
case EEXIST:
|
||||
return CGE_FS_EEXIST;
|
||||
|
||||
case EACCES:
|
||||
return CGE_FS_EACCES;
|
||||
|
||||
case ENOTDIR:
|
||||
return CGE_FS_ENOTDIR;
|
||||
|
||||
case EMFILE:
|
||||
return CGE_FS_EMFILE;
|
||||
|
||||
case ENAMETOOLONG:
|
||||
return CGE_FS_ENAMETOOLONG;
|
||||
|
||||
case EIO:
|
||||
return CGE_FS_EIO;
|
||||
|
||||
default:
|
||||
return CGE_FS_EUNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
int CgeFsStat(const char *path, CgeFsInfo *info, int *result) {
|
||||
struct stat st;
|
||||
const char *p;
|
||||
const char *base;
|
||||
|
||||
if (stat(path, &st) != 0) {
|
||||
if (result)
|
||||
*result = mapErrno(errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
base = NULL;
|
||||
for (p = path; *p; p++)
|
||||
if (*p == '/') base = p + 1;
|
||||
|
||||
if (!base)
|
||||
base = path;
|
||||
|
||||
strncpy(info->name, base, CGE_FS_NAME_MAX - 1);
|
||||
info->name[CGE_FS_NAME_MAX - 1] = '\0';
|
||||
info->type = S_ISDIR(st.st_mode) ? CGE_FS_DIR : CGE_FS_FILE;
|
||||
info->size = (uint64_t)st.st_size;
|
||||
info->mtime = (uint64_t)st.st_mtime;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CgeFsDelete(const char *path, int *result) {
|
||||
if (unlink(path) != 0) {
|
||||
if (result)
|
||||
*result = mapErrno(errno);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CgeFsRename(const char *from, const char *to, int *result) {
|
||||
if (rename(from, to) != 0) {
|
||||
if (result)
|
||||
*result = mapErrno(errno);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CgeFsCopy(const char *from, const char *to, int *result) {
|
||||
FILE *fin, *fout;
|
||||
char buf[PATH_MAX];
|
||||
size_t n;
|
||||
|
||||
if (!(fin = fopen(from, "rb"))) {
|
||||
if (result)
|
||||
*result = mapErrno(errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!(fout = fopen(to, "wb"))) {
|
||||
fclose(fin);
|
||||
if (result)
|
||||
*result = mapErrno(errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
while ((n = fread(buf, 1, sizeof(buf), fin)) > 0) {
|
||||
if (fwrite(buf, 1, n, fout) != n) {
|
||||
if (result)
|
||||
*result = CGE_FS_EIO;
|
||||
fclose(fin);
|
||||
fclose(fout);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fin);
|
||||
fclose(fout);
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user