Commit e5ad44c2 authored by Cédric de Saint Martin's avatar Cédric de Saint Martin

Merge branch 'chownDirectory'

Conflicts:
	slapos/format.py
parents 2e1a7478 d671e3d5
......@@ -52,6 +52,7 @@ import zipfile
import lxml.etree
import xml_marshaller.xml_marshaller
from slapos.util import chownDirectory
from slapos.util import mkdir_p
import slapos.slap as slap
......@@ -386,7 +387,7 @@ class Computer(object):
if alter_user:
slapsoft.create()
slapsoft_pw = pwd.getpwnam(slapsoft.name)
os.chown(self.software_root, slapsoft_pw.pw_uid, slapsoft_pw.pw_gid)
chownDirectory(slapsoft.path, slapsoft_pw.pw_uid, slapsoft_pw.pw_gid)
os.chmod(self.software_root, 0o755)
# Speed hack:
......@@ -513,8 +514,8 @@ class Partition(object):
os.mkdir(self.path, 0o750)
if alter_user:
owner_pw = pwd.getpwnam(owner.name)
os.chown(self.path, owner_pw.pw_uid, owner_pw.pw_gid)
os.chmod(self.path, 0o750)
chownDirectory(self.path, owner_pw.pw_uid, owner_pw.pw_gid)
os.chmod(self.path, 0o755)
class User(object):
......
......@@ -28,6 +28,8 @@ import os
import slapos.util
import tempfile
import unittest
import shutil
from pwd import getpwnam
class TestMkdirP(unittest.TestCase):
......@@ -43,6 +45,7 @@ class TestMkdirP(unittest.TestCase):
wanted_directory = os.path.join(root_directory, 'foo', 'bar')
slapos.util.mkdir_p(wanted_directory)
self.assertTrue(os.path.isdir(wanted_directory))
shutil.rmtree(root_directory)
def test_mkdir_already_existing(self):
"""
......@@ -51,7 +54,58 @@ class TestMkdirP(unittest.TestCase):
root_directory = tempfile.mkdtemp()
slapos.util.mkdir_p(root_directory)
self.assertTrue(os.path.isdir(root_directory))
shutil.rmtree(root_directory)
def test_chown_directory(self):
"""
Test that slapos.util.chownDirectory correctly changes owner.
Note: requires root privileges.
"""
root_slaptest = tempfile.mkdtemp()
wanted_directory0 = os.path.join(root_slaptest, 'slap-write0')
wanted_directory1 = os.path.join(root_slaptest, 'slap-write0', 'write-slap1')
wanted_directory2 = os.path.join(root_slaptest, 'slap-write0', 'write-slap1', 'write-teste2')
wanted_directory_mkdir0 = os.makedirs(wanted_directory0, mode=0777)
wanted_directory_mkdir1 = os.makedirs(wanted_directory1, mode=0777)
wanted_directory_mkdir2 = os.makedirs(wanted_directory2, mode=0777)
create_file_txt = tempfile.mkstemp(suffix='.txt', prefix='tmp', dir=wanted_directory2, text=True)
user = 'nobody'
try:
uid = getpwnam(user)[2]
gid = getpwnam(user)[3]
except KeyError:
raise unittest.SkipTest("user %s doesn't exist." % user)
try:
slapos.util.chownDirectory(root_slaptest, uid, gid)
except OSError:
raise unittest.SkipTest("No root privileges, impossible to chown.")
uid_check_root_slaptest = os.stat(root_slaptest)[4]
gid_check_root_slaptest = os.stat(root_slaptest)[5]
self.assertTrue(uid == uid_check_root_slaptest)
self.assertTrue(gid == gid_check_root_slaptest)
uid_check_wanted_directory0 = os.stat(wanted_directory0)[4]
gid_check_wanted_directory0 = os.stat(wanted_directory0)[5]
self.assertTrue(uid == uid_check_wanted_directory0)
self.assertTrue(gid == gid_check_wanted_directory0)
uid_check_wanted_directory1 = os.stat(wanted_directory1)[4]
gid_check_wanted_directory1 = os.stat(wanted_directory1)[5]
self.assertTrue(uid == uid_check_wanted_directory1)
self.assertTrue(gid == gid_check_wanted_directory1)
uid_check_wanted_directory2 = os.stat(wanted_directory2)[4]
gid_check_wanted_directory2 = os.stat(wanted_directory2)[5]
self.assertTrue(uid == uid_check_wanted_directory2)
self.assertTrue(gid == gid_check_wanted_directory2)
uid_check_file_txt = os.stat(create_file_txt[1])[4]
gid_check_file_txt = os.stat(create_file_txt[1])[5]
self.assertTrue(uid == uid_check_file_txt)
self.assertTrue(gid == gid_check_file_txt)
shutil.rmtree(root_slaptest)
if __name__ == '__main__':
unittest.main()
......@@ -17,3 +17,10 @@ def mkdir_p(path, mode=0o777):
else:
raise
def chownDirectory(path, uid, gid):
os.chown(path, uid, gid)
for root, dirs, files in os.walk(path):
for items in dirs, files:
for item in items:
if not os.path.islink(os.path.join(root, item)):
os.chown(os.path.join(root, item), uid, gid)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment