summaryrefslogtreecommitdiffstats
path: root/lib/dpkg/file.c
blob: 4d02520d8e9cd6dbff6920b129b3eed9c181b0f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * libdpkg - Debian packaging suite library routines
 * file.c - file handling functions
 *
 * Copyright © 1994, 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
 * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <compat.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#include <dpkg/dpkg.h>
#include <dpkg/i18n.h>
#include <dpkg/pager.h>
#include <dpkg/fdio.h>
#include <dpkg/buffer.h>
#include <dpkg/file.h>

/**
 * Check whether a filename is executable.
 *
 * @param filename The filename to check.
 */
bool
file_is_exec(const char *filename)
{
	struct stat st;

	if (stat(filename, &st) < 0)
		return false;

	if (!S_ISREG(st.st_mode))
		return false;

	return st.st_mode & 0111;
}

/**
 * Copy file ownership and permissions from one file to another.
 *
 * @param src The source filename.
 * @param dst The destination filename.
 */
void
file_copy_perms(const char *src, const char *dst)
{
	struct stat stab;

	if (stat(src, &stab) == -1) {
		if (errno == ENOENT)
			return;
		ohshite(_("unable to stat source file '%.250s'"), src);
	}

	if (chown(dst, stab.st_uid, stab.st_gid) == -1)
		ohshite(_("unable to change ownership of target file '%.250s'"),
		        dst);

	if (chmod(dst, (stab.st_mode & ~S_IFMT)) == -1)
		ohshite(_("unable to set mode of target file '%.250s'"), dst);
}

static int
file_slurp_fd(int fd, const char *filename, struct varbuf *vb,
              struct dpkg_error *err)
{
	struct stat st;

	if (fstat(fd, &st) < 0)
		return dpkg_put_errno(err, _("cannot stat %s"), filename);

	if (!S_ISREG(st.st_mode))
		return dpkg_put_error(err, _("%s is not a regular file"),
		                      filename);

	if (st.st_size == 0)
		return 0;

	varbuf_init(vb, st.st_size);
	if (fd_read(fd, vb->buf, st.st_size) < 0)
		return dpkg_put_errno(err, _("cannot read %s"), filename);
	vb->used = st.st_size;

	return 0;
}

int
file_slurp(const char *filename, struct varbuf *vb, struct dpkg_error *err)
{
	int fd;
	int rc;

	varbuf_init(vb, 0);

	fd = open(filename, O_RDONLY);
	if (fd < 0)
		return dpkg_put_errno(err, _("cannot open %s"), filename);

	rc = file_slurp_fd(fd, filename, vb, err);

	(void)close(fd);

	return rc;
}

static void
file_lock_setup(struct flock *fl, short type)
{
	fl->l_type = type;
	fl->l_whence = SEEK_SET;
	fl->l_start = 0;
	fl->l_len = 0;
	fl->l_pid = 0;
}

/**
 * Unlock a previously locked file.
 */
void
file_unlock(int lockfd, const char *lockfile, const char *lockdesc)
{
	struct flock fl;

	if (lockfd < 0)
		internerr("%s (%s) fd is %d < 0", lockdesc, lockfile, lockfd);

	file_lock_setup(&fl, F_UNLCK);

	if (fcntl(lockfd, F_SETLK, &fl) == -1)
		ohshite(_("unable to unlock %s"), lockdesc);
}

static void
file_unlock_cleanup(int argc, void **argv)
{
	int lockfd = *(int *)argv[0];
	const char *lockfile = argv[1];
	const char *lockdesc = argv[2];

	file_unlock(lockfd, lockfile, lockdesc);
}

/**
 * Check if a file has a lock acquired.
 *
 * @param lockfd The file descriptor for the lock.
 * @param filename The file name associated to the file descriptor.
 */
bool
file_is_locked(int lockfd, const char *filename)
{
	struct flock fl;

	file_lock_setup(&fl, F_WRLCK);

	if (fcntl(lockfd, F_GETLK, &fl) == -1)
		ohshit(_("unable to check file '%s' lock status"), filename);

	if (fl.l_type == F_WRLCK && fl.l_pid != getpid())
		return true;
	else
		return false;
}

/**
 * Lock a file.
 *
 * @param lockfd The pointer to the lock file descriptor. It must be allocated
 *        statically as its addresses is passed to a cleanup handler.
 * @param flags The lock flags specifying what type of locking to perform.
 * @param filename The name of the file to lock.
 * @param desc The description of the file to lock.
 */
void
file_lock(int *lockfd, enum file_lock_flags flags, const char *filename,
          const char *desc)
{
	struct flock fl;
	int lock_cmd;

	setcloexec(*lockfd, filename);

	file_lock_setup(&fl, F_WRLCK);

	if (flags == FILE_LOCK_WAIT)
		lock_cmd = F_SETLKW;
	else
		lock_cmd = F_SETLK;

	if (fcntl(*lockfd, lock_cmd, &fl) == -1) {
		const char *warnmsg;

		if (errno != EACCES && errno != EAGAIN)
			ohshite(_("unable to lock %s"), desc);

		warnmsg = _("Note: removing the lock file is always wrong, "
		            "can damage the locked area\n"
		            "and the entire system. "
		            "See <https://wiki.debian.org/Teams/Dpkg/FAQ#db-lock>.");

		file_lock_setup(&fl, F_WRLCK);
		if (fcntl(*lockfd, F_GETLK, &fl) == -1)
			ohshit(_("%s was locked by another process\n%s"),
			       desc, warnmsg);

		ohshit(_("%s was locked by another process with pid %d\n%s"),
		       desc, fl.l_pid, warnmsg);
	}

	push_cleanup(file_unlock_cleanup, ~0, 3, lockfd, filename, desc);
}

void
file_show(const char *filename)
{
	struct pager *pager;
	struct dpkg_error err;
	int fd, rc;

	if (filename == NULL)
		internerr("filename is NULL");

	fd = open(filename, O_RDONLY);
	if (fd < 0)
		ohshite(_("cannot open file %s"), filename);

	pager = pager_spawn(_("pager to show file"));
	rc = fd_fd_copy(fd, STDOUT_FILENO, -1, &err);
	pager_reap(pager);

	close(fd);

	if (rc < 0 && err.syserrno != EPIPE) {
		errno = err.syserrno;
		ohshite(_("cannot write file %s into the pager"), filename);
	}
}