#include "config.h"
#include "FileSystem.h"
#include "NotImplemented.h"
#include <Ecore.h>
#include <Ecore_File.h>
#include <Eina.h>
#include <dirent.h>
#include <dlfcn.h>
#include <errno.h>
#include <fnmatch.h>
#include <glib.h> // TODO: remove me after following TODO is solved.
#include <limits.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wtf/text/CString.h>
namespace WebCore {
CString fileSystemRepresentation(const String& path)
{
return path.utf8();
}
char* filenameFromString(const String& string)
{
#if PLATFORM(WIN_OS)
return g_strdup(string.utf8().data());
#else
return g_uri_unescape_string(string.utf8().data(), 0);
#endif
}
CString openTemporaryFile(const char* prefix, PlatformFileHandle& handle)
{
char buffer[PATH_MAX];
const char* tmpDir = getenv("TMPDIR");
if (!tmpDir)
tmpDir = "/tmp";
if (snprintf(buffer, PATH_MAX, "%s/%sXXXXXX", tmpDir, prefix) >= PATH_MAX)
goto end;
handle = mkstemp(buffer);
if (handle < 0)
goto end;
return CString(buffer);
end:
handle = invalidPlatformFileHandle;
return CString();
}
bool unloadModule(PlatformModule module)
{
return !dlclose(module);
}
String homeDirectoryPath()
{
const char *home = getenv("HOME");
if (!home) {
home = getenv("TMPDIR");
if (!home)
home = "/tmp";
}
return String::fromUTF8(home);
}
Vector<String> listDirectory(const String& path, const String& filter)
{
Vector<String> entries;
CString cpath = path.utf8();
CString cfilter = filter.utf8();
char filePath[PATH_MAX];
char* fileName;
size_t fileNameSpace;
DIR* dir;
if (cpath.length() + NAME_MAX >= sizeof(filePath))
return entries;
memcpy(filePath, cpath.data(), cpath.length());
fileName = filePath + cpath.length();
if (cpath.length() > 0 && filePath[cpath.length() - 1] != '/') {
fileName[0] = '/';
fileName++;
}
fileNameSpace = sizeof(filePath) - (fileName - filePath) - 1;
dir = opendir(cpath.data());
if (!dir)
return entries;
struct dirent* de;
while (de = readdir(dir)) {
size_t nameLen;
if (de->d_name[0] == '.') {
if (de->d_name[1] == '\0')
continue;
if (de->d_name[1] == '.' && de->d_name[2] == '\0')
continue;
}
if (fnmatch(cfilter.data(), de->d_name, 0))
continue;
nameLen = strlen(de->d_name);
if (nameLen >= fileNameSpace)
continue;
memcpy(fileName, de->d_name, nameLen + 1);
entries.append(filePath);
}
closedir(dir);
return entries;
}
}