/*** Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: FileTrust.Unix.cpp Date: 2023-1-25 Author: Reece ***/ #include #include "FS.hpp" #include "FileTrust.Unix.hpp" #include 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; } }