scomd_tmpfiles.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # This file is part of free software (scomd); you can redistribute it and/or
  5. # modify it under the terms of the GNU Library General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Library General Public License for more details.
  13. """
  14. Pisilinux system for creation, deletion and cleaning of volatile and temporary files.
  15. """
  16. import os
  17. import re
  18. import sys
  19. import stat
  20. import shutil
  21. from pwd import getpwnam
  22. from grp import getgrnam
  23. # search files order
  24. DEFAULT_CONFIG_DIRS_SO = ["/etc/tmpfiles.d", "/run/tmpfiles.d", "/usr/lib/tmpfiles.d"]
  25. # execution order
  26. DEFAULT_CONFIG_DIRS_EO = ["/run/tmpfiles.d", "/usr/lib/tmpfiles.d", "/etc/tmpfiles.d"]
  27. def read_file(path):
  28. with open(path) as f:
  29. return f.read().strip()
  30. def write_file(path, content, mode = "w"):
  31. open(path, mode).write(content)
  32. def create(type, path, mode, uid, gid, age, arg):
  33. if type == "d" and \
  34. os.path.isdir(path) and \
  35. (not uid == os.stat(path).st_uid or not gid == os.stat(path).st_gid):
  36. type = "D"
  37. if type == "L":
  38. if not os.path.islink(path): os.symlink(arg, path)
  39. return
  40. elif type == "D":
  41. if os.path.isdir(path): shutil.rmtree(path)
  42. elif os.path.islink(path): os.remove(path)
  43. elif type == "c":
  44. if not os.path.isdir(os.path.dirname(path)):
  45. os.makedirs(os.path.dirname(path))
  46. if not os.path.exists(path):
  47. dev = [int(x) for x in arg.split(":")]
  48. os.mknod(path, mode|stat.S_IFCHR, os.makedev(dev[0], dev[1]))
  49. os.chown(path, uid, gid)
  50. if type.lower() == "d":
  51. if not os.path.isdir(path): os.makedirs(path, mode)
  52. os.chown(path, uid, gid)
  53. elif type in ["f", "F", "w"]:
  54. if not os.path.isfile(path) and type == "w": return
  55. if not os.path.isdir(os.path.dirname(path)):
  56. os.makedirs(os.path.dirname(path))
  57. os.chown(os.path.dirname(path), uid, gid)
  58. write_file(path, arg, mode = "a" if type == "f" else "w")
  59. os.chmod(path, mode)
  60. os.chown(path, uid, gid)
  61. USAGE = """\
  62. %s PATH(S)
  63. \tparsing specified .conf files.
  64. %s
  65. \tparsing .conf files in:
  66. \t%s
  67. """ % (sys.argv[0], sys.argv[0], "; ".join(DEFAULT_CONFIG_DIRS_SO))
  68. def usage():
  69. print(USAGE)
  70. sys.exit(0)
  71. if __name__ == "__main__":
  72. if ("-h" or "--help") in sys.argv: usage()
  73. boot = True if "--boot" in sys.argv else False
  74. config_files = {}
  75. errors = []
  76. def add_config_file(head, tail):
  77. try:
  78. config_files[head].append(tail)
  79. except KeyError:
  80. config_files[head] = [tail]
  81. if sys.argv[1:] and not boot:
  82. for arg in sys.argv[1:]:
  83. (head, tail) = os.path.split(arg)
  84. if not tail.endswith(".conf"): errors.append("%s is not .conf file" % tail)
  85. elif not head: errors.append("Full path is needed for %s args." % sys.argv[0])
  86. elif not os.path.isdir(head): errors.append("Path %s not exists." % head)
  87. elif not os.path.isfile(arg): errors.append("File %s not exists." % arg)
  88. add_config_file(head, tail)
  89. else:
  90. all_files_names = []
  91. for head in DEFAULT_CONFIG_DIRS_SO:
  92. if not os.path.isdir(head): continue
  93. ls = os.listdir(head)
  94. for tail in ls:
  95. # only .conf files and don't override (previous paths have higer priority)
  96. if not tail.endswith(".conf") or tail in all_files_names: continue
  97. all_files_names.append(tail)
  98. add_config_file(head, tail)
  99. if ls and "baselayout.conf" in config_files[head]:
  100. config_files[head].insert(0, config_files[head].pop(config_files[head].index("baselayout.conf")))
  101. for d in DEFAULT_CONFIG_DIRS_EO:
  102. try:
  103. fs = config_files[d]
  104. except KeyError:
  105. continue
  106. for f in fs:
  107. conf = read_file(os.path.join(d, f))
  108. # parse config file
  109. for line in [l for l in conf.split("\n") if l and not (l.startswith("#") or l.isspace())]:
  110. cerr = len(errors)
  111. fields = line.split()
  112. if len(fields) < 3: errors.append("%s is invalid .conf file. Not enough args in line: %s" % (os.path.join(d, f), line))
  113. if len(fields) < 7: fields.extend(["",] * (7 - len(fields)))
  114. elif len(fields) > 7: fields = fields[0:6] + [re.sub(".*?(%s)\s*$" % "\s+".join(fields[6:]), "\\1", line)]
  115. if fields[0] == "c" and not re.search("\d+:\d+", fields[6]): errors.append("%s - wrong argument for type 'c' in file: %s" % (fields[6], os.path.join(d, f)))
  116. for n, i in enumerate(fields):
  117. if i == "-": fields[n] = ""
  118. if not fields[3]: fields[3] = "root"
  119. if not fields[4]: fields[4] = "root"
  120. if fields[0].endswith("!"):
  121. if not boot: continue
  122. else: fields[0] = fields[0].replace("!", "")
  123. if not fields[0] in ["c", "d", "D", "f", "F", "L", "w"]: errors.append("%s - wrong type in file: %s" % (fields[0], os.path.join(d, f)))
  124. elif fields[0] == "L":
  125. if not fields[6]: errors.append("No arg for type 'L' specified in file: %s" % os.path.join(d, f))
  126. elif not os.path.exists(fields[6]): errors.append("%s - wrong path in file: %s" % (fields[6], os.path.join(d, f)))
  127. elif fields[0] in ["f", "F", "w"] and os.path.isdir(fields[1]): errors.append("Cannot write to file. %s is directory." % fields[1])
  128. else:
  129. if not fields[2]: errors.append("No mode specified in file: %s" % os.path.join(d, f))
  130. elif not re.search("^\d{3,4}$", fields[2]): errors.append("%s - wrong mode in file: %s" % (fields[2], os.path.join(d, f)))
  131. else: fields[2] = int(fields[2], 8)
  132. try:
  133. fields[3] = getpwnam(fields[3]).pw_uid
  134. except KeyError:
  135. errors.append("User %s not exists (%s)" % (fields[3], os.path.join(d, f)))
  136. try:
  137. fields[4] = getgrnam(fields[4]).gr_gid
  138. except KeyError:
  139. errors.append("Group %s not exists (%s)" % (fields[4], os.path.join(d, f)))
  140. #
  141. if len(errors) == cerr: create(*fields)
  142. print("\n".join(errors))