summaryrefslogtreecommitdiffstats
path: root/lib/dpkg/atomic-file.c
blob: 20b1fb37e9b885ac44e57f259aff54f27949075f (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
/*
 * libdpkg - Debian packaging suite library routines
 * atomic-file.c - atomic file helper functions
 *
 * Copyright © 2011-2014 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/stat.h>

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include <dpkg/i18n.h>
#include <dpkg/dpkg.h>
#include <dpkg/atomic-file.h>

#define ATOMIC_FILE_NEW_EXT "-new"
#define ATOMIC_FILE_OLD_EXT "-old"

struct atomic_file *
atomic_file_new(const char *filename, enum atomic_file_flags flags)
{
	struct atomic_file *file;

	file = m_malloc(sizeof(*file));
	file->flags = flags;
	file->fp = NULL;
	file->name = m_strdup(filename);
	file->name_new = str_fmt("%s%s", filename, ATOMIC_FILE_NEW_EXT);

	return file;
}

void
atomic_file_open(struct atomic_file *file)
{
	file->fp = fopen(file->name_new, "w");
	if (file->fp == NULL)
		ohshite(_("unable to create new file '%.250s'"),
		        file->name_new);
	fchmod(fileno(file->fp), 0644);

	push_cleanup(cu_closestream, ~ehflag_normaltidy, 1, file->fp);
}

void
atomic_file_sync(struct atomic_file *file)
{
	if (ferror(file->fp))
		ohshite(_("unable to write new file '%.250s'"), file->name_new);
	if (fflush(file->fp))
		ohshite(_("unable to flush new file '%.250s'"), file->name_new);
	if (fsync(fileno(file->fp)))
		ohshite(_("unable to sync new file '%.250s'"), file->name_new);
}

void
atomic_file_close(struct atomic_file *file)
{
	pop_cleanup(ehflag_normaltidy); /* fopen */

	if (fclose(file->fp))
		ohshite(_("unable to close new file '%.250s'"), file->name_new);
}

static void
atomic_file_backup(struct atomic_file *file)
{
	char *name_old;

	name_old = str_fmt("%s%s", file->name, ATOMIC_FILE_OLD_EXT);

	if (unlink(name_old) && errno != ENOENT)
		ohshite(_("error removing old backup file '%s'"), name_old);
	if (link(file->name, name_old) && errno != ENOENT)
		ohshite(_("error creating new backup file '%s'"), name_old);

	free(name_old);
}

void
atomic_file_remove(struct atomic_file *file)
{
	if (unlink(file->name_new))
		ohshite(_("cannot remove '%.250s'"), file->name_new);
	if (unlink(file->name) && errno != ENOENT)
		ohshite(_("cannot remove '%.250s'"), file->name);
}

void
atomic_file_commit(struct atomic_file *file)
{
	if (file->flags & ATOMIC_FILE_BACKUP)
		atomic_file_backup(file);

	if (rename(file->name_new, file->name))
		ohshite(_("error installing new file '%s'"), file->name);
}

void
atomic_file_free(struct atomic_file *file)
{
	free(file->name_new);
	free(file->name);
	free(file);
}