If you actually open a file, you update it's access time, and you interfere with the process that should actually open the file. Those both break perfectly simple business logic.
If you want to get the access time, why don't you just query the access time? Which is likely the only thing almost universally guaranteed not to change it... And I'm sure VB6 has an API for that, considering that VBA had a global API for it (FileDateTime).
(Which is, btw, yet another way to one-line check if a file exists).
The way you probably did in your now-deleted comment claiming that stat() updated atime?
Citation needed. https://learn.microsoft.com/en-us/dotnet/api/system.io.file.... doesn't warn of any such thing.
No.
> Citation needed. https://learn.microsoft.com/en-us/dotnet/api/system.io.file.... doesn't warn of any such thing.
https://learn.microsoft.com/en-us/windows/win32/api/winbase/...
OF_EXIST: Opens a file and then closes it.
OpenFile() opens the file??? c'est incroyable!
OF_EXISTS is just a side effect of some actual operation. It's some related info you you get along the way. You would not use that as the way to only check the file. Holy cow. Please do continue trying to spread the wisom.
> In fact, I imagine File.Exists()/OF_EXISTS also opens the file (then immediately closes it).
I never said any such thing.
Yet another trivially verified false statement. Fits the pattern.
If you didn't say any such thing, then why did you bother countering my comment at https://news.ycombinator.com/item?id=42107195 ? Aha! Gotcha!
I never said, either explicitly nor implicitly, that File.Exists() modifies access time.
There is nothing there in that comment nor it's parent.
OF_EXISTS is not a thing by itself. It does not "open a file and then close it" which is a ridiculous and incorrect thing you DID actually say. OF_EXISTS is just a bitmask flag that can be returned by several functions, including ones that open files.
You have said a continuos string of incorrect things and employed invalid logic this whole time, and this yet again fits.
correction: you said opens a file and closes it about OF_EXIST the bitmask flag to tell OpenFile() how you want to open it. Slightly less ridiculous, yet still not a thing that opens a file, OpenFile() opens the file.
exists followed by open is a pointless exists because anything can happen in between.
Conversely, if you are neither the producer nor consumer of the file at this time, you don't want to actually open it just as a way to stat it, because actually opening it updates it's access time and interferes with the actual consumer(s). Even read-only non-exclusive.
I thought you were saying you could use a seperate lock to guard the time between exist() and open(), which is possible but silly.
Now I'm not sure what you meant. Is "it" exists() or open()? You can use open() as a lock for other things. It doesn't need any other locks itself. You can't use exists() as a lock.
Or, I guess you are saying one process can open the file and others can merely check exists()? Yes depending on what the purpose was, what you intend to do with the results of exists(), because it isn't true any more the instant after exists() returns, only while exists() is actually running.
That makes it sound useless and therefor what is the problem that VB6 didn't have it? That might very well be why it was omitted.
But you do need some form of stat(), a way to get info about files without affecting them in any way. A process needs to be able to watch a files access time to know if it's been accessed yet, or when last, without that very observation changing the access time or making the file appear open()ed to other processes that may care about that. Even being opened read-only andbnon-exclusive is still opened. Another process may need to know that it has eclusive access or else it can't do it's thing, so it will fail it's own open() exclusive.
Even if the other process retries and open succeeds a few cycles later, changing the access time breaks the process that's waiting for the file to be accessed by it's actual consumer so it can be automatically deleted for example, or simply logged the fact that the customer has downloaded the edi file, the remote client/browser has loaded the temp file, someone somewhere has accessed super secret file, etc.
(setting aside that these days at least on *ix you frequently disable atime updates in the filesystem altogether and just do not build anything on top of that any more for performance and flash write life reasons. But I have processes that use it only in tmpfs in ram even while it's off for the real filesystem.)