浏览代码

LibGUI: Have GFileSystemModel use stat instead of lstat for the root

This allows you to set the GFileSystemModel root to a symlink to a
directory and it nicely opens that directory like you would expect.
Andreas Kling 5 年之前
父节点
当前提交
f2f0965edd
共有 2 个文件被更改,包括 10 次插入7 次删除
  1. 9 6
      Libraries/LibGUI/GFileSystemModel.cpp
  2. 1 1
      Libraries/LibGUI/GFileSystemModel.h

+ 9 - 6
Libraries/LibGUI/GFileSystemModel.cpp

@@ -49,12 +49,16 @@ GModelIndex GFileSystemModel::Node::index(const GFileSystemModel& model, int col
     ASSERT_NOT_REACHED();
 }
 
-bool GFileSystemModel::Node::fetch_data_using_lstat(const String& full_path)
+bool GFileSystemModel::Node::fetch_data(const String& full_path, bool is_root)
 {
     struct stat st;
-    int rc = lstat(full_path.characters(), &st);
+    int rc;
+    if (is_root)
+        rc = stat(full_path.characters(), &st);
+    else
+        rc = lstat(full_path.characters(), &st);
     if (rc < 0) {
-        perror("lstat");
+        perror("stat/lstat");
         return false;
     }
 
@@ -64,7 +68,6 @@ bool GFileSystemModel::Node::fetch_data_using_lstat(const String& full_path)
     gid = st.st_gid;
     inode = st.st_ino;
     mtime = st.st_mtime;
-
     return true;
 }
 
@@ -86,7 +89,7 @@ void GFileSystemModel::Node::traverse_if_needed(const GFileSystemModel& model)
         String name = di.next_path();
         String child_path = String::format("%s/%s", full_path.characters(), name.characters());
         NonnullOwnPtr<Node> child = make<Node>();
-        bool ok = child->fetch_data_using_lstat(child_path);
+        bool ok = child->fetch_data(child_path, false);
         if (!ok)
             continue;
         if (model.m_mode == DirectoriesOnly && !S_ISDIR(child->mode))
@@ -126,7 +129,7 @@ void GFileSystemModel::Node::reify_if_needed(const GFileSystemModel& model)
     traverse_if_needed(model);
     if (mode != 0)
         return;
-    fetch_data_using_lstat(full_path(model));
+    fetch_data(full_path(model), parent == nullptr);
 }
 
 String GFileSystemModel::Node::full_path(const GFileSystemModel& model) const

+ 1 - 1
Libraries/LibGUI/GFileSystemModel.h

@@ -88,7 +88,7 @@ public:
         GModelIndex index(const GFileSystemModel&, int column) const;
         void traverse_if_needed(const GFileSystemModel&);
         void reify_if_needed(const GFileSystemModel&);
-        bool fetch_data_using_lstat(const String& full_path);
+        bool fetch_data(const String& full_path, bool is_root);
     };
 
     static NonnullRefPtr<GFileSystemModel> create(const StringView& root_path = "/", Mode mode = Mode::FilesAndDirectories)