Merge lp:~salgado/linaro-image-tools/bug-789093 into lp:linaro-image-tools/11.11

Proposed by Guilherme Salgado
Status: Merged
Merged at revision: 377
Proposed branch: lp:~salgado/linaro-image-tools/bug-789093
Merge into: lp:linaro-image-tools/11.11
Diff against target: 85 lines (+40/-1)
2 files modified
linaro_image_tools/media_create/rootfs.py (+15/-1)
linaro_image_tools/media_create/tests/test_media_create.py (+25/-0)
To merge this branch: bzr merge lp:~salgado/linaro-image-tools/bug-789093
Reviewer Review Type Date Requested Status
James Tunnicliffe (community) Approve
Linaro Maintainers Pending
Review via email: mp+68266@code.launchpad.net

Description of the change

Make sure rootfs.move_contents() doesn't skip files that are not
world-readable

To post a comment you must log in.
Revision history for this message
James Tunnicliffe (dooferlad) wrote :

Looks good to me.

Prompts me to wonder again why we have any part of this process running as root other than writing an image to a device, but I will move that conversation to an email thread.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'linaro_image_tools/media_create/rootfs.py'
2--- linaro_image_tools/media_create/rootfs.py 2011-04-05 09:26:47 +0000
3+++ linaro_image_tools/media_create/rootfs.py 2011-07-18 15:18:40 +0000
4@@ -19,6 +19,7 @@
5
6 import glob
7 import os
8+import subprocess
9 import tempfile
10
11 from linaro_image_tools import cmd_runner
12@@ -107,13 +108,26 @@
13 flash_kernel, "UBOOT_PART=%s" % target_boot_dev)
14
15
16+def _list_files(directory):
17+ """List the files and dirs under the given directory.
18+
19+ Runs as root because we want to list everything, including stuff that may
20+ not be world-readable.
21+ """
22+ p = cmd_runner.run(
23+ ['find', directory, '-maxdepth', '1', '-mindepth', '1'],
24+ stdout=subprocess.PIPE, as_root=True)
25+ stdout, _ = p.communicate()
26+ return stdout.split()
27+
28+
29 def move_contents(from_, root_disk):
30 """Move everything under from_ to the given root disk.
31
32 Uses sudo for moving.
33 """
34 assert os.path.isdir(from_), "%s is not a directory" % from_
35- files = glob.glob(os.path.join(from_, '*'))
36+ files = _list_files(from_)
37 mv_cmd = ['mv']
38 mv_cmd.extend(sorted(files))
39 mv_cmd.append(root_disk)
40
41=== modified file 'linaro_image_tools/media_create/tests/test_media_create.py'
42--- linaro_image_tools/media_create/tests/test_media_create.py 2011-07-05 15:00:22 +0000
43+++ linaro_image_tools/media_create/tests/test_media_create.py 2011-07-18 15:18:40 +0000
44@@ -2121,6 +2121,14 @@
45 os.makedirs(contents_bin)
46 os.makedirs(contents_etc)
47
48+ # Must mock rootfs._list_files() because populate_rootfs() uses its
49+ # return value but since we mock cmd_runner.run() _list_files() would
50+ # return an invalid value.
51+ def mock_list_files(directory):
52+ return [contents_bin, contents_etc]
53+ self.useFixture(MockSomethingFixture(
54+ rootfs, '_list_files', mock_list_files))
55+
56 populate_rootfs(
57 contents_dir, root_disk, partition='/dev/rootfs',
58 rootfs_type='ext3', rootfs_uuid='uuid', should_create_swap=True,
59@@ -2162,10 +2170,27 @@
60 fixture.mock.commands_executed[0])
61 self.assertEqual('UBOOT_PART=/dev/mmcblk0p1', open(tmpfile).read())
62
63+ def test_list_files(self):
64+ tempdir = self.useFixture(CreateTempDirFixture()).tempdir
65+ # We don't want to mock cmd_runner.run() because we're testing the
66+ # command that it runs, but we need to monkey-patch SUDO_ARGS because
67+ # we don't want to use 'sudo' in tests.
68+ orig_sudo_args = cmd_runner.SUDO_ARGS
69+ def restore_sudo_args():
70+ cmd_runner.SUDO_ARGS = orig_sudo_args
71+ self.addCleanup(restore_sudo_args)
72+ cmd_runner.SUDO_ARGS = []
73+ file1 = self.createTempFileAsFixture(dir=tempdir)
74+ self.assertEqual([file1], rootfs._list_files(tempdir))
75+
76 def test_move_contents(self):
77 tempdir = self.useFixture(CreateTempDirFixture()).tempdir
78 popen_fixture = self.useFixture(MockCmdRunnerPopenFixture())
79 file1 = self.createTempFileAsFixture(dir=tempdir)
80+ def mock_list_files(directory):
81+ return [file1]
82+ self.useFixture(MockSomethingFixture(
83+ rootfs, '_list_files', mock_list_files))
84
85 move_contents(tempdir, '/tmp/')
86

Subscribers

People subscribed via source and target branches