Skip to content

Commit

Permalink
Remove temp files and dir when exit (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
bet4it committed Mar 12, 2020
1 parent 64ca9a6 commit 08a78ad
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
13 changes: 6 additions & 7 deletions pwndbg/file.py
Expand Up @@ -11,18 +11,17 @@
from __future__ import unicode_literals

import binascii
import errno as _errno
import os
import subprocess
import tempfile

import gdb

import pwndbg.qemu
import pwndbg.remote
import pwndbg.symbol


def get_file(path, recurse=1):
def get_file(path):
"""
Downloads the specified file from the system where the current process is
being debugged.
Expand All @@ -32,10 +31,10 @@ def get_file(path, recurse=1):
"""
local_path = path

if pwndbg.qemu.root() and recurse:
if pwndbg.qemu.root():
return os.path.join(pwndbg.qemu.binfmt_root, path)
elif pwndbg.remote.is_remote() and not pwndbg.qemu.is_qemu():
local_path = tempfile.mktemp()
local_path = tempfile.mktemp(dir=pwndbg.symbol.remote_files_dir)
error = None
try:
error = gdb.execute('remote get "%s" "%s"' % (path, local_path),
Expand All @@ -49,15 +48,15 @@ def get_file(path, recurse=1):

return local_path

def get(path, recurse=1):
def get(path):
"""
Retrieves the contents of the specified file on the system
where the current process is being debugged.
Returns:
A byte array, or None.
"""
local_path = get_file(path, recurse)
local_path = get_file(path)

try:
with open(local_path,'rb') as f:
Expand Down
5 changes: 4 additions & 1 deletion pwndbg/symbol.py
Expand Up @@ -14,6 +14,7 @@

import os
import re
import shutil
import tempfile

import elftools.common.exceptions
Expand Down Expand Up @@ -71,7 +72,9 @@ def reset_remote_files():
global remote_files
global remote_files_dir
remote_files = {}
remote_files_dir = tempfile.mkdtemp()
if remote_files_dir is not None:
shutil.rmtree(remote_files_dir)
remote_files_dir = None

@pwndbg.events.new_objfile
def autofetch():
Expand Down

0 comments on commit 08a78ad

Please sign in to comment.