summaryrefslogtreecommitdiffstats
path: root/meta
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
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')
-rw-r--r--meta/classes-global/package.bbclass238
-rw-r--r--meta/lib/oe/package.py242
2 files changed, 243 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):
diff --git a/meta/lib/oe/package.py b/meta/lib/oe/package.py
index 4aa40d70ee..05447f8808 100644
--- a/meta/lib/oe/package.py
+++ b/meta/lib/oe/package.py
@@ -292,3 +292,245 @@ def read_shlib_providers(d):
292 shlib_provider[s[0]] = {} 292 shlib_provider[s[0]] = {}
293 shlib_provider[s[0]][s[1]] = (dep_pkg, s[2]) 293 shlib_provider[s[0]][s[1]] = (dep_pkg, s[2])
294 return shlib_provider 294 return shlib_provider
295
296# We generate a master list of directories to process, we start by
297# seeding this list with reasonable defaults, then load from
298# the fs-perms.txt files
299def fixup_perms(d):
300 import pwd, grp
301 import oe.cachedpath
302
303 cpath = oe.cachedpath.CachedPath()
304 dvar = d.getVar('PKGD')
305
306 # init using a string with the same format as a line as documented in
307 # the fs-perms.txt file
308 # <path> <mode> <uid> <gid> <walk> <fmode> <fuid> <fgid>
309 # <path> link <link target>
310 #
311 # __str__ can be used to print out an entry in the input format
312 #
313 # if fs_perms_entry.path is None:
314 # an error occurred
315 # if fs_perms_entry.link, you can retrieve:
316 # fs_perms_entry.path = path
317 # fs_perms_entry.link = target of link
318 # if not fs_perms_entry.link, you can retrieve:
319 # fs_perms_entry.path = path
320 # fs_perms_entry.mode = expected dir mode or None
321 # fs_perms_entry.uid = expected uid or -1
322 # fs_perms_entry.gid = expected gid or -1
323 # fs_perms_entry.walk = 'true' or something else
324 # fs_perms_entry.fmode = expected file mode or None
325 # fs_perms_entry.fuid = expected file uid or -1
326 # fs_perms_entry_fgid = expected file gid or -1
327 class fs_perms_entry():
328 def __init__(self, line):
329 lsplit = line.split()
330 if len(lsplit) == 3 and lsplit[1].lower() == "link":
331 self._setlink(lsplit[0], lsplit[2])
332 elif len(lsplit) == 8:
333 self._setdir(lsplit[0], lsplit[1], lsplit[2], lsplit[3], lsplit[4], lsplit[5], lsplit[6], lsplit[7])
334 else:
335 msg = "Fixup Perms: invalid config line %s" % line
336 oe.qa.handle_error("perm-config", msg, d)
337 self.path = None
338 self.link = None
339
340 def _setdir(self, path, mode, uid, gid, walk, fmode, fuid, fgid):
341 self.path = os.path.normpath(path)
342 self.link = None
343 self.mode = self._procmode(mode)
344 self.uid = self._procuid(uid)
345 self.gid = self._procgid(gid)
346 self.walk = walk.lower()
347 self.fmode = self._procmode(fmode)
348 self.fuid = self._procuid(fuid)
349 self.fgid = self._procgid(fgid)
350
351 def _setlink(self, path, link):
352 self.path = os.path.normpath(path)
353 self.link = link
354
355 def _procmode(self, mode):
356 if not mode or (mode and mode == "-"):
357 return None
358 else:
359 return int(mode,8)
360
361 # Note uid/gid -1 has special significance in os.lchown
362 def _procuid(self, uid):
363 if uid is None or uid == "-":
364 return -1
365 elif uid.isdigit():
366 return int(uid)
367 else:
368 return pwd.getpwnam(uid).pw_uid
369
370 def _procgid(self, gid):
371 if gid is None or gid == "-":
372 return -1
373 elif gid.isdigit():
374 return int(gid)
375 else:
376 return grp.getgrnam(gid).gr_gid
377
378 # Use for debugging the entries
379 def __str__(self):
380 if self.link:
381 return "%s link %s" % (self.path, self.link)
382 else:
383 mode = "-"
384 if self.mode:
385 mode = "0%o" % self.mode
386 fmode = "-"
387 if self.fmode:
388 fmode = "0%o" % self.fmode
389 uid = self._mapugid(self.uid)
390 gid = self._mapugid(self.gid)
391 fuid = self._mapugid(self.fuid)
392 fgid = self._mapugid(self.fgid)
393 return "%s %s %s %s %s %s %s %s" % (self.path, mode, uid, gid, self.walk, fmode, fuid, fgid)
394
395 def _mapugid(self, id):
396 if id is None or id == -1:
397 return "-"
398 else:
399 return "%d" % id
400
401 # Fix the permission, owner and group of path
402 def fix_perms(path, mode, uid, gid, dir):
403 if mode and not os.path.islink(path):
404 #bb.note("Fixup Perms: chmod 0%o %s" % (mode, dir))
405 os.chmod(path, mode)
406 # -1 is a special value that means don't change the uid/gid
407 # if they are BOTH -1, don't bother to lchown
408 if not (uid == -1 and gid == -1):
409 #bb.note("Fixup Perms: lchown %d:%d %s" % (uid, gid, dir))
410 os.lchown(path, uid, gid)
411
412 # Return a list of configuration files based on either the default
413 # files/fs-perms.txt or the contents of FILESYSTEM_PERMS_TABLES
414 # paths are resolved via BBPATH
415 def get_fs_perms_list(d):
416 str = ""
417 bbpath = d.getVar('BBPATH')
418 fs_perms_tables = d.getVar('FILESYSTEM_PERMS_TABLES') or ""
419 for conf_file in fs_perms_tables.split():
420 confpath = bb.utils.which(bbpath, conf_file)
421 if confpath:
422 str += " %s" % bb.utils.which(bbpath, conf_file)
423 else:
424 bb.warn("cannot find %s specified in FILESYSTEM_PERMS_TABLES" % conf_file)
425 return str
426
427 fs_perms_table = {}
428 fs_link_table = {}
429
430 # By default all of the standard directories specified in
431 # bitbake.conf will get 0755 root:root.
432 target_path_vars = [ 'base_prefix',
433 'prefix',
434 'exec_prefix',
435 'base_bindir',
436 'base_sbindir',
437 'base_libdir',
438 'datadir',
439 'sysconfdir',
440 'servicedir',
441 'sharedstatedir',
442 'localstatedir',
443 'infodir',
444 'mandir',
445 'docdir',
446 'bindir',
447 'sbindir',
448 'libexecdir',
449 'libdir',
450 'includedir',
451 'oldincludedir' ]
452
453 for path in target_path_vars:
454 dir = d.getVar(path) or ""
455 if dir == "":
456 continue
457 fs_perms_table[dir] = fs_perms_entry(d.expand("%s 0755 root root false - - -" % (dir)))
458
459 # Now we actually load from the configuration files
460 for conf in get_fs_perms_list(d).split():
461 if not os.path.exists(conf):
462 continue
463 with open(conf) as f:
464 for line in f:
465 if line.startswith('#'):
466 continue
467 lsplit = line.split()
468 if len(lsplit) == 0:
469 continue
470 if len(lsplit) != 8 and not (len(lsplit) == 3 and lsplit[1].lower() == "link"):
471 msg = "Fixup perms: %s invalid line: %s" % (conf, line)
472 oe.qa.handle_error("perm-line", msg, d)
473 continue
474 entry = fs_perms_entry(d.expand(line))
475 if entry and entry.path:
476 if entry.link:
477 fs_link_table[entry.path] = entry
478 if entry.path in fs_perms_table:
479 fs_perms_table.pop(entry.path)
480 else:
481 fs_perms_table[entry.path] = entry
482 if entry.path in fs_link_table:
483 fs_link_table.pop(entry.path)
484
485 # Debug -- list out in-memory table
486 #for dir in fs_perms_table:
487 # bb.note("Fixup Perms: %s: %s" % (dir, str(fs_perms_table[dir])))
488 #for link in fs_link_table:
489 # bb.note("Fixup Perms: %s: %s" % (link, str(fs_link_table[link])))
490
491 # We process links first, so we can go back and fixup directory ownership
492 # for any newly created directories
493 # Process in sorted order so /run gets created before /run/lock, etc.
494 for entry in sorted(fs_link_table.values(), key=lambda x: x.link):
495 link = entry.link
496 dir = entry.path
497 origin = dvar + dir
498 if not (cpath.exists(origin) and cpath.isdir(origin) and not cpath.islink(origin)):
499 continue
500
501 if link[0] == "/":
502 target = dvar + link
503 ptarget = link
504 else:
505 target = os.path.join(os.path.dirname(origin), link)
506 ptarget = os.path.join(os.path.dirname(dir), link)
507 if os.path.exists(target):
508 msg = "Fixup Perms: Unable to correct directory link, target already exists: %s -> %s" % (dir, ptarget)
509 oe.qa.handle_error("perm-link", msg, d)
510 continue
511
512 # Create path to move directory to, move it, and then setup the symlink
513 bb.utils.mkdirhier(os.path.dirname(target))
514 #bb.note("Fixup Perms: Rename %s -> %s" % (dir, ptarget))
515 bb.utils.rename(origin, target)
516 #bb.note("Fixup Perms: Link %s -> %s" % (dir, link))
517 os.symlink(link, origin)
518
519 for dir in fs_perms_table:
520 origin = dvar + dir
521 if not (cpath.exists(origin) and cpath.isdir(origin)):
522 continue
523
524 fix_perms(origin, fs_perms_table[dir].mode, fs_perms_table[dir].uid, fs_perms_table[dir].gid, dir)
525
526 if fs_perms_table[dir].walk == 'true':
527 for root, dirs, files in os.walk(origin):
528 for dr in dirs:
529 each_dir = os.path.join(root, dr)
530 fix_perms(each_dir, fs_perms_table[dir].mode, fs_perms_table[dir].uid, fs_perms_table[dir].gid, dir)
531 for f in files:
532 each_file = os.path.join(root, f)
533 fix_perms(each_file, fs_perms_table[dir].fmode, fs_perms_table[dir].fuid, fs_perms_table[dir].fgid, dir)
534
535
536