2009-10-01 01:10:58 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <gccore.h>
|
|
|
|
#include <sys/dir.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "listfiles.h"
|
2009-11-15 22:30:44 +01:00
|
|
|
#include "libfat/fat.h"
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
|
|
static char alldirfiles[300][70];
|
|
|
|
char filenames[80];
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
bool findfile(const char * filename, const char * path)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
DIR *dir;
|
|
|
|
struct dirent *file;
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
dir = opendir(path);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
|
|
|
char temp[11];
|
2010-09-24 02:48:03 +02:00
|
|
|
while ((file = readdir(dir)))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
snprintf(temp, sizeof(temp), "%s", file->d_name);
|
|
|
|
if (!strncmpi(temp, filename, 11))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
closedir(dir);
|
2009-10-01 01:10:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
closedir(dir);
|
2009-10-01 01:10:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
bool subfoldercreate(const char * fullpath)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
//check forsubfolders
|
|
|
|
char dir[300];
|
|
|
|
char * pch = NULL;
|
|
|
|
u32 len;
|
|
|
|
struct stat st;
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
strlcpy(dir, fullpath, sizeof(dir));
|
|
|
|
len = strlen(dir);
|
|
|
|
if (len && len < sizeof(dir) - 2 && dir[len - 1] != '/')
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
dir[len++] = '/';
|
|
|
|
dir[len] = '\0';
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
if (stat(dir, &st) != 0) // fullpath not exist?
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
while (len && dir[len - 1] == '/')
|
|
|
|
dir[--len] = '\0'; // remove all trailing /
|
|
|
|
pch = strrchr(dir, '/');
|
|
|
|
if (pch == NULL) return false;
|
2010-09-19 01:16:05 +02:00
|
|
|
*pch = '\0';
|
2010-09-24 02:48:03 +02:00
|
|
|
if (subfoldercreate(dir))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
*pch = '/';
|
2010-09-24 02:48:03 +02:00
|
|
|
if (mkdir(dir, 0777) == -1) return false;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
else return false;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
return true;
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
bool subfolderremove(const char * fullpath, const char*fp)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
struct stat st;
|
2010-09-24 02:48:03 +02:00
|
|
|
if (stat(fullpath, &st) != 0) // fullpath not exist?
|
|
|
|
return false;
|
|
|
|
if (S_ISDIR( st.st_mode ))
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
DIR_ITER *dir = NULL;
|
|
|
|
char filename[256];
|
|
|
|
bool cont = true;
|
2010-09-24 02:48:03 +02:00
|
|
|
while (cont)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
cont = false;
|
2010-09-24 02:48:03 +02:00
|
|
|
dir = diropen(fullpath);
|
|
|
|
if (dir)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
char* bind = fullpath[strlen(fullpath) - 1] == '/' ? "" : "/";
|
|
|
|
while (dirnext(dir, filename, &st) == 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strcmp(filename, ".") != 0 && strcmp(filename, "..") != 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
|
|
|
char currentname[256];
|
2010-09-24 02:48:03 +02:00
|
|
|
if (S_ISDIR( st.st_mode ))
|
|
|
|
snprintf(currentname, sizeof(currentname), "%s%s%s/", fullpath, bind, filename);
|
|
|
|
else snprintf(currentname, sizeof(currentname), "%s%s%s", fullpath, bind, filename);
|
|
|
|
subfolderremove(currentname, fp);
|
2010-09-19 01:16:05 +02:00
|
|
|
cont = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
dirclose(dir);
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
return unlink(fullpath) == 0;
|
2009-11-10 00:03:13 +01:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
char * GetFileName(int i)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
return alldirfiles[i];
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
s32 filenamescmp(const void *a, const void *b)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
/* Compare strings */
|
2010-09-24 02:48:03 +02:00
|
|
|
return stricmp((char *) a, (char *) b);
|
2009-10-01 01:10:58 +02:00
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
int GetAllDirFiles(char * filespath)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
int countfiles = 0;
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
DIR_ITER* dir;
|
2010-09-24 02:48:03 +02:00
|
|
|
dir = diropen(filespath);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
if (dir == NULL) //If empty
|
|
|
|
return 0;
|
|
|
|
while (dirnext(dir, filenames, &st) == 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if ((st.st_mode & S_IFDIR) == 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
// st.st_mode & S_IFDIR indicates a directory
|
2010-09-24 02:48:03 +02:00
|
|
|
snprintf(alldirfiles[countfiles], 70, "%s", filenames);
|
2009-10-01 01:10:58 +02:00
|
|
|
countfiles++;
|
|
|
|
}
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
dirclose(dir);
|
|
|
|
qsort(alldirfiles, countfiles, sizeof(char[70]), filenamescmp);
|
2009-10-01 01:10:58 +02:00
|
|
|
return countfiles;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
bool checkfile(char * path)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2009-10-01 01:10:58 +02:00
|
|
|
FILE * f;
|
2010-09-24 02:48:03 +02:00
|
|
|
f = fopen(path, "r");
|
|
|
|
if (f)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
fclose(f);
|
2009-10-01 01:10:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
bool SearchFile(const char * searchpath, const char * searched_filename, char * outfilepath)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2009-12-28 17:05:16 +01:00
|
|
|
struct stat st;
|
|
|
|
DIR_ITER *dir = NULL;
|
|
|
|
bool result = false;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2009-12-28 17:05:16 +01:00
|
|
|
char filename[1024];
|
2010-09-24 02:48:03 +02:00
|
|
|
char pathptr[strlen(searchpath) + 1];
|
|
|
|
snprintf(pathptr, sizeof(pathptr), "%s", searchpath);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
if (pathptr[strlen(pathptr) - 1] == '/')
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
pathptr[strlen(pathptr) - 1] = '\0';
|
2009-12-28 17:05:16 +01:00
|
|
|
}
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
dir = diropen(pathptr);
|
|
|
|
if (!dir) return false;
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2010-09-24 02:48:03 +02:00
|
|
|
while (dirnext(dir, filename, &st) == 0 && result == false)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strcasecmp(filename, searched_filename) == 0)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (outfilepath)
|
2010-09-19 01:16:05 +02:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
sprintf(outfilepath, "%s/%s", pathptr, filename);
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
result = true;
|
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
else if ((st.st_mode & S_IFDIR) != 0)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-24 02:48:03 +02:00
|
|
|
if (strcmp(filename, ".") != 0 && strcmp(filename, "..") != 0)
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2009-12-28 17:05:16 +01:00
|
|
|
char newpath[1024];
|
2010-09-24 02:48:03 +02:00
|
|
|
snprintf(newpath, sizeof(newpath), "%s/%s", pathptr, filename);
|
|
|
|
result = SearchFile(newpath, searched_filename, outfilepath);
|
2009-12-28 17:05:16 +01:00
|
|
|
}
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
2010-09-24 02:48:03 +02:00
|
|
|
dirclose(dir);
|
2009-10-01 01:10:58 +02:00
|
|
|
|
2009-12-28 17:05:16 +01:00
|
|
|
return result;
|
|
|
|
}
|