问题

安装ubuntu 24.04以后,文件管理器启动特别缓慢,经常需要8秒才能完成加载,简直无法使用。

调试

按理说,ubuntu发布的时候,肯定经过测试的,这么低级的问题,不可能没注意到的,一定是什么用法导致的,联想到ubuntu最近一些年一直在去root化,root运行的问题越来越多了。切换回普通账号,文件管理器明显正常了,证实的确是root账号登陆引起的。

要找到缓慢的原因才好修改,祭出strace,可以看到nautilus执行到这里sleep了7秒

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/charset.alias", O_RDONLY) = -1 ENOENT (No such file or directory)
write(2, "\n** (org.gnome.Nautilus:2791): \33"..., 128
** (org.gnome.Nautilus:2791): WARNING **: 10:05:23.254: 
==================================================) = 128
write(2, "======\nThis app cannot work corr"..., 128======
This app cannot work correctly if run as root (not even
with sudo). Consider running `nautilus admin:/` instead.
========) = 128
write(2, "================================"..., 49================================================
) = 49
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=7, tv_nsec=0},

原来是推荐安装admin插件,然后执行类似nautilus admin:/的命令,但它就不能自动转成admin方式,非要等7秒?

git clone https://gitlab.gnome.org/GNOME/nautilus下载nautilus的源码,在nautilus-main.c里面找到了如下的代码,确实是root执行sleep 7秒

1
2
3
4
5
6
7
8
if (getuid () == 0)
    {
        g_warning (_("\n========================================================"
                     "\nThis app cannot work correctly if run as root (not even"
                     "\nwith sudo). Consider running `nautilus admin:/` instead."
                     "\n========================================================"));
        sleep (7);
    }

翻看提交历史记录,改动并不多,可谓简单粗暴

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
commit 5d82b80ea3eddb6e90bcaf57ae0bf92926c610d9
Author: António Fernandes <antoniof@gnome.org>
Date:   Mon Dec 11 21:48:26 2023 +0000

    main: Recommend admin backend if run as root
    
    GUI apps shouldn't run as root, neither directly nor through sudo.
    Explaining why that's a very bad idea for the 1000th time is out of
    scope for this commit message.
    
    Introduce a small nudge to guide people to use the admin backend.
    
    In order to have it localized, initialize the locale here. Also
    include <locale.h> unconditionally. (We already do so elsewhere.)

diff --git a/src/nautilus-main.c b/src/nautilus-main.c
index 586002987..03935c524 100644
--- a/src/nautilus-main.c
+++ b/src/nautilus-main.c
@@ -34,9 +34,7 @@
 #include <gtk/gtk.h>
 #include <gio/gdesktopappinfo.h>
 
-#ifdef HAVE_LOCALE_H
 #include <locale.h>
-#endif
 #ifdef HAVE_MALLOC_H
 #include <malloc.h>
 #endif
@@ -50,14 +48,23 @@ main (int   argc,
 {
     gint retval;
     NautilusApplication *application;
-
     /* Initialize gettext support */
+    setlocale (LC_ALL, "");
     bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
     bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
     textdomain (GETTEXT_PACKAGE);
 
     g_set_prgname (APPLICATION_ID);
 
+    if (getuid () == 0)
+    {
+        g_warning (_("\n========================================================"
+                     "\nThis app cannot work correctly if run as root (not even"
+                     "\nwith sudo). Consider running `nautilus admin:/` instead."
+                     "\n========================================================"));
+        sleep (7);
+    }
+
     nautilus_register_resource ();
     /* Run the nautilus application. */
     application = nautilus_application_new ();

修复

知道了原因,那么就好修复了,罪魁祸首就是那个sleep 7秒,删掉重编nautilus就行。在ubuntu下

1
2
3
4
5
6
apt source nautilus
apt build-dep nautilus
cd nautilus
注释掉nautilus-main.c里面的sleep 7秒
dpkg-buildpackage
dpkg -i nautilus包

小插曲:ubuntu 24.04.2目前很多包的依赖关系有问题,编译环境配置困难,24.04.1则完全没有问题。它们默认带的nautilus版本是一样的,可以在24.04.1下面编译,然后装到24.04.2上,完美解决。

附上已经编好的包 ,装上即可使用。链接: https://pan.baidu.com/s/129O7kAZGn6MwwLar4_NpRg?pwd=x96f 提取码: x96f