summaryrefslogtreecommitdiff
path: root/client/file.c
blob: 8e161032d7bf165f2e7fdf18e3d188e958fed0cd (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
/*  $Id$
**
**  File handling for the wallet client.
**
**  Written by Russ Allbery <rra@stanford.edu>
**  Copyright 2007 Board of Trustees, Leland Stanford Jr. University
**
**  See LICENSE for licensing terms.
*/

#include <config.h>
#include <system.h>

#include <fcntl.h>

#include <client/internal.h>
#include <util/util.h>

/*
**  Given a filename, some data, and a length, write that data to the given
**  file safely and atomically by creating file.new, writing the data, linking
**  file to file.bak, and then renaming file.new to file.
*/
void
write_file(const char *name, const void *data, size_t length)
{
    int fd;
    ssize_t status;
    char *temp, *backup;

    temp = concat(name, ".new", (char *) 0);
    backup = concat(name, ".bak", (char *) 0);
    fd = open(temp, O_WRONLY | O_CREAT | O_TRUNC, 0600);
    if (fd < 0)
        sysdie("open of %s failed", temp);
    status = write(fd, data, length);
    if (status < 0)
        sysdie("write to %s failed", temp);
    else if (status != (ssize_t) length)
        die("write to %s truncated", temp);
    if (close(fd) < 0)
        sysdie("close of %s failed (file probably truncated)", temp);
    if (access(name, F_OK) == 0) {
        if (access(backup, F_OK) == 0)
            if (unlink(backup) < 0)
                sysdie("unlink of old backup %s failed", backup);
        if (link(name, backup) < 0)
            sysdie("link of %s to %s failed", name, backup);
    }
    if (rename(temp, name) < 0)
        sysdie("rename of %s to %s failed", temp, name);
    free(temp);
    free(backup);
}