Last active
July 16, 2021 10:11
-
-
Save kYroL01/fff567a441b8a06b0aa3f7d8af3fcb90 to your computer and use it in GitHub Desktop.
Directory Cleaner with date format name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* -*- compile-command: "gcc -Wall -pedantic -g3 dir_cleaner.c -o dir_cleaner" -*- */ | |
#define _XOPEN_SOURCE 500 | |
#define _GNU_SOURCE | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <time.h> | |
#include <ftw.h> | |
#include <dirent.h> | |
#include <unistd.h> | |
#define DAYS_DEL 30 | |
static char pcap_storage_sip[100] = "/tmp/pcap/sip"; | |
static char pcap_storage_rtp[100] = "/tmp/pcap/rtp"; | |
int date_time_now(char *buff) | |
{ | |
time_t now = time(&now); | |
int ret = 0; | |
ret = strftime(buff, 20, "%Y%m%d", localtime(&now)); | |
if(ret == 0) | |
return -1; | |
return 0; | |
} | |
int extract_date_dir(const char *path) { | |
if(path == NULL) | |
return -1; | |
const char *tmp, *end; | |
char buff[10] = {'\0'}; | |
int len, dir_name_int; | |
len = strlen(path); | |
end = path+len-1; | |
tmp = strrchr(path, '/'); | |
strncpy(buff, tmp+1, end-tmp); | |
dir_name_int = atoi(buff); | |
printf("%d\n", dir_name_int); | |
return dir_name_int; | |
} | |
// callback for nftw | |
int rmrf_cb(const char *path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) | |
{ | |
char dir_date_now[10]; | |
int r, rv = 0, diff_time = 0; | |
int dnow, dpath; | |
/* TIME NOW */ | |
r = date_time_now(dir_date_now); | |
if(r == -1) { | |
printf("ERROR date_time_now\n"); | |
return -1; | |
} | |
// We exclude the sip/ and rtp/ folder from the check | |
if((strncmp(path, pcap_storage_sip, strlen(path)) != 0) && (strncmp(path, pcap_storage_rtp, strlen(path)) != 0)) { | |
dnow = atoi(dir_date_now); | |
dpath = extract_date_dir(path); | |
/* struct tm tm1, tm2; */ | |
/* time_t epoch1, epoch2; */ | |
/* if (strptime(dnow, "%Y%Y%Y%Y%m%m%d%d", &tm1) != NULL) */ | |
/* epoch1 = mktime(&tm); */ | |
/* if (strptime(dpath, "%Y%Y%Y%Y%m%m%d%d", &tm2) != NULL) */ | |
/* epoch2 = mktime(&tm); */ | |
diff_time = dnow - dpath; | |
if(diff_time >= DAYS_DEL) { | |
printf("Deleting %s...\n", path); | |
rv = remove(path); | |
if(rv){ | |
perror(path); | |
printf("ERROR in remove_cb: %s\n", path); | |
} | |
} | |
} | |
return rv; | |
} | |
// implementation of rm -rf command using the nftw (file tree walk) function | |
int rmrf(char *path) | |
{ | |
return nftw(path, rmrf_cb, 20, FTW_DEPTH | FTW_PHYS); | |
} | |
int remove_dir(char *path) | |
{ | |
if(path == NULL) | |
return -1; | |
printf("Checking path for %s\n", path); | |
struct dirent *dir; | |
int ret = -1; | |
DIR *d = opendir(path); // open the path | |
if(d == NULL) | |
return -1; | |
while((dir = readdir(d))) | |
{ | |
if(dir-> d_type == DT_DIR && strcmp(dir->d_name,".") != 0 && strcmp(dir->d_name,"..") != 0) | |
{ | |
// remove it | |
ret = rmrf(path); | |
if(ret != 0) { | |
printf("Cannot delete %s\n", dir->d_name); | |
} | |
} | |
} | |
closedir(d); | |
return 0; | |
} | |
int main(int argc, char *argv[]) { | |
char string_sip[100] = {'\0'}; | |
char string_rtp[100] = {'\0'}; | |
int r = 0, len_sip = 0, len_rtp = 0; | |
if(argc > 1) { | |
printf("arg 1 = %s\n", argv[1]); | |
strncpy(string_sip, argv[1], strlen(argv[1])); | |
printf("string SIP = %s\n", string_sip); | |
strncpy(string_rtp, argv[1], strlen(argv[1])); | |
printf("string RTP = %s\n", string_rtp); | |
len_sip = strlen("/sip"); | |
len_rtp = strlen("/rtp"); | |
strncat(string_sip, "/sip", len_sip); | |
printf("strncat SIP = %s\n", string_sip); | |
strncpy(pcap_storage_sip, string_sip, sizeof(pcap_storage_sip)); | |
printf("final string SIP = %s\n", pcap_storage_sip); | |
strncat(string_rtp, "/rtp", len_rtp); | |
printf("strncat RTP = %s\n", string_rtp); | |
strncpy(pcap_storage_rtp, string_rtp, sizeof(pcap_storage_rtp)); | |
printf("final string RTP = %s\n", pcap_storage_rtp); | |
} else { | |
printf("Default path\n"); | |
printf("final string SIP = %s\n", pcap_storage_sip); | |
printf("final string RTP = %s\n", pcap_storage_rtp); | |
} | |
// DIR REC PATH CHECK | |
r = remove_dir(pcap_storage_sip); | |
if(r != 0) | |
printf("Error in remove_dir for %s!!!\n", pcap_storage_sip); | |
r = remove_dir(pcap_storage_rtp); | |
if(r != 0) | |
printf("Error in remove_dir for %s!!!\n", pcap_storage_rtp); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment