Mercurial > public > mercurial-scm > hg
comparison mercurial/posix.py @ 31537:c6cbe5720353
posix: use local reference to unlink
We have a local reference to os.unlink in module scope, but we still used
os.unlink inside functions. This changes util to use the local reference,
which will pave the way for combining duplicated code in future patches.
author | Ryan McElroy <rmcelroy@fb.com> |
---|---|
date | Tue, 21 Mar 2017 06:50:28 -0700 |
parents | 53575feed7c0 |
children | e6d4cc29fd60 |
comparison
equal
deleted
inserted
replaced
31536:48b9c9ca09b7 | 31537:c6cbe5720353 |
---|---|
103 if not stat.S_ISLNK(s): | 103 if not stat.S_ISLNK(s): |
104 # switch file to link | 104 # switch file to link |
105 fp = open(f) | 105 fp = open(f) |
106 data = fp.read() | 106 data = fp.read() |
107 fp.close() | 107 fp.close() |
108 os.unlink(f) | 108 unlink(f) |
109 try: | 109 try: |
110 os.symlink(data, f) | 110 os.symlink(data, f) |
111 except OSError: | 111 except OSError: |
112 # failed to make a link, rewrite file | 112 # failed to make a link, rewrite file |
113 fp = open(f, "w") | 113 fp = open(f, "w") |
116 # no chmod needed at this point | 116 # no chmod needed at this point |
117 return | 117 return |
118 if stat.S_ISLNK(s): | 118 if stat.S_ISLNK(s): |
119 # switch link to file | 119 # switch link to file |
120 data = os.readlink(f) | 120 data = os.readlink(f) |
121 os.unlink(f) | 121 unlink(f) |
122 fp = open(f, "w") | 122 fp = open(f, "w") |
123 fp.write(data) | 123 fp.write(data) |
124 fp.close() | 124 fp.close() |
125 s = 0o666 & ~umask # avoid restatting for chmod | 125 s = 0o666 & ~umask # avoid restatting for chmod |
126 | 126 |
185 m = os.stat(checknoexec).st_mode | 185 m = os.stat(checknoexec).st_mode |
186 if m & EXECFLAGS == 0: | 186 if m & EXECFLAGS == 0: |
187 # check-exec is exec and check-no-exec is not exec | 187 # check-exec is exec and check-no-exec is not exec |
188 return True | 188 return True |
189 # checknoexec exists but is exec - delete it | 189 # checknoexec exists but is exec - delete it |
190 os.unlink(checknoexec) | 190 unlink(checknoexec) |
191 # checkisexec exists but is not exec - delete it | 191 # checkisexec exists but is not exec - delete it |
192 os.unlink(checkisexec) | 192 unlink(checkisexec) |
193 | 193 |
194 # check using one file, leave it as checkisexec | 194 # check using one file, leave it as checkisexec |
195 checkdir = cachedir | 195 checkdir = cachedir |
196 else: | 196 else: |
197 # check directly in path and don't leave checkisexec behind | 197 # check directly in path and don't leave checkisexec behind |
208 os.rename(fn, checkisexec) | 208 os.rename(fn, checkisexec) |
209 fn = None | 209 fn = None |
210 return True | 210 return True |
211 finally: | 211 finally: |
212 if fn is not None: | 212 if fn is not None: |
213 os.unlink(fn) | 213 unlink(fn) |
214 except (IOError, OSError): | 214 except (IOError, OSError): |
215 # we don't care, the user probably won't be able to commit anyway | 215 # we don't care, the user probably won't be able to commit anyway |
216 return False | 216 return False |
217 | 217 |
218 def checklink(path): | 218 def checklink(path): |
246 target = 'checklink-target' | 246 target = 'checklink-target' |
247 open(os.path.join(cachedir, target), 'w').close() | 247 open(os.path.join(cachedir, target), 'w').close() |
248 try: | 248 try: |
249 os.symlink(target, name) | 249 os.symlink(target, name) |
250 if cachedir is None: | 250 if cachedir is None: |
251 os.unlink(name) | 251 unlink(name) |
252 else: | 252 else: |
253 try: | 253 try: |
254 os.rename(name, checklink) | 254 os.rename(name, checklink) |
255 except OSError: | 255 except OSError: |
256 os.unlink(name) | 256 unlink(name) |
257 return True | 257 return True |
258 except OSError as inst: | 258 except OSError as inst: |
259 # link creation might race, try again | 259 # link creation might race, try again |
260 if inst[0] == errno.EEXIST: | 260 if inst[0] == errno.EEXIST: |
261 continue | 261 continue |
266 except AttributeError: | 266 except AttributeError: |
267 return False | 267 return False |
268 except OSError as inst: | 268 except OSError as inst: |
269 # sshfs might report failure while successfully creating the link | 269 # sshfs might report failure while successfully creating the link |
270 if inst[0] == errno.EIO and os.path.exists(name): | 270 if inst[0] == errno.EIO and os.path.exists(name): |
271 os.unlink(name) | 271 unlink(name) |
272 return False | 272 return False |
273 | 273 |
274 def checkosfilename(path): | 274 def checkosfilename(path): |
275 '''Check that the base-relative path is a valid filename on this platform. | 275 '''Check that the base-relative path is a valid filename on this platform. |
276 Returns None if the path is ok, or a UI string describing the problem.''' | 276 Returns None if the path is ok, or a UI string describing the problem.''' |
537 os.mkdir(path) | 537 os.mkdir(path) |
538 | 538 |
539 def unlinkpath(f, ignoremissing=False): | 539 def unlinkpath(f, ignoremissing=False): |
540 """unlink and remove the directory if it is empty""" | 540 """unlink and remove the directory if it is empty""" |
541 try: | 541 try: |
542 os.unlink(f) | 542 unlink(f) |
543 except OSError as e: | 543 except OSError as e: |
544 if not (ignoremissing and e.errno == errno.ENOENT): | 544 if not (ignoremissing and e.errno == errno.ENOENT): |
545 raise | 545 raise |
546 # try removing directories that might now be empty | 546 # try removing directories that might now be empty |
547 try: | 547 try: |