Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/lib/file/vfs/vfs.cpp
/* Copyright (C) 2017 Wildfire Games. | /* Copyright (C) 2021 Wildfire Games. | ||||
* | * | ||||
* Permission is hereby granted, free of charge, to any person obtaining | * Permission is hereby granted, free of charge, to any person obtaining | ||||
* a copy of this software and associated documentation files (the | * a copy of this software and associated documentation files (the | ||||
* "Software"), to deal in the Software without restriction, including | * "Software"), to deal in the Software without restriction, including | ||||
* without limitation the rights to use, copy, modify, merge, publish, | * without limitation the rights to use, copy, modify, merge, publish, | ||||
* distribute, sublicense, and/or sell copies of the Software, and to | * distribute, sublicense, and/or sell copies of the Software, and to | ||||
* permit persons to whom the Software is furnished to do so, subject to | * permit persons to whom the Software is furnished to do so, subject to | ||||
* the following conditions: | * the following conditions: | ||||
▲ Show 20 Lines • Show All 114 Lines • ▼ Show 20 Lines | virtual Status GetDirectoryEntries(const VfsPath& path, CFileInfos* fileInfos, DirectoryNames* subdirectoryNames) const | ||||
return INFO::OK; | return INFO::OK; | ||||
} | } | ||||
virtual Status CreateFile(const VfsPath& pathname, const shared_ptr<u8>& fileContents, size_t size) | virtual Status CreateFile(const VfsPath& pathname, const shared_ptr<u8>& fileContents, size_t size) | ||||
{ | { | ||||
std::lock_guard<std::mutex> lock(vfs_mutex); | std::lock_guard<std::mutex> lock(vfs_mutex); | ||||
VfsDirectory* directory; | VfsDirectory* directory; | ||||
Status st; | Status st; | ||||
st = vfs_Lookup(pathname, &m_rootDirectory, directory, 0, VFS_LOOKUP_ADD|VFS_LOOKUP_CREATE|VFS_LOOKUP_CREATE_ALWAYS); | st = vfs_Lookup(pathname, &m_rootDirectory, directory, 0, VFS_LOOKUP_ADD|VFS_LOOKUP_REAL_PATH); | ||||
if (st == ERR::FILE_ACCESS) | if (st == ERR::FILE_ACCESS) | ||||
return ERR::FILE_ACCESS; | return ERR::FILE_ACCESS; | ||||
WARN_RETURN_STATUS_IF_ERR(st); | WARN_RETURN_STATUS_IF_ERR(st); | ||||
const PRealDirectory& realDirectory = directory->AssociatedDirectory(); | const PRealDirectory& realDirectory = directory->AssociatedDirectory(); | ||||
const OsPath name = pathname.Filename(); | const OsPath name = pathname.Filename(); | ||||
RETURN_STATUS_IF_ERR(realDirectory->Store(name, fileContents, size)); | RETURN_STATUS_IF_ERR(realDirectory->Store(name, fileContents, size)); | ||||
const VfsFile file(name, size, time(0), realDirectory->Priority(), realDirectory); | const VfsFile file(name, size, time(0), realDirectory->Priority(), realDirectory); | ||||
directory->AddFile(file); | directory->AddFile(file); | ||||
m_trace->NotifyStore(pathname, size); | m_trace->NotifyStore(pathname, size); | ||||
return INFO::OK; | return INFO::OK; | ||||
} | } | ||||
virtual Status ReplaceFile(const VfsPath& pathname, const shared_ptr<u8>& fileContents, size_t size) | |||||
{ | |||||
std::unique_lock<std::mutex> lock(vfs_mutex); | |||||
VfsDirectory* directory; | |||||
VfsFile* file; | |||||
Status st; | |||||
st = vfs_Lookup(pathname, &m_rootDirectory, directory, &file, VFS_LOOKUP_ADD|VFS_LOOKUP_CREATE); | |||||
// There is no such file, create it. | |||||
if (st == ERR::VFS_FILE_NOT_FOUND) | |||||
{ | |||||
lock.unlock(); | |||||
return CreateFile(pathname, fileContents, size); | |||||
} | |||||
WARN_RETURN_STATUS_IF_ERR(st); | |||||
RealDirectory realDirectory(file->Loader()->Path(), file->Priority(), directory->AssociatedDirectory()->Flags()); | |||||
RETURN_STATUS_IF_ERR(realDirectory.Store(pathname.Filename(), fileContents, size)); | |||||
directory->AddFile(*file); | |||||
m_trace->NotifyStore(pathname, size); | |||||
return INFO::OK; | |||||
} | |||||
virtual Status LoadFile(const VfsPath& pathname, shared_ptr<u8>& fileContents, size_t& size) | virtual Status LoadFile(const VfsPath& pathname, shared_ptr<u8>& fileContents, size_t& size) | ||||
{ | { | ||||
std::lock_guard<std::mutex> lock(vfs_mutex); | std::lock_guard<std::mutex> lock(vfs_mutex); | ||||
VfsDirectory* directory; VfsFile* file; | VfsDirectory* directory; VfsFile* file; | ||||
// per 2010-05-01 meeting, this shouldn't raise 'scary error | // per 2010-05-01 meeting, this shouldn't raise 'scary error | ||||
// dialogs', which might fail to display the culprit pathname | // dialogs', which might fail to display the culprit pathname | ||||
// instead, callers should log the error, including pathname. | // instead, callers should log the error, including pathname. | ||||
Show All 15 Lines | public: | ||||
{ | { | ||||
std::lock_guard<std::mutex> lock(vfs_mutex); | std::lock_guard<std::mutex> lock(vfs_mutex); | ||||
std::wstring textRepresentation; | std::wstring textRepresentation; | ||||
textRepresentation.reserve(100*KiB); | textRepresentation.reserve(100*KiB); | ||||
DirectoryDescriptionR(textRepresentation, m_rootDirectory, 0); | DirectoryDescriptionR(textRepresentation, m_rootDirectory, 0); | ||||
return textRepresentation; | return textRepresentation; | ||||
} | } | ||||
virtual Status GetRealPath(const VfsPath& pathname, OsPath& realPathname) | virtual Status GetOriginalPath(const VfsPath& pathname, OsPath& realPathname) | ||||
{ | { | ||||
std::lock_guard<std::mutex> lock(vfs_mutex); | std::lock_guard<std::mutex> lock(vfs_mutex); | ||||
VfsDirectory* directory; VfsFile* file; | VfsDirectory* directory; VfsFile* file; | ||||
WARN_RETURN_STATUS_IF_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file)); | WARN_RETURN_STATUS_IF_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file)); | ||||
realPathname = file->Loader()->Path() / pathname.Filename(); | realPathname = file->Loader()->Path() / pathname.Filename(); | ||||
return INFO::OK; | return INFO::OK; | ||||
} | } | ||||
virtual Status GetDirectoryRealPath(const VfsPath& pathname, OsPath& realPathname) | virtual Status GetRealPath(const VfsPath& pathname, OsPath& realPathname, bool createMissingDirectories) | ||||
{ | |||||
std::lock_guard<std::mutex> lock(vfs_mutex); | |||||
VfsDirectory* directory; VfsFile* file; | |||||
size_t flags = VFS_LOOKUP_REAL_PATH | (createMissingDirectories ? VFS_LOOKUP_ADD : 0); | |||||
WARN_RETURN_STATUS_IF_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, &file, flags)); | |||||
ENSURE(directory->AssociatedDirectory()); | |||||
realPathname = directory->AssociatedDirectory()->Path() / pathname.Filename(); | |||||
return INFO::OK; | |||||
} | |||||
virtual Status GetDirectoryRealPath(const VfsPath& pathname, OsPath& realPathname, bool createMissingDirectories) | |||||
{ | { | ||||
std::lock_guard<std::mutex> lock(vfs_mutex); | std::lock_guard<std::mutex> lock(vfs_mutex); | ||||
VfsDirectory* directory; | VfsDirectory* directory; | ||||
WARN_RETURN_STATUS_IF_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, NULL)); | size_t flags = VFS_LOOKUP_REAL_PATH | (createMissingDirectories ? VFS_LOOKUP_ADD : 0); | ||||
WARN_RETURN_STATUS_IF_ERR(vfs_Lookup(pathname, &m_rootDirectory, directory, NULL, flags)); | |||||
ENSURE(directory->AssociatedDirectory()); | |||||
realPathname = directory->AssociatedDirectory()->Path(); | realPathname = directory->AssociatedDirectory()->Path(); | ||||
return INFO::OK; | return INFO::OK; | ||||
} | } | ||||
virtual Status GetVirtualPath(const OsPath& realPathname, VfsPath& pathname) | virtual Status GetVirtualPath(const OsPath& realPathname, VfsPath& pathname) | ||||
{ | { | ||||
std::lock_guard<std::mutex> lock(vfs_mutex); | std::lock_guard<std::mutex> lock(vfs_mutex); | ||||
const OsPath realPath = realPathname.Parent()/""; | const OsPath realPath = realPathname.Parent()/""; | ||||
▲ Show 20 Lines • Show All 67 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator