3 """ Various different sanity checks
5 @contact: Debian FTP Master <ftpmaster@debian.org>
6 @copyright: (C) 2000, 2001, 2002, 2003, 2004, 2006 James Troup <james@nocrew.org>
7 @license: GNU General Public License version 2 or later
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 ################################################################################
26 # And, lo, a great and menacing voice rose from the depths, and with
27 # great wrath and vehemence it's voice boomed across the
28 # land... ``hehehehehehe... that *tickles*''
31 ################################################################################
41 from daklib.dbconn import *
42 from daklib import utils
43 from daklib.config import Config
44 from daklib.dak_exceptions import InvalidDscError, ChangesUnicodeError, CantOpenError
46 ################################################################################
48 db_files = {} #: Cache of filenames as known by the database
49 waste = 0.0 #: How many bytes are "wasted" by files not referenced in database
50 excluded = {} #: List of files which are excluded from files check
53 current_time = time.time() #: now()
55 ################################################################################
57 def usage(exit_code=0):
58 print """Usage: dak check-archive MODE
59 Run various sanity checks of the archive and/or database.
61 -h, --help show this help and exit.
63 The following MODEs are available:
65 checksums - validate the checksums stored in the database
66 files - check files in the database against what's in the archive
67 dsc-syntax - validate the syntax of .dsc files in the archive
68 missing-overrides - check for missing overrides
69 source-in-one-dir - ensure the source for each package is in one directory
70 timestamps - check for future timestamps in .deb's
71 files-in-dsc - ensure each .dsc references appropriate Files
72 validate-indices - ensure files mentioned in Packages & Sources exist
73 files-not-symlinks - check files in the database aren't symlinks
74 validate-builddeps - validate build-dependencies of .dsc files in the archive
75 add-missing-source-checksums - add missing checksums for source packages
79 ################################################################################
81 def process_dir (unused, dirname, filenames):
83 Process a directory and output every files name which is not listed already
84 in the C{filenames} or global C{excluded} dictionaries.
87 @param dirname: the directory to look at
90 @param filenames: Known filenames to ignore
92 global waste, db_files, excluded
94 if dirname.find('/disks-') != -1 or dirname.find('upgrade-') != -1:
96 # hack; can't handle .changes files
97 if dirname.find('proposed-updates') != -1:
99 for name in filenames:
100 filename = os.path.abspath(os.path.join(dirname,name))
101 if os.path.isfile(filename) and not os.path.islink(filename) and not db_files.has_key(filename) and not excluded.has_key(filename):
102 waste += os.stat(filename)[stat.ST_SIZE]
103 print "%s" % (filename)
105 ################################################################################
109 Prepare the dictionary of existing filenames, then walk through the archive
110 pool/ directory to compare it.
113 session = DBConn().session()
116 SELECT archive.name, suite.suite_name, f.filename
118 JOIN bin_associations ba ON b.id = ba.bin
119 JOIN suite ON ba.suite = suite.id
120 JOIN archive ON suite.archive_id = archive.id
121 JOIN files f ON b.file = f.id
122 WHERE NOT EXISTS (SELECT 1 FROM files_archive_map af
123 WHERE af.archive_id = suite.archive_id
124 AND af.file_id = b.file)
125 ORDER BY archive.name, suite.suite_name, f.filename
127 for row in session.execute(query):
128 print "MISSING-ARCHIVE-FILE {0} {1} {2}".vformat(row)
131 SELECT archive.name, suite.suite_name, f.filename
133 JOIN src_associations sa ON s.id = sa.source
134 JOIN suite ON sa.suite = suite.id
135 JOIN archive ON suite.archive_id = archive.id
136 JOIN dsc_files df ON s.id = df.source
137 JOIN files f ON df.file = f.id
138 WHERE NOT EXISTS (SELECT 1 FROM files_archive_map af
139 WHERE af.archive_id = suite.archive_id
140 AND af.file_id = df.file)
141 ORDER BY archive.name, suite.suite_name, f.filename
143 for row in session.execute(query):
144 print "MISSING-ARCHIVE-FILE {0} {1} {2}".vformat(row)
146 archive_files = session.query(ArchiveFile) \
147 .join(ArchiveFile.archive).join(ArchiveFile.file) \
148 .order_by(Archive.archive_name, PoolFile.filename)
150 expected_files = set()
151 for af in archive_files:
153 expected_files.add(af.path)
154 if not os.path.exists(path):
155 print "MISSING-FILE {0} {1} {2}".format(af.archive.archive_name, af.file.filename, path)
157 archives = session.query(Archive).order_by(Archive.archive_name)
160 top = os.path.join(a.path, 'pool')
161 for dirpath, dirnames, filenames in os.walk(top):
163 path = os.path.join(dirpath, fn)
164 if path in expected_files:
166 print "UNEXPECTED-FILE {0} {1}".format(a.archive_name, path)
168 ################################################################################
172 Parse every .dsc file in the archive and check for it's validity.
177 for src in DBConn().session().query(DBSource).order_by(DBSource.source, DBSource.version):
178 f = src.poolfile.fullpath
180 utils.parse_changes(f, signing_rules=1, dsc_file=1)
181 except InvalidDscError:
182 utils.warn("syntax error in .dsc file %s" % f)
184 except ChangesUnicodeError:
185 utils.warn("found invalid dsc file (%s), not properly utf-8 encoded" % f)
187 except CantOpenError:
188 utils.warn("missing dsc file (%s)" % f)
190 except Exception as e:
191 utils.warn("miscellaneous error parsing dsc file (%s): %s" % (f, str(e)))
195 utils.warn("Found %s invalid .dsc files." % (count))
197 ################################################################################
199 def check_override():
201 Check for missing overrides in stable and unstable.
203 session = DBConn().session()
205 for suite_name in [ "stable", "unstable" ]:
207 print "-" * len(suite_name)
209 suite = get_suite(suite_name)
210 q = session.execute("""
211 SELECT DISTINCT b.package FROM binaries b, bin_associations ba
212 WHERE b.id = ba.bin AND ba.suite = :suiteid AND NOT EXISTS
213 (SELECT 1 FROM override o WHERE o.suite = :suiteid AND o.package = b.package)"""
214 % {'suiteid': suite.suite_id})
216 for j in q.fetchall():
219 q = session.execute("""
220 SELECT DISTINCT s.source FROM source s, src_associations sa
221 WHERE s.id = sa.source AND sa.suite = :suiteid AND NOT EXISTS
222 (SELECT 1 FROM override o WHERE o.suite = :suiteid and o.package = s.source)"""
223 % {'suiteid': suite.suite_id})
224 for j in q.fetchall():
227 ################################################################################
230 def check_source_in_one_dir():
232 Ensure that the source files for any given package is all in one
233 directory so that 'apt-get source' works...
236 # Not the most enterprising method, but hey...
239 session = DBConn().session()
241 q = session.query(DBSource)
247 qf = session.query(PoolFile).join(Location).join(DSCFile).filter_by(source_id=s.source_id)
251 filename = os.path.join(f.location.path, f.filename)
252 path = os.path.dirname(filename)
256 first_filename = filename
257 elif first_path != path:
258 symlink = path + '/' + os.path.basename(first_filename)
259 if not os.path.exists(symlink):
261 print "WOAH, we got a live one here... %s [%s] {%s}" % (filename, s.source_id, symlink)
265 print "Found %d source packages where the source is not all in one directory." % (broken_count)
267 ################################################################################
268 def check_checksums():
272 print "Getting file information from database..."
273 q = DBConn().session().query(PoolFile)
275 print "Checking file checksums & sizes..."
277 filename = f.fullpath
280 fi = utils.open_file(filename)
282 utils.warn("can't open '%s'." % (filename))
285 size = os.stat(filename)[stat.ST_SIZE]
286 if size != f.filesize:
287 utils.warn("**WARNING** size mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, size, f.filesize))
289 md5sum = apt_pkg.md5sum(fi)
290 if md5sum != f.md5sum:
291 utils.warn("**WARNING** md5sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, md5sum, f.md5sum))
294 sha1sum = apt_pkg.sha1sum(fi)
295 if sha1sum != f.sha1sum:
296 utils.warn("**WARNING** sha1sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, sha1sum, f.sha1sum))
299 sha256sum = apt_pkg.sha256sum(fi)
300 if sha256sum != f.sha256sum:
301 utils.warn("**WARNING** sha256sum mismatch for '%s' ('%s' [current] vs. '%s' [db])." % (filename, sha256sum, f.sha256sum))
305 ################################################################################
308 def Ent(Kind,Name,Link,Mode,UID,GID,Size,MTime,Major,Minor):
311 if MTime > current_time:
312 future_files[current_file] = MTime
313 print "%s: %s '%s','%s',%u,%u,%u,%u,%u,%u,%u" % (current_file, Kind,Name,Link,Mode,UID,GID,Size, MTime, Major, Minor)
315 def check_timestamps():
317 Check all files for timestamps in the future; common from hardware
318 (e.g. alpha) which have far-future dates as their default dates.
323 q = DBConn().session().query(PoolFile).filter(PoolFile.filename.like('.deb$'))
329 filename = os.path.abspath(os.path.join(pf.location.path, pf.filename))
330 if os.access(filename, os.R_OK):
331 f = utils.open_file(filename)
332 current_file = filename
333 sys.stderr.write("Processing %s.\n" % (filename))
334 apt_inst.debExtract(f, Ent, "control.tar.gz")
336 apt_inst.debExtract(f, Ent, "data.tar.gz")
339 print "Checked %d files (out of %d)." % (count, len(db_files.keys()))
341 ################################################################################
343 def check_files_in_dsc():
345 Ensure each .dsc lists appropriate files in its Files field (according
346 to the format announced in its Format field).
350 print "Building list of database files..."
351 q = DBConn().session().query(PoolFile).filter(PoolFile.filename.like('.dsc$'))
354 print "Checking %d files..." % len(ql)
356 print "No files to check."
359 filename = os.path.abspath(os.path.join(pf.location.path + pf.filename))
362 # NB: don't enforce .dsc syntax
363 dsc = utils.parse_changes(filename, dsc_file=1)
365 utils.fubar("error parsing .dsc file '%s'." % (filename))
367 reasons = utils.check_dsc_files(filename, dsc)
375 utils.warn("Found %s invalid .dsc files." % (count))
378 ################################################################################
380 def validate_sources(suite, component):
382 Ensure files mentioned in Sources exist
384 filename = "%s/dists/%s/%s/source/Sources.gz" % (Cnf["Dir::Root"], suite, component)
385 print "Processing %s..." % (filename)
386 # apt_pkg.TagFile needs a real file handle and can't handle a GzipFile instance...
387 (fd, temp_filename) = utils.temp_filename()
388 (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename))
390 sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
392 sources = utils.open_file(temp_filename)
393 Sources = apt_pkg.TagFile(sources)
394 while Sources.step():
395 source = Sources.section.find('Package')
396 directory = Sources.section.find('Directory')
397 files = Sources.section.find('Files')
398 for i in files.split('\n'):
399 (md5, size, name) = i.split()
400 filename = "%s/%s/%s" % (Cnf["Dir::Root"], directory, name)
401 if not os.path.exists(filename):
402 if directory.find("potato") == -1:
403 print "W: %s missing." % (filename)
405 pool_location = utils.poolify (source, component)
406 pool_filename = "%s/%s/%s" % (Cnf["Dir::Pool"], pool_location, name)
407 if not os.path.exists(pool_filename):
408 print "E: %s missing (%s)." % (filename, pool_filename)
411 pool_filename = os.path.normpath(pool_filename)
412 filename = os.path.normpath(filename)
413 src = utils.clean_symlink(pool_filename, filename, Cnf["Dir::Root"])
414 print "Symlinking: %s -> %s" % (filename, src)
415 #os.symlink(src, filename)
417 os.unlink(temp_filename)
419 ########################################
421 def validate_packages(suite, component, architecture):
423 Ensure files mentioned in Packages exist
425 filename = "%s/dists/%s/%s/binary-%s/Packages.gz" \
426 % (Cnf["Dir::Root"], suite, component, architecture)
427 print "Processing %s..." % (filename)
428 # apt_pkg.TagFile needs a real file handle and can't handle a GzipFile instance...
429 (fd, temp_filename) = utils.temp_filename()
430 (result, output) = commands.getstatusoutput("gunzip -c %s > %s" % (filename, temp_filename))
432 sys.stderr.write("Gunzip invocation failed!\n%s\n" % (output))
434 packages = utils.open_file(temp_filename)
435 Packages = apt_pkg.TagFile(packages)
436 while Packages.step():
437 filename = "%s/%s" % (Cnf["Dir::Root"], Packages.section.find('Filename'))
438 if not os.path.exists(filename):
439 print "W: %s missing." % (filename)
441 os.unlink(temp_filename)
443 ########################################
445 def check_indices_files_exist():
447 Ensure files mentioned in Packages & Sources exist
449 for suite in [ "stable", "testing", "unstable" ]:
450 for component in get_component_names():
451 architectures = get_suite_architectures(suite)
452 for arch in [ i.arch_string.lower() for i in architectures ]:
454 validate_sources(suite, component)
458 validate_packages(suite, component, arch)
460 ################################################################################
462 def check_files_not_symlinks():
464 Check files in the database aren't symlinks
466 print "Building list of database files... ",
468 q = DBConn().session().query(PoolFile).filter(PoolFile.filename.like('.dsc$'))
471 filename = os.path.abspath(os.path.join(pf.location.path, pf.filename))
472 if os.access(filename, os.R_OK) == 0:
473 utils.warn("%s: doesn't exist." % (filename))
475 if os.path.islink(filename):
476 utils.warn("%s: is a symlink." % (filename))
478 ################################################################################
480 def chk_bd_process_dir (unused, dirname, filenames):
481 for name in filenames:
482 if not name.endswith(".dsc"):
484 filename = os.path.abspath(dirname+'/'+name)
485 dsc = utils.parse_changes(filename, dsc_file=1)
486 for field_name in [ "build-depends", "build-depends-indep" ]:
487 field = dsc.get(field_name)
490 apt_pkg.parse_src_depends(field)
492 print "E: [%s] %s: %s" % (filename, field_name, field)
495 ################################################################################
497 def check_build_depends():
498 """ Validate build-dependencies of .dsc files in the archive """
500 os.path.walk(cnf["Dir::Root"], chk_bd_process_dir, None)
502 ################################################################################
504 _add_missing_source_checksums_query = R"""
505 INSERT INTO source_metadata
506 (src_id, key_id, value)
511 (SELECT STRING_AGG(' ' || tmp.checksum || ' ' || tmp.size || ' ' || tmp.basename, E'\n' ORDER BY tmp.basename)
515 WHEN 'Files' THEN f.md5sum
516 WHEN 'Checksums-Sha1' THEN f.sha1sum
517 WHEN 'Checksums-Sha256' THEN f.sha256sum
520 SUBSTRING(f.filename FROM E'/([^/]*)\\Z') AS basename
521 FROM files f JOIN dsc_files ON f.id = dsc_files.file
522 WHERE dsc_files.source = s.id AND f.id != s.file
528 WHERE NOT EXISTS (SELECT 1 FROM source_metadata md WHERE md.src_id=s.id AND md.key_id = :checksum_key);
531 def add_missing_source_checksums():
532 """ Add missing source checksums to source_metadata """
533 session = DBConn().session()
534 for checksum in ['Files', 'Checksums-Sha1', 'Checksums-Sha256']:
535 checksum_key = get_or_set_metadatakey(checksum, session).key_id
536 rows = session.execute(_add_missing_source_checksums_query,
537 {'checksum_key': checksum_key, 'checksum_type': checksum}).rowcount
539 print "Added {0} missing entries for {1}".format(rows, checksum)
542 ################################################################################
545 global db_files, waste, excluded
549 Arguments = [('h',"help","Check-Archive::Options::Help")]
551 if not cnf.has_key("Check-Archive::Options::%s" % (i)):
552 cnf["Check-Archive::Options::%s" % (i)] = ""
554 args = apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
556 Options = cnf.subtree("Check-Archive::Options")
561 utils.warn("dak check-archive requires at least one argument")
564 utils.warn("dak check-archive accepts only one argument")
566 mode = args[0].lower()
571 if mode == "checksums":
573 elif mode == "files":
575 elif mode == "dsc-syntax":
577 elif mode == "missing-overrides":
579 elif mode == "source-in-one-dir":
580 check_source_in_one_dir()
581 elif mode == "timestamps":
583 elif mode == "files-in-dsc":
585 elif mode == "validate-indices":
586 check_indices_files_exist()
587 elif mode == "files-not-symlinks":
588 check_files_not_symlinks()
589 elif mode == "validate-builddeps":
590 check_build_depends()
591 elif mode == "add-missing-source-checksums":
592 add_missing_source_checksums()
594 utils.warn("unknown mode '%s'" % (mode))
597 ################################################################################
599 if __name__ == '__main__':