FileSystemPOSIX.cpp [plain text]
#include "config.h"
#include "FileSystem.h"
#include "CString.h"
#include "PlatformString.h"
#include <sys/stat.h>
#include <unistd.h>
namespace WebCore {
bool fileExists(const String& path)
{
if (path.isNull())
return false;
CString fsRep = fileSystemRepresentation(path);
if (!fsRep.data() || fsRep.data()[0] == '\0')
return false;
struct stat fileInfo;
return !stat(fsRep.data(), &fileInfo);
}
bool deleteFile(const String& path)
{
CString fsRep = fileSystemRepresentation(path);
if (!fsRep.data() || fsRep.data()[0] == '\0')
return false;
return !unlink(fsRep.data());
}
bool deleteEmptyDirectory(const String& path)
{
CString fsRep = fileSystemRepresentation(path);
if (!fsRep.data() || fsRep.data()[0] == '\0')
return false;
return !rmdir(fsRep.data());
}
bool getFileSize(const String& path, long long& result)
{
CString fsRep = fileSystemRepresentation(path);
if (!fsRep.data() || fsRep.data()[0] == '\0')
return false;
struct stat fileInfo;
if (stat(fsRep.data(), &fileInfo))
return false;
result = fileInfo.st_size;
return true;
}
bool getFileModificationTime(const String& path, time_t& result)
{
CString fsRep = fileSystemRepresentation(path);
if (!fsRep.data() || fsRep.data()[0] == '\0')
return false;
struct stat fileInfo;
if (stat(fsRep.data(), &fileInfo))
return false;
result = fileInfo.st_mtime;
return true;
}
String pathByAppendingComponent(const String& path, const String& component)
{
if (path.endsWith("/"))
return path + component;
else
return path + "/" + component;
}
bool makeAllDirectories(const String& path)
{
CString fullPath = fileSystemRepresentation(path);
if (!access(fullPath.data(), F_OK))
return true;
char* p = fullPath.mutableData() + 1;
int length = fullPath.length();
if(p[length - 1] == '/')
p[length - 1] = '\0';
for (; *p; ++p)
if (*p == '/') {
*p = '\0';
if (access(fullPath.data(), F_OK))
if (mkdir(fullPath.data(), S_IRWXU))
return false;
*p = '/';
}
if (access(fullPath.data(), F_OK))
if (mkdir(fullPath.data(), S_IRWXU))
return false;
return true;
}
}