Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2545)

Unified Diff: build/android/devil/android/device_temp_file.py

Issue 1412433003: Revert of Android: Trust large random numbers for temp files (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | build/android/devil/android/device_temp_file_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: build/android/devil/android/device_temp_file.py
diff --git a/build/android/devil/android/device_temp_file.py b/build/android/devil/android/device_temp_file.py
index 01a91d4524c7390cf9312847dc3eb08b9b3a05de..cd9e8db5613f11f1b2f229853cafa4110359494d 100644
--- a/build/android/devil/android/device_temp_file.py
+++ b/build/android/devil/android/device_temp_file.py
@@ -6,17 +6,25 @@
# pylint: disable=W0622
-import posixpath
-import random
import threading
from devil.android import device_errors
from devil.utils import cmd_helper
+_COMMAND_TEMPLATE = (
+ # Make sure that the temp dir is writable
+ 'test -d {dir} && '
+ # If 5 random attempts fail, something is up.
+ 'for i in 1 2 3 4 5; do '
+ 'fn={dir}/{prefix}-$(date +%s)-"$RANDOM"{suffix};'
+ 'test -e "$fn" || break;'
+ 'done && '
+ # Touch the file, so other temp files can't get the same name.
+ 'touch "$fn" && echo -n "$fn"')
class DeviceTempFile(object):
def __init__(self, adb, suffix='', prefix='temp_file', dir='/data/local/tmp'):
- """Find an unused temporary file path on the device.
+ """Find an unused temporary file path in the devices external directory.
When this object is closed, the file will be deleted on the device.
@@ -27,9 +35,11 @@
dir: The directory on the device where to place the temp file.
"""
self._adb = adb
- # Python's random module use 52-bit numbers according to its docs.
- random_hex = hex(random.randint(0, 2 ** 52))[2:]
- self.name = posixpath.join(dir, '%s-%s%s' % (prefix, random_hex, suffix))
+ command = _COMMAND_TEMPLATE.format(
+ dir=cmd_helper.SingleQuote(dir),
+ suffix=cmd_helper.SingleQuote(suffix),
+ prefix=cmd_helper.SingleQuote(prefix))
+ self.name = self._adb.Shell(command)
self.name_quoted = cmd_helper.SingleQuote(self.name)
def close(self):
« no previous file with comments | « no previous file | build/android/devil/android/device_temp_file_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698