Make openLocked always return BlockingIOError on fail
This commit is contained in:
parent
e6c2937c1b
commit
4aee7a6c61
3 changed files with 15 additions and 12 deletions
|
@ -68,7 +68,7 @@ class TestHelper:
|
|||
def testOpenLocked(self):
|
||||
locked_f = helper.openLocked(config.data_dir + "/locked.file")
|
||||
assert locked_f
|
||||
with pytest.raises(PermissionError):
|
||||
with pytest.raises(BlockingIOError):
|
||||
locked_f_again = helper.openLocked(config.data_dir + "/locked.file")
|
||||
locked_f_different = helper.openLocked(config.data_dir + "/locked_different.file")
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ if config.action == "main":
|
|||
try:
|
||||
lock = helper.openLocked("%s/lock.pid" % config.data_dir, "w")
|
||||
lock.write("%s" % os.getpid())
|
||||
except IOError as err:
|
||||
except BlockingIOError as err:
|
||||
print("Can't open lock file, your ZeroNet client is probably already running, exiting... (%s)" % err)
|
||||
if config.open_browser and config.open_browser != "False":
|
||||
print("Opening browser: %s...", config.open_browser)
|
||||
|
|
|
@ -37,16 +37,19 @@ def atomicWrite(dest, content, mode="wb"):
|
|||
|
||||
|
||||
def openLocked(path, mode="wb"):
|
||||
if os.name == "posix":
|
||||
import fcntl
|
||||
f = open(path, mode)
|
||||
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
elif os.name == "nt":
|
||||
import msvcrt
|
||||
f = open(path, mode)
|
||||
msvcrt.locking(f.fileno(), msvcrt.LK_NBLCK, 1)
|
||||
else:
|
||||
f = open(path, mode)
|
||||
try:
|
||||
if os.name == "posix":
|
||||
import fcntl
|
||||
f = open(path, mode)
|
||||
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
elif os.name == "nt":
|
||||
import msvcrt
|
||||
f = open(path, mode)
|
||||
msvcrt.locking(f.fileno(), msvcrt.LK_NBLCK, 1)
|
||||
else:
|
||||
f = open(path, mode)
|
||||
except (IOError, PermissionError, BlockingIOError) as err:
|
||||
raise BlockingIOError("Unable to lock file: %s" % err)
|
||||
return f
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue