0

I have the following C code:

#include <ftw.h>

#define MAXPATHLEN 100

static time_t createtime = 0;
static char ftwpath[MAXPATHLEN];

[...]

ftw(somepath, get_writedir, 10);
if (ftwpath[0] == '\0') {
    //Code assuming that the directory does not exist.
} else {
    //Some code handeling 
}

That is the method that ftw calls:

int get_writedir(const char *path, const struct stat *sb, int typeflag)
{
    if (typeflag == FTW_D && sb->st_ctime > createtime) {
        createtime = sb->st_ctime;
        strlcpy(ftwpath, path, MAXPATHLEN);
    }
    return 0;
}

Generally speking this code works to some extend when typeflag is set to FTW_F, not FTW_D. When I do the latter, nothing happens. When I do the prior: I do not always get the "newest created directory". What am I doing wrong here?

TacoVox
  • 141
  • 10
  • Hm, this should work in general, but you have to note that the ctime is _not_ the _creation time_, as it is often misinterpreted, but the _inode change time_. For a directory, this is for example updated when a file is created or deleted there. The creation time itself is not tracked with commn filesystems – Ctx Nov 16 '17 at 12:15
  • The problem is also that if I use "/mnt/somedevice/folderinthat" as my rootpath, I sometimes get "/mnt/somedevice/folderinthat/."... – TacoVox Nov 16 '17 at 12:19
  • why is that a problem? Just ignore the entry, when you don't need to process it – Ctx Nov 16 '17 at 12:20
  • Embarrassing as it is. It turns out, that this just works fine. I made some bus myself in handeling code -.- (wasted my whole morning/noon)... – TacoVox Nov 16 '17 at 12:37
  • happens ;) Don't worry ;) – Ctx Nov 16 '17 at 12:42

0 Answers0