Browse Source

feat: download file function

dev
Dnomd343 2 years ago
parent
commit
9ef69995ce
  1. 1
      include/common/system.h
  2. 2
      include/constant.h
  3. 2
      include/utils/process.h
  4. 7
      src/applet/crontab.c
  5. 1
      src/cleardns.c
  6. 9
      src/common/system.c
  7. 17
      src/utils/process.c

1
include/common/system.h

@ -9,6 +9,7 @@ int run_command(const char *command);
void create_folder(const char *folder);
uint8_t is_file_exist(const char *file);
void save_file(const char *file, const char *content);
void download_file(const char *file, const char *url);
void save_string_list(const char *file, char **string_list);
void file_append(const char *base_file, const char *append_file);

2
include/constant.h

@ -5,7 +5,7 @@
#define FALSE 0
#define VERSION "1.3.0-dev"
#define CONFIG_FILE "test.yml"
#define CONFIG_FILE "cleardns.yml"
#define DNS_PORT 53
#define ADGUARD_PORT 80

2
include/utils/process.h

@ -11,7 +11,7 @@ typedef struct {
char *cwd; // working directory
} process;
extern process **process_list;
//extern process **process_list;
void process_list_run();
void process_list_init();

7
src/applet/crontab.c

@ -1,5 +1,6 @@
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include "config.h"
#include "logger.h"
#include "sundry.h"
@ -15,10 +16,14 @@ void assets_update() { // update all assets
for (char **file = update_file; *file != NULL; ++file) {
char **url = file - update_file + update_url;
log_info("Update asset `%s` -> %s", *file, *url);
download_file(*file, *url); // download asset from url
}
log_info("Restart overture");
run_command("pgrep overture | xargs kill"); // restart overture
log_info("Assets update complete");
}
void assets_free(assets_config *info) {
void assets_free(assets_config *info) { // free assets config
string_list_free(info->update_file);
string_list_free(info->update_url);
free(info->cron);

1
src/cleardns.c

@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include "loader.h"
#include "logger.h"
#include "sundry.h"

9
src/common/system.c

@ -106,3 +106,12 @@ void save_string_list(const char *file, char **string_list) { // save string lis
fclose(fp);
log_debug("Save `%s` success", file);
}
void download_file(const char *file, const char *url) { // download file
log_debug("Download file `%s` -> %s", file, url);
char *download_cmd = (char *)malloc(strlen(file) + strlen(url) + 15);
sprintf(download_cmd, "wget -T 8 -O %s %s", file, url);
if (run_command(download_cmd)) {
log_warn("File `%s` download failed", url);
}
}

17
src/utils/process.c

@ -11,6 +11,9 @@
process **process_list;
uint8_t EXITING = FALSE;
uint8_t EXITED = FALSE;
void get_sub_exit();
void get_exit_signal();
void server_exit(int exit_code);
@ -91,12 +94,18 @@ void process_list_run() { // start process list
}
void process_list_daemon() {
int status;
wait(&status);
while (!EXITED) {
pause();
}
}
void server_exit(int exit_code) { // kill sub process and exit
log_info("Kill subprocess");
while (EXITING) { // only run once
pause();
}
EXITING = TRUE;
log_info("ClearDNS exiting");
// TODO: kill subprocess and exit cleardns
@ -105,7 +114,7 @@ void server_exit(int exit_code) { // kill sub process and exit
void get_exit_signal() { // get SIGINT or SIGTERM signal
log_info("Get exit signal");
server_exit(EXIT_NORMAL);
server_exit(EXIT_NORMAL); // normally exit
}
void get_sub_exit() { // catch child process exit

Loading…
Cancel
Save