Skip to content

Commit

Permalink
Give an actual name to MemoryIO on FreeBSD
Browse files Browse the repository at this point in the history
  • Loading branch information
quantum5 committed Jan 30, 2022
1 parent 0d500be commit 4b68ee6
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion dmoj/cptbox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
import io
import mmap
import os
from tempfile import mktemp
from typing import Optional

from dmoj.cptbox._cptbox import memory_fd_create, memory_fd_seal
from dmoj.cptbox.tracer import FREEBSD


class MemoryIO(io.FileIO):
def __init__(self, prefill: Optional[bytes] = None, seal=False) -> None:
super().__init__(memory_fd_create(), 'r+')
self._name = None
if prefill:
self.write(prefill)
if seal:
Expand All @@ -32,8 +35,28 @@ def seal(self) -> None:
finally:
os.close(new_fd)

def close(self) -> None:
super().close()
if self._name:
os.unlink(self._name)

def to_path(self) -> str:
return f'/proc/{os.getpid()}/fd/{self.fileno()}'
if not FREEBSD:
return f'/proc/{os.getpid()}/fd/{self.fileno()}'

if self._name:
return self._name

while True:
name = mktemp()
try:
os.link(f'/dev/fd/{self.fileno()}', name, follow_symlinks=True)
except OSError as e:
if e.errno != errno.EEXIST:
raise
else:
self._name = name
return name

def to_bytes(self) -> bytes:
try:
Expand Down

0 comments on commit 4b68ee6

Please sign in to comment.