【问题标题】:What determines the order directory entries are returned by getdents?什么决定了 getdents 返回的订单目录条目?
【发布时间】:2012-02-10 14:25:51
【问题描述】:

背景是我有一个列出目录条目的现有应用程序; strace 显示它只是调用 getdents 并按返回的顺序列出它们。我希望它们以与不带参数的 ls 调用相同的顺序显示。是否可以通过某种方式更新目录数据来实现这一点?

FS 是 ext4,如果这有什么不同的话。

谢谢

【问题讨论】:

  • 它是 ext4 的事实肯定会有所作为。对于 vfat,您可以将目录移动到另一个文件系统,然后以正确的顺序将文件复制回来。我一直在为便携式 mp3 播放器做这件事。顺便说一句,shell glob 也类似于ls。所以 - cp -r music /media/my_player 会给我错误的顺序,但 mkdir /media/my_player/music; cp -v music/* /media/my_player/music - 正确复制文件!但是这个技巧对 ext4 没有帮助! (也许它有哈希表目录或其他东西......)
  • 好吧,我在这里找到了相同的结论:superuser.com/a/373621/269542 - 所以你只能排序。如果您有上述应用程序的来源,您可以在那里添加排序,而不是使用 LD_PRELOAD 进行破解。

标签: linux system-calls


【解决方案1】:

不,您无法操纵文件系统元数据以使getdents(2) 返回目录整体的顺序与ls(1) 应用于目录整体的排序顺序相同。

您始终可以修改您的程序以使用ls(1) 提供的相同算法对条目进行排序,尽管这需要至少 O(N) 内存和 O(N Log N) 时间来对具有 N 个条目的目录进行排序。您必须决定是否值得执行、内存和时间,以与 ls(1) 相同的方式进行排序。

【讨论】:

    【解决方案2】:

    如果你真的决心改变这个程序的行为(我假设你没有可用的源代码),你可以使用LD_PRELOAD 来挂钩对@987654324 的调用@ 和 readdir 并将其替换为您自己的排序包装器。下面是这样一个钩子的示例:

    #define _GNU_SOURCE 1
    #include <stdio.h>
    #include <dirent.h>
    #include <dlfcn.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct __dirstream
    {
      int __fd;
      char *__data;
      size_t __allocation;
      size_t __offset;
      size_t __size;
      struct dirent __entry;
    };
    
    typedef struct _dirent_list {
      struct dirent *value;
      struct _dirent_list *next;
    } dirent_list;
    
    typedef struct _my_DIR {
      struct __dirstream orig;
      dirent_list *first_entry;
      int first_readdir;
    } my_DIR;
    
    DIR *opendir(const char *name) {
      DIR *(*orig_opendir)(const char*) = dlsym(RTLD_NEXT, "opendir");
      DIR *dir = orig_opendir(name);
    
      // save additional information along with the
      // original DIR structure
      my_DIR *my_dir = calloc(1, sizeof(*my_dir));
      my_dir->first_readdir = 1;
      memcpy(my_dir, dir, sizeof(*dir));
      return (DIR*)my_dir;
    }
    
    struct dirent *readdir(DIR *dir) {
      struct dirent *(*orig_readdir)(DIR*) = dlsym(RTLD_NEXT, "readdir");
      my_DIR *my_dir = (my_DIR*)dir;
      dirent_list *item;
    
      if (my_dir->first_readdir) {
        struct dirent *entry;
        while ((entry = orig_readdir(dir))) {
          // exercise for the reader:
          // implement insertion sort here 
          item = calloc(1, sizeof(*item));
          item->value = entry;
          item->next = my_dir->first_entry;
          my_dir->first_entry = item;
        }
        my_dir->first_readdir = 0;
      }
    
      if (!my_dir->first_entry)
        return NULL;
    
      item = my_dir->first_entry;
      struct dirent *result = item->value;
      my_dir->first_entry = item->next;
      free(item);
    
      return result;
    }
    

    它覆盖opendir 和readdir 以相反的顺序返回条目(您也可以调整它以进行排序)。这就是你如何将它与一个程序 test 一起使用,它只是按照接收顺序列出目录条目:

    $ gcc -Wall -shared -fPIC -o libhookdir.so hookdir.c -ldl
    $ ./test
    ..
    test
    .
    hookdir.c
    libhookdir.so
    test.c
    $ LD_PRELOAD=./libhookdir.so ./test
    test.c
    libhookdir.so
    hookdir.c
    .
    test
    ..
    

    哈!这行得通。我们只是挂钩了一个 libc 函数。

    【讨论】:

    • 感谢您的建议 - 我刚刚使用此处的代码实现了同样的目标:LD_PRELOAD library to speed directory traversal。看起来这将是目前的解决方案,尽管我仍然对原始 getdents 使用的算法感到好奇。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 2021-07-19
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 2013-11-09
    相关资源
    最近更新 更多