dpkg (1.18.25) stretch; urgency=medium
[dpkg] / lib / dpkg / file.c
CommitLineData
1479465f
GJ
1/*
2 * libdpkg - Debian packaging suite library routines
3 * file.c - file handling functions
4 *
5 * Copyright © 1994, 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
7 *
8 * This is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <config.h>
23#include <compat.h>
24
25#include <sys/types.h>
26#include <sys/stat.h>
27
28#include <assert.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <unistd.h>
32
33#include <dpkg/dpkg.h>
34#include <dpkg/i18n.h>
35#include <dpkg/subproc.h>
36#include <dpkg/command.h>
37#include <dpkg/file.h>
38
39/**
40 * Copy file ownership and permissions from one file to another.
41 *
42 * @param src The source filename.
43 * @param dst The destination filename.
44 */
45void
46file_copy_perms(const char *src, const char *dst)
47{
48 struct stat stab;
49
50 if (stat(src, &stab) == -1) {
51 if (errno == ENOENT)
52 return;
53 ohshite(_("unable to stat source file '%.250s'"), src);
54 }
55
56 if (chown(dst, stab.st_uid, stab.st_gid) == -1)
57 ohshite(_("unable to change ownership of target file '%.250s'"),
58 dst);
59
60 if (chmod(dst, (stab.st_mode & ~S_IFMT)) == -1)
61 ohshite(_("unable to set mode of target file '%.250s'"), dst);
62}
63
64static void
65file_lock_setup(struct flock *fl, short type)
66{
67 fl->l_type = type;
68 fl->l_whence = SEEK_SET;
69 fl->l_start = 0;
70 fl->l_len = 0;
71 fl->l_pid = 0;
72}
73
74/**
75 * Unlock a previously locked file.
76 */
77void
78file_unlock(int lockfd, const char *lock_desc)
79{
80 struct flock fl;
81
82 assert(lockfd >= 0);
83
84 file_lock_setup(&fl, F_UNLCK);
85
86 if (fcntl(lockfd, F_SETLK, &fl) == -1)
87 ohshite(_("unable to unlock %s"), lock_desc);
88}
89
90static void
91file_unlock_cleanup(int argc, void **argv)
92{
93 int lockfd = *(int *)argv[0];
94 const char *lock_desc = argv[1];
95
96 file_unlock(lockfd, lock_desc);
97}
98
99/**
100 * Check if a file has a lock acquired.
101 *
102 * @param lockfd The file descriptor for the lock.
103 * @param filename The file name associated to the file descriptor.
104 */
105bool
106file_is_locked(int lockfd, const char *filename)
107{
108 struct flock fl;
109
110 file_lock_setup(&fl, F_WRLCK);
111
112 if (fcntl(lockfd, F_GETLK, &fl) == -1)
113 ohshit(_("unable to check file '%s' lock status"), filename);
114
115 if (fl.l_type == F_WRLCK && fl.l_pid != getpid())
116 return true;
117 else
118 return false;
119}
120
121/**
122 * Lock a file.
123 *
124 * @param lockfd The pointer to the lock file descriptor. It must be allocated
125 * statically as its addresses is passed to a cleanup handler.
126 * @param flags The lock flags specifying what type of locking to perform.
127 * @param filename The name of the file to lock.
128 * @param desc The description of the file to lock.
129 */
130void
131file_lock(int *lockfd, enum file_lock_flags flags, const char *filename,
132 const char *desc)
133{
134 struct flock fl;
135 int lock_cmd;
136
137 setcloexec(*lockfd, filename);
138
139 file_lock_setup(&fl, F_WRLCK);
140
141 if (flags == FILE_LOCK_WAIT)
142 lock_cmd = F_SETLKW;
143 else
144 lock_cmd = F_SETLK;
145
146 if (fcntl(*lockfd, lock_cmd, &fl) == -1) {
147 if (errno == EACCES || errno == EAGAIN)
148 ohshit(_("%s is locked by another process"), desc);
149 else
150 ohshite(_("unable to lock %s"), desc);
151 }
152
153 push_cleanup(file_unlock_cleanup, ~0, NULL, 0, 2, lockfd, desc);
154}
155
156void
157file_show(const char *filename)
158{
159 pid_t pid;
160
161 if (filename == NULL)
162 internerr("file '%s' does not exist", filename);
163
164 pid = subproc_fork();
165 if (pid == 0) {
166 struct command cmd;
167 const char *pager;
168
169 pager = command_get_pager();
170
171 command_init(&cmd, pager, _("showing file on pager"));
172 command_add_arg(&cmd, pager);
173 command_add_arg(&cmd, filename);
174 command_exec(&cmd);
175 }
176 subproc_reap(pid, _("showing file on pager"), SUBPROC_NOCHECK);
177}