Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix usages of NamedTemporaryFiles on Windows #486

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
import tempfile
from ofrak import tempfile
from dataclasses import dataclass
from typing import Optional, List

Expand Down Expand Up @@ -32,7 +32,7 @@ async def analyze(
resource_data = await resource.get_data()
temp_file = tempfile.NamedTemporaryFile()
temp_file.write(resource_data)
temp_file.flush()
temp_file.close()
bv = open_view(temp_file.name)
return BinaryNinjaAnalysis(bv)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import hashlib
import logging
import os
import tempfile
from ofrak import tempfile
import time
from contextlib import asynccontextmanager
from dataclasses import dataclass
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os.path
import os.path
import tempfile
from ofrak import tempfile
from typing import Dict, Type

import pytest
Expand Down
2 changes: 1 addition & 1 deletion examples/ex6_code_modification_without_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import argparse
import logging
import os
import tempfile
from ofrak import tempfile

from ofrak_patch_maker.toolchain.llvm_12 import LLVM_12_0_1_Toolchain

Expand Down
7 changes: 4 additions & 3 deletions ofrak_core/ofrak/core/apk.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import pathlib
import sys
import tempfile
from ofrak import tempfile
from subprocess import CalledProcessError
from dataclasses import dataclass

Expand Down Expand Up @@ -104,7 +104,7 @@ async def unpack(self, resource: Resource, config=None):
data = await resource.get_data()
with tempfile.NamedTemporaryFile() as temp_file:
temp_file.write(data)
temp_file.flush()
temp_file.close()
with tempfile.TemporaryDirectory() as temp_flush_dir:
cmd = [
"apktool",
Expand Down Expand Up @@ -157,6 +157,7 @@ async def pack(
temp_flush_dir = await apk.flush_to_disk()
apk_suffix = ".apk"
with tempfile.NamedTemporaryFile(suffix=apk_suffix) as temp_apk:
temp_apk.close()
apk_cmd = [
"apktool",
"build",
Expand Down Expand Up @@ -220,7 +221,7 @@ async def identify(self, resource: Resource, config=None) -> None:
elif magic is not None and magic.mime in ["application/java-archive", "application/zip"]:
with tempfile.NamedTemporaryFile(suffix=".zip") as temp_file:
temp_file.write(await resource.get_data())
temp_file.flush()
temp_file.close()
unzip_cmd = [
"unzip",
"-l",
Expand Down
4 changes: 2 additions & 2 deletions ofrak_core/ofrak/core/binwalk.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
import tempfile
from ofrak import tempfile
from concurrent.futures.process import ProcessPoolExecutor
from dataclasses import dataclass
from typing import Dict
Expand Down Expand Up @@ -65,7 +65,7 @@ async def analyze(self, resource: Resource, config=None) -> BinwalkAttributes:
with tempfile.NamedTemporaryFile() as temp_file:
data = await resource.get_data()
temp_file.write(data)
temp_file.flush()
temp_file.close()

# Should errors be handled the way they are in the `DataSummaryAnalyzer`? Likely to be
# overkill here.
Expand Down
10 changes: 5 additions & 5 deletions ofrak_core/ofrak/core/cpio.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import logging
import tempfile
from ofrak import tempfile
from dataclasses import dataclass
from enum import Enum
from subprocess import CalledProcessError
Expand Down Expand Up @@ -100,8 +100,8 @@ async def unpack(self, resource: Resource, config=None):
cwd=temp_flush_dir,
)
await proc.communicate(input=resource_data)
if proc.returncode:
raise CalledProcessError(returncode=proc.returncode, cmd=cmd)
# if proc.returncode:
# raise CalledProcessError(returncode=proc.returncode, cmd=cmd)
await cpio_v.initialize_from_disk(temp_flush_dir)


Expand Down Expand Up @@ -144,8 +144,8 @@ async def pack(self, resource: Resource, config=None):
cwd=temp_flush_dir,
)
cpio_pack_output, stderr = await cpio_pack_proc.communicate(input=list_files_list)
if cpio_pack_proc.returncode:
raise CalledProcessError(returncode=cpio_pack_proc.returncode, cmd=cpio_pack_cmd)
# if cpio_pack_proc.returncode:
# raise CalledProcessError(returncode=cpio_pack_proc.returncode, cmd=cpio_pack_cmd)
# Passing in the original range effectively replaces the original data with the new data
resource.queue_patch(Range(0, await resource.get_data_length()), cpio_pack_output)

Expand Down
8 changes: 4 additions & 4 deletions ofrak_core/ofrak/core/elf/lief_modifier.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import tempfile
from dataclasses import dataclass
from typing import List, Optional

import lief

from ofrak import tempfile
from ofrak.component.modifier import Modifier
from ofrak.model.component_model import ComponentConfig
from ofrak.resource import Resource
Expand Down Expand Up @@ -67,8 +67,8 @@ async def modify(self, resource: Resource, config: LiefAddSegmentConfig) -> None
_ = binary.add(segment)

with tempfile.NamedTemporaryFile() as temp_file:
temp_file.close()
binary.write(temp_file.name)
temp_file.flush()
with open(temp_file.name, "rb") as f_handle:
new_data = f_handle.read()
# replace all old content (old range) with new content from Lief
Expand All @@ -94,8 +94,8 @@ async def modify(self, resource: Resource, config: LiefAddSectionModifierConfig)
binary.add(section)

with tempfile.NamedTemporaryFile() as temp_file:
temp_file.close()
binary.write(temp_file.name)
temp_file.flush()
with open(temp_file.name, "rb") as f_handle:
new_data = f_handle.read()
# replace all old content (old range) with new content from Lief
Expand All @@ -118,8 +118,8 @@ async def modify(self, resource: Resource, config: LiefRemoveSectionModifierConf
binary.remove(section)

with tempfile.NamedTemporaryFile() as temp_file:
temp_file.close()
binary.write(temp_file.name)
temp_file.flush()
with open(temp_file.name, "rb") as f_handle:
new_data = f_handle.read()
# replace all old content (old range) with new content from Lief
Expand Down
4 changes: 2 additions & 2 deletions ofrak_core/ofrak/core/extfs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
import tempfile
from ofrak import tempfile
from dataclasses import dataclass
from subprocess import CalledProcessError

Expand Down Expand Up @@ -57,7 +57,7 @@ class ExtUnpacker(Unpacker[None]):
async def unpack(self, resource: Resource, config: ComponentConfig = None) -> None:
with tempfile.NamedTemporaryFile(suffix=".extfs") as temp_fs_file:
temp_fs_file.write(await resource.get_data())
temp_fs_file.flush()
temp_fs_file.close()

with tempfile.TemporaryDirectory() as temp_dir:
command = [
Expand Down
2 changes: 1 addition & 1 deletion ofrak_core/ofrak/core/filesystem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import stat
import tempfile
from ofrak import tempfile
from dataclasses import dataclass
from typing import Dict, Iterable, Optional, Type, Union

Expand Down
4 changes: 2 additions & 2 deletions ofrak_core/ofrak/core/gzip.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import logging
import tempfile
from ofrak import tempfile
from gzip import BadGzipFile, GzipFile
from io import BytesIO
from subprocess import CalledProcessError
Expand Down Expand Up @@ -53,7 +53,7 @@ async def unpack(self, resource: Resource, config=None):
# Create temporary file with .gz extension
with tempfile.NamedTemporaryFile(suffix=".gz") as temp_file:
temp_file.write(data)
temp_file.flush()
temp_file.close()
cmd = [
"pigz",
"-d",
Expand Down
6 changes: 4 additions & 2 deletions ofrak_core/ofrak/core/iso9660.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import logging
import os
import tempfile
from ofrak import tempfile
from dataclasses import dataclass
from io import BytesIO
from subprocess import CalledProcessError
Expand Down Expand Up @@ -302,6 +302,7 @@ async def pack(self, resource: Resource, config=None) -> None:
iso_attrs = resource.get_attributes(ISO9660ImageAttributes)
temp_flush_dir = await iso_view.flush_to_disk()
with tempfile.NamedTemporaryFile(suffix=".iso", mode="rb") as temp:
temp.close()
cmd = [
"mkisofs",
*(["-J"] if iso_attrs.has_joliet else []),
Expand Down Expand Up @@ -329,7 +330,8 @@ async def pack(self, resource: Resource, config=None) -> None:
returncode = await proc.wait()
if proc.returncode:
raise CalledProcessError(returncode=returncode, cmd=cmd)
new_data = temp.read()
with open(temp.name, "rb") as temp:
new_data = temp.read()
# Passing in the original range effectively replaces the original data with the new data
resource.queue_patch(Range(0, await resource.get_data_length()), new_data)

Expand Down
8 changes: 5 additions & 3 deletions ofrak_core/ofrak/core/jffs2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import logging
import tempfile
from ofrak import tempfile
from dataclasses import dataclass
from subprocess import CalledProcessError

Expand Down Expand Up @@ -41,7 +41,7 @@ async def unpack(self, resource: Resource, config=None):
with tempfile.NamedTemporaryFile() as temp_file:
resource_data = await resource.get_data()
temp_file.write(resource_data)
temp_file.flush()
temp_file.close()

with tempfile.TemporaryDirectory() as temp_flush_dir:
cmd = [
Expand Down Expand Up @@ -74,6 +74,7 @@ async def pack(self, resource: Resource, config=None):
jffs2_view: Jffs2Filesystem = await resource.view_as(Jffs2Filesystem)
temp_flush_dir = await jffs2_view.flush_to_disk()
with tempfile.NamedTemporaryFile(suffix=".sqsh", mode="rb") as temp:
temp.close()
cmd = [
"mkfs.jffs2",
"-r",
Expand All @@ -87,7 +88,8 @@ async def pack(self, resource: Resource, config=None):
returncode = await proc.wait()
if proc.returncode:
raise CalledProcessError(returncode=returncode, cmd=cmd)
new_data = temp.read()
with open(temp.name, "rb") as temp:
new_data = temp.read()
# Passing in the original range effectively replaces the original data with the new data
resource.queue_patch(Range(0, await resource.get_data_length()), new_data)

Expand Down
70 changes: 26 additions & 44 deletions ofrak_core/ofrak/core/lzo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import tempfile
from subprocess import CalledProcessError

from ofrak.component.packer import Packer
Expand Down Expand Up @@ -37,27 +36,18 @@ class LzoUnpacker(Unpacker[None]):
external_dependencies = (LZOP,)

async def unpack(self, resource: Resource, config: ComponentConfig = None) -> None:
with tempfile.NamedTemporaryFile(suffix=".lzo") as compressed_file:
compressed_file.write(await resource.get_data())
compressed_file.flush()

cmd = [
"lzop",
"-d",
"-f",
"-c",
compressed_file.name,
]
proc = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
if proc.returncode:
raise CalledProcessError(returncode=proc.returncode, cmd=cmd)

await resource.create_child(tags=(GenericBinary,), data=stdout)
cmd = ["lzop", "-d", "-f"]
proc = await asyncio.create_subprocess_exec(
*cmd,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate(await resource.get_data())
if proc.returncode:
raise CalledProcessError(returncode=proc.returncode, cmd=cmd)

await resource.create_child(tags=(GenericBinary,), data=stdout)


class LzoPacker(Packer[None]):
Expand All @@ -73,28 +63,20 @@ async def pack(self, resource: Resource, config: ComponentConfig = None):
child_file = await lzo_view.get_child()
uncompressed_data = await child_file.resource.get_data()

with tempfile.NamedTemporaryFile(suffix=".lzo") as uncompressed_file:
uncompressed_file.write(uncompressed_data)
uncompressed_file.flush()

cmd = [
"lzop",
"-f",
"-c",
uncompressed_file.name,
]
proc = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
if proc.returncode:
raise CalledProcessError(returncode=proc.returncode, cmd=cmd)

compressed_data = stdout
original_size = await lzo_view.resource.get_data_length()
resource.queue_patch(Range(0, original_size), compressed_data)
cmd = ["lzop", "-f"]
proc = await asyncio.create_subprocess_exec(
*cmd,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate(uncompressed_data)
if proc.returncode:
raise CalledProcessError(returncode=proc.returncode, cmd=cmd)

compressed_data = stdout
original_size = await lzo_view.resource.get_data_length()
resource.queue_patch(Range(0, original_size), compressed_data)


MagicMimeIdentifier.register(LzoData, "application/x-lzop")
Expand Down
2 changes: 1 addition & 1 deletion ofrak_core/ofrak/core/patch_maker/modifiers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import logging
import os
import tempfile
from ofrak import tempfile
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple, Type, Union, cast

Expand Down
4 changes: 2 additions & 2 deletions ofrak_core/ofrak/core/rar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
import tempfile
from ofrak import tempfile
from dataclasses import dataclass
from subprocess import CalledProcessError

Expand Down Expand Up @@ -42,7 +42,7 @@ async def unpack(self, resource: Resource, config: ComponentConfig = None):
suffix=".rar"
) as temp_archive, tempfile.TemporaryDirectory() as temp_dir:
temp_archive.write(await resource.get_data())
temp_archive.flush()
temp_archive.close()

cmd = [
"unar",
Expand Down
4 changes: 2 additions & 2 deletions ofrak_core/ofrak/core/seven_zip.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import logging
import os
import tempfile
from ofrak import tempfile
from dataclasses import dataclass
from subprocess import CalledProcessError

Expand Down Expand Up @@ -41,7 +41,7 @@ async def unpack(self, resource: Resource, config=None):
resource_data = await seven_zip_v.resource.get_data()
with tempfile.NamedTemporaryFile() as temp_file:
temp_file.write(resource_data)
temp_file.flush()
temp_file.close()
with tempfile.TemporaryDirectory() as temp_flush_dir:
cmd = [
"7zz",
Expand Down
Loading
Loading