2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* dir_access_unix.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "dir_access_unix.h"
|
|
|
|
|
2022-10-03 09:43:20 +00:00
|
|
|
#if defined(UNIX_ENABLED)
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/memory.h"
|
2020-01-27 16:08:12 +00:00
|
|
|
#include "core/os/os.h"
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/string/print_string.h"
|
|
|
|
#include "core/templates/list.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include <errno.h>
|
2017-03-05 15:44:50 +00:00
|
|
|
#include <stdio.h>
|
2017-09-12 15:01:10 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-09-11 16:13:45 +00:00
|
|
|
#include <sys/statvfs.h>
|
|
|
|
|
2017-09-12 15:01:10 +00:00
|
|
|
#ifdef HAVE_MNTENT
|
|
|
|
#include <mntent.h>
|
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-14 12:16:41 +00:00
|
|
|
Error DirAccessUnix::list_dir_begin() {
|
2014-02-10 01:10:30 +00:00
|
|
|
list_dir_end(); //close any previous dir opening!
|
|
|
|
|
2017-01-14 11:26:56 +00:00
|
|
|
//char real_current_dir_name[2048]; //is this enough?!
|
2014-02-10 01:10:30 +00:00
|
|
|
//getcwd(real_current_dir_name,2048);
|
2018-09-13 01:38:39 +00:00
|
|
|
//chdir(current_path.utf8().get_data());
|
2014-02-10 01:10:30 +00:00
|
|
|
dir_stream = opendir(current_dir.utf8().get_data());
|
|
|
|
//chdir(real_current_dir_name);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!dir_stream) {
|
2017-01-14 12:16:41 +00:00
|
|
|
return ERR_CANT_OPEN; //error!
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-14 12:16:41 +00:00
|
|
|
return OK;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DirAccessUnix::file_exists(String p_file) {
|
2017-03-05 15:44:50 +00:00
|
|
|
GLOBAL_LOCK_FUNCTION
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-08-29 23:43:47 +00:00
|
|
|
if (p_file.is_relative_path()) {
|
2022-08-30 00:34:01 +00:00
|
|
|
p_file = current_dir.path_join(p_file);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-30 21:23:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p_file = fix_path(p_file);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-10-01 19:09:22 +00:00
|
|
|
struct stat flags = {};
|
2017-03-05 15:44:50 +00:00
|
|
|
bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (success && S_ISDIR(flags.st_mode)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
success = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2014-05-25 03:34:51 +00:00
|
|
|
bool DirAccessUnix::dir_exists(String p_dir) {
|
|
|
|
GLOBAL_LOCK_FUNCTION
|
|
|
|
|
2021-08-29 23:43:47 +00:00
|
|
|
if (p_dir.is_relative_path()) {
|
2022-08-30 00:34:01 +00:00
|
|
|
p_dir = get_current_dir().path_join(p_dir);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-30 21:23:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p_dir = fix_path(p_dir);
|
2014-05-25 03:34:51 +00:00
|
|
|
|
2022-10-01 19:09:22 +00:00
|
|
|
struct stat flags = {};
|
2017-03-05 15:44:50 +00:00
|
|
|
bool success = (stat(p_dir.utf8().get_data(), &flags) == 0);
|
2014-05-25 03:34:51 +00:00
|
|
|
|
2019-06-26 13:08:25 +00:00
|
|
|
return (success && S_ISDIR(flags.st_mode));
|
2014-05-25 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 12:01:03 +00:00
|
|
|
bool DirAccessUnix::is_readable(String p_dir) {
|
|
|
|
GLOBAL_LOCK_FUNCTION
|
|
|
|
|
2021-08-29 23:43:47 +00:00
|
|
|
if (p_dir.is_relative_path()) {
|
2022-08-30 00:34:01 +00:00
|
|
|
p_dir = get_current_dir().path_join(p_dir);
|
2021-03-19 12:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p_dir = fix_path(p_dir);
|
|
|
|
return (access(p_dir.utf8().get_data(), R_OK) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DirAccessUnix::is_writable(String p_dir) {
|
|
|
|
GLOBAL_LOCK_FUNCTION
|
|
|
|
|
2021-08-29 23:43:47 +00:00
|
|
|
if (p_dir.is_relative_path()) {
|
2022-08-30 00:34:01 +00:00
|
|
|
p_dir = get_current_dir().path_join(p_dir);
|
2021-03-19 12:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p_dir = fix_path(p_dir);
|
|
|
|
return (access(p_dir.utf8().get_data(), W_OK) == 0);
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
uint64_t DirAccessUnix::get_modified_time(String p_file) {
|
2021-08-29 23:43:47 +00:00
|
|
|
if (p_file.is_relative_path()) {
|
2022-08-30 00:34:01 +00:00
|
|
|
p_file = current_dir.path_join(p_file);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-30 21:23:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p_file = fix_path(p_file);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-10-01 19:09:22 +00:00
|
|
|
struct stat flags = {};
|
2017-03-05 15:44:50 +00:00
|
|
|
bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
return flags.st_mtime;
|
|
|
|
} else {
|
|
|
|
ERR_FAIL_V(0);
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
return 0;
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String DirAccessUnix::get_next() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!dir_stream) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return "";
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-07-29 06:04:22 +00:00
|
|
|
dirent *entry = readdir(dir_stream);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
if (entry == nullptr) {
|
2014-02-10 01:10:30 +00:00
|
|
|
list_dir_end();
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2019-07-21 03:48:53 +00:00
|
|
|
String fname = fix_unicode_name(entry->d_name);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-07-29 06:04:22 +00:00
|
|
|
// Look at d_type to determine if the entry is a directory, unless
|
|
|
|
// its type is unknown (the file system does not support it) or if
|
|
|
|
// the type is a link, in that case we want to resolve the link to
|
|
|
|
// known if it points to a directory. stat() will resolve the link
|
|
|
|
// for us.
|
|
|
|
if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK) {
|
2022-08-30 00:34:01 +00:00
|
|
|
String f = current_dir.path_join(fname);
|
2019-07-29 06:04:22 +00:00
|
|
|
|
2022-10-01 19:09:22 +00:00
|
|
|
struct stat flags = {};
|
2019-07-29 06:04:22 +00:00
|
|
|
if (stat(f.utf8().get_data(), &flags) == 0) {
|
|
|
|
_cisdir = S_ISDIR(flags.st_mode);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
_cisdir = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-07-29 06:04:22 +00:00
|
|
|
_cisdir = (entry->d_type == DT_DIR);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 06:06:36 +00:00
|
|
|
_cishidden = is_hidden(fname);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return fname;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DirAccessUnix::current_is_dir() const {
|
|
|
|
return _cisdir;
|
|
|
|
}
|
|
|
|
|
2015-03-21 17:33:32 +00:00
|
|
|
bool DirAccessUnix::current_is_hidden() const {
|
|
|
|
return _cishidden;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void DirAccessUnix::list_dir_end() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (dir_stream) {
|
2014-02-10 01:10:30 +00:00
|
|
|
closedir(dir_stream);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2020-04-01 23:20:12 +00:00
|
|
|
dir_stream = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
_cisdir = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 16:01:35 +00:00
|
|
|
#if defined(HAVE_MNTENT) && defined(LINUXBSD_ENABLED)
|
2017-09-12 15:01:10 +00:00
|
|
|
static bool _filter_drive(struct mntent *mnt) {
|
|
|
|
// Ignore devices that don't point to /dev
|
|
|
|
if (strncmp(mnt->mnt_fsname, "/dev", 4) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:43:17 +00:00
|
|
|
// Accept devices mounted at common locations
|
2017-09-12 15:01:10 +00:00
|
|
|
if (strncmp(mnt->mnt_dir, "/media", 6) == 0 ||
|
|
|
|
strncmp(mnt->mnt_dir, "/mnt", 4) == 0 ||
|
2017-09-14 19:43:17 +00:00
|
|
|
strncmp(mnt->mnt_dir, "/home", 5) == 0 ||
|
|
|
|
strncmp(mnt->mnt_dir, "/run/media", 10) == 0) {
|
2017-09-12 15:01:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore everything else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void _get_drives(List<String> *list) {
|
2020-01-27 16:08:12 +00:00
|
|
|
// Add root.
|
2021-12-15 05:41:00 +00:00
|
|
|
list->push_back("/");
|
|
|
|
|
2023-01-16 16:01:35 +00:00
|
|
|
#if defined(HAVE_MNTENT) && defined(LINUXBSD_ENABLED)
|
2020-01-27 16:08:12 +00:00
|
|
|
// Check /etc/mtab for the list of mounted partitions.
|
2017-09-12 15:01:10 +00:00
|
|
|
FILE *mtab = setmntent("/etc/mtab", "r");
|
|
|
|
if (mtab) {
|
|
|
|
struct mntent mnt;
|
|
|
|
char strings[4096];
|
|
|
|
|
|
|
|
while (getmntent_r(mtab, &mnt, strings, sizeof(strings))) {
|
2020-04-01 23:20:12 +00:00
|
|
|
if (mnt.mnt_dir != nullptr && _filter_drive(&mnt)) {
|
2017-09-12 15:01:10 +00:00
|
|
|
// Avoid duplicates
|
2021-04-30 18:22:39 +00:00
|
|
|
String name = String::utf8(mnt.mnt_dir);
|
|
|
|
if (!list->find(name)) {
|
|
|
|
list->push_back(name);
|
2017-09-12 15:01:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endmntent(mtab);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-01-27 16:08:12 +00:00
|
|
|
// Add $HOME.
|
2017-09-12 15:01:10 +00:00
|
|
|
const char *home = getenv("HOME");
|
|
|
|
if (home) {
|
|
|
|
// Only add if it's not a duplicate
|
2021-04-30 18:22:39 +00:00
|
|
|
String home_name = String::utf8(home);
|
|
|
|
if (!list->find(home_name)) {
|
|
|
|
list->push_back(home_name);
|
2017-09-12 15:01:10 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 16:08:12 +00:00
|
|
|
// Check GTK+3 bookmarks for both XDG locations (Documents, Downloads, etc.)
|
|
|
|
// and potential user-defined bookmarks.
|
2017-09-12 15:01:10 +00:00
|
|
|
char path[1024];
|
|
|
|
snprintf(path, 1024, "%s/.config/gtk-3.0/bookmarks", home);
|
|
|
|
FILE *fd = fopen(path, "r");
|
|
|
|
if (fd) {
|
|
|
|
char string[1024];
|
|
|
|
while (fgets(string, 1024, fd)) {
|
|
|
|
// Parse only file:// links
|
|
|
|
if (strncmp(string, "file://", 7) == 0) {
|
2020-01-27 16:08:12 +00:00
|
|
|
// Strip any unwanted edges on the strings and push_back if it's not a duplicate.
|
2021-04-30 18:22:39 +00:00
|
|
|
String fpath = String::utf8(string + 7).strip_edges().split_spaces()[0].uri_decode();
|
2017-09-12 15:01:10 +00:00
|
|
|
if (!list->find(fpath)) {
|
|
|
|
list->push_back(fpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fd);
|
|
|
|
}
|
2020-01-27 16:08:12 +00:00
|
|
|
|
|
|
|
// Add Desktop dir.
|
|
|
|
String dpath = OS::get_singleton()->get_system_dir(OS::SystemDir::SYSTEM_DIR_DESKTOP);
|
|
|
|
if (dpath.length() > 0 && !list->find(dpath)) {
|
|
|
|
list->push_back(dpath);
|
|
|
|
}
|
2017-09-12 15:01:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list->sort();
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int DirAccessUnix::get_drive_count() {
|
2017-09-12 15:01:10 +00:00
|
|
|
List<String> list;
|
|
|
|
_get_drives(&list);
|
|
|
|
|
|
|
|
return list.size();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-09-12 15:01:10 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
String DirAccessUnix::get_drive(int p_drive) {
|
2017-09-12 15:01:10 +00:00
|
|
|
List<String> list;
|
|
|
|
_get_drives(&list);
|
|
|
|
|
|
|
|
ERR_FAIL_INDEX_V(p_drive, list.size(), "");
|
|
|
|
|
|
|
|
return list[p_drive];
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 05:41:00 +00:00
|
|
|
int DirAccessUnix::get_current_drive() {
|
|
|
|
int drive = 0;
|
|
|
|
int max_length = -1;
|
|
|
|
const String path = get_current_dir().to_lower();
|
|
|
|
for (int i = 0; i < get_drive_count(); i++) {
|
|
|
|
const String d = get_drive(i).to_lower();
|
|
|
|
if (max_length < d.length() && path.begins_with(d)) {
|
|
|
|
max_length = d.length();
|
|
|
|
drive = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return drive;
|
|
|
|
}
|
|
|
|
|
2020-02-10 08:19:29 +00:00
|
|
|
bool DirAccessUnix::drives_are_shortcuts() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Error DirAccessUnix::make_dir(String p_dir) {
|
|
|
|
GLOBAL_LOCK_FUNCTION
|
|
|
|
|
2021-08-29 23:43:47 +00:00
|
|
|
if (p_dir.is_relative_path()) {
|
2022-08-30 00:34:01 +00:00
|
|
|
p_dir = get_current_dir().path_join(p_dir);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-30 21:23:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p_dir = fix_path(p_dir);
|
2016-06-30 21:23:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool success = (mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
|
2016-06-18 14:12:08 +00:00
|
|
|
int err = errno;
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (success) {
|
|
|
|
return OK;
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (err == EEXIST) {
|
|
|
|
return ERR_ALREADY_EXISTS;
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return ERR_CANT_CREATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccessUnix::change_dir(String p_dir) {
|
|
|
|
GLOBAL_LOCK_FUNCTION
|
|
|
|
|
2017-09-03 04:16:05 +00:00
|
|
|
p_dir = fix_path(p_dir);
|
2016-07-02 16:32:07 +00:00
|
|
|
|
2017-09-06 08:39:27 +00:00
|
|
|
// prev_dir is the directory we are changing out of
|
|
|
|
String prev_dir;
|
|
|
|
char real_current_dir_name[2048];
|
2020-04-01 23:20:12 +00:00
|
|
|
ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == nullptr, ERR_BUG);
|
2022-07-05 12:18:29 +00:00
|
|
|
if (prev_dir.parse_utf8(real_current_dir_name) != OK) {
|
2017-09-06 08:39:27 +00:00
|
|
|
prev_dir = real_current_dir_name; //no utf8, maybe latin?
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-09-06 08:39:27 +00:00
|
|
|
// try_dir is the directory we are trying to change into
|
|
|
|
String try_dir = "";
|
2021-08-29 23:43:47 +00:00
|
|
|
if (p_dir.is_relative_path()) {
|
2022-08-30 00:34:01 +00:00
|
|
|
String next_dir = current_dir.path_join(p_dir);
|
2017-09-03 04:16:05 +00:00
|
|
|
next_dir = next_dir.simplify_path();
|
2017-09-06 08:39:27 +00:00
|
|
|
try_dir = next_dir;
|
2017-09-03 04:16:05 +00:00
|
|
|
} else {
|
2017-09-06 08:39:27 +00:00
|
|
|
try_dir = p_dir;
|
2017-09-03 04:16:05 +00:00
|
|
|
}
|
2017-08-21 21:06:01 +00:00
|
|
|
|
2017-09-06 08:39:27 +00:00
|
|
|
bool worked = (chdir(try_dir.utf8().get_data()) == 0); // we can only give this utf8
|
2017-09-03 04:16:05 +00:00
|
|
|
if (!worked) {
|
|
|
|
return ERR_INVALID_PARAMETER;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 02:21:03 +00:00
|
|
|
String base = _get_root_path();
|
2021-12-09 09:42:46 +00:00
|
|
|
if (!base.is_empty() && !try_dir.begins_with(base)) {
|
2020-04-01 23:20:12 +00:00
|
|
|
ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == nullptr, ERR_BUG);
|
2018-11-23 02:21:03 +00:00
|
|
|
String new_dir;
|
|
|
|
new_dir.parse_utf8(real_current_dir_name);
|
|
|
|
|
|
|
|
if (!new_dir.begins_with(base)) {
|
|
|
|
try_dir = current_dir; //revert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-06 08:39:27 +00:00
|
|
|
// the directory exists, so set current_dir to try_dir
|
|
|
|
current_dir = try_dir;
|
2018-10-02 19:07:32 +00:00
|
|
|
ERR_FAIL_COND_V(chdir(prev_dir.utf8().get_data()) != 0, ERR_BUG);
|
2017-09-03 04:16:05 +00:00
|
|
|
return OK;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:08:58 +00:00
|
|
|
String DirAccessUnix::get_current_dir(bool p_include_drive) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
String base = _get_root_path();
|
2021-12-09 09:42:46 +00:00
|
|
|
if (!base.is_empty()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
String bd = current_dir.replace_first(base, "");
|
2020-05-14 14:41:43 +00:00
|
|
|
if (bd.begins_with("/")) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return _get_root_string() + bd.substr(1, bd.length());
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
return _get_root_string() + bd;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
return current_dir;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Error DirAccessUnix::rename(String p_path, String p_new_path) {
|
2021-08-29 23:43:47 +00:00
|
|
|
if (p_path.is_relative_path()) {
|
2022-08-30 00:34:01 +00:00
|
|
|
p_path = get_current_dir().path_join(p_path);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-30 21:23:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p_path = fix_path(p_path);
|
2015-04-18 23:11:33 +00:00
|
|
|
|
2021-08-29 23:43:47 +00:00
|
|
|
if (p_new_path.is_relative_path()) {
|
2022-08-30 00:34:01 +00:00
|
|
|
p_new_path = get_current_dir().path_join(p_new_path);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-30 21:23:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p_new_path = fix_path(p_new_path);
|
2015-04-18 23:11:33 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return ::rename(p_path.utf8().get_data(), p_new_path.utf8().get_data()) == 0 ? OK : FAILED;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2018-02-13 23:04:11 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Error DirAccessUnix::remove(String p_path) {
|
2021-08-29 23:43:47 +00:00
|
|
|
if (p_path.is_relative_path()) {
|
2022-08-30 00:34:01 +00:00
|
|
|
p_path = get_current_dir().path_join(p_path);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-30 21:23:39 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p_path = fix_path(p_path);
|
2016-03-01 10:40:31 +00:00
|
|
|
|
2022-10-01 19:09:22 +00:00
|
|
|
struct stat flags = {};
|
2020-05-14 14:41:43 +00:00
|
|
|
if ((stat(p_path.utf8().get_data(), &flags) != 0)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return FAILED;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (S_ISDIR(flags.st_mode)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
return ::rmdir(p_path.utf8().get_data()) == 0 ? OK : FAILED;
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
return ::unlink(p_path.utf8().get_data()) == 0 ? OK : FAILED;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 10:55:31 +00:00
|
|
|
bool DirAccessUnix::is_link(String p_file) {
|
2021-08-29 23:43:47 +00:00
|
|
|
if (p_file.is_relative_path()) {
|
2022-08-30 00:34:01 +00:00
|
|
|
p_file = get_current_dir().path_join(p_file);
|
2021-03-10 10:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p_file = fix_path(p_file);
|
|
|
|
|
2022-10-01 19:09:22 +00:00
|
|
|
struct stat flags = {};
|
2021-03-10 10:55:31 +00:00
|
|
|
if ((lstat(p_file.utf8().get_data(), &flags) != 0)) {
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_ISLNK(flags.st_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
String DirAccessUnix::read_link(String p_file) {
|
2021-08-29 23:43:47 +00:00
|
|
|
if (p_file.is_relative_path()) {
|
2022-08-30 00:34:01 +00:00
|
|
|
p_file = get_current_dir().path_join(p_file);
|
2021-03-10 10:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p_file = fix_path(p_file);
|
|
|
|
|
|
|
|
char buf[256];
|
|
|
|
memset(buf, 0, 256);
|
|
|
|
ssize_t len = readlink(p_file.utf8().get_data(), buf, sizeof(buf));
|
|
|
|
String link;
|
|
|
|
if (len > 0) {
|
|
|
|
link.parse_utf8(buf, len);
|
|
|
|
}
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccessUnix::create_link(String p_source, String p_target) {
|
2021-08-29 23:43:47 +00:00
|
|
|
if (p_target.is_relative_path()) {
|
2022-08-30 00:34:01 +00:00
|
|
|
p_target = get_current_dir().path_join(p_target);
|
2021-03-10 10:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p_source = fix_path(p_source);
|
|
|
|
p_target = fix_path(p_target);
|
|
|
|
|
|
|
|
if (symlink(p_source.utf8().get_data(), p_target.utf8().get_data()) == 0) {
|
|
|
|
return OK;
|
|
|
|
} else {
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 17:51:13 +00:00
|
|
|
uint64_t DirAccessUnix::get_space_left() {
|
2014-02-10 01:10:30 +00:00
|
|
|
struct statvfs vfs;
|
|
|
|
if (statvfs(current_dir.utf8().get_data(), &vfs) != 0) {
|
2016-06-18 14:32:45 +00:00
|
|
|
return 0;
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-05-31 09:57:18 +00:00
|
|
|
return (uint64_t)vfs.f_bavail * (uint64_t)vfs.f_frsize;
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-01-21 18:23:08 +00:00
|
|
|
String DirAccessUnix::get_filesystem_type() const {
|
|
|
|
return ""; //TODO this should be implemented
|
|
|
|
}
|
|
|
|
|
2020-09-28 06:06:36 +00:00
|
|
|
bool DirAccessUnix::is_hidden(const String &p_name) {
|
|
|
|
return p_name != "." && p_name != ".." && p_name.begins_with(".");
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
DirAccessUnix::DirAccessUnix() {
|
2020-04-01 23:20:12 +00:00
|
|
|
dir_stream = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
_cisdir = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
/* determine drive count */
|
|
|
|
|
2017-09-10 09:02:27 +00:00
|
|
|
// set current directory to an absolute path of the current directory
|
|
|
|
char real_current_dir_name[2048];
|
2020-04-01 23:20:12 +00:00
|
|
|
ERR_FAIL_COND(getcwd(real_current_dir_name, 2048) == nullptr);
|
2022-07-05 12:18:29 +00:00
|
|
|
if (current_dir.parse_utf8(real_current_dir_name) != OK) {
|
2017-09-10 09:02:27 +00:00
|
|
|
current_dir = real_current_dir_name;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-09-10 09:02:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
change_dir(current_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
DirAccessUnix::~DirAccessUnix() {
|
|
|
|
list_dir_end();
|
|
|
|
}
|
|
|
|
|
2022-10-03 09:43:20 +00:00
|
|
|
#endif // UNIX_ENABLED
|