-
Notifications
You must be signed in to change notification settings - Fork 8
/
write.c
112 lines (94 loc) · 2.34 KB
/
write.c
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
/*
* copyfs - copy on write filesystem http://n0x.org/copyfs/
* Copyright (C) 2004 Nicolas Vigier <[email protected]>
* Thomas Joubert <[email protected]>
* This program can be distributed under the terms of the GNU GPL.
* See the file COPYING.
*/
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include "helper.h"
#include "structs.h"
#include "write.h"
/*
* Worker function for version writing.
*/
static int write_metadata_file_worker(FILE *fh, version_t *version)
{
char *name;
/* Write them in order */
if (version->v_next)
if (write_metadata_file_worker(fh, version->v_next) != 0)
return -1;
/* Strip the path */
name = rindex(version->v_rfile, '/');
if (!name)
name = version->v_rfile;
else
name++;
/* Write this version's information */
if (fprintf(fh, "%i:%i:%04o:%i:%i:%s\n", version->v_vid, version->v_svid,
version->v_mode, version->v_uid, version->v_gid, name) < 0)
return -1;
return 0;
}
/*
* Write a metadata file on disk from the in-memory structures. We rewrite
* the whole file to avoid leaving "deletion lines" at all the points the
* file was deleted and re-created. Plus it cleans up partially corrupt files
* automatically. Returns non-zero on error.
*/
int write_metadata_file(char *metafile, metadata_t *metadata)
{
FILE *fh;
/* Open metadata file */
fh = fopen(metafile, "w");
if (!fh)
return -1;
/* Write normal versions */
if (write_metadata_file_worker(fh, metadata->md_versions) != 0)
{
fclose(fh);
return -1;
}
/* If the file is marked deleted, put a killer version */
if (metadata->md_deleted)
if (fprintf(fh, "0:0:0000:0:0:\n") < 0)
{
fclose(fh);
return -1;
}
fclose(fh);
return 0;
}
/*
* Write a default version file for the given version. Return non-zero on
* error.
*/
int write_default_file(char *dflfile, int vid, int svid)
{
FILE *fh;
/* If we want to return it to default, remove the file */
if (vid == LATEST)
{
int result;
result = unlink(dflfile);
if (!result || (errno == ENOENT))
return 0;
else
return result;
}
fh = fopen(dflfile, "w");
if (!fh)
return -1;
/* Write new value */
if ((fprintf(fh, "%i.%i\n", vid, svid) < 0))
{
fclose(fh);
return -1;
}
fclose(fh);
return 0;
}