Regularly cleanup files in the httptemp directory (#343)
* Regularly cleanup files in the httptemp directory * Add scan http temp dir default configuration * Fix mem leak for scan httptemp dir * Modify int to gint64
This commit is contained in:
parent
cb696853c4
commit
50882b41f4
3 changed files with 111 additions and 0 deletions
|
@ -20,6 +20,13 @@ fileserver_config_get_integer(GKeyFile *config, char *key, GError **error)
|
|||
return g_key_file_get_integer (config, group, key, error);
|
||||
}
|
||||
|
||||
int
|
||||
fileserver_config_get_int64(GKeyFile *config, char *key, GError **error)
|
||||
{
|
||||
const char *group = get_group_name(config);
|
||||
return g_key_file_get_int64 (config, group, key, error);
|
||||
}
|
||||
|
||||
char *
|
||||
fileserver_config_get_string(GKeyFile *config, char *key, GError **error)
|
||||
{
|
||||
|
|
|
@ -9,6 +9,9 @@ fileserver_config_get_integer(GKeyFile *config, char *key, GError **error);
|
|||
char *
|
||||
fileserver_config_get_string(GKeyFile *config, char *key, GError **error);
|
||||
|
||||
int
|
||||
fileserver_config_get_int64(GKeyFile *config, char *key, GError **error);
|
||||
|
||||
gboolean
|
||||
fileserver_config_get_boolean(GKeyFile *config, char *key, GError **error);
|
||||
|
||||
|
|
|
@ -41,6 +41,11 @@
|
|||
#define HOST "host"
|
||||
#define PORT "port"
|
||||
|
||||
#define HTTP_TEMP_FILE_SCAN_INTERVAL 3600 /*1h*/
|
||||
#define HTTP_TEMP_FILE_DEFAULT_TTL 3600 * 24 * 3 /*3days*/
|
||||
#define HTTP_TEMP_FILE_TTL "http_temp_file_ttl"
|
||||
#define HTTP_SCAN_INTERVAL "http_temp_scan_interval"
|
||||
|
||||
#define INIT_INFO "If you see this page, Seafile HTTP syncing component works."
|
||||
#define PROTO_VERSION "{\"version\": 2}"
|
||||
|
||||
|
@ -2508,6 +2513,95 @@ seaf_http_server_new (struct _SeafileSession *session)
|
|||
return server;
|
||||
}
|
||||
|
||||
gint64
|
||||
get_last_modify_time (const char *path)
|
||||
{
|
||||
struct stat st;
|
||||
if (stat (path, &st) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return st.st_mtime;
|
||||
}
|
||||
|
||||
static gint64
|
||||
check_httptemp_dir_recursive (const char *parent_dir, gint64 expired_time)
|
||||
{
|
||||
char *full_path;
|
||||
const char *dname;
|
||||
gint64 cur_time;
|
||||
gint64 last_modify = -1;
|
||||
GDir *dir = NULL;
|
||||
gint64 file_num = 0;
|
||||
|
||||
dir = g_dir_open (parent_dir, 0, NULL);
|
||||
|
||||
while ((dname = g_dir_read_name(dir)) != NULL) {
|
||||
full_path = g_build_path ("/", parent_dir, dname, NULL);
|
||||
|
||||
if (g_file_test (full_path, G_FILE_TEST_IS_DIR)) {
|
||||
file_num += check_httptemp_dir_recursive (full_path, expired_time);
|
||||
} else {
|
||||
cur_time = time (NULL);
|
||||
last_modify = get_last_modify_time (full_path);
|
||||
if (last_modify == -1) {
|
||||
g_free (full_path);
|
||||
continue;
|
||||
}
|
||||
/*remove blokc cache from local*/
|
||||
if (last_modify + expired_time <= cur_time) {
|
||||
g_unlink (full_path);
|
||||
file_num ++;
|
||||
}
|
||||
}
|
||||
g_free (full_path);
|
||||
}
|
||||
|
||||
g_dir_close (dir);
|
||||
|
||||
return file_num;
|
||||
}
|
||||
|
||||
static int
|
||||
scan_httptemp_dir (const char *httptemp_dir, gint64 expired_time)
|
||||
{
|
||||
return check_httptemp_dir_recursive (httptemp_dir, expired_time);
|
||||
}
|
||||
|
||||
static void *
|
||||
cleanup_expired_httptemp_file (void *arg)
|
||||
{
|
||||
GError *error = NULL;
|
||||
HttpServerStruct *server = arg;
|
||||
SeafileSession *session = server->seaf_session;
|
||||
gint64 ttl = 0;
|
||||
gint64 scan_interval = 0;
|
||||
gint64 file_num = 0;
|
||||
|
||||
ttl = fileserver_config_get_int64 (session->config, HTTP_TEMP_FILE_TTL, &error);
|
||||
if (error) {
|
||||
ttl = HTTP_TEMP_FILE_DEFAULT_TTL;
|
||||
g_clear_error (&error);
|
||||
}
|
||||
|
||||
scan_interval = fileserver_config_get_int64 (session->config, HTTP_SCAN_INTERVAL, &error);
|
||||
if (error) {
|
||||
scan_interval = HTTP_TEMP_FILE_SCAN_INTERVAL;
|
||||
g_clear_error (&error);
|
||||
}
|
||||
|
||||
while (TRUE) {
|
||||
sleep (scan_interval);
|
||||
file_num = scan_httptemp_dir (server->http_temp_dir, ttl);
|
||||
if (file_num) {
|
||||
seaf_message ("Clean up %ld http temp files\n", file_num);
|
||||
file_num = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int
|
||||
seaf_http_server_start (HttpServerStruct *server)
|
||||
{
|
||||
|
@ -2516,6 +2610,13 @@ seaf_http_server_start (HttpServerStruct *server)
|
|||
return -1;
|
||||
|
||||
pthread_detach (server->priv->thread_id);
|
||||
|
||||
pthread_t tid;
|
||||
ret = pthread_create (&tid, NULL, cleanup_expired_httptemp_file, server);
|
||||
if (ret != 0)
|
||||
return -1;
|
||||
|
||||
pthread_detach (tid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue