Browse Source

update: create folder and remove file

dev
Dnomd343 2 years ago
parent
commit
c9083b6706
  1. 1
      src/applet/crontab.c
  2. 3
      src/cleardns.c
  3. 28
      src/common/system.c

1
src/applet/crontab.c

@ -7,6 +7,7 @@ process* assets_load(assets_config *info) { // load assets update options
// TODO: update assets process
// TODO: save cron exp in `/var/spool/cron/crontabs/root`
log_info("Crontab exp -> `%s %s`", info->cron, "update.sh");
return process_init("Crontab", "crond");
}

3
src/cleardns.c

@ -48,6 +48,9 @@ int main(int argc, char *argv[]) { // ClearDNS service
LOG_LEVEL = LOG_DEBUG;
log_info("ClearDNS server start (%s)", VERSION);
// remove_file("/tmp/test.txt");
create_folder("/tmp/test");
return 0;
// process *test = process_init("TEST", "lls");
// process *test = process_init("TEST", "ls");

28
src/common/system.c

@ -1,10 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include "logger.h"
#include "sundry.h"
#include "system.h"
#include "constant.h"
int run_command(const char *command) { // running command under system shell
@ -17,10 +18,13 @@ int run_command(const char *command) { // running command under system shell
}
void remove_file(const char *file) { // delete file
// TODO: use system interface (not `rm` command)
char *remove_cmd = string_join("rm -f ", file);
run_command(remove_cmd);
free(remove_cmd);
if (!is_file_exist(file)) { // file not found
log_debug("Delete file `%s` skip -> file not exist", file);
} else if (remove(file)) { // remove failed
log_perror("Delete file `%s` failed -> ", file);
} else { // remove success
log_debug("Delete file `%s` success", file);
}
}
void create_folder(const char *folder) { // create folder
@ -28,15 +32,15 @@ void create_folder(const char *folder) { // create folder
struct stat buf;
stat(folder, &buf);
if (!(S_IFDIR & buf.st_mode)) { // target is not folder
log_error("Create folder failed -> target is file");
log_error("Create folder `%s` failed -> target is file", folder);
} else {
log_debug("Create folder `%s` skip -> folder exist", folder);
}
return;
} else if (mkdir(folder, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) { // folder -> 755
log_perror("Create folder `%s` failed -> ", folder);
} else {
log_debug("Create folder `%s` success", folder);
}
log_debug("Create folder -> %s", folder);
// TODO: use system command (not `mkdir` command)
char *command = string_join("mkdir -p ", folder);
system(command);
free(command);
}
uint8_t is_file_exist(const char *file) { // whether file exist

Loading…
Cancel
Save