Matthias Wandel
Matthias Wandel

Reputation: 6483

Setting windows creation date using POSIX api

I have a program (jhead) that compiles with very few tweaks for both Windows and generic Unix variants. From time to time, windows users ask if it can be modified to also set the "creation date/time" of the files, but I don't see a way to do this with the POSIX api. What I'm currently doing is:

{
    struct utimbuf mtime;
    mtime.actime = NewUnixTime;
    mtime.modtime = NewUnixTime;
    utime(FileName, &mtime);
}

Ideally, struct utimebuf would just have a creation time, but it doesn't. It strikes me it would take a lot of windows specific, non-portable code to change the creation time under Windows. Is there another POSIX way of doing this? Any suggestions?

Upvotes: 2

Views: 818

Answers (2)

Daira-Emma Hopwood
Daira-Emma Hopwood

Reputation: 2359

The Win32 API for this isn't really all that bad, as Windows APIs go: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724933%28v=vs.85%29.aspx . The trickiest thing is to work out how many seconds Windows thinks there were between 1st January 1601 and 1st January 1970; the rest is straightforward.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798686

POSIX only recognizes three different file times:

  1. atime (access time): The last time the file was read
  2. mtime (modification time): The last time the file was written
  3. ctime (attribute change time): The last time the file's metadata was modified

Any other file times that may exist in the underlying OS require OS-specific API calls in order to be modified.

And don't worry about creating non-portable code; only these times really exist under most *nix variants.

Upvotes: 1

Related Questions