Re: Calling FileStream.Create after File.Move gets old file handle



C# Dev wrote:
"C# Dev" wrote:

Greetings all,

Have a question regarding File.Move / FileStreams

My steps,
1. Create file1 demo.log [FileMode.CreateNew]
2. Move file to demo1.log
3. Create file2 with same name as in #1: demo.log [FileMode.CreateNew]

Now, problem is that the filehandle in #1 and #3 are the same.
Are they expected to be?

I suspect this is the reason why file2 inherits the CreateDate from file1
which is my main problem :-/

Can anyone please enlighten me what is going on?

Cheers

Anyone?

If this can be replicated it seems like a bug to me..

I've managed to repeat this, using VB.Net. I believe it may be a
windows filesystem "feature". I cannot find a reference for it, but I
believe it's been done like this to support scenarios where an
application is about to write out an updated copy of a file, but wants
to support being able to retrieve the "old" version, if there's a
problem during the write.

Obviously, you cannot overwrite the existing file, so you have to
rename it first, and then write out the new version using the old
filename. And then windows (I believe. Might be .Net, but I suspect
windows) steps in and says "Aha! This is obviously just an updated copy
of the file that's just been moved, it should inherit some meta-data,
such as the creation date (and permissions)". I think it's done on a
per-process basis (so if one process renames a file, and another writes
the new one, it will not inherit the meta data).

Now, final two bits. First, they have the same handle because the first
free handle is obtained when you open the file, and then is freed by
the Close() call, so that when you come to open the second file, it
finds the first free handle (which happens to be the same handle
previously used). Had you reopened the moved file (for reading say),
before you had created the second file, you'd have probably had
different handle values.

Secondly, if you need the new file to have the correct creation
datetime, I think you'll need to open it twice. Create it, Close it,
Call SetCreationTime, then open it for writing.

Damien

.



Relevant Pages

  • Re: Question on os.tempnam() vulnerability
    ... When you create a new process, you have the option to inherit ... So the parent should open the ... Under Unix you can safely create a temp file with ... to do that under Windows as well. ...
    (comp.lang.python)
  • Re: PWDUMP Parser
    ... file1 the list of 200 names you want ... file2 the 3000 line list of names /hashes ... If this is under windows just download grep for windows otherwise you should ... > approximately 200 user names/hash pairs from it. ...
    (Pen-Test)
  • Problem with Windows XP and Themes
    ... I have Windows XP Profession and Office XP 2003. ... the way that the controls look. ... chosen a theme other than Windows Classic, Access will inherit the theme ... Exceptions are reports (report: An Access database object that prints ...
    (microsoft.public.access.forms)
  • Re: Problem with Windows XP and Themes
    ... > I also have WindowBlinds V4 installed and this has a Windows XP Theme ... > changes the way that the controls look. ... > my forms to inherit this look. ... Exceptions are reports (report: An Access database ...
    (microsoft.public.access.forms)
  • Re: inherit problem on ntfs
    ... This is all expected behavior in Windows where an account ... that no longer exists has abandoned residual permission grants. ... Advanced avenue to a checkbox that will set a Do not inherit. ... > This is a problem of me on both windows 2k and windows xp. ...
    (microsoft.public.windowsxp.security_admin)