summaryrefslogtreecommitdiffstats
path: root/drivers/unix/dir_access_unix.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/unix/dir_access_unix.cpp')
-rw-r--r--drivers/unix/dir_access_unix.cpp114
1 files changed, 98 insertions, 16 deletions
diff --git a/drivers/unix/dir_access_unix.cpp b/drivers/unix/dir_access_unix.cpp
index b85a63a44a..e7054e11a3 100644
--- a/drivers/unix/dir_access_unix.cpp
+++ b/drivers/unix/dir_access_unix.cpp
@@ -35,10 +35,17 @@
#include <sys/statvfs.h>
#endif
+#include "core/list.h"
#include "os/memory.h"
#include "print_string.h"
#include <errno.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef HAVE_MNTENT
+#include <mntent.h>
+#endif
DirAccess *DirAccessUnix::create_fs() {
@@ -176,13 +183,96 @@ void DirAccessUnix::list_dir_end() {
_cisdir = false;
}
+#if defined(HAVE_MNTENT) && defined(X11_ENABLED)
+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;
+ }
+
+ // Accept devices mounted at common locations
+ if (strncmp(mnt->mnt_dir, "/media", 6) == 0 ||
+ strncmp(mnt->mnt_dir, "/mnt", 4) == 0 ||
+ strncmp(mnt->mnt_dir, "/home", 5) == 0 ||
+ strncmp(mnt->mnt_dir, "/run/media", 10) == 0) {
+ return true;
+ }
+
+ // Ignore everything else
+ return false;
+}
+#endif
+
+static void _get_drives(List<String> *list) {
+
+#if defined(HAVE_MNTENT) && defined(X11_ENABLED)
+ // Check /etc/mtab for the list of mounted partitions
+ FILE *mtab = setmntent("/etc/mtab", "r");
+ if (mtab) {
+ struct mntent mnt;
+ char strings[4096];
+
+ while (getmntent_r(mtab, &mnt, strings, sizeof(strings))) {
+ if (mnt.mnt_dir != NULL && _filter_drive(&mnt)) {
+ // Avoid duplicates
+ if (!list->find(mnt.mnt_dir)) {
+ list->push_back(mnt.mnt_dir);
+ }
+ }
+ }
+
+ endmntent(mtab);
+ }
+#endif
+
+ // Add $HOME
+ const char *home = getenv("HOME");
+ if (home) {
+ // Only add if it's not a duplicate
+ if (!list->find(home)) {
+ list->push_back(home);
+ }
+
+ // Check $HOME/.config/gtk-3.0/bookmarks
+ 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) {
+ // Strip any unwanted edges on the strings and push_back if it's not a duplicate
+ String fpath = String(string + 7).strip_edges();
+ if (!list->find(fpath)) {
+ list->push_back(fpath);
+ }
+ }
+ }
+
+ fclose(fd);
+ }
+ }
+
+ list->sort();
+}
+
int DirAccessUnix::get_drive_count() {
- return 0;
+ List<String> list;
+ _get_drives(&list);
+
+ return list.size();
}
+
String DirAccessUnix::get_drive(int p_drive) {
- return "";
+ List<String> list;
+ _get_drives(&list);
+
+ ERR_FAIL_INDEX_V(p_drive, list.size(), "");
+
+ return list[p_drive];
}
Error DirAccessUnix::make_dir(String p_dir) {
@@ -221,37 +311,24 @@ Error DirAccessUnix::change_dir(String p_dir) {
if (prev_dir.parse_utf8(real_current_dir_name))
prev_dir = real_current_dir_name; //no utf8, maybe latin?
- //print_line("directory we are changing out of (prev_dir): " + prev_dir);
-
// try_dir is the directory we are trying to change into
String try_dir = "";
if (p_dir.is_rel_path()) {
String next_dir = current_dir + "/" + p_dir;
- //print_line("p_dir is relative: " + p_dir + " about to simplfy: " + next_dir);
next_dir = next_dir.simplify_path();
try_dir = next_dir;
} else {
try_dir = p_dir;
- //print_line("p_dir is absolute: " + p_dir);
- }
-
- // if try_dir is nothing, it is not changing directory so change it to a "." otherwise chdir will fail
- if (try_dir == "") {
- try_dir = ".";
}
- //print_line("directory we are changing in to (try_dir): " + try_dir);
-
bool worked = (chdir(try_dir.utf8().get_data()) == 0); // we can only give this utf8
if (!worked) {
- //print_line("directory does not exist");
return ERR_INVALID_PARAMETER;
}
// the directory exists, so set current_dir to try_dir
current_dir = try_dir;
chdir(prev_dir.utf8().get_data());
- //print_line("directory exists, setting current_dir to: " + current_dir);
return OK;
}
@@ -319,11 +396,16 @@ size_t DirAccessUnix::get_space_left() {
DirAccessUnix::DirAccessUnix() {
dir_stream = 0;
- current_dir = ".";
_cisdir = false;
/* determine drive count */
+ // set current directory to an absolute path of the current directory
+ char real_current_dir_name[2048];
+ getcwd(real_current_dir_name, 2048);
+ if (current_dir.parse_utf8(real_current_dir_name))
+ current_dir = real_current_dir_name;
+
change_dir(current_dir);
}