2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* dir_access_unix.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "dir_access_unix.h"
|
|
|
|
|
|
|
|
#if defined(UNIX_ENABLED) || defined(LIBC_FILEIO_ENABLED)
|
|
|
|
|
|
|
|
#ifndef ANDROID_ENABLED
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "os/memory.h"
|
|
|
|
#include "print_string.h"
|
|
|
|
#include <errno.h>
|
2017-03-05 16:44:50 +01:00
|
|
|
#include <stdio.h>
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
DirAccess *DirAccessUnix::create_fs() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return memnew(DirAccessUnix);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-01-14 13:16:41 +01:00
|
|
|
Error DirAccessUnix::list_dir_begin() {
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
list_dir_end(); //close any previous dir opening!
|
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
//char real_current_dir_name[2048]; //is this enough?!
|
2014-02-10 02:10:30 +01:00
|
|
|
//getcwd(real_current_dir_name,2048);
|
|
|
|
//chdir(curent_path.utf8().get_data());
|
|
|
|
dir_stream = opendir(current_dir.utf8().get_data());
|
|
|
|
//chdir(real_current_dir_name);
|
|
|
|
if (!dir_stream)
|
2017-01-14 13:16:41 +01:00
|
|
|
return ERR_CANT_OPEN; //error!
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-01-14 13:16:41 +01:00
|
|
|
return OK;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DirAccessUnix::file_exists(String p_file) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
GLOBAL_LOCK_FUNCTION
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (p_file.is_rel_path())
|
2017-03-05 16:44:50 +01:00
|
|
|
p_file = current_dir.plus_file(p_file);
|
2016-06-30 23:23:39 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_file = fix_path(p_file);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
struct stat flags;
|
2017-03-05 16:44:50 +01:00
|
|
|
bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (success && S_ISDIR(flags.st_mode)) {
|
2017-03-05 16:44:50 +01:00
|
|
|
success = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2014-05-25 05:34:51 +02:00
|
|
|
bool DirAccessUnix::dir_exists(String p_dir) {
|
|
|
|
|
|
|
|
GLOBAL_LOCK_FUNCTION
|
|
|
|
|
|
|
|
if (p_dir.is_rel_path())
|
2017-03-05 16:44:50 +01:00
|
|
|
p_dir = get_current_dir().plus_file(p_dir);
|
2016-06-30 23:23:39 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_dir = fix_path(p_dir);
|
2014-05-25 05:34:51 +02:00
|
|
|
|
|
|
|
struct stat flags;
|
2017-03-05 16:44:50 +01:00
|
|
|
bool success = (stat(p_dir.utf8().get_data(), &flags) == 0);
|
2014-05-25 05:34:51 +02:00
|
|
|
|
|
|
|
if (success && S_ISDIR(flags.st_mode))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
uint64_t DirAccessUnix::get_modified_time(String p_file) {
|
|
|
|
|
|
|
|
if (p_file.is_rel_path())
|
2017-03-05 16:44:50 +01:00
|
|
|
p_file = current_dir.plus_file(p_file);
|
2016-06-30 23:23:39 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_file = fix_path(p_file);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
struct stat flags;
|
2017-03-05 16:44:50 +01:00
|
|
|
bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
return flags.st_mtime;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
ERR_FAIL_V(0);
|
|
|
|
};
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String DirAccessUnix::get_next() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (!dir_stream)
|
|
|
|
return "";
|
|
|
|
dirent *entry;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
entry = readdir(dir_stream);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (entry == NULL) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
list_dir_end();
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
//typedef struct stat Stat;
|
|
|
|
struct stat flags;
|
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
String fname = fix_unicode_name(entry->d_name);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String f = current_dir.plus_file(fname);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (stat(f.utf8().get_data(), &flags) == 0) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (S_ISDIR(flags.st_mode)) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_cisdir = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_cisdir = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_cisdir = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_cishidden = (fname != "." && fname != ".." && fname.begins_with("."));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return fname;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DirAccessUnix::current_is_dir() const {
|
|
|
|
|
|
|
|
return _cisdir;
|
|
|
|
}
|
|
|
|
|
2015-03-21 18:33:32 +01:00
|
|
|
bool DirAccessUnix::current_is_hidden() const {
|
|
|
|
|
|
|
|
return _cishidden;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void DirAccessUnix::list_dir_end() {
|
|
|
|
|
|
|
|
if (dir_stream)
|
|
|
|
closedir(dir_stream);
|
2017-03-05 16:44:50 +01:00
|
|
|
dir_stream = 0;
|
|
|
|
_cisdir = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int DirAccessUnix::get_drive_count() {
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
String DirAccessUnix::get_drive(int p_drive) {
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccessUnix::make_dir(String p_dir) {
|
|
|
|
|
|
|
|
GLOBAL_LOCK_FUNCTION
|
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
if (p_dir.is_rel_path())
|
2017-03-05 16:44:50 +01:00
|
|
|
p_dir = get_current_dir().plus_file(p_dir);
|
2016-06-30 23:23:39 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_dir = fix_path(p_dir);
|
2016-06-30 23:23:39 +02:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
#if 1
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool success = (mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
|
2016-06-18 16:12:08 +02:00
|
|
|
int err = errno;
|
|
|
|
|
|
|
|
#else
|
2014-02-10 02:10:30 +01:00
|
|
|
char real_current_dir_name[2048];
|
2017-03-05 16:44:50 +01:00
|
|
|
getcwd(real_current_dir_name, 2048);
|
2014-02-10 02:10:30 +01:00
|
|
|
chdir(current_dir.utf8().get_data()); //ascii since this may be unicode or wathever the host os wants
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool success = (mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
|
2014-02-10 02:10:30 +01:00
|
|
|
int err = errno;
|
|
|
|
|
|
|
|
chdir(real_current_dir_name);
|
2016-06-18 16:12:08 +02:00
|
|
|
#endif
|
2014-02-10 02:10:30 +01:00
|
|
|
if (success) {
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (err == EEXIST) {
|
|
|
|
return ERR_ALREADY_EXISTS;
|
|
|
|
};
|
|
|
|
|
|
|
|
return ERR_CANT_CREATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccessUnix::change_dir(String p_dir) {
|
|
|
|
|
|
|
|
GLOBAL_LOCK_FUNCTION
|
2017-03-05 16:44:50 +01:00
|
|
|
p_dir = fix_path(p_dir);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
char real_current_dir_name[2048];
|
2017-03-05 16:44:50 +01:00
|
|
|
getcwd(real_current_dir_name, 2048);
|
2014-02-10 02:10:30 +01:00
|
|
|
String prev_dir;
|
|
|
|
if (prev_dir.parse_utf8(real_current_dir_name))
|
2017-03-05 16:44:50 +01:00
|
|
|
prev_dir = real_current_dir_name; //no utf8, maybe latin?
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
chdir(current_dir.utf8().get_data()); //ascii since this may be unicode or wathever the host os wants
|
2017-03-05 16:44:50 +01:00
|
|
|
bool worked = (chdir(p_dir.utf8().get_data()) == 0); // we can only give this utf8
|
2016-07-02 18:32:07 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
String base = _get_root_path();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (base != "") {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
getcwd(real_current_dir_name, 2048);
|
2014-02-10 02:10:30 +01:00
|
|
|
String new_dir;
|
|
|
|
new_dir.parse_utf8(real_current_dir_name);
|
|
|
|
if (!new_dir.begins_with(base))
|
2017-03-05 16:44:50 +01:00
|
|
|
worked = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-07-02 18:32:07 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (worked) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
getcwd(real_current_dir_name, 2048);
|
2014-02-10 02:10:30 +01:00
|
|
|
if (current_dir.parse_utf8(real_current_dir_name))
|
2017-03-05 16:44:50 +01:00
|
|
|
current_dir = real_current_dir_name; //no utf8, maybe latin?
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
chdir(prev_dir.utf8().get_data());
|
2017-03-05 16:44:50 +01:00
|
|
|
return worked ? OK : ERR_INVALID_PARAMETER;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String DirAccessUnix::get_current_dir() {
|
|
|
|
|
|
|
|
String base = _get_root_path();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (base != "") {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
String bd = current_dir.replace_first(base, "");
|
2014-02-10 02:10:30 +01:00
|
|
|
if (bd.begins_with("/"))
|
2017-03-05 16:44:50 +01:00
|
|
|
return _get_root_string() + bd.substr(1, bd.length());
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
return _get_root_string() + bd;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
return current_dir;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error DirAccessUnix::rename(String p_path, String p_new_path) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-04-19 01:11:33 +02:00
|
|
|
if (p_path.is_rel_path())
|
2017-03-05 16:44:50 +01:00
|
|
|
p_path = get_current_dir().plus_file(p_path);
|
2016-06-30 23:23:39 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_path = fix_path(p_path);
|
2015-04-19 01:11:33 +02:00
|
|
|
|
|
|
|
if (p_new_path.is_rel_path())
|
2017-03-05 16:44:50 +01:00
|
|
|
p_new_path = get_current_dir().plus_file(p_new_path);
|
2016-06-30 23:23:39 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_new_path = fix_path(p_new_path);
|
2015-04-19 01:11:33 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return ::rename(p_path.utf8().get_data(), p_new_path.utf8().get_data()) == 0 ? OK : FAILED;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
Error DirAccessUnix::remove(String p_path) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-03-01 11:40:31 +01:00
|
|
|
if (p_path.is_rel_path())
|
2017-03-05 16:44:50 +01:00
|
|
|
p_path = get_current_dir().plus_file(p_path);
|
2016-06-30 23:23:39 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_path = fix_path(p_path);
|
2016-03-01 11:40:31 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
struct stat flags;
|
2017-03-05 16:44:50 +01:00
|
|
|
if ((stat(p_path.utf8().get_data(), &flags) != 0))
|
2014-02-10 02:10:30 +01:00
|
|
|
return FAILED;
|
|
|
|
|
|
|
|
if (S_ISDIR(flags.st_mode))
|
2017-03-05 16:44:50 +01:00
|
|
|
return ::rmdir(p_path.utf8().get_data()) == 0 ? OK : FAILED;
|
2014-02-10 02:10:30 +01:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
return ::unlink(p_path.utf8().get_data()) == 0 ? OK : FAILED;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t DirAccessUnix::get_space_left() {
|
|
|
|
|
|
|
|
#ifndef NO_STATVFS
|
|
|
|
struct statvfs vfs;
|
|
|
|
if (statvfs(current_dir.utf8().get_data(), &vfs) != 0) {
|
|
|
|
|
2016-06-18 16:32:45 +02:00
|
|
|
return 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return vfs.f_bfree * vfs.f_bsize;
|
|
|
|
#else
|
|
|
|
#warning THIS IS BROKEN
|
|
|
|
return 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
#endif
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
DirAccessUnix::DirAccessUnix() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
dir_stream = 0;
|
|
|
|
current_dir = ".";
|
|
|
|
_cisdir = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
/* determine drive count */
|
|
|
|
|
|
|
|
change_dir(current_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
DirAccessUnix::~DirAccessUnix() {
|
|
|
|
|
|
|
|
list_dir_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //posix_enabled
|