117 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			117 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
 | 
						|
#include "./stacked.hpp"
 | 
						|
 | 
						|
#include <algorithm>
 | 
						|
#include "../detect.hpp"
 | 
						|
 | 
						|
namespace mijin
 | 
						|
{
 | 
						|
 | 
						|
//
 | 
						|
// internal defines
 | 
						|
//
 | 
						|
 | 
						|
//
 | 
						|
// internal constants
 | 
						|
//
 | 
						|
 | 
						|
//
 | 
						|
// internal types
 | 
						|
//
 | 
						|
 | 
						|
//
 | 
						|
// internal variables
 | 
						|
//
 | 
						|
 | 
						|
//
 | 
						|
// internal functions
 | 
						|
//
 | 
						|
 | 
						|
//
 | 
						|
// public functions
 | 
						|
//
 | 
						|
 | 
						|
std::vector<FolderEntry> StackedFileSystemAdapter::listFiles(PathView folder)
 | 
						|
{
 | 
						|
    std::vector<FolderEntry> files;
 | 
						|
 | 
						|
    for (auto& adapter : adapters_)
 | 
						|
    {
 | 
						|
        for (const FolderEntry& entry : adapter->listFiles(folder))
 | 
						|
        {
 | 
						|
            auto it = std::ranges::find_if(files, [&](const FolderEntry& existing)
 | 
						|
            {
 | 
						|
                return existing.path == entry.path;
 | 
						|
            });
 | 
						|
            if (it == files.end()) {
 | 
						|
                files.push_back(entry);
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    return files;
 | 
						|
}
 | 
						|
 | 
						|
FileInfo StackedFileSystemAdapter::getFileInfo(PathView file)
 | 
						|
{
 | 
						|
    for (auto& adapter : adapters_)
 | 
						|
    {
 | 
						|
        FileInfo fileInfo = adapter->getFileInfo(file);
 | 
						|
        if (fileInfo.exists) {
 | 
						|
            return fileInfo;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    return {};
 | 
						|
}
 | 
						|
 | 
						|
Optional<NativePath> StackedFileSystemAdapter::getNativePath(PathView file)
 | 
						|
{
 | 
						|
    for (auto& adapter : adapters_)
 | 
						|
    {
 | 
						|
        Optional<NativePath> result = adapter->getNativePath(file);
 | 
						|
        if (!result.empty())
 | 
						|
        {
 | 
						|
            return result;
 | 
						|
        }
 | 
						|
    }
 | 
						|
    return NULL_OPTIONAL;
 | 
						|
}
 | 
						|
 | 
						|
StreamError StackedFileSystemAdapter::open(PathView path, FileOpenMode mode, std::unique_ptr<Stream>& outStream)
 | 
						|
{
 | 
						|
    // try to open existing files first
 | 
						|
    for (auto& adapter : adapters_)
 | 
						|
    {
 | 
						|
        const FileInfo fileInfo = adapter->getFileInfo(path);
 | 
						|
        if (fileInfo.exists)
 | 
						|
        {
 | 
						|
            return adapter->open(path, mode, outStream);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    // if that doesn't work we attempt to write, try creating it
 | 
						|
    if (mode == FileOpenMode::WRITE || mode == FileOpenMode::READ_WRITE)
 | 
						|
    {
 | 
						|
        for (auto& adapter : adapters_)
 | 
						|
        {
 | 
						|
            const StreamError error = adapter->open(path, mode, outStream);
 | 
						|
            if (error == StreamError::SUCCESS)
 | 
						|
            {
 | 
						|
                return StreamError::SUCCESS;
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
    return StreamError::IO_ERROR;
 | 
						|
}
 | 
						|
 | 
						|
void StackedFileSystemAdapter::getAllPaths(PathView path, std::vector<PathReference>& outPaths)
 | 
						|
{
 | 
						|
    for (auto& adapter : adapters_)
 | 
						|
    {
 | 
						|
        adapter->getAllPaths(path, outPaths);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
} // namespace mijin
 |