Discussion:
fopen and file time
Add Reply
bartc
2017-05-05 20:33:00 UTC
Reply
Permalink
Raw Message
Given a handle returned by fopen(), is there a way of determining file
time from it? Either creation time or last modified time.

There seem to be dozens of different functions (variations of stat and
fstat, 64-bit versions, loads of ways of returning a result), but I
can't see anything that takes a FILE* parameter.

If there isn't anyway, then is there at least a function that takes a
filename?

I'm lost at the minute.
--
bartc
Ian Collins
2017-05-05 20:44:33 UTC
Reply
Permalink
Raw Message
Post by bartc
Given a handle returned by fopen(), is there a way of determining file
time from it? Either creation time or last modified time.
There seem to be dozens of different functions (variations of stat and
fstat, 64-bit versions, loads of ways of returning a result), but I
can't see anything that takes a FILE* parameter.
If there isn't anyway, then is there at least a function that takes a
filename?
There isn't anything platform neutral.
--
Ian
Keith Thompson
2017-05-05 20:50:52 UTC
Reply
Permalink
Raw Message
Post by Ian Collins
Post by bartc
Given a handle returned by fopen(), is there a way of determining file
time from it? Either creation time or last modified time.
There seem to be dozens of different functions (variations of stat and
fstat, 64-bit versions, loads of ways of returning a result), but I
can't see anything that takes a FILE* parameter.
If there isn't anyway, then is there at least a function that takes a
filename?
There isn't anything platform neutral.
And not all systems record creation time.

<OT>For POSIX, fileno() takes a FILE* and gives you the int file descriptor
required by fstat().</OT>
--
Keith Thompson (The_Other_Keith) kst-***@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Gordon Burditt
2017-05-06 02:55:25 UTC
Reply
Permalink
Raw Message
Post by bartc
Given a handle returned by fopen(), is there a way of determining file
time from it? Either creation time or last modified time.
There seem to be dozens of different functions (variations of stat and
fstat, 64-bit versions, loads of ways of returning a result), but I
can't see anything that takes a FILE* parameter.
In POSIX, there is fileno(stream) which returns an integer file
descriptor, which can then be passed to fstat(), which is also not
in standard C.
Post by bartc
If there isn't anyway, then is there at least a function that takes a
filename?
stat() is in POSIX but it isn't in standard C. However, you specify
the file by name.

UNIX traditionally keeps 3 file times, none of which are a creation
time. In particular, the one given by "ls -lc" is the "changed"
(not "creation") time, and it is normally the *latest* time of the
three, not the earliest (exceptions can come about if you are
directly setting the file times, and especially if you are directly
setting the time into the future). The "changed" time has a specific
use for doing incremental backups: if the "changed" time is later
than the time of the last backup, then the file needs to be backed
up, because *something* (file contents, ownership/permissions,
file times, link count, name, etc.) about the file has changed.

Creation times are not part of POSIX.

I consider a "creation time" to be pretty useless. Some systems
do not keep one. Perhaps more to the point, some systems keep a
creation time only on certain filesystem types (e.g. FreeBSD does
keep a "birth time" on UFS2 but not UFS). The biggest problem with
a creation time is that usually:

fp = fopen(filename, "w");

does not change the creation time, but

unlink(filename);
fp = fopen(filename, "w");

does, and when this applies to object files, it may result in a
meaningless difference in creation time of years or decades, but
the contents of the files are still up to date.

If you want to convince me that the creation time is useful, try
to convince me that whether or not the file was deleted before it
was completely re-written (say, by a compiler producing a new object
file or executable file) is something that is useful to track. I
see it as a difference that makes no difference.

Some text editors can also have a big but useless difference in a
"creation time". Those that rename the original file to a backup
name and create a new file for the newly-saved copies always have
a creation time that is roughly the same as the last-modified time
(the difference may be about the time required to write the file).
Those editors that just write over the file may leave files with
ancient creation times even though all the ancient data has long
since been removed from the file.

Actually, most editors can operate in either mode, depending on
user-settable options.
s***@casperkitty.com
2017-05-06 15:20:29 UTC
Reply
Permalink
Raw Message
Post by Gordon Burditt
The biggest problem with
fp = fopen(filename, "w");
does not change the creation time, but
unlink(filename);
fp = fopen(filename, "w");
does, and when this applies to object files, it may result in a
meaningless difference in creation time of years or decades, but
the contents of the files are still up to date.
If files are copied, it can be useful to have both the date when the copy
was created and the date when the content was created. If a source file
is rolled back from a backup, it makes sense to have a date stamp which
matches that of the original, but the file should be considered "newer"
than any associated object files, even those built from versions of
source which edited after the date of the backup (and have since been
abandoned.

I would also suggest that the distinction between replacing a file's content
versus unlinking and then replacing extends far beyond timestamps. If two
or more hard links exist to the same file, opening with "w" or "wb" without
first unlinking will trash the file seen by both links, while unlinking and
then writing will let the other links behave as though they hold a "copy"
of the file. While there are some cases where it could be useful to have
multiple hard links to a changeable file, I would think that a symlink would
be more appropriate in most such cases, and it would be more helpful if the
hard-link semantics treated open-for-replace as as unlink-then-write.
Scott Lurndal
2017-05-06 14:29:02 UTC
Reply
Permalink
Raw Message
Post by bartc
Given a handle returned by fopen(), is there a way of determining file
time from it? Either creation time or last modified time.
man 2 fstat.
Kenny McCormack
2017-05-06 15:10:40 UTC
Reply
Permalink
Raw Message
Post by Scott Lurndal
Post by bartc
Given a handle returned by fopen(), is there a way of determining file
time from it? Either creation time or last modified time.
man 2 fstat.
Completely missing the point.

Re-read the OP.
--
The plural of "anecdote" is _not_ "data".
Loading...