Skip to content

Commit

Permalink
Add HACK_TMP_DIR opt to force temp files into specific directory
Browse files Browse the repository at this point in the history
Fixes: markokr#43
  • Loading branch information
markokr committed Sep 20, 2019
1 parent 9b39f22 commit af10e45
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Module Configuration
.. autodata:: PATH_SEP
.. autodata:: USE_EXTRACT_HACK
.. autodata:: HACK_SIZE_LIMIT
.. autodata:: HACK_TMP_DIR

Constants
---------
Expand Down
6 changes: 6 additions & 0 deletions doc/news.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ rarfile history
master
------

New feature:

* Add :data:`HACK_TMP_DIR` option, to force temp files into
specific directory.
[`#43 <https://github.com/markokr/rarfile/issues/43>`_]

Cleanups:

* Drop support for Python 2 and 3.5 and earlier. Python 2 is dead
Expand Down
9 changes: 6 additions & 3 deletions rarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ def tohex(data):
#: limit the filesize for tmp archive usage
HACK_SIZE_LIMIT = 20 * 1024 * 1024

#: set specific directory for mkstemp() used by hack dir usage
HACK_TMP_DIR = None

#: Separator for path name components. RAR internally uses '\\'.
#: Use '/' to be similar with zipfile.
PATH_SEP = '/'
Expand Down Expand Up @@ -1116,7 +1119,7 @@ def _open_hack_core(self, inf, psw, prefix, suffix):
rf = XFile(inf.volume_file, 0)
rf.seek(inf.header_offset)

tmpfd, tmpname = mkstemp(suffix='.rar')
tmpfd, tmpname = mkstemp(suffix='.rar', dir=HACK_TMP_DIR)
tmpf = os.fdopen(tmpfd, "wb")

try:
Expand Down Expand Up @@ -2759,7 +2762,7 @@ def rar3_decompress(vers, meth, data, declen=0, flags=0, crc=0, psw=None, salt=N
mh = S_BLK_HDR.pack(0x90CF, RAR_BLOCK_MAIN, 0, 13) + ZERO * (2 + 4)

# decompress via temp rar
tmpfd, tmpname = mkstemp(suffix='.rar')
tmpfd, tmpname = mkstemp(suffix='.rar', dir=HACK_TMP_DIR)
tmpf = os.fdopen(tmpfd, "wb")
try:
tmpf.write(RAR_ID + mh + hdr + data)
Expand Down Expand Up @@ -2904,7 +2907,7 @@ def membuf_tempfile(memfile):
"""Write in-memory file object to real file."""
memfile.seek(0, 0)

tmpfd, tmpname = mkstemp(suffix='.rar')
tmpfd, tmpname = mkstemp(suffix='.rar', dir=HACK_TMP_DIR)
tmpf = os.fdopen(tmpfd, "wb")

try:
Expand Down

0 comments on commit af10e45

Please sign in to comment.