2 * libdpkg - Debian packaging suite library routines
3 * db-fsys-digest.c - management of filesystem digests database
5 * Copyright © 2012-2014 Guillem Jover <guillem@debian.org>
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
32 #include <dpkg/i18n.h>
33 #include <dpkg/dpkg.h>
34 #include <dpkg/dpkg-db.h>
35 #include <dpkg/debug.h>
36 #include <dpkg/fdio.h>
38 #include <dpkg/db-ctrl.h>
39 #include <dpkg/db-fsys.h>
42 * If mask is nonzero, will not write any file whose fsys_namenode
43 * has any flag bits set in mask.
46 write_filehash_except(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
47 struct fsys_namenode_list
*list
, enum fsys_namenode_flags mask
)
49 struct atomic_file
*file
;
52 debug(dbg_general
, "generating infodb hashfile");
54 if (pkg_infodb_has_file(pkg
, &pkg
->available
, HASHFILE
))
57 hashfile
= pkg_infodb_get_file(pkg
, pkgbin
, HASHFILE
);
59 file
= atomic_file_new(hashfile
, 0);
60 atomic_file_open(file
);
62 for (; list
; list
= list
->next
) {
63 const struct fsys_namenode
*namenode
= list
->namenode
;
65 if (mask
&& (namenode
->flags
& mask
))
67 if (namenode
->newhash
== NULL
)
70 fprintf(file
->fp
, "%s %s\n",
71 namenode
->newhash
, namenode
->name
+ 1);
74 atomic_file_sync(file
);
75 atomic_file_close(file
);
76 atomic_file_commit(file
);
77 atomic_file_free(file
);
79 dir_sync_path(pkg_infodb_get_dir());
83 parse_filehash_buffer(struct varbuf
*buf
,
84 struct pkginfo
*pkg
, struct pkgbin
*pkgbin
)
86 char *thisline
, *nextline
;
87 const char *pkgname
= pkg_name(pkg
, pnaw_nonambig
);
88 const char *buf_end
= buf
->buf
+ buf
->used
;
90 for (thisline
= buf
->buf
; thisline
< buf_end
; thisline
= nextline
) {
91 struct fsys_namenode
*namenode
;
92 char *endline
, *hash_end
, *filename
;
94 endline
= memchr(thisline
, '\n', buf_end
- thisline
);
96 ohshit(_("control file '%s' for package '%s' is "
97 "missing final newline"), HASHFILE
, pkgname
);
99 /* The md5sum hash has a constant length. */
100 hash_end
= thisline
+ MD5HASHLEN
;
102 filename
= hash_end
+ 2;
103 if (filename
+ 1 > endline
)
104 ohshit(_("control file '%s' for package '%s' is "
105 "missing value"), HASHFILE
, pkgname
);
107 if (hash_end
[0] != ' ' || hash_end
[1] != ' ')
108 ohshit(_("control file '%s' for package '%s' is "
109 "missing value separator"), HASHFILE
, pkgname
);
112 /* Where to start next time around. */
113 nextline
= endline
+ 1;
115 /* Strip trailing ‘/’. */
116 if (endline
> thisline
&& endline
[-1] == '/')
120 if (endline
== thisline
)
121 ohshit(_("control file '%s' for package '%s' "
122 "contains empty filename"), HASHFILE
, pkgname
);
124 debug(dbg_eachfiledetail
, "load digest '%s' for filename '%s'",
127 /* Add the file to the list. */
128 namenode
= fsys_hash_find_node(filename
, FHFF_NONE
);
129 namenode
->newhash
= nfstrsave(thisline
);
134 parse_filehash(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
)
136 const char *hashfile
;
137 struct varbuf buf
= VARBUF_INIT
;
138 struct dpkg_error err
= DPKG_ERROR_INIT
;
140 hashfile
= pkg_infodb_get_file(pkg
, pkgbin
, HASHFILE
);
142 if (file_slurp(hashfile
, &buf
, &err
) < 0 && err
.syserrno
!= ENOENT
)
143 dpkg_error_print(&err
,
144 _("loading control file '%s' for package '%s'"),
145 HASHFILE
, pkg_name(pkg
, pnaw_nonambig
));
148 parse_filehash_buffer(&buf
, pkg
, pkgbin
);
150 varbuf_destroy(&buf
);