/*
* Includes
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef struct dirstruct{
char fname[256];
char localtime[256];
time_t time;
}DirStruct_t;
static int dirlist(const gchar *path, GList **list);
static int file_time_comparefunc(DirStruct_t *fileA, DirStruct_t *fileB);
#define FILE_PATH "/tmp/DirectoryList.txt"
/*
* First argument can be the directory that needs to be listed
* For example if we want to list all the files in Backups folder,
* ./dirlisting /mnt/pools/B/B0/Backups
* This will list all the files in sorted order in /tmp/DirectoryList.txt
*/
int
main(int argc, char **argv)
{
printf("Starting the directory list");
gchar path[256];
GList *list = NULL;
GList *next = NULL;
FILE *fp = NULL;
if(argc > 1){
strncpy(path, argv[1], 256);
}else{
strncpy(path, "/usr/local/amazon", 256);
}
dirlist(path, &list);
list = g_list_sort(list, (GCompareFunc)file_time_comparefunc);
next = list;
if(next != NULL){
fp = fopen(FILE_PATH, "w");
while(next != NULL){
DirStruct_t *data = (DirStruct_t *)(next->data);
if(data != NULL){
fprintf(fp, "/n %ld %s %s ", data->time, data->localtime, data->fname);
g_free(data);
}else{
printf("/n Data is NULL");
}
next = g_list_next(next);
}
g_list_free(list);
fclose(fp);
}else{
printf("/n List is NULL");
}
return (0);
}
static int
file_time_comparefunc(DirStruct_t *fileA, DirStruct_t *fileB)
{
if(fileA->time > fileB->time){
return 1;
}else if(fileA->time == fileB->time){
return 0;
}else {
return -1;
}
}
static int
dirlist(const gchar *path, GList **list)
{
GError *error = NULL;
struct stat statbuf;
struct tm *tm;
char datestring[256];
const gchar *fname = NULL;
GDir *dir = g_dir_open(path, 0, &error);
while((fname = g_dir_read_name(dir)) != NULL){
char *fullpath = g_strconcat(path,"/", fname, NULL);
if(stat(fullpath, &statbuf) != -1){
tm = localtime(&statbuf.st_ctime);
strftime(datestring, sizeof(datestring), nl_langinfo(D_T_FMT), tm);
gboolean isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR)? TRUE : FALSE;
time_t t = mktime(tm);
if(isdir == FALSE){
DirStruct_t *fstruct = g_try_new0(DirStruct_t, 1);
fstruct->time = t;
strncpy(fstruct->localtime, datestring, 256);
strncpy(fstruct->fname, fullpath, 256);
*list = g_list_append(*list, fstruct);
//printf("/n %s %s %ld", fullpath, datestring, t);
}else{
//printf("/n DIR %s %s %ld", fullpath, datestring, t);
dirlist(fullpath, list);
}
}else{
printf("/n Stat error on file %s ", fname);
}
g_free(fullpath);
}
g_dir_close(dir);
return (0);
}