8049 leading / not stripped from file action path on Windows
authorTom Mueller <Tom.Mueller@sun.com>
Mon, 12 Oct 2009 08:18:41 -0500
changeset 1407 56042994e222
parent 1406 46c950f90950
child 1408 9cce06d4da41
8049 leading / not stripped from file action path on Windows 8698 search fails when pkg.depotd runs on Windows but packages published on Unix 11469 cannot run pkg.depotd as readonly on Windows (permissions problem)
src/modules/actions/file.py
src/modules/manifest.py
src/modules/portable/__init__.py
src/modules/portable/os_aix.py
src/modules/portable/os_darwin.py
src/modules/portable/os_sunos.py
src/modules/portable/os_unix.py
src/modules/portable/os_windows.py
src/modules/server/catalog.py
src/tests/api/t_catalog.py
--- a/src/modules/actions/file.py	Fri Oct 09 12:46:38 2009 +0100
+++ b/src/modules/actions/file.py	Mon Oct 12 08:18:41 2009 -0500
@@ -57,8 +57,7 @@
                 self.hash = "NOHASH"
                 self.replace_required = False
                 if "path" in self.attrs:
-                        self.attrs["path"] = self.attrs["path"].lstrip(
-                            os.path.sep)
+                        self.attrs["path"] = self.attrs["path"].lstrip("/")
                         if not self.attrs["path"]:
                                 raise pkg.actions.InvalidActionError(
                                     str(self), _("Empty path attribute"))
--- a/src/modules/manifest.py	Fri Oct 09 12:46:38 2009 +0100
+++ b/src/modules/manifest.py	Mon Oct 12 08:18:41 2009 -0500
@@ -380,7 +380,7 @@
                 if log is None:
                         log = lambda x: None
 
-                file_handle = file(file_path)
+                file_handle = file(file_path, "rb")
                 cur_pos = 0
                 line = file_handle.readline()
                 action_dict = {}
--- a/src/modules/portable/__init__.py	Fri Oct 09 12:46:38 2009 +0100
+++ b/src/modules/portable/__init__.py	Mon Oct 12 08:18:41 2009 -0500
@@ -198,6 +198,14 @@
         often leads to ambiguity in cross-platform code)."""
         raise NotImplementedError
 
+def assert_mode(path, mode):
+        """ Checks that the file identified by path has the given mode to
+        the extent possible by the host operating system.  Otherwise raises
+        an AssertionError where the mode attribute of the assertion is the 
+        mode of the file."""
+        raise NotImplementedError
+
+
 # File type constants
 # -------------------
 ELF, EXEC, UNFOUND = range(0, 3)
--- a/src/modules/portable/os_aix.py	Fri Oct 09 12:46:38 2009 +0100
+++ b/src/modules/portable/os_aix.py	Mon Oct 12 08:18:41 2009 -0500
@@ -33,7 +33,7 @@
 from os_unix import \
     get_isainfo, get_release, get_platform, get_group_by_name, \
     get_user_by_name, get_name_by_gid, get_name_by_uid, is_admin, get_userid, \
-    get_username, rename, remove, link, split_path, get_root, copyfile
+    get_username, rename, remove, link, split_path, get_root, assert_mode, copyfile
 
 def chown(path, owner, group):
         # The "nobody" user on AIX has uid -2, which is an invalid UID on NFS
--- a/src/modules/portable/os_darwin.py	Fri Oct 09 12:46:38 2009 +0100
+++ b/src/modules/portable/os_darwin.py	Mon Oct 12 08:18:41 2009 -0500
@@ -31,7 +31,7 @@
 from os_unix import \
     get_isainfo, get_release, get_platform, get_group_by_name, \
     get_user_by_name, get_name_by_gid, get_name_by_uid, is_admin, get_userid, \
-    get_username, chown, rename, remove, link, split_path, get_root
+    get_username, chown, rename, remove, link, split_path, get_root, assert_mode
 
 import macostools
 
--- a/src/modules/portable/os_sunos.py	Fri Oct 09 12:46:38 2009 +0100
+++ b/src/modules/portable/os_sunos.py	Mon Oct 12 08:18:41 2009 -0500
@@ -36,7 +36,7 @@
 from os_unix import \
     get_group_by_name, get_user_by_name, get_name_by_gid, get_name_by_uid, \
     is_admin, get_userid, get_username, chown, rename, remove, link, \
-    copyfile, split_path, get_root
+    copyfile, split_path, get_root, assert_mode
 from pkg.portable import ELF, EXEC, UNFOUND
 import pkg.arch as arch
 
--- a/src/modules/portable/os_unix.py	Fri Oct 09 12:46:38 2009 +0100
+++ b/src/modules/portable/os_unix.py	Mon Oct 12 08:18:41 2009 -0500
@@ -37,6 +37,7 @@
 import os
 import platform
 import shutil
+import stat
 import sys
 import util as os_util
 # used to cache contents of passwd and group files
@@ -240,6 +241,14 @@
 def get_root(path):
         return '/'
 
+def assert_mode(path, mode):
+        fmode = stat.S_IMODE(os.lstat(path).st_mode)
+        if mode != fmode:
+                ae = AssertionError("mode mismatch for %s, has %o, want %o" % 
+                    (path, fmode, mode))
+                ae.mode = fmode;
+                raise ae
+
 def copyfile(src, dst):
         shutil.copyfile(src, dst)
 
--- a/src/modules/portable/os_windows.py	Fri Oct 09 12:46:38 2009 +0100
+++ b/src/modules/portable/os_windows.py	Mon Oct 12 08:18:41 2009 -0500
@@ -36,6 +36,7 @@
 import shutil
 import os
 import errno
+import stat
 import tempfile
 import threading
 import util as os_util
@@ -223,5 +224,14 @@
         else:
                 return drivepath[0] + '\\'
 
+def assert_mode(path, mode):
+        # only compare user's permission bits on Windows
+        fmode = stat.S_IMODE(os.lstat(path).st_mode)
+        if (mode & stat.S_IRWXU) != (fmode & stat.S_IRWXU):
+                ae = AssertionError("mode mismatch for %s, has %o, want %o" % 
+                    (path, fmode, mode))
+                ae.mode = fmode;
+                raise ae
+
 def copyfile(src, dst):
         shutil.copyfile(src, dst)
--- a/src/modules/server/catalog.py	Fri Oct 09 12:46:38 2009 +0100
+++ b/src/modules/server/catalog.py	Mon Oct 12 08:18:41 2009 -0500
@@ -294,12 +294,13 @@
                 for fpath in (apath, cpath):
                         try:
                                 if self.read_only:
-                                        fmode = stat.S_IMODE(os.lstat(
-                                            fpath).st_mode)
-                                        if fmode != self.file_mode:
+                                        try:
+                                                portable.assert_mode(fpath,
+                                                    self.file_mode)
+                                        except AssertionError, ae:
                                                 bad_modes.append((fpath,
                                                     "%o" % self.file_mode,
-                                                    "%o" % fmode))
+                                                    "%o" % ae.mode))
                                 else:
                                         os.chmod(fpath, self.file_mode)
                         except EnvironmentError, e:
@@ -310,13 +311,14 @@
                                 # If the mode change failed for another reason,
                                 # check to see if we actually needed to change
                                 # it, and if so, add it to bad_modes.
-                                fmode = stat.S_IMODE(os.lstat(
-                                    fpath).st_mode)
-                                if fmode != self.file_mode:
+                                try:
+                                        portable.assert_mode(fpath,
+                                            self.file_mode)
+                                except AssertionError, ae:
                                         bad_modes.append((fpath,
                                             "%o" % self.file_mode,
-                                            "%o" % fmode))
-
+                                            "%o" % ae.mode))
+ 
                 if bad_modes:
                         raise CatalogPermissionsException(bad_modes)
 
--- a/src/tests/api/t_catalog.py	Fri Oct 09 12:46:38 2009 +0100
+++ b/src/tests/api/t_catalog.py	Mon Oct 12 08:18:41 2009 -0500
@@ -521,16 +521,16 @@
                 c.save()
 
                 for fname in c.signatures:
-                        fs = os.lstat(os.path.join(c.meta_root, fname))
-                        self.assert_(stat.S_IMODE(fs.st_mode) == mode)
+                        fn = os.path.join(c.meta_root, fname)
+                        portable.assert_mode(fn, mode)
 
                 # Now test old catalog case.
                 for fname in c.signatures:
                         os.chmod(os.path.join(c.meta_root, fname), bad_mode)
                 c = catalog.Catalog(meta_root=cpath, log_updates=True)
                 for fname in c.signatures:
-                        fs = os.lstat(os.path.join(c.meta_root, fname))
-                        self.assert_(stat.S_IMODE(fs.st_mode) == mode)
+                        fn = os.path.join(c.meta_root, fname)
+                        portable.assert_mode(fn, mode)
 
                 # Need to add an fmri to it and then re-test the permissions
                 # since this causes the catalog file to be re-created.
@@ -539,8 +539,8 @@
                 c.save()
 
                 for fname in c.signatures:
-                        fs = os.lstat(os.path.join(c.meta_root, fname))
-                        self.assert_(stat.S_IMODE(fs.st_mode) == mode)
+                        fn = os.path.join(c.meta_root, fname)
+                        portable.assert_mode(fn, mode)
 
                 # Finally, test read_only old catalog case.
                 for fname in c.signatures: