FileSystemBrew.cpp [plain text]
#include "config.h"
#include "FileSystem.h"
#include "NotImplemented.h"
#include <AEEAppGen.h>
#include <AEEFile.h>
#include <AEEStdLib.h>
#include <wtf/RandomNumber.h>
#include <wtf/brew/RefPtrBrew.h>
#include <wtf/brew/ShellBrew.h>
#include <wtf/text/CString.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
bool getFileSize(const String& path, long long& result)
{
RefPtr<IFileMgr> fileMgr = createRefPtrInstance<IFileMgr>(AEECLSID_FILEMGR);
FileInfo info;
if (IFILEMGR_GetInfo(fileMgr.get(), path.utf8().data(), &info) == SUCCESS) {
result = info.dwSize;
return true;
}
return false;
}
bool getFileModificationTime(const String& path, time_t& result)
{
return false;
}
bool fileExists(const String& path)
{
RefPtr<IFileMgr> fileMgr = createRefPtrInstance<IFileMgr>(AEECLSID_FILEMGR);
return (IFILEMGR_Test(fileMgr.get(), path.utf8().data()) == SUCCESS);
}
bool deleteFile(const String& path)
{
RefPtr<IFileMgr> fileMgr = createRefPtrInstance<IFileMgr>(AEECLSID_FILEMGR);
return (IFILEMGR_Remove(fileMgr.get(), path.utf8().data()) == SUCCESS);
}
bool deleteEmptyDirectory(const String& path)
{
RefPtr<IFileMgr> fileMgr = createRefPtrInstance<IFileMgr>(AEECLSID_FILEMGR);
return (IFILEMGR_RmDir(fileMgr.get(), path.utf8().data()) == SUCCESS);
}
String pathByAppendingComponent(const String& path, const String& component)
{
if (component.isEmpty())
return path;
Vector<UChar, 1024> buffer;
buffer.append(path.characters(), path.length());
if (buffer.last() != L'/' && component[0] != L'/')
buffer.append(L'/');
buffer.append(component.characters(), component.length());
return String(buffer.data(), buffer.size());
}
CString fileSystemRepresentation(const String& path)
{
return path.utf8();
}
static String canonicalPath(const String& path)
{
RefPtr<IFileMgr> fileMgr = createRefPtrInstance<IFileMgr>(AEECLSID_FILEMGR);
int canonPathLen;
IFILEMGR_ResolvePath(fileMgr.get(), path.utf8().data(), 0, &canonPathLen);
Vector<char> canonPathBuffer(canonPathLen);
IFILEMGR_ResolvePath(fileMgr.get(), path.utf8().data(), canonPathBuffer.data(), &canonPathLen);
String canonPath(canonPathBuffer.data());
int lastDivPos = canonPath.reverseFind('/');
int endPos = canonPath.length();
if (lastDivPos == endPos - 1)
canonPath = canonPath.substring(0, canonPath.length() - 1);
return canonPath;
}
static bool makeAllDirectories(IFileMgr* fileManager, const String& path)
{
if (path == canonicalPath(AEEFS_HOME_DIR))
return true;
int lastDivPos = path.reverseFind('/');
int endPos = path.length();
if (lastDivPos == path.length() - 1) {
endPos -= 1;
lastDivPos = path.reverseFind('/', lastDivPos);
}
if (lastDivPos > 0) {
if (!makeAllDirectories(fileManager, path.substring(0, lastDivPos)))
return false;
}
String folder(path.substring(0, endPos));
IFILEMGR_MkDir(fileManager, folder.utf8().data());
FileInfo fileInfo;
if (IFILEMGR_GetInfo(fileManager, folder.utf8().data(), &fileInfo) != SUCCESS)
return false;
return fileInfo.attrib & _FA_DIR;
}
bool makeAllDirectories(const String& path)
{
RefPtr<IFileMgr> fileMgr = createRefPtrInstance<IFileMgr>(AEECLSID_FILEMGR);
return makeAllDirectories(fileMgr.get(), canonicalPath(path));
}
String homeDirectoryPath()
{
const int webViewClassId = 0x010aa04c;
return String::format("fs:/~0X%08X/", webViewClassId);
}
String pathGetFileName(const String& path)
{
return path.substring(path.reverseFind('/') + 1);
}
String directoryName(const String& path)
{
String fileName = pathGetFileName(path);
String dirName = String(path);
dirName.truncate(dirName.length() - pathGetFileName(path).length());
return dirName;
}
String openTemporaryFile(const String& prefix, PlatformFileHandle& handle)
{
String tempPath("fs:/~/tmp");
RefPtr<IFileMgr> fileMgr = createRefPtrInstance<IFileMgr>(AEECLSID_FILEMGR);
IFILEMGR_MkDir(fileMgr.get(), tempPath.utf8().data());
int number = static_cast<int>(randomNumber() * 10000);
String filename;
do {
StringBuilder builder;
builder.append(tempPath);
builder.append('/');
builder.append(prefix);
builder.append(String::number(number));
filename = builder.toString();
number++;
} while (IFILEMGR_Test(fileMgr.get(), filename.utf8().data()) == SUCCESS);
IFile* tempFile = IFILEMGR_OpenFile(fileMgr.get(), filename.utf8().data(), _OFM_CREATE);
if (tempFile) {
handle = tempFile;
return filename;
}
return String();
}
void closeFile(PlatformFileHandle& handle)
{
if (isHandleValid(handle)) {
IFILE_Release(handle);
handle = invalidPlatformFileHandle;
}
}
int writeToFile(PlatformFileHandle handle, const char* data, int length)
{
if (!isHandleValid(handle))
return -1;
int bytesWritten = IFILE_Write(handle, data, length);
if (!bytesWritten)
return -1;
return bytesWritten;
}
bool unloadModule(PlatformModule module)
{
notImplemented();
return false;
}
Vector<String> listDirectory(const String& path, const String& filter)
{
Vector<String> entries;
notImplemented();
return entries;
}
}