Skip to content

Commit

Permalink
FS|libcore: Deleting directories; cleanup
Browse files Browse the repository at this point in the history
DirectoryFeed did not have the ability to delete directories even when requested.
  • Loading branch information
skyjake committed Nov 8, 2018
1 parent 3631dab commit 759ad13
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 17 deletions.
11 changes: 8 additions & 3 deletions doomsday/sdk/libcore/include/de/filesys/filesystem.h
Expand Up @@ -351,17 +351,22 @@ class DENG2_PUBLIC FileSystem : public System

void changeBusyLevel(int increment);
int busyLevel() const;
void waitForIdle();

public:
static void waitForIdle();

template <typename T>
static T &locate(String const &path) {
return FileSystem::get().root().locate<T>(path);
return get().root().locate<T>(path);
}

template <typename T>
static T *tryLocate(String const &path) {
return FileSystem::get().root().tryLocate<T>(path);
return get().root().tryLocate<T>(path);
}

static inline bool exists(const String &path) {
return get().root().tryLocate<const File>(path) != nullptr;
}

enum CopyBehavior
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/include/de/filesys/folder.h
Expand Up @@ -179,7 +179,7 @@ class DENG2_PUBLIC Folder : public File
bool tryDestroyFile(String const &name);

/**
* Removes all files in the folder. The files will be delted. If the files
* Removes all files in the folder. The files will be deleted. If the files
* have origin feeds, the feed will be asked to remove the files as well.
* The folder remains locked during the entire operation.
*/
Expand Down
7 changes: 6 additions & 1 deletion doomsday/sdk/libcore/include/de/filesys/nativepath.h
Expand Up @@ -157,6 +157,9 @@ class DENG2_PUBLIC NativePath : public Path

bool isReadable() const;

inline void create() { createPath(*this); }
inline bool destroy() { return destroyPath(*this); }

/**
* Returns the current native working path.
*/
Expand Down Expand Up @@ -185,7 +188,9 @@ class DENG2_PUBLIC NativePath : public Path
*
* @param nativePath Native directory to create.
*/
static void createPath(NativePath const &nativePath);
static void createPath(const NativePath &nativePath);

static bool destroyPath(const NativePath &nativePath);

/**
* Returns the native path separator character.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/src/data/bank.cpp
Expand Up @@ -570,7 +570,7 @@ DENG2_PIMPL(Bank)
{
DENG2_ASSERT(serialCache);

FS::get().waitForIdle();
FS::waitForIdle();
if (Folder *folder = serialCache->folder())
{
folder->destroyAllFilesRecursively();
Expand Down
9 changes: 4 additions & 5 deletions doomsday/sdk/libcore/src/filesys/directoryfeed.cpp
Expand Up @@ -217,9 +217,9 @@ bool DirectoryFeed::prune(File &file) const
if (subFolder->feeds().size() == 1)
{
DirectoryFeed *dirFeed = maybeAs<DirectoryFeed>(subFolder->feeds().front());
if (dirFeed && !NativePath::exists(dirFeed->d->nativePath))
if (dirFeed && !dirFeed->d->nativePath.exists())
{
LOG_RES_NOTE("Pruning \"%s\": no longer exists") << d->nativePath;
LOG_RES_NOTE("Pruning %s: no longer exists") << dirFeed->description(); //d->nativePath;
return true;
}
}
Expand Down Expand Up @@ -247,13 +247,12 @@ void DirectoryFeed::destroyFile(String const &name)
{
NativePath path = d->nativePath / name;

if (!NativePath::exists(path))
if (!path.exists())
{
// The file doesn't exist in the native file system, we can ignore this.
return;
}

if (!QDir::current().remove(path))
if (!path.destroy())
{
/// @throw RemoveError The file @a name exists but could not be removed.
throw RemoveError("DirectoryFeed::destroyFile", "Cannot remove \"" + name +
Expand Down
9 changes: 5 additions & 4 deletions doomsday/sdk/libcore/src/filesys/filesystem.cpp
Expand Up @@ -381,15 +381,16 @@ int FileSystem::busyLevel() const
return d->busyLevel;
}

void FileSystem::waitForIdle()
void FileSystem::waitForIdle() // static
{
using namespace std;

unique_lock<mutex> lk(d->busyMutex);
if (d->busyLevel > 0)
auto &fs = get();
unique_lock<mutex> lk(fs.d->busyMutex);
if (fs.d->busyLevel > 0)
{
LOG_MSG("Waiting until file system is ready");
d->busyFinished.wait(lk);
fs.d->busyFinished.wait(lk);
}
}

Expand Down
20 changes: 18 additions & 2 deletions doomsday/sdk/libcore/src/filesys/nativepath.cpp
Expand Up @@ -295,18 +295,34 @@ void NativePath::createPath(NativePath const &nativePath)
NativePath parentPath = nativePath.fileNamePath();
if (!parentPath.isEmpty() && !exists(parentPath))
{
createPath(parentPath);
parentPath.create();
}

QDir::current().mkdir(nativePath);

if (!exists(nativePath))
if (!nativePath.exists())
{
/// @throw CreateDirError Failed to create directory @a nativePath.
throw CreateDirError("NativePath::createPath", "Could not create: " + nativePath);
}
}

bool NativePath::destroyPath(const NativePath &nativePath)
{
if (!nativePath.isEmpty())
{
if (nativePath.isDirectory())
{
return QDir::current().rmdir(nativePath);
}
else
{
return QDir::current().remove(nativePath);
}
}
return true;
}

QChar NativePath::separator()
{
return DIR_SEPARATOR;
Expand Down

0 comments on commit 759ad13

Please sign in to comment.