2023-06-20 03:57:14 +08:00
|
|
|
/* filesystem.c
|
|
|
|
|
|
|
|
Copyright (c) 2023, Nikolaj Schlej. All rights reserved.
|
|
|
|
This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "filesystem.h"
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fstream>
|
|
|
|
|
2023-06-20 09:01:25 +08:00
|
|
|
bool readFileIntoBuffer(const UString& inPath, UByteArray& buf)
|
|
|
|
{
|
2023-06-20 03:57:14 +08:00
|
|
|
if (!isExistOnFs(inPath))
|
2023-06-20 09:01:25 +08:00
|
|
|
return false;
|
2023-06-20 03:57:14 +08:00
|
|
|
|
|
|
|
std::ifstream inputFile(inPath.toLocal8Bit(), std::ios::in | std::ios::binary);
|
|
|
|
if (!inputFile)
|
2023-06-20 09:01:25 +08:00
|
|
|
return false;
|
2023-06-20 03:57:14 +08:00
|
|
|
std::vector<char> buffer(std::istreambuf_iterator<char>(inputFile),
|
|
|
|
(std::istreambuf_iterator<char>()));
|
|
|
|
inputFile.close();
|
|
|
|
|
|
|
|
buf = buffer;
|
|
|
|
|
2023-06-20 09:01:25 +08:00
|
|
|
return true;
|
2023-06-20 03:57:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(__MINGW32__)
|
|
|
|
#include <direct.h>
|
|
|
|
#include <stdlib.h>
|
2023-06-20 09:01:25 +08:00
|
|
|
bool isExistOnFs(const UString & path)
|
|
|
|
{
|
2023-06-20 03:57:14 +08:00
|
|
|
struct _stat buf;
|
|
|
|
return (_stat(path.toLocal8Bit(), &buf) == 0);
|
|
|
|
}
|
|
|
|
|
2023-06-20 09:01:25 +08:00
|
|
|
bool makeDirectory(const UString & dir)
|
|
|
|
{
|
2023-06-20 03:57:14 +08:00
|
|
|
return (_mkdir(dir.toLocal8Bit()) == 0);
|
|
|
|
}
|
|
|
|
|
2023-06-20 09:01:25 +08:00
|
|
|
bool changeDirectory(const UString & dir)
|
|
|
|
{
|
2023-06-20 03:57:14 +08:00
|
|
|
return (_chdir(dir.toLocal8Bit()) == 0);
|
|
|
|
}
|
|
|
|
|
2023-06-20 09:01:25 +08:00
|
|
|
bool removeDirectory(const UString & dir)
|
|
|
|
{
|
2023-06-20 03:57:14 +08:00
|
|
|
int r = _rmdir(dir.toLocal8Bit());
|
2023-06-20 09:01:25 +08:00
|
|
|
// Hack: unlike *nix, Windows does not permit deleting current directories
|
2023-06-20 03:57:14 +08:00
|
|
|
if (r < 0 && errno == EACCES && changeDirectory(dir + UString("/../"))) {
|
2023-06-20 09:01:25 +08:00
|
|
|
return (_rmdir(dir.toLocal8Bit()) == 0);
|
2023-06-20 03:57:14 +08:00
|
|
|
}
|
2023-06-20 09:01:25 +08:00
|
|
|
return (r == 0);
|
2023-06-20 03:57:14 +08:00
|
|
|
}
|
|
|
|
|
2023-06-20 09:01:25 +08:00
|
|
|
UString getAbsPath(const UString & path)
|
|
|
|
{
|
|
|
|
char * abs = (char*)calloc(0x8000, 1);
|
|
|
|
UString new_path;
|
|
|
|
if (_fullpath(abs, path.toLocal8Bit(), 0x8000))
|
|
|
|
new_path = UString(abs);
|
|
|
|
else
|
|
|
|
new_path = path;
|
|
|
|
free(abs);
|
|
|
|
return new_path;
|
2023-06-20 03:57:14 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2024-02-21 02:33:36 +08:00
|
|
|
#if !defined(ACCESSPERMS)
|
|
|
|
#define ACCESSPERMS (S_IRWXU|S_IRWXG|S_IRWXO)
|
|
|
|
#endif
|
2023-06-20 09:01:25 +08:00
|
|
|
bool isExistOnFs(const UString & path)
|
|
|
|
{
|
2023-06-20 03:57:14 +08:00
|
|
|
struct stat buf;
|
|
|
|
return (stat(path.toLocal8Bit(), &buf) == 0);
|
|
|
|
}
|
|
|
|
|
2023-06-20 09:01:25 +08:00
|
|
|
bool makeDirectory(const UString & dir)
|
|
|
|
{
|
2023-06-20 03:57:14 +08:00
|
|
|
return (mkdir(dir.toLocal8Bit(), ACCESSPERMS) == 0);
|
|
|
|
}
|
|
|
|
|
2023-06-20 09:01:25 +08:00
|
|
|
bool removeDirectory(const UString & dir)
|
|
|
|
{
|
|
|
|
return (rmdir(dir.toLocal8Bit()) == 0);
|
2023-06-20 03:57:14 +08:00
|
|
|
}
|
|
|
|
|
2023-06-20 09:01:25 +08:00
|
|
|
bool changeDirectory(const UString & dir)
|
|
|
|
{
|
2023-06-20 03:57:14 +08:00
|
|
|
return (chdir(dir.toLocal8Bit()) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
UString getAbsPath(const UString & path) {
|
|
|
|
char abs[PATH_MAX] = {};
|
2023-06-20 09:01:25 +08:00
|
|
|
// Last is a non-standard extension for non-existent files
|
2023-06-20 03:57:14 +08:00
|
|
|
if (realpath(path.toLocal8Bit(), abs) || abs[0] != '\0')
|
|
|
|
return UString(abs);
|
|
|
|
return path;
|
|
|
|
}
|
2024-02-21 02:33:36 +08:00
|
|
|
#endif
|