2 * dpkg - main program for package management
3 * filesdb-hash.c - management of database of files installed on system
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>
43 * If mask is nonzero, will not write any file whose filenamenode
44 * has any flag bits set in mask.
47 write_filehash_except(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
48 struct fileinlist
*list
, enum filenamenode_flags mask
)
50 struct atomic_file
*file
;
53 debug(dbg_general
, "generating infodb hashfile");
55 if (pkg_infodb_has_file(pkg
, &pkg
->available
, HASHFILE
))
58 hashfile
= pkg_infodb_get_file(pkg
, pkgbin
, HASHFILE
);
60 file
= atomic_file_new(hashfile
, 0);
61 atomic_file_open(file
);
63 for (; list
; list
= list
->next
) {
64 struct filenamenode
*namenode
= list
->namenode
;
66 if (mask
&& (namenode
->flags
& mask
))
68 if (strcmp(namenode
->newhash
, EMPTYHASHFLAG
) == 0)
71 fprintf(file
->fp
, "%s %s\n",
72 namenode
->newhash
, namenode
->name
+ 1);
75 atomic_file_sync(file
);
76 atomic_file_close(file
);
77 atomic_file_commit(file
);
78 atomic_file_free(file
);
80 dir_sync_path(pkg_infodb_get_dir());
84 parse_filehash_buffer(char *buf
, char *buf_end
,
85 struct pkginfo
*pkg
, struct pkgbin
*pkgbin
)
87 char *thisline
, *nextline
;
88 const char *pkgname
= pkg_name(pkg
, pnaw_nonambig
);
90 for (thisline
= buf
; thisline
< buf_end
; thisline
= nextline
) {
91 struct filenamenode
*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 hash '%s' for filename '%s'",
127 /* Add the file to the list. */
128 namenode
= findnamenode(filename
, fnn_nocopy
);
129 namenode
->newhash
= thisline
;
134 parse_filehash(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
)
137 const char *hashfile
;
140 hashfile
= pkg_infodb_get_file(pkg
, pkgbin
, HASHFILE
);
142 fd
= open(hashfile
, O_RDONLY
);
147 ohshite(_("cannot open control file '%s' for package '%s'"),
148 HASHFILE
, pkg_name(pkg
, pnaw_nonambig
));
151 if (fstat(fd
, &st
) < 0)
152 ohshite(_("cannot stat control file '%s' for package '%s'"),
153 HASHFILE
, pkg_name(pkg
, pnaw_nonambig
));
155 if (!S_ISREG(st
.st_mode
))
156 ohshit(_("control file '%s' for package '%s' is not a regular file"),
157 HASHFILE
, pkg_name(pkg
, pnaw_nonambig
));
159 if (st
.st_size
> 0) {
162 buf
= nfmalloc(st
.st_size
);
163 buf_end
= buf
+ st
.st_size
;
165 if (fd_read(fd
, buf
, st
.st_size
) < 0)
166 ohshite(_("cannot read control file '%s' for package '%s'"),
167 HASHFILE
, pkg_name(pkg
, pnaw_nonambig
));
169 parse_filehash_buffer(buf
, buf_end
, pkg
, pkgbin
);
173 ohshite(_("cannot close control file '%s' for package '%s'"),
174 HASHFILE
, pkg_name(pkg
, pnaw_nonambig
));