Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 0.8, 0.8.1, and 0.8.2

shellext: allow other processes to rename files we have open

- introduce new function fopenReadRenameAllowed in TortoiseUtils.cpp
- use it to open .hg/dirstate and .hg/thgstatus

Changeset 9f8691c956e3

Parent b32528945028

by Adrian Buehlmann

Changes to 4 files · Browse files at 9f8691c956e3 Showing diff from parent b32528945028 Diff from another changeset...

 
18
19
20
 
21
22
23
 
61
62
63
64
 
65
66
67
 
18
19
20
21
22
23
24
 
62
63
64
 
65
66
67
68
@@ -18,6 +18,7 @@
   #include "DirectoryStatus.h"  #include "Thgstatus.h" +#include "TortoiseUtils.h"      char DirectoryStatus::status(const std::string& relpath_) const @@ -61,7 +62,7 @@
    std::string p = hgroot + "\\.hg\\thgstatus";   - FILE *f = fopen(p.c_str(), "rb"); + FILE *f = fopenReadRenameAllowed(p.c_str());   if (!f)   {   TDEBUG_TRACE("DirectoryStatus::read: can't open '" << p << "'");
 
4
5
6
 
 
 
 
7
8
9
 
242
243
244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
5
6
7
8
9
10
11
12
13
 
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
@@ -4,6 +4,10 @@
 #include "errno.h"  #include <assert.h>   +#include <io.h> +#include "FCNTL.H" + +  int WideCharToLocal(LPTSTR pLocal, LPWSTR pWide, DWORD dwChars)  {   *pLocal = 0; @@ -242,3 +246,36 @@
  return !GetHgRepoRoot(path).empty();  }   + +// open a file for reading, allowing renames and deletes by other +// processes while we have it open +FILE* fopenReadRenameAllowed(const char* path) +{ + HANDLE fh = ::CreateFileA( + path, GENERIC_READ, + FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, + 0, OPEN_EXISTING, 0, 0 + ); + + if (fh == INVALID_HANDLE_VALUE) + return 0; + + // get C runtime file descriptor from file handle + int fd = ::_open_osfhandle((intptr_t)fh, _O_RDONLY); + if (fd == -1) + { + TDEBUG_TRACE("fopenReadRenameAllowed: _open_osfhandle failed"); + return 0; + } + + // get C runtime FILE from file descriptor + FILE* f = ::_fdopen(fd, "r"); + if (f == 0) + { + TDEBUG_TRACE("fopenReadRenameAllowed: _fdopen failed"); + return 0; + } + + return f; +} +
 
38
39
40
 
41
42
 
38
39
40
41
42
43
@@ -38,5 +38,6 @@
 std::string GetHgRepoRoot(const std::string& path);  bool IsHgRepo(const std::string& path);  int GetRegistryConfig(const std::string& name, std::string& res); +FILE* fopenReadRenameAllowed(const char* path);    #endif
 
18
19
20
 
21
22
23
24
25
26
27
 
28
29
30
 
18
19
20
21
22
23
24
25
26
27
 
28
29
30
31
@@ -18,13 +18,14 @@
 #include "stdafx.h"    #include "dirstate.h" +#include "TortoiseUtils.h"      std::auto_ptr<Dirstate> Dirstate::read(const std::string& path, bool& unset)  {   unset = false;   - FILE *f = fopen(path.c_str(), "rb"); + FILE *f = fopenReadRenameAllowed(path.c_str());   if (!f)   {   TDEBUG_TRACE("Dirstate::read: can't open " << path);