higan/nall/inode.hpp
Tim Allen 46656e3b4c Update to v106r98 release.
byuu says:

Another thirteen hours of work put in ... things are starting to shape
up.

I fixed the issue with relasing all the shared pointers properly, and
higan always exits cleanly now. I even went back to Application::quit()
instead of kill/exit().

The templates directory is now "fixed" to ~/.local/share/higan, which
holds all of the system folders. The configuration directory is now
fixed to ~/.config/higan, which holds the template and data paths, and
eventually program settings as well.

I removed as much of the old Emulator::Interface and Emulator::Platform
cruft as possible. I had to disable all but Gamepads for now, which is
fine since they were the only inputs that actually worekd anyway. It's
going to require a lot of reworking to get all the peripherals up and
running again ... sigh.

I killed off the properties/options support, and with it,
nall/settings.hpp, in favor of just using Nodes to represent things.

My trickery with trying to get us to have Node, Node::Input,
Node::Input::Button, etc was backfiring. I had to make Node::Input a
member of a Node struct, which inherited from
shared_pointer<Core::Node>, and so forth. This was leading to all kinds
of overloads and using statements necessary, and eventually things just
weren't converting properly to the derived types, so I scrapped it all
and now every Node::Object is just a shared_pointer<Core::Object> and
nothing more. Not crazy about this, but ... oh well. Ten-page compiler
errors on typos was clearly a sign I was pushing things.

<Platform::attach> and detach work properly now, which kills off the
GUI's Emulator::load,save hacks. You can attach and detach controllers
and have all the state saved properly now.

I sped up input polling by creating an intermediate shared_pointer
object that holds decoded group/input IDs. Probably doesn't matter much
in practice, but why not?

I have an initial fix for the annoying ipl.rom error ... it'll try to
import missing files from the templates folder for a given system. Won't
work for the GBA BIOS, but it's a start.

The next major thing I need to work on is SNES cartridge loading ... I
need it to just load for now, and do the mapping stuff at power-on.

I guess I'm going to get this as far as I can by the end of this
weekend, and then I'm going back to NGPC.
2019-02-05 20:58:57 +11:00

164 lines
5.1 KiB
C++

#pragma once
//generic abstraction layer for common storage operations against both files and directories
//these functions are not recursive; use directory::create() and directory::remove() for recursion
#include <nall/platform.hpp>
#include <nall/string.hpp>
namespace nall {
struct inode {
enum class time : uint { create, modify, access };
inode() = delete;
inode(const inode&) = delete;
auto operator=(const inode&) -> inode& = delete;
static auto exists(const string& name) -> bool {
return access(name, F_OK) == 0;
}
static auto readable(const string& name) -> bool {
return access(name, R_OK) == 0;
}
static auto writable(const string& name) -> bool {
return access(name, W_OK) == 0;
}
static auto executable(const string& name) -> bool {
return access(name, X_OK) == 0;
}
static auto hidden(const string& name) -> bool {
#if defined(PLATFORM_WINDOWS)
auto attributes = GetFileAttributes(utf16_t(name));
return attributes & FILE_ATTRIBUTE_HIDDEN;
#else
//todo: is this really the best way to do this? stat doesn't have S_ISHIDDEN ...
return name.split("/").last().beginsWith(".");
#endif
}
static auto mode(const string& name) -> uint {
struct stat data{};
stat(name, &data);
return data.st_mode;
}
static auto uid(const string& name) -> uint {
struct stat data{};
stat(name, &data);
return data.st_uid;
}
static auto gid(const string& name) -> uint {
struct stat data{};
stat(name, &data);
return data.st_gid;
}
static auto owner(const string& name) -> string {
#if !defined(PLATFORM_WINDOWS)
struct passwd* pw = getpwuid(uid(name));
if(pw && pw->pw_name) return pw->pw_name;
#endif
return {};
}
static auto group(const string& name) -> string {
#if !defined(PLATFORM_WINDOWS)
struct group* gr = getgrgid(gid(name));
if(gr && gr->gr_name) return gr->gr_name;
#endif
return {};
}
static auto timestamp(const string& name, time mode = time::modify) -> uint64_t {
struct stat data{};
stat(name, &data);
switch(mode) {
#if defined(PLATFORM_WINDOWS)
//on Windows, the last status change time (ctime) holds the file creation time instead
case time::create: return data.st_ctime;
#elif defined(PLATFORM_BSD) || defined(PLATFORM_MACOS)
//st_birthtime may return -1 or st_atime if it is not supported by the file system
//the best that can be done in this case is to return st_mtime if it's older
case time::create: return min((uint)data.st_birthtime, (uint)data.st_mtime);
#else
//Linux simply doesn't support file creation time at all
//this is also our fallback case for unsupported operating systems
case time::create: return data.st_mtime;
#endif
case time::modify: return data.st_mtime;
//for performance reasons, last access time is usually not enabled on various filesystems
//ensure that the last access time is not older than the last modify time (eg for NTFS)
case time::access: return max((uint)data.st_atime, data.st_mtime);
}
return 0;
}
static auto setMode(const string& name, uint mode) -> bool {
#if !defined(PLATFORM_WINDOWS)
return chmod(name, mode) == 0;
#else
return _wchmod(utf16_t(name), (mode & 0400 ? _S_IREAD : 0) | (mode & 0200 ? _S_IWRITE : 0)) == 0;
#endif
}
static auto setOwner(const string& name, const string& owner) -> bool {
#if !defined(PLATFORM_WINDOWS)
struct passwd* pwd = getpwnam(owner);
if(!pwd) return false;
return chown(name, pwd->pw_uid, inode::gid(name)) == 0;
#else
return true;
#endif
}
static auto setGroup(const string& name, const string& group) -> bool {
#if !defined(PLATFORM_WINDOWS)
struct group* grp = getgrnam(group);
if(!grp) return false;
return chown(name, inode::uid(name), grp->gr_gid) == 0;
#else
return true;
#endif
}
static auto setTimestamp(const string& name, uint64_t value, time mode = time::modify) -> bool {
struct utimbuf timeBuffer;
timeBuffer.modtime = mode == time::modify ? value : inode::timestamp(name, time::modify);
timeBuffer.actime = mode == time::access ? value : inode::timestamp(name, time::access);
return utime(name, &timeBuffer) == 0;
}
//returns true if 'name' already exists
static auto create(const string& name, uint permissions = 0755) -> bool {
if(exists(name)) return true;
if(name.endsWith("/")) return mkdir(name, permissions) == 0;
int fd = open(name, O_CREAT | O_EXCL, permissions);
if(fd < 0) return false;
return close(fd), true;
}
//returns false if 'name' and 'targetname' are on different file systems (requires copy)
static auto rename(const string& name, const string& targetname) -> bool {
return ::rename(name, targetname) == 0;
}
//returns false if 'name' is a directory that is not empty
static auto remove(const string& name) -> bool {
#if defined(PLATFORM_WINDOWS)
if(name.endsWith("/")) return _wrmdir(utf16_t(name)) == 0;
return _wunlink(utf16_t(name)) == 0;
#else
if(name.endsWith("/")) return rmdir(name) == 0;
return unlink(name) == 0;
#endif
}
};
}