代码之家  ›  专栏  ›  技术社区  ›  Kagemand Andersen

递归遍历目录时C中的gmtime不一致

  •  0
  • Kagemand Andersen  · 技术社区  · 6 年前

    我有一个C程序,我使用递归函数遍历目录结构。它需要一个 time_t

    递归dir函数来自 How to recursively list directories in C on Linux?

    void traversedir(time_t cutoff, const char* name, int indent)
    {
        if (indent > 9)
            return;
    
        DIR* dir;
        struct dirent* entry;
    
        if (!(dir = opendir(name)))
            return;
    
        struct tm *t = gmtime(&cutoff);
        printf("%d-%d-%d %d:%d:%d (%lld)\n", (1900 + t->tm_year), (t->tm_mon + 1), t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, (long long) cutoff);
        sleep(1);  // only for debugging
    
        while ((entry = readdir(dir)) != NULL) {
            if (entry->d_type == DT_DIR) {
                char path[1024];
                if (entry->d_name[0] == '.' && !isValidNumber(entry->d_name))
                    continue;
                snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
    
                ...
    
                traversedir(cutoff, path, indent + 2);
            }
        }
        closedir(dir);
    }
    
    void imageCleanup()
    {
        time_t cutoff = (time(NULL) - (86400 * 10));
        do {
            printf("Cutoff: %lld\n", (long long) cutoff);
            traversedir(cutoff, "/path/to/dir", 0);
            sleep(2);
            cutoff += 3600;
        } while (!available_storage() && cutoff < time(NULL));
    }
    

    Cutoff: 1534245930
    2018-8-14 11:25:30 (1534245930)
    2018-8-24 11:25:31 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-24 11:25:33 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-14 11:25:35 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-24 11:25:38 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-24 11:25:43 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-24 11:25:52 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-24 11:25:54 (1534245930)
    2018-8-14 11:25:55 (1534245930)
    2018-8-24 11:25:56 (1534245930)
    2018-8-14 11:25:30 (1534245930)
    2018-8-24 11:25:58 (1534245930)
    

    时间 从中转换结构的位置在括号中。它是始终如一的 1534245930 或2018年8月14日上午11:25:30,即程序执行时的负十天。

    你可以看到转换非常不一致。执行时,秒数会跟随实际时间几次。日子也变了几次。

    有人知道或知道是什么导致的吗?我试着做一个简单的循环来转换一个静态的 时间

    1 回复  |  直到 6 年前
        1
  •  2
  •   Basile Starynkevitch    6 年前

    仔细阅读 time(7) gmtime(3) .

    请注意 gmtime 不是 reentrant 因为它返回一些静态数据的地址。你想要的 gmtime_r (自从你 traversedir struct tm automatic variable .

    struct tm mytm;
    struct tm *t = gmtime_r(&cutoff, &mytm);
    

    那么 t 会指向 mytm call stack

    strftime(3) (同样,有足够大的缓冲区声明为自动变量,或一些堆分配的缓冲区)。

    nftw(3) stat(2) .

    How to debug small programs .