Skip to content
Permalink
Browse files

[OpenWrt] Added file permission feature

  • Loading branch information
nemesisdesign committed Nov 24, 2015
1 parent 6b33a98 commit 66ee96d1cc5a0e7823082e6c6abaa7d8f7f63909
@@ -137,9 +137,10 @@ def _add_files(self, tar, timestamp):
self._add_file(tar=tar,
name=path,
contents=contents,
timestamp=timestamp)
timestamp=timestamp,
mode=file_item.get('mode', '644'))

def _add_file(self, tar, name, contents, timestamp):
def _add_file(self, tar, name, contents, timestamp, mode='644'):
"""
adds a single file in tar object
"""
@@ -148,4 +149,5 @@ def _add_file(self, tar, name, contents, timestamp):
info.size = len(contents)
info.mtime = timestamp
info.type = tarfile.REGTYPE
info.mode = int(mode, 8) # permissions converted to decimal notation
tar.addfile(tarinfo=info, fileobj=byte_contents)
@@ -295,6 +295,10 @@
{"type": "string", "format": "textarea"},
{"type": "array"}
]
},
"mode": {
"type": "string",
"maxLength": 4
}
}
}
@@ -281,10 +281,12 @@ def test_file_inclusion(self):
contents = tar.extractfile(crontab).read().decode()
self.assertEqual(contents, o.config['files'][0]['contents'])
self.assertNotEqual(crontab.mtime, 0)
self.assertEqual(crontab.mode, 420)
# second file
dummy = tar.getmember('etc/dummy.conf')
contents = tar.extractfile(dummy).read().decode()
self.assertEqual(contents, o.config['files'][1]['contents'])
self.assertEqual(dummy.mode, 420)
# close and delete tar.gz file
tar.close()
os.remove('openwrt-config.tar.gz')
@@ -312,3 +314,22 @@ def test_file_inclusion_list_contents(self):
# close and delete tar.gz file
tar.close()
os.remove('openwrt-config.tar.gz')

def test_file_permissions(self):
o = OpenWrt({
"files": [
{
"path": "/tmp/hello.sh",
"contents": "echo 'hello world'",
"mode": "0755"
}
]
})
o.generate()
tar = tarfile.open('openwrt-config.tar.gz', 'r:gz')
script = tar.getmember('tmp/hello.sh')
# check permissions
self.assertEqual(script.mode, 493)
# close and delete tar.gz file
tar.close()
os.remove('openwrt-config.tar.gz')

0 comments on commit 66ee96d

Please sign in to comment.
You can’t perform that action at this time.