Skip to content

Commit 2988d5b

Browse files
correctly handle empty mutex file (fixes #18)
1 parent 45ca578 commit 2988d5b

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

livesync/mutex.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ def __init__(self, host: str, port: int) -> None:
1414
self.occupant: Optional[str] = None
1515
self.user_id = socket.gethostname()
1616

17-
def is_free(self, info: str) -> bool:
17+
def is_free(self) -> bool:
1818
try:
19-
output = self._run_ssh_command(f'cat {self.DEFAULT_FILEPATH} || echo "{self.tag}\n{info}"').splitlines()[0]
20-
words = output.strip().split()
19+
command = f'[ -f {self.DEFAULT_FILEPATH} ] && cat {self.DEFAULT_FILEPATH} || echo'
20+
output = self._run_ssh_command(command).strip()
21+
if not output:
22+
return True
23+
words = output.splitlines()[0].strip().split()
2124
self.occupant = words[0]
2225
occupant_ok = self.occupant == self.user_id
2326
mutex_datetime = datetime.fromisoformat(words[1])
@@ -28,7 +31,7 @@ def is_free(self, info: str) -> bool:
2831
return False
2932

3033
def set(self, info: str) -> bool:
31-
if not self.is_free(info):
34+
if not self.is_free():
3235
return False
3336
try:
3437
self._run_ssh_command(f'echo "{self.tag}\n{info}" > {self.DEFAULT_FILEPATH}')

0 commit comments

Comments
 (0)