瀏覽代碼

Init now works, and some error system changes

master
mort 10 年之前
父節點
當前提交
ea23c699b2
共有 7 個文件被更改,包括 69 次插入19 次删除
  1. 3
    0
      Makefile
  2. 7
    0
      resources/placeholder
  3. 8
    0
      resources/subdirtest/placeholder
  4. 23
    9
      src/cms_err.c
  5. 4
    3
      src/cms_err.h
  6. 16
    5
      src/cms_util.c
  7. 8
    2
      src/main.c

+ 3
- 0
Makefile 查看文件

@@ -3,6 +3,9 @@ appname = cms
build:
gcc -o $(appname) src/*.c -std=c99

debug:
gcc -o $(appname) src/*.c -std=c99 -DDEBUG=1

install:
mv $(appname) /usr/bin/$(appname)


+ 7
- 0
resources/placeholder 查看文件

@@ -0,0 +1,7 @@
This is placeholder text.
This is placeholder text.
This is placeholder text.
This is placeholder text.
This is placeholder text.
This is placeholder text.
This is placeholder text.

+ 8
- 0
resources/subdirtest/placeholder 查看文件

@@ -0,0 +1,8 @@
This is placeholder text.
This is placeholder text.
This is placeholder text.
This is placeholder text.
This is placeholder text.
This is placeholder text.
This is placeholder text.
This is placeholder text.

+ 23
- 9
src/cms_err.c 查看文件

@@ -3,7 +3,11 @@
#include <stdlib.h>
#include <errno.h>

static char* get_message(cms_err err)
#ifdef DEBUG
#include <error.h>
#endif

static char* get_error_message(cms_err err)
{
switch (err)
{
@@ -15,14 +19,16 @@ static char* get_message(cms_err err)
return "Memory allocation failed.";
case CMS_ERR_PARSE:
return "Parse error.";
case CMS_ERR_FILENOENT:
return "File doesn't exist.";
case CMS_ERR_DIRNOENT:
return "Directory doesn't exist.";
case CMS_ERR_NOENT:
return "No such file or directory.";
case CMS_ERR_NOTFILE:
return "Not a file.";
case CMS_ERR_NOTDIR:
return "Not a directory.";
case CMS_ERR_FILEEXISTS:
return "File exists.";
case CMS_ERR_DIREXISTS:
return "Directory exists.";
case CMS_ERR_PERM:
return "Permission denied.";
case CMS_ERR_INITED:
@@ -32,21 +38,29 @@ static char* get_message(cms_err err)
}
}

void cms_err_panic(cms_err err, char* msg)
void _cms_err_panic(cms_err err, char* msg, const char* file, int line)
{
if (err == CMS_ERR_NONE)
return;

#ifdef DEBUG
fprintf(stderr, "File %s, line %i:\n\t", file, line);
#endif

if (msg == NULL)
fprintf(stderr, "Error: %s\n", get_message(err));
fprintf(stderr, "Error: %s\n", get_error_message(err));
else
fprintf(stderr, "Error: %s (%s)\n", get_message(err), msg);
fprintf(stderr, "Error: %s: %s\n", msg, get_error_message(err));

exit(1);
}

cms_err cms_err_from_std_err(int err)
{
#ifdef DEBUG
error(0, err, "converting to cms_err");
#endif

switch (err)
{
case EACCES:
@@ -58,7 +72,7 @@ cms_err cms_err_from_std_err(int err)
case EISDIR:
return CMS_ERR_NOTFILE;
case ENOENT:
return CMS_ERR_FILENOENT;
return CMS_ERR_NOENT;
case ENOMEM:
return CMS_ERR_ALLOC;
case ENOTDIR:

+ 4
- 3
src/cms_err.h 查看文件

@@ -7,8 +7,7 @@ typedef enum cms_err
CMS_ERR_UNKNOWN,
CMS_ERR_ALLOC,
CMS_ERR_PARSE,
CMS_ERR_FILENOENT,
CMS_ERR_DIRNOENT,
CMS_ERR_NOENT,
CMS_ERR_NOTFILE,
CMS_ERR_NOTDIR,
CMS_ERR_FILEEXISTS,
@@ -18,8 +17,10 @@ typedef enum cms_err
CMS_ERR_NOTINITED
} cms_err;

void cms_err_panic(cms_err err, char* msg);
void _cms_err_panic(cms_err err, char* msg, const char* file, int line);
#define cms_err_panic(err, msg) _cms_err_panic(err, msg, __FILE__, __LINE__)

cms_err cms_err_from_std_err(int err);

#endif


+ 16
- 5
src/cms_util.c 查看文件

@@ -28,7 +28,7 @@ int cms_util_file_exists(char* fname)

cms_err cms_util_file_create(char* fname)
{
int f = open(fname, O_CREAT);
int f = open(fname, O_CREAT, 0777);
close(f);

if (f == -1)
@@ -40,7 +40,12 @@ cms_err cms_util_file_create(char* fname)
cms_err cms_util_file_copy(char* fname1, char* fname2)
{
int f1 = open(fname1, O_RDONLY);
int f2 = open(fname2, O_WRONLY);
if (f1 == -1)
return cms_err_from_std_err(errno);

int f2 = open(fname2, O_WRONLY | O_CREAT, 0777);
if (f1 == -1)
return cms_err_from_std_err(errno);

struct stat* s = malloc(sizeof(struct stat));
if (s == NULL)
@@ -66,7 +71,7 @@ cms_err cms_util_file_copy(char* fname1, char* fname2)

cms_err cms_util_dir_copy_recursive(char* dir1, char* dir2)
{
if (mkdir(dir2, 0777) == -1)
if (mkdir(dir2, 0777) == -1 && errno != EEXIST)
return cms_err_from_std_err(errno);

DIR* dp = opendir(dir1);
@@ -81,9 +86,14 @@ cms_err cms_util_dir_copy_recursive(char* dir1, char* dir2)

while (ep = readdir(dp))
{
if (ep->d_name[0] == '.')
continue;

char* path1 = cms_util_path_join(dir1, ep->d_name);
char* path2 = cms_util_path_join(dir2, ep->d_name);
stat(path1, st);

if (stat(path1, st) == -1)
return cms_err_from_std_err(errno);

if (S_ISDIR(st->st_mode))
{
@@ -99,9 +109,10 @@ cms_err cms_util_dir_copy_recursive(char* dir1, char* dir2)
}
}

closedir(dp);
free(st);

return 0;
return CMS_ERR_NONE;
}

char* cms_util_path_join(char* str1, char* str2)

+ 8
- 2
src/main.c 查看文件

@@ -9,7 +9,7 @@ int main(int argc, char** argv)
{
if (argc < 2)
{
fprintf(stderr, "Usage: %s <init|build>\n", argv[0]);
fprintf(stderr, "Usage: %s <init|deinit|build>\n", argv[0]);
return 1;
}

@@ -32,8 +32,14 @@ int main(int argc, char** argv)
if (cms_util_file_exists(initedPath))
cms_err_panic(CMS_ERR_INITED, NULL);

//Copy files from resources
cms_err_panic(
cms_util_dir_copy_recursive(CMS_FILE_RESOURCES, dirname),
dirname
);

//Create .cmsinited file
cms_util_file_create(initedPath);
cms_err_panic(cms_util_file_create(initedPath), initedPath);
}

//Build

Loading…
取消
儲存