[+] AuFS::TrustFile

[+] AuFS::UnblockFile
[+] AuFS::TrustFile
[*] Fixup UNIX open logic a bit

(amended. i did not mess up octals >:( )
This commit is contained in:
Reece Wilson 2023-01-25 16:47:25 +00:00
parent d4dfe22c6c
commit bf8c1eb8c7
6 changed files with 213 additions and 15 deletions

View File

@ -135,6 +135,27 @@ namespace Aurora::IO::FS
*/
AUKN_SYM bool Copy(const AuString &src, const AuString &dest);
/**
* @brief Specifies download level of trust
* @param path
* @return
*/
AUKN_SYM bool BlockFile(const AuString &path);
/**
* @brief Specifies generic level of trust
* @param path
* @return
*/
AUKN_SYM bool UnblockFile(const AuString &path);
/**
* @brief Specifies user/internal level trust of a file
* @param path
* @return
*/
AUKN_SYM bool TrustFile(const AuString &path);
/**
* @brief Normalizes an arbitrary string of in
* @param out

View File

@ -44,7 +44,7 @@ namespace Aurora::IO::FS
mode_t mode { 0775 };
struct stat s;
if (::stat(path.c_str(), &s) != -1)
if (::stat(subdir.c_str(), &s) != -1)
{
mode = s.st_mode;
}
@ -236,23 +236,25 @@ namespace Aurora::IO::FS
AUKN_SYM bool ReadFile(const AuString &path, AuByteBuffer &buffer)
{
AuMemoryViewWrite writeView;
AuUInt read;
auto file = OpenReadUnique(path, EFileAdvisoryLockLevel::eNoSafety);
SysCheckReturn(file, false);
bool bIsStupidFD =
AuStartsWith(path, "/proc/") ||
AuStartsWith(path, "/sys/") ||
AuStartsWith(path, "/dev/");
auto pFile = OpenReadUnique(path, bIsStupidFD ? EFileAdvisoryLockLevel::eNoSafety : EFileAdvisoryLockLevel::eBlockWrite);
SysCheckReturn(pFile, false);
bool isZero = buffer.readPtr == buffer.base;
auto len = file->GetLength();
bool bIsZero = buffer.readPtr == buffer.base;
auto qwLength = pFile->GetLength();
// NOTE: Linux filesystems are such a cluster fuck of unimplemented interfaces and half-assed drivers
// It's not unusual for these "files" to not support the required seek operations across NIX-like oses.
if (len == 0)
{
if (AuStartsWith(path, "/proc/") ||
AuStartsWith(path, "/sys/") ||
AuStartsWith(path, "/dev/"))
if (bIsStupidFD)
{
len = 4096 * 10;
qwLength = 4096 * 10;
}
else
{
@ -260,13 +262,14 @@ namespace Aurora::IO::FS
}
}
writeView = buffer.GetOrAllocateLinearWriteable(len);
writeView = buffer.GetOrAllocateLinearWriteable(qwLength);
if (!writeView)
{
return {};
}
if (!file->Read(Memory::MemoryViewStreamWrite { writeView, read}))
AuUInt uLength { qwLength };
if (!pFile->Read(Memory::MemoryViewStreamWrite { writeView, uLength }))
{
SysPushErrorIO();
return false;
@ -275,11 +278,11 @@ namespace Aurora::IO::FS
// NOTE: File devices love to lie
// Do not entertain an arbitrarily large page length provided by non-regular fds
buffer.writePtr += read;
buffer.writePtr += uLength;
if (isZero)
if (bIsZero)
{
AuTryDownsize(buffer, read);
AuTryDownsize(buffer, uLength);
}
return true;

View File

@ -0,0 +1,36 @@
/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FileTrust.NT.cpp
Date: 2023-1-25
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "FS.hpp"
#include "FileTrust.NT.hpp"
namespace Aurora::IO::FS
{
AUKN_SYM bool BlockFile(const AuString &path)
{
return AuFS::WriteFile(path + ":Zone.Identifier", "[ZoneTransfer]\r\nZoneId=3\r\n");
}
AUKN_SYM bool UnblockFile(const AuString &path)
{
return AuFS::WriteFile(path + ":Zone.Identifier", "[ZoneTransfer]\r\nZoneId=0\r\n");
}
AUKN_SYM bool TrustFile(const AuString &path)
{
AuString idc;
auto uri = path + ":Zone.Identifier";
if (AuFS::ReadString(uri, idc))
{
return AuFS::Remove(uri);
}
return true;
}
}

View File

@ -0,0 +1,8 @@
/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FileTrust.NT.hpp
Date: 2023-1-25
Author: Reece
***/
#pragma once

View File

@ -0,0 +1,122 @@
/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FileTrust.Unix.cpp
Date: 2023-1-25
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "FS.hpp"
#include "FileTrust.Unix.hpp"
#include <sys/xattr.h>
namespace Aurora::IO::FS
{
AUKN_SYM bool BlockFile(const AuString &path)
{
AuString path2 = path;
AuFS::NormalizePath(path2, path2);
AuString subdir;
if ((path.size() > 1) &&
((path[path.size() - 1] == '/') ||
(path[path.size() - 1] == '\\')))
{
subdir = path2.substr(0, path2.size() - 1);
}
else
{
subdir = path2;
}
GoUpToSeparator(subdir, subdir);
subdir = "file:///" + subdir;
if (::setxattr(path2.c_str(), "user.xdg.origin.url", subdir.c_str(), subdir.size(), XATTR_CREATE) == -1)
{
if (errno == EEXIST)
{
if (::setxattr(path2.c_str(), "user.xdg.origin.url", subdir.c_str(), subdir.size(), XATTR_REPLACE) == -1)
{
return false;
}
}
}
if (::setxattr(path2.c_str(), "user.xdg.referrer.url", subdir.c_str(), subdir.size(), XATTR_CREATE) == -1)
{
if (errno == EEXIST)
{
if (::setxattr(path2.c_str(), "user.xdg.referrer.url", subdir.c_str(), subdir.size(), XATTR_REPLACE) == -1)
{
return false;
}
}
}
mode_t mode { 0644 };
struct stat s;
if (::stat(path2.c_str(), &s) != -1)
{
mode = s.st_mode;
mode &= ~( 0111 );
}
if (::chmod(path2.c_str(), mode) != 0)
{
SysPushErrorIO("BlockFile chmod failed: {}", path);
}
return true;
}
AUKN_SYM bool UnblockFile(const AuString &path)
{
AuString path2 = path;
AuFS::NormalizePath(path2, path2);
::removexattr(path2.c_str(), "user.xdg.origin.url");
::removexattr(path2.c_str(), "user.xdg.referrer.url");
mode_t mode { 0644 };
struct stat s;
if (::stat(path2.c_str(), &s) != -1)
{
mode = s.st_mode;
mode &= ~(0111);
}
if (::chmod(path2.c_str(), mode) != 0)
{
SysPushErrorIO("UnblockFile chmod failed: {}", path);
}
return AuFS::FileExists(path2);
}
AUKN_SYM bool TrustFile(const AuString &path)
{
mode_t mode { 0755 };
AuString path2 = path;
AuFS::NormalizePath(path2, path2);
struct stat s;
if (::stat(path2.c_str(), &s) != -1)
{
mode = s.st_mode;
mode |= 0111;
}
if (::chmod(path2.c_str(), mode) != 0)
{
SysPushErrorIO("chmod failed: {}", path);
return false;
}
return true;
}
}

View File

@ -0,0 +1,8 @@
/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FileTrust.Unix.hpp
Date: 2023-1-25
Author: Reece
***/
#pragma once