34
34
#include <dpkg/i18n.h>
35
35
#include <dpkg/file.h>
38
* Copy file ownership and permissions from one file to another.
40
* @param src The source filename.
41
* @param dst The destination filename.
38
44
file_copy_perms(const char *src, const char *dst)
67
file_unlock_cleanup(int argc, void **argv)
73
* Unlock a previously locked file.
76
file_unlock(int lockfd, const char *lock_desc)
69
int lockfd = *(int*)argv[0];
72
80
assert(lockfd >= 0);
74
82
file_lock_setup(&fl, F_UNLCK);
76
84
if (fcntl(lockfd, F_SETLK, &fl) == -1)
77
ohshite(_("unable to unlock dpkg status database"));
85
ohshite(_("unable to unlock %s"), lock_desc);
89
file_unlock_cleanup(int argc, void **argv)
83
pop_cleanup(ehflag_normaltidy); /* Calls file_unlock_cleanup. */
91
int lockfd = *(int *)argv[0];
92
const char *lock_desc = argv[1];
94
file_unlock(lockfd, lock_desc);
108
/* lockfd must be allocated statically as its addresses is passed to
109
* a cleanup handler. */
122
* @param lockfd The pointer to the lock file descriptor. It must be allocated
123
* statically as its addresses is passed to a cleanup handler.
124
* @param flags The lock flags specifiying what type of locking to perform.
125
* @param filename The name of the file to lock.
126
* @param desc The description of the file to lock.
111
file_lock(int *lockfd, const char *filename,
112
const char *emsg, const char *emsg_eagain)
129
file_lock(int *lockfd, enum file_lock_flags flags, const char *filename,
116
135
setcloexec(*lockfd, filename);
118
137
file_lock_setup(&fl, F_WRLCK);
120
if (fcntl(*lockfd, emsg_eagain ? F_SETLK : F_SETLKW, &fl) == -1) {
121
if (emsg_eagain && (errno == EACCES || errno == EAGAIN))
139
if (flags == FILE_LOCK_WAIT)
144
if (fcntl(*lockfd, lock_cmd, &fl) == -1) {
145
if (errno == EACCES || errno == EAGAIN)
146
ohshit(_("%s is locked by another process"), desc);
148
ohshite(_("unable to lock %s"), desc);
126
push_cleanup(file_unlock_cleanup, ~0, NULL, 0, 1, lockfd);
151
push_cleanup(file_unlock_cleanup, ~0, NULL, 0, 2, lockfd, desc);