##// END OF EJS Templates
util_win32: make os_link more robust (issue 761)...
Patrick Mezard -
r5879:cacfeee3 default
parent child Browse files
Show More
@@ -1,310 +1,319 b''
1 1 # util_win32.py - utility functions that use win32 API
2 2 #
3 3 # Copyright 2005 Matt Mackall <mpm@selenic.com>
4 4 # Copyright 2006 Vadim Gelfer <vadim.gelfer@gmail.com>
5 5 #
6 6 # This software may be used and distributed according to the terms of
7 7 # the GNU General Public License, incorporated herein by reference.
8 8
9 9 # Mark Hammond's win32all package allows better functionality on
10 10 # Windows. this module overrides definitions in util.py. if not
11 11 # available, import of this module will fail, and generic code will be
12 12 # used.
13 13
14 14 import win32api
15 15
16 16 from i18n import _
17 17 import errno, os, pywintypes, win32con, win32file, win32process
18 18 import cStringIO, winerror
19 19 from win32com.shell import shell,shellcon
20 20
21 21 class WinError:
22 22 winerror_map = {
23 23 winerror.ERROR_ACCESS_DENIED: errno.EACCES,
24 24 winerror.ERROR_ACCOUNT_DISABLED: errno.EACCES,
25 25 winerror.ERROR_ACCOUNT_RESTRICTION: errno.EACCES,
26 26 winerror.ERROR_ALREADY_ASSIGNED: errno.EBUSY,
27 27 winerror.ERROR_ALREADY_EXISTS: errno.EEXIST,
28 28 winerror.ERROR_ARITHMETIC_OVERFLOW: errno.ERANGE,
29 29 winerror.ERROR_BAD_COMMAND: errno.EIO,
30 30 winerror.ERROR_BAD_DEVICE: errno.ENODEV,
31 31 winerror.ERROR_BAD_DRIVER_LEVEL: errno.ENXIO,
32 32 winerror.ERROR_BAD_EXE_FORMAT: errno.ENOEXEC,
33 33 winerror.ERROR_BAD_FORMAT: errno.ENOEXEC,
34 34 winerror.ERROR_BAD_LENGTH: errno.EINVAL,
35 35 winerror.ERROR_BAD_PATHNAME: errno.ENOENT,
36 36 winerror.ERROR_BAD_PIPE: errno.EPIPE,
37 37 winerror.ERROR_BAD_UNIT: errno.ENODEV,
38 38 winerror.ERROR_BAD_USERNAME: errno.EINVAL,
39 39 winerror.ERROR_BROKEN_PIPE: errno.EPIPE,
40 40 winerror.ERROR_BUFFER_OVERFLOW: errno.ENAMETOOLONG,
41 41 winerror.ERROR_BUSY: errno.EBUSY,
42 42 winerror.ERROR_BUSY_DRIVE: errno.EBUSY,
43 43 winerror.ERROR_CALL_NOT_IMPLEMENTED: errno.ENOSYS,
44 44 winerror.ERROR_CANNOT_MAKE: errno.EACCES,
45 45 winerror.ERROR_CANTOPEN: errno.EIO,
46 46 winerror.ERROR_CANTREAD: errno.EIO,
47 47 winerror.ERROR_CANTWRITE: errno.EIO,
48 48 winerror.ERROR_CRC: errno.EIO,
49 49 winerror.ERROR_CURRENT_DIRECTORY: errno.EACCES,
50 50 winerror.ERROR_DEVICE_IN_USE: errno.EBUSY,
51 51 winerror.ERROR_DEV_NOT_EXIST: errno.ENODEV,
52 52 winerror.ERROR_DIRECTORY: errno.EINVAL,
53 53 winerror.ERROR_DIR_NOT_EMPTY: errno.ENOTEMPTY,
54 54 winerror.ERROR_DISK_CHANGE: errno.EIO,
55 55 winerror.ERROR_DISK_FULL: errno.ENOSPC,
56 56 winerror.ERROR_DRIVE_LOCKED: errno.EBUSY,
57 57 winerror.ERROR_ENVVAR_NOT_FOUND: errno.EINVAL,
58 58 winerror.ERROR_EXE_MARKED_INVALID: errno.ENOEXEC,
59 59 winerror.ERROR_FILENAME_EXCED_RANGE: errno.ENAMETOOLONG,
60 60 winerror.ERROR_FILE_EXISTS: errno.EEXIST,
61 61 winerror.ERROR_FILE_INVALID: errno.ENODEV,
62 62 winerror.ERROR_FILE_NOT_FOUND: errno.ENOENT,
63 63 winerror.ERROR_GEN_FAILURE: errno.EIO,
64 64 winerror.ERROR_HANDLE_DISK_FULL: errno.ENOSPC,
65 65 winerror.ERROR_INSUFFICIENT_BUFFER: errno.ENOMEM,
66 66 winerror.ERROR_INVALID_ACCESS: errno.EACCES,
67 67 winerror.ERROR_INVALID_ADDRESS: errno.EFAULT,
68 68 winerror.ERROR_INVALID_BLOCK: errno.EFAULT,
69 69 winerror.ERROR_INVALID_DATA: errno.EINVAL,
70 70 winerror.ERROR_INVALID_DRIVE: errno.ENODEV,
71 71 winerror.ERROR_INVALID_EXE_SIGNATURE: errno.ENOEXEC,
72 72 winerror.ERROR_INVALID_FLAGS: errno.EINVAL,
73 73 winerror.ERROR_INVALID_FUNCTION: errno.ENOSYS,
74 74 winerror.ERROR_INVALID_HANDLE: errno.EBADF,
75 75 winerror.ERROR_INVALID_LOGON_HOURS: errno.EACCES,
76 76 winerror.ERROR_INVALID_NAME: errno.EINVAL,
77 77 winerror.ERROR_INVALID_OWNER: errno.EINVAL,
78 78 winerror.ERROR_INVALID_PARAMETER: errno.EINVAL,
79 79 winerror.ERROR_INVALID_PASSWORD: errno.EPERM,
80 80 winerror.ERROR_INVALID_PRIMARY_GROUP: errno.EINVAL,
81 81 winerror.ERROR_INVALID_SIGNAL_NUMBER: errno.EINVAL,
82 82 winerror.ERROR_INVALID_TARGET_HANDLE: errno.EIO,
83 83 winerror.ERROR_INVALID_WORKSTATION: errno.EACCES,
84 84 winerror.ERROR_IO_DEVICE: errno.EIO,
85 85 winerror.ERROR_IO_INCOMPLETE: errno.EINTR,
86 86 winerror.ERROR_LOCKED: errno.EBUSY,
87 87 winerror.ERROR_LOCK_VIOLATION: errno.EACCES,
88 88 winerror.ERROR_LOGON_FAILURE: errno.EACCES,
89 89 winerror.ERROR_MAPPED_ALIGNMENT: errno.EINVAL,
90 90 winerror.ERROR_META_EXPANSION_TOO_LONG: errno.E2BIG,
91 91 winerror.ERROR_MORE_DATA: errno.EPIPE,
92 92 winerror.ERROR_NEGATIVE_SEEK: errno.ESPIPE,
93 93 winerror.ERROR_NOACCESS: errno.EFAULT,
94 94 winerror.ERROR_NONE_MAPPED: errno.EINVAL,
95 95 winerror.ERROR_NOT_ENOUGH_MEMORY: errno.ENOMEM,
96 96 winerror.ERROR_NOT_READY: errno.EAGAIN,
97 97 winerror.ERROR_NOT_SAME_DEVICE: errno.EXDEV,
98 98 winerror.ERROR_NO_DATA: errno.EPIPE,
99 99 winerror.ERROR_NO_MORE_SEARCH_HANDLES: errno.EIO,
100 100 winerror.ERROR_NO_PROC_SLOTS: errno.EAGAIN,
101 101 winerror.ERROR_NO_SUCH_PRIVILEGE: errno.EACCES,
102 102 winerror.ERROR_OPEN_FAILED: errno.EIO,
103 103 winerror.ERROR_OPEN_FILES: errno.EBUSY,
104 104 winerror.ERROR_OPERATION_ABORTED: errno.EINTR,
105 105 winerror.ERROR_OUTOFMEMORY: errno.ENOMEM,
106 106 winerror.ERROR_PASSWORD_EXPIRED: errno.EACCES,
107 107 winerror.ERROR_PATH_BUSY: errno.EBUSY,
108 108 winerror.ERROR_PATH_NOT_FOUND: errno.ENOENT,
109 109 winerror.ERROR_PIPE_BUSY: errno.EBUSY,
110 110 winerror.ERROR_PIPE_CONNECTED: errno.EPIPE,
111 111 winerror.ERROR_PIPE_LISTENING: errno.EPIPE,
112 112 winerror.ERROR_PIPE_NOT_CONNECTED: errno.EPIPE,
113 113 winerror.ERROR_PRIVILEGE_NOT_HELD: errno.EACCES,
114 114 winerror.ERROR_READ_FAULT: errno.EIO,
115 115 winerror.ERROR_SEEK: errno.EIO,
116 116 winerror.ERROR_SEEK_ON_DEVICE: errno.ESPIPE,
117 117 winerror.ERROR_SHARING_BUFFER_EXCEEDED: errno.ENFILE,
118 118 winerror.ERROR_SHARING_VIOLATION: errno.EACCES,
119 119 winerror.ERROR_STACK_OVERFLOW: errno.ENOMEM,
120 120 winerror.ERROR_SWAPERROR: errno.ENOENT,
121 121 winerror.ERROR_TOO_MANY_MODULES: errno.EMFILE,
122 122 winerror.ERROR_TOO_MANY_OPEN_FILES: errno.EMFILE,
123 123 winerror.ERROR_UNRECOGNIZED_MEDIA: errno.ENXIO,
124 124 winerror.ERROR_UNRECOGNIZED_VOLUME: errno.ENODEV,
125 125 winerror.ERROR_WAIT_NO_CHILDREN: errno.ECHILD,
126 126 winerror.ERROR_WRITE_FAULT: errno.EIO,
127 127 winerror.ERROR_WRITE_PROTECT: errno.EROFS,
128 128 }
129 129
130 130 def __init__(self, err):
131 131 self.win_errno, self.win_function, self.win_strerror = err
132 132 if self.win_strerror.endswith('.'):
133 133 self.win_strerror = self.win_strerror[:-1]
134 134
135 135 class WinIOError(WinError, IOError):
136 136 def __init__(self, err, filename=None):
137 137 WinError.__init__(self, err)
138 138 IOError.__init__(self, self.winerror_map.get(self.win_errno, 0),
139 139 self.win_strerror)
140 140 self.filename = filename
141 141
142 142 class WinOSError(WinError, OSError):
143 143 def __init__(self, err):
144 144 WinError.__init__(self, err)
145 145 OSError.__init__(self, self.winerror_map.get(self.win_errno, 0),
146 146 self.win_strerror)
147 147
148 148 def os_link(src, dst):
149 # NB will only succeed on NTFS
150 149 try:
151 150 win32file.CreateHardLink(dst, src)
151 # CreateHardLink sometimes succeeds on mapped drives but
152 # following nlinks() returns 1. Check it now and bail out.
153 if nlinks(src) < 2:
154 try:
155 win32file.DeleteFile(dst)
156 except:
157 pass
158 # Fake hardlinking error
159 raise WinOSError((18, 'CreateHardLink', 'The system cannot '
160 'move the file to a different disk drive'))
152 161 except pywintypes.error, details:
153 162 raise WinOSError(details)
154 163
155 164 def nlinks(pathname):
156 165 """Return number of hardlinks for the given file."""
157 166 try:
158 167 fh = win32file.CreateFile(pathname,
159 168 win32file.GENERIC_READ, win32file.FILE_SHARE_READ,
160 169 None, win32file.OPEN_EXISTING, 0, None)
161 170 res = win32file.GetFileInformationByHandle(fh)
162 171 fh.Close()
163 172 return res[7]
164 173 except pywintypes.error:
165 174 return os.lstat(pathname).st_nlink
166 175
167 176 def testpid(pid):
168 177 '''return True if pid is still running or unable to
169 178 determine, False otherwise'''
170 179 try:
171 180 handle = win32api.OpenProcess(
172 181 win32con.PROCESS_QUERY_INFORMATION, False, pid)
173 182 if handle:
174 183 status = win32process.GetExitCodeProcess(handle)
175 184 return status == win32con.STILL_ACTIVE
176 185 except pywintypes.error, details:
177 186 return details[0] != winerror.ERROR_INVALID_PARAMETER
178 187 return True
179 188
180 189 def system_rcpath_win32():
181 190 '''return default os-specific hgrc search path'''
182 191 proc = win32api.GetCurrentProcess()
183 192 try:
184 193 # This will fail on windows < NT
185 194 filename = win32process.GetModuleFileNameEx(proc, 0)
186 195 except:
187 196 filename = win32api.GetModuleFileName(0)
188 197 return [os.path.join(os.path.dirname(filename), 'mercurial.ini')]
189 198
190 199 def user_rcpath_win32():
191 200 '''return os-specific hgrc search path to the user dir'''
192 201 userdir = os.path.expanduser('~')
193 202 if sys.getwindowsversion() != 2 and userdir == '~':
194 203 # We are on win < nt: fetch the APPDATA directory location and use
195 204 # the parent directory as the user home dir.
196 205 appdir = shell.SHGetPathFromIDList(
197 206 shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_APPDATA))
198 207 userdir = os.path.dirname(appdir)
199 208 return os.path.join(userdir, 'mercurial.ini')
200 209
201 210 class posixfile_nt(object):
202 211 '''file object with posix-like semantics. on windows, normal
203 212 files can not be deleted or renamed if they are open. must open
204 213 with win32file.FILE_SHARE_DELETE. this flag does not exist on
205 214 windows < nt, so do not use this class there.'''
206 215
207 216 # tried to use win32file._open_osfhandle to pass fd to os.fdopen,
208 217 # but does not work at all. wrap win32 file api instead.
209 218
210 219 def __init__(self, name, mode='rb'):
211 220 access = 0
212 221 if 'r' in mode or '+' in mode:
213 222 access |= win32file.GENERIC_READ
214 223 if 'w' in mode or 'a' in mode or '+' in mode:
215 224 access |= win32file.GENERIC_WRITE
216 225 if 'r' in mode:
217 226 creation = win32file.OPEN_EXISTING
218 227 elif 'a' in mode:
219 228 creation = win32file.OPEN_ALWAYS
220 229 else:
221 230 creation = win32file.CREATE_ALWAYS
222 231 try:
223 232 self.handle = win32file.CreateFile(name,
224 233 access,
225 234 win32file.FILE_SHARE_READ |
226 235 win32file.FILE_SHARE_WRITE |
227 236 win32file.FILE_SHARE_DELETE,
228 237 None,
229 238 creation,
230 239 win32file.FILE_ATTRIBUTE_NORMAL,
231 240 0)
232 241 except pywintypes.error, err:
233 242 raise WinIOError(err, name)
234 243 self.closed = False
235 244 self.name = name
236 245 self.mode = mode
237 246
238 247 def __iter__(self):
239 248 for line in self.read().splitlines(True):
240 249 yield line
241 250
242 251 def read(self, count=-1):
243 252 try:
244 253 cs = cStringIO.StringIO()
245 254 while count:
246 255 wincount = int(count)
247 256 if wincount == -1:
248 257 wincount = 1048576
249 258 val, data = win32file.ReadFile(self.handle, wincount)
250 259 if not data: break
251 260 cs.write(data)
252 261 if count != -1:
253 262 count -= len(data)
254 263 return cs.getvalue()
255 264 except pywintypes.error, err:
256 265 raise WinIOError(err)
257 266
258 267 def write(self, data):
259 268 try:
260 269 if 'a' in self.mode:
261 270 win32file.SetFilePointer(self.handle, 0, win32file.FILE_END)
262 271 nwrit = 0
263 272 while nwrit < len(data):
264 273 val, nwrit = win32file.WriteFile(self.handle, data)
265 274 data = data[nwrit:]
266 275 except pywintypes.error, err:
267 276 raise WinIOError(err)
268 277
269 278 def seek(self, pos, whence=0):
270 279 try:
271 280 win32file.SetFilePointer(self.handle, int(pos), whence)
272 281 except pywintypes.error, err:
273 282 raise WinIOError(err)
274 283
275 284 def tell(self):
276 285 try:
277 286 return win32file.SetFilePointer(self.handle, 0,
278 287 win32file.FILE_CURRENT)
279 288 except pywintypes.error, err:
280 289 raise WinIOError(err)
281 290
282 291 def close(self):
283 292 if not self.closed:
284 293 self.handle = None
285 294 self.closed = True
286 295
287 296 def flush(self):
288 297 try:
289 298 win32file.FlushFileBuffers(self.handle)
290 299 except pywintypes.error, err:
291 300 raise WinIOError(err)
292 301
293 302 def truncate(self, pos=0):
294 303 try:
295 304 win32file.SetFilePointer(self.handle, int(pos),
296 305 win32file.FILE_BEGIN)
297 306 win32file.SetEndOfFile(self.handle)
298 307 except pywintypes.error, err:
299 308 raise WinIOError(err)
300 309
301 310 getuser_fallback = win32api.GetUserName
302 311
303 312 def set_signal_handler_win32():
304 313 """Register a termination handler for console events including
305 314 CTRL+C. python signal handlers do not work well with socket
306 315 operations.
307 316 """
308 317 def handler(event):
309 318 win32process.ExitProcess(1)
310 319 win32api.SetConsoleCtrlHandler(handler)
General Comments 0
You need to be logged in to leave comments. Login now