2019-03-22 15:49:10 +00:00
|
|
|
/*
|
2019-04-08 02:00:49 +00:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2019-03-22 15:49:10 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include <switch.h>
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
|
|
|
class IDirectory {
|
|
|
|
public:
|
|
|
|
virtual ~IDirectory() {}
|
2019-03-22 17:20:36 +00:00
|
|
|
|
2019-03-22 15:49:10 +00:00
|
|
|
Result Read(uint64_t *out_count, FsDirectoryEntry *out_entries, uint64_t max_entries) {
|
|
|
|
if (out_count == nullptr) {
|
|
|
|
return ResultFsNullptrArgument;
|
|
|
|
}
|
|
|
|
if (max_entries == 0) {
|
|
|
|
*out_count = 0;
|
2019-10-24 08:40:44 +00:00
|
|
|
return ResultSuccess();
|
2019-03-22 15:49:10 +00:00
|
|
|
}
|
|
|
|
if (out_entries == nullptr) {
|
|
|
|
return ResultFsNullptrArgument;
|
|
|
|
}
|
|
|
|
return ReadImpl(out_count, out_entries, max_entries);
|
|
|
|
}
|
2019-03-22 17:20:36 +00:00
|
|
|
|
2019-03-22 15:49:10 +00:00
|
|
|
Result GetEntryCount(uint64_t *count) {
|
|
|
|
if (count == nullptr) {
|
|
|
|
return ResultFsNullptrArgument;
|
|
|
|
}
|
|
|
|
return GetEntryCountImpl(count);
|
|
|
|
}
|
2019-03-22 17:20:36 +00:00
|
|
|
|
2019-03-22 15:49:10 +00:00
|
|
|
protected:
|
|
|
|
/* ...? */
|
|
|
|
private:
|
|
|
|
virtual Result ReadImpl(uint64_t *out_count, FsDirectoryEntry *out_entries, uint64_t max_entries) = 0;
|
|
|
|
virtual Result GetEntryCountImpl(uint64_t *count) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class IDirectoryInterface : public IServiceObject {
|
2019-06-28 06:34:26 +00:00
|
|
|
private:
|
|
|
|
enum class CommandId {
|
|
|
|
Read = 0,
|
|
|
|
GetEntryCount = 1,
|
|
|
|
};
|
2019-03-22 15:49:10 +00:00
|
|
|
private:
|
|
|
|
std::unique_ptr<IDirectory> base_dir;
|
|
|
|
public:
|
|
|
|
IDirectoryInterface(IDirectory *d) : base_dir(d) {
|
|
|
|
/* ... */
|
|
|
|
};
|
|
|
|
IDirectoryInterface(std::unique_ptr<IDirectory> d) : base_dir(std::move(d)) {
|
|
|
|
/* ... */
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
/* Actual command API. */
|
|
|
|
virtual Result Read(OutBuffer<FsDirectoryEntry> buffer, Out<u64> out_count) final {
|
|
|
|
return this->base_dir->Read(out_count.GetPointer(), buffer.buffer, buffer.num_elements);
|
|
|
|
};
|
|
|
|
virtual Result GetEntryCount(Out<u64> out_count) final {
|
|
|
|
return this->base_dir->GetEntryCount(out_count.GetPointer());
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
DEFINE_SERVICE_DISPATCH_TABLE {
|
|
|
|
/* 1.0.0- */
|
2019-06-28 06:34:26 +00:00
|
|
|
MAKE_SERVICE_COMMAND_META(IDirectoryInterface, Read),
|
|
|
|
MAKE_SERVICE_COMMAND_META(IDirectoryInterface, GetEntryCount),
|
2019-03-22 15:49:10 +00:00
|
|
|
};
|
|
|
|
};
|
2019-03-22 17:20:36 +00:00
|
|
|
|
|
|
|
class ProxyDirectory : public IDirectory {
|
|
|
|
private:
|
|
|
|
std::unique_ptr<FsDir> base_dir;
|
|
|
|
public:
|
|
|
|
ProxyDirectory(FsDir *d) : base_dir(d) {
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
ProxyDirectory(std::unique_ptr<FsDir> d) : base_dir(std::move(d)) {
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
ProxyDirectory(FsDir d) {
|
|
|
|
this->base_dir = std::make_unique<FsDir>(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ProxyDirectory() {
|
|
|
|
fsDirClose(this->base_dir.get());
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
virtual Result ReadImpl(uint64_t *out_count, FsDirectoryEntry *out_entries, uint64_t max_entries) {
|
|
|
|
size_t count;
|
2019-06-20 11:04:33 +00:00
|
|
|
R_TRY(fsDirRead(this->base_dir.get(), 0, &count, max_entries, out_entries));
|
2019-03-22 17:20:36 +00:00
|
|
|
|
2019-06-20 11:04:33 +00:00
|
|
|
*out_count = count;
|
2019-10-24 08:40:44 +00:00
|
|
|
return ResultSuccess();
|
2019-03-22 17:20:36 +00:00
|
|
|
}
|
|
|
|
virtual Result GetEntryCountImpl(uint64_t *count) {
|
|
|
|
return fsDirGetEntryCount(this->base_dir.get(), count);
|
|
|
|
}
|
|
|
|
};
|