[PATCH 2 of 2] Reorder rename operations to minimise risk of leaving repository in unknown state

Adrian Buehlmann adrian at cadifra.com
Sat Oct 3 12:08:53 UTC 2009


On 03.10.2009 13:57, Adrian Buehlmann wrote:
> On 02.10.2009 17:44, Steve Borho wrote:
>> It should be possible to prove the efficacy of the patch, one way or the
>> other, with two interactive Python sessions on Windows.  I can, but probably
>> can't get to it for a couple of days.
> 
> os.unlink fails if the file is held open, for example
> with a python
> 
>   f = open(tempname)
> 
> If I open the temporary with my
> http://bitbucket.org/abuehl/opentest/src/tip/opentest.cpp
> 
> unlink succeeds.
> 
> So it all depends on what kind of 'open' the virus scanner is
> doing.

Bitbucket is down, so I'll paste it here:


// opentest.cpp - testprogram to hold a file open on Windows
//
// Copyright 2009 Adrian Buehlmann <adrian at cadifra.com>
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2, incorporated herein by reference.

#include <stdio.h>
#include <iostream>

#include "Windows.h"
#include <io.h>
#include "FCNTL.H"

void test(const char* name)
{
    HANDLE fh = ::CreateFileA(
      name, GENERIC_READ,
      FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
      0, OPEN_EXISTING, 0, 0
    );

    if (fh == INVALID_HANDLE_VALUE)
    {
        std::cout << "can't open " << name << std::endl;
        return;
    }

    std::cout << "opened " << name << std::endl;
    std::cout << "(hit <return> to stop)" << std::endl;

    int fd = ::_open_osfhandle ((intptr_t)fh, _O_RDONLY);
    if (fd == -1)
    {
        std::cout << "_open_osfhandle failed" << std::endl;
    }

    FILE* f = _fdopen(fd, "r");
    if (f == 0)
    {
        std::cout << "_fdopen failed" << std::endl;
    }

    getchar();
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "filename parameter missing!" << std::endl;
        std::cout << "usage: " << argv[0] << " <filename>" << std::endl;
        return 1;
    }

    test(argv[1]);
    return 0;
}



More information about the Mercurial-devel mailing list