[comp.unix.wizards] Setting the access/modify time on a file

tom@mims-iris.waterloo.edu (Tom Haapanen) (03/12/90)

I wish to update a file (with an advisory lock), and then to close it,
setting the access and modify times to (almost) the same time.  Here
is what I'm attempting:

	FILE *fp;
	long times;

	fp = fopen("filename", "w");
	flock(fileno(fp), LOCK_EX);

	... much i/o to the file ...

	fflush(fp);
	times[1] = time(&times[0]) - (long)2;
	utime(mailfile, times);

	flock(fileno(fp), LOCK_UN);
	fclose(fp);

Now, at what point does the modify time get set?  the utime() call appears
to set the access time, but the modify time appears to be set AGAIN after
the access time.  Where SHOULD it get set?  And what should I do to make
sure that my access time > modify time?

The OS is IRIX 3.2 -- aka System V.3.  Thanks for any and all replies!

[ \tom haapanen -- university of waterloo -- tom@mims-iris.waterloo.edu    ]
[ "i say what i say, but i say it for myself and myself only" -- me        ]
[ "i don't even know what street canada is on"                -- al capone ]

decot@hpisod2.HP.COM (Dave Decot) (03/13/90)

> Here is what I'm attempting:
> 
> 	FILE *fp;
> >>>   long times;
> 
> 	fp = fopen("filename", "w");
> 	flock(fileno(fp), LOCK_EX);
> 
> 	... much i/o to the file ...
> 
> 	fflush(fp);
> >>>	times[1] = time(&times[0]) - (long)2;
> >>>	utime(mailfile, times);
> 
> 	flock(fileno(fp), LOCK_UN);
> 	fclose(fp);

The lines I marked with >>> above are parts of the problem:

    You did not declare "times" to be an array or initialize it as
    a pointer, but later use it as an array or initialized pointer.

    The argument and return value of time() are supposed to be a pointers
    to type time_t, which is not necessarily a long.

    You did not use lint before bothering 30,000 people.

Dave Decot

yohn@tumult.sgi.com (Mike Thompson) (03/15/90)

In article <1413@watserv1.waterloo.edu>, tom@mims-iris.waterloo.edu (Tom Haapanen) writes:
> I wish to update a file (with an advisory lock), and then to close it,
> setting the access and modify times to (almost) the same time.  Here
> is what I'm attempting:
> 
> 	FILE *fp;
> 	long times;
> 
> 	fp = fopen("filename", "w");
> 	flock(fileno(fp), LOCK_EX);
> 
> 	... much i/o to the file ...
> 
> 	fflush(fp);
> 	times[1] = time(&times[0]) - (long)2;
> 	utime(mailfile, times);
> 
> 	flock(fileno(fp), LOCK_UN);
> 	fclose(fp);
> 
> Now, at what point does the modify time get set?  the utime() call appears
> to set the access time, but the modify time appears to be set AGAIN after
> the access time.  Where SHOULD it get set?  And what should I do to make
> sure that my access time > modify time?
> 
> The OS is IRIX 3.2 -- aka System V.3.  Thanks for any and all replies!

This is a bug in 3.2 -- the Extent File System routine which is deallocating
preallocated file blocks on close is (mistakenly) forcing the modify time
to be set (to the close time).  It has been fixed for the upcoming release.

For now, reopen the file after the fclose, and *then* call utime and
reclose the file.  The deallocation routine will not be called (actually,
will return without doing anything) since there had been no file activity
since the open.

Michael Thompson