summaryrefslogtreecommitdiffstats
path: root/meta/classes-global/package.bbclass
diff options
context:
space:
mode:
authorRichard Purdie <richard.purdie@linuxfoundation.org>2022-11-24 22:10:40 +0000
committerRichard Purdie <richard.purdie@linuxfoundation.org>2023-01-05 11:52:50 +0000
commited07d52b476a22959cdd1c61d1c396345f996bbf (patch)
treee40a1d120058b4d474def57ca9b0e8e924aea212 /meta/classes-global/package.bbclass
parenta19687acd12497d727203e63d74b2703387f34a6 (diff)
downloadpoky-ed07d52b476a22959cdd1c61d1c396345f996bbf.tar.gz
package: Move fixup_perms function to bb function library
To avoid reparsing the bbclass code all the time, move the function to the python function library code which is more efficient. (From OE-Core rev: 7c8c4cfd9355a9cee1144e0444e1b54402f1951c) Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/classes-global/package.bbclass')
-rw-r--r--meta/classes-global/package.bbclass238
1 files changed, 1 insertions, 237 deletions
diff --git a/meta/classes-global/package.bbclass b/meta/classes-global/package.bbclass
index 7a0a428b30..29f0c80abd 100644
--- a/meta/classes-global/package.bbclass
+++ b/meta/classes-global/package.bbclass
@@ -861,244 +861,8 @@ python perform_packagecopy () {
861perform_packagecopy[cleandirs] = "${PKGD}" 861perform_packagecopy[cleandirs] = "${PKGD}"
862perform_packagecopy[dirs] = "${PKGD}" 862perform_packagecopy[dirs] = "${PKGD}"
863 863
864# We generate a master list of directories to process, we start by
865# seeding this list with reasonable defaults, then load from
866# the fs-perms.txt files
867python fixup_perms () { 864python fixup_perms () {
868 import pwd, grp 865 oe.package.fixup_perms(d)
869
870 # init using a string with the same format as a line as documented in
871 # the fs-perms.txt file
872 # <path> <mode> <uid> <gid> <walk> <fmode> <fuid> <fgid>
873 # <path> link <link target>
874 #
875 # __str__ can be used to print out an entry in the input format
876 #
877 # if fs_perms_entry.path is None:
878 # an error occurred
879 # if fs_perms_entry.link, you can retrieve:
880 # fs_perms_entry.path = path
881 # fs_perms_entry.link = target of link
882 # if not fs_perms_entry.link, you can retrieve:
883 # fs_perms_entry.path = path
884 # fs_perms_entry.mode = expected dir mode or None
885 # fs_perms_entry.uid = expected uid or -1
886 # fs_perms_entry.gid = expected gid or -1
887 # fs_perms_entry.walk = 'true' or something else
888 # fs_perms_entry.fmode = expected file mode or None
889 # fs_perms_entry.fuid = expected file uid or -1
890 # fs_perms_entry_fgid = expected file gid or -1
891 class fs_perms_entry():
892 def __init__(self, line):
893 lsplit = line.split()
894 if len(lsplit) == 3 and lsplit[1].lower() == "link":
895 self._setlink(lsplit[0], lsplit[2])
896 elif len(lsplit) == 8:
897 self._setdir(lsplit[0], lsplit[1], lsplit[2], lsplit[3], lsplit[4], lsplit[5], lsplit[6], lsplit[7])
898 else:
899 msg = "Fixup Perms: invalid config line %s" % line
900 oe.qa.handle_error("perm-config", msg, d)
901 self.path = None
902 self.link = None
903
904 def _setdir(self, path, mode, uid, gid, walk, fmode, fuid, fgid):
905 self.path = os.path.normpath(path)
906 self.link = None
907 self.mode = self._procmode(mode)
908 self.uid = self._procuid(uid)
909 self.gid = self._procgid(gid)
910 self.walk = walk.lower()
911 self.fmode = self._procmode(fmode)
912 self.fuid = self._procuid(fuid)
913 self.fgid = self._procgid(fgid)
914
915 def _setlink(self, path, link):
916 self.path = os.path.normpath(path)
917 self.link = link
918
919 def _procmode(self, mode):
920 if not mode or (mode and mode == "-"):
921 return None
922 else:
923 return int(mode,8)
924
925 # Note uid/gid -1 has special significance in os.lchown
926 def _procuid(self, uid):
927 if uid is None or uid == "-":
928 return -1
929 elif uid.isdigit():
930 return int(uid)
931 else:
932 return pwd.getpwnam(uid).pw_uid
933
934 def _procgid(self, gid):
935 if gid is None or gid == "-":
936 return -1
937 elif gid.isdigit():
938 return int(gid)
939 else:
940 return grp.getgrnam(gid).gr_gid
941
942 # Use for debugging the entries
943 def __str__(self):
944 if self.link:
945 return "%s link %s" % (self.path, self.link)
946 else:
947 mode = "-"
948 if self.mode:
949 mode = "0%o" % self.mode
950 fmode = "-"
951 if self.fmode:
952 fmode = "0%o" % self.fmode
953 uid = self._mapugid(self.uid)
954 gid = self._mapugid(self.gid)
955 fuid = self._mapugid(self.fuid)
956 fgid = self._mapugid(self.fgid)
957 return "%s %s %s %s %s %s %s %s" % (self.path, mode, uid, gid, self.walk, fmode, fuid, fgid)
958
959 def _mapugid(self, id):
960 if id is None or id == -1:
961 return "-"
962 else:
963 return "%d" % id
964
965 # Fix the permission, owner and group of path
966 def fix_perms(path, mode, uid, gid, dir):
967 if mode and not os.path.islink(path):
968 #bb.note("Fixup Perms: chmod 0%o %s" % (mode, dir))
969 os.chmod(path, mode)
970 # -1 is a special value that means don't change the uid/gid
971 # if they are BOTH -1, don't bother to lchown
972 if not (uid == -1 and gid == -1):
973 #bb.note("Fixup Perms: lchown %d:%d %s" % (uid, gid, dir))
974 os.lchown(path, uid, gid)
975
976 # Return a list of configuration files based on either the default
977 # files/fs-perms.txt or the contents of FILESYSTEM_PERMS_TABLES
978 # paths are resolved via BBPATH
979 def get_fs_perms_list(d):
980 str = ""
981 bbpath = d.getVar('BBPATH')
982 fs_perms_tables = d.getVar('FILESYSTEM_PERMS_TABLES') or ""
983 for conf_file in fs_perms_tables.split():
984 confpath = bb.utils.which(bbpath, conf_file)
985 if confpath:
986 str += " %s" % bb.utils.which(bbpath, conf_file)
987 else:
988 bb.warn("cannot find %s specified in FILESYSTEM_PERMS_TABLES" % conf_file)
989 return str
990
991
992
993 dvar = d.getVar('PKGD')
994
995 fs_perms_table = {}
996 fs_link_table = {}
997
998 # By default all of the standard directories specified in
999 # bitbake.conf will get 0755 root:root.
1000 target_path_vars = [ 'base_prefix',
1001 'prefix',
1002 'exec_prefix',
1003 'base_bindir',
1004 'base_sbindir',
1005 'base_libdir',
1006 'datadir',
1007 'sysconfdir',
1008 'servicedir',
1009 'sharedstatedir',
1010 'localstatedir',
1011 'infodir',
1012 'mandir',
1013 'docdir',
1014 'bindir',
1015 'sbindir',
1016 'libexecdir',
1017 'libdir',
1018 'includedir',
1019 'oldincludedir' ]
1020
1021 for path in target_path_vars:
1022 dir = d.getVar(path) or ""
1023 if dir == "":
1024 continue
1025 fs_perms_table[dir] = fs_perms_entry(d.expand("%s 0755 root root false - - -" % (dir)))
1026
1027 # Now we actually load from the configuration files
1028 for conf in get_fs_perms_list(d).split():
1029 if not os.path.exists(conf):
1030 continue
1031 with open(conf) as f:
1032 for line in f:
1033 if line.startswith('#'):
1034 continue
1035 lsplit = line.split()
1036 if len(lsplit) == 0:
1037 continue
1038 if len(lsplit) != 8 and not (len(lsplit) == 3 and lsplit[1].lower() == "link"):
1039 msg = "Fixup perms: %s invalid line: %s" % (conf, line)
1040 oe.qa.handle_error("perm-line", msg, d)
1041 continue
1042 entry = fs_perms_entry(d.expand(line))
1043 if entry and entry.path:
1044 if entry.link:
1045 fs_link_table[entry.path] = entry
1046 if entry.path in fs_perms_table:
1047 fs_perms_table.pop(entry.path)
1048 else:
1049 fs_perms_table[entry.path] = entry
1050 if entry.path in fs_link_table:
1051 fs_link_table.pop(entry.path)
1052
1053 # Debug -- list out in-memory table
1054 #for dir in fs_perms_table:
1055 # bb.note("Fixup Perms: %s: %s" % (dir, str(fs_perms_table[dir])))
1056 #for link in fs_link_table:
1057 # bb.note("Fixup Perms: %s: %s" % (link, str(fs_link_table[link])))
1058
1059 # We process links first, so we can go back and fixup directory ownership
1060 # for any newly created directories
1061 # Process in sorted order so /run gets created before /run/lock, etc.
1062 for entry in sorted(fs_link_table.values(), key=lambda x: x.link):
1063 link = entry.link
1064 dir = entry.path
1065 origin = dvar + dir
1066 if not (cpath.exists(origin) and cpath.isdir(origin) and not cpath.islink(origin)):
1067 continue
1068
1069 if link[0] == "/":
1070 target = dvar + link
1071 ptarget = link
1072 else:
1073 target = os.path.join(os.path.dirname(origin), link)
1074 ptarget = os.path.join(os.path.dirname(dir), link)
1075 if os.path.exists(target):
1076 msg = "Fixup Perms: Unable to correct directory link, target already exists: %s -> %s" % (dir, ptarget)
1077 oe.qa.handle_error("perm-link", msg, d)
1078 continue
1079
1080 # Create path to move directory to, move it, and then setup the symlink
1081 bb.utils.mkdirhier(os.path.dirname(target))
1082 #bb.note("Fixup Perms: Rename %s -> %s" % (dir, ptarget))
1083 bb.utils.rename(origin, target)
1084 #bb.note("Fixup Perms: Link %s -> %s" % (dir, link))
1085 os.symlink(link, origin)
1086
1087 for dir in fs_perms_table:
1088 origin = dvar + dir
1089 if not (cpath.exists(origin) and cpath.isdir(origin)):
1090 continue
1091
1092 fix_perms(origin, fs_perms_table[dir].mode, fs_perms_table[dir].uid, fs_perms_table[dir].gid, dir)
1093
1094 if fs_perms_table[dir].walk == 'true':
1095 for root, dirs, files in os.walk(origin):
1096 for dr in dirs:
1097 each_dir = os.path.join(root, dr)
1098 fix_perms(each_dir, fs_perms_table[dir].mode, fs_perms_table[dir].uid, fs_perms_table[dir].gid, dir)
1099 for f in files:
1100 each_file = os.path.join(root, f)
1101 fix_perms(each_file, fs_perms_table[dir].fmode, fs_perms_table[dir].fuid, fs_perms_table[dir].fgid, dir)
1102} 866}
1103 867
1104def package_debug_vars(d): 868def package_debug_vars(d):