~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to shlibs/blkid/src/save.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * save.c - write the cache struct to disk
3
 
 *
4
 
 * Copyright (C) 2001 by Andreas Dilger
5
 
 * Copyright (C) 2003 Theodore Ts'o
6
 
 *
7
 
 * %Begin-Header%
8
 
 * This file may be redistributed under the terms of the
9
 
 * GNU Lesser General Public License.
10
 
 * %End-Header%
11
 
 */
12
 
 
13
 
#include <stdio.h>
14
 
#include <string.h>
15
 
#include <stdlib.h>
16
 
#include <unistd.h>
17
 
#include <sys/types.h>
18
 
#ifdef HAVE_SYS_STAT_H
19
 
#include <sys/stat.h>
20
 
#endif
21
 
#ifdef HAVE_ERRNO_H
22
 
#include <errno.h>
23
 
#endif
24
 
#include "blkidP.h"
25
 
 
26
 
static int save_dev(blkid_dev dev, FILE *file)
27
 
{
28
 
        struct list_head *p;
29
 
 
30
 
        if (!dev || dev->bid_name[0] != '/')
31
 
                return 0;
32
 
 
33
 
        DBG(DEBUG_SAVE,
34
 
            printf("device %s, type %s\n", dev->bid_name, dev->bid_type ?
35
 
                   dev->bid_type : "(null)"));
36
 
 
37
 
        fprintf(file, "<device DEVNO=\"0x%04lx\" TIME=\"%ld.%ld\"",
38
 
                        (unsigned long) dev->bid_devno,
39
 
                        (long) dev->bid_time,
40
 
                        (long) dev->bid_utime);
41
 
 
42
 
        if (dev->bid_pri)
43
 
                fprintf(file, " PRI=\"%d\"", dev->bid_pri);
44
 
        list_for_each(p, &dev->bid_tags) {
45
 
                blkid_tag tag = list_entry(p, struct blkid_struct_tag, bit_tags);
46
 
                fprintf(file, " %s=\"%s\"", tag->bit_name,tag->bit_val);
47
 
        }
48
 
        fprintf(file, ">%s</device>\n", dev->bid_name);
49
 
 
50
 
        return 0;
51
 
}
52
 
 
53
 
/*
54
 
 * Write out the cache struct to the cache file on disk.
55
 
 */
56
 
int blkid_flush_cache(blkid_cache cache)
57
 
{
58
 
        struct list_head *p;
59
 
        char *tmp = NULL;
60
 
        const char *opened = NULL;
61
 
        const char *filename;
62
 
        FILE *file = NULL;
63
 
        int fd, ret = 0;
64
 
        struct stat st;
65
 
 
66
 
        if (!cache)
67
 
                return -BLKID_ERR_PARAM;
68
 
 
69
 
        if (list_empty(&cache->bic_devs) ||
70
 
            !(cache->bic_flags & BLKID_BIC_FL_CHANGED)) {
71
 
                DBG(DEBUG_SAVE, printf("skipping cache file write\n"));
72
 
                return 0;
73
 
        }
74
 
 
75
 
        filename = cache->bic_filename ? cache->bic_filename: BLKID_CACHE_FILE;
76
 
 
77
 
        /* If we can't write to the cache file, then don't even try */
78
 
        if (((ret = stat(filename, &st)) < 0 && errno != ENOENT) ||
79
 
            (ret == 0 && access(filename, W_OK) < 0)) {
80
 
                DBG(DEBUG_SAVE,
81
 
                    printf("can't write to cache file %s\n", filename));
82
 
                return 0;
83
 
        }
84
 
 
85
 
        /*
86
 
         * Try and create a temporary file in the same directory so
87
 
         * that in case of error we don't overwrite the cache file.
88
 
         * If the cache file doesn't yet exist, it isn't a regular
89
 
         * file (e.g. /dev/null or a socket), or we couldn't create
90
 
         * a temporary file then we open it directly.
91
 
         */
92
 
        if (ret == 0 && S_ISREG(st.st_mode)) {
93
 
                tmp = malloc(strlen(filename) + 8);
94
 
                if (tmp) {
95
 
                        sprintf(tmp, "%s-XXXXXX", filename);
96
 
                        fd = mkstemp(tmp);
97
 
                        if (fd >= 0) {
98
 
                                file = fdopen(fd, "w");
99
 
                                opened = tmp;
100
 
                        }
101
 
                        fchmod(fd, 0644);
102
 
                }
103
 
        }
104
 
 
105
 
        if (!file) {
106
 
                file = fopen(filename, "w");
107
 
                opened = filename;
108
 
        }
109
 
 
110
 
        DBG(DEBUG_SAVE,
111
 
            printf("writing cache file %s (really %s)\n",
112
 
                   filename, opened));
113
 
 
114
 
        if (!file) {
115
 
                ret = errno;
116
 
                goto errout;
117
 
        }
118
 
 
119
 
        list_for_each(p, &cache->bic_devs) {
120
 
                blkid_dev dev = list_entry(p, struct blkid_struct_dev, bid_devs);
121
 
                if (!dev->bid_type || (dev->bid_flags & BLKID_BID_FL_REMOVABLE))
122
 
                        continue;
123
 
                if ((ret = save_dev(dev, file)) < 0)
124
 
                        break;
125
 
        }
126
 
 
127
 
        if (ret >= 0) {
128
 
                cache->bic_flags &= ~BLKID_BIC_FL_CHANGED;
129
 
                ret = 1;
130
 
        }
131
 
 
132
 
        fclose(file);
133
 
        if (opened != filename) {
134
 
                if (ret < 0) {
135
 
                        unlink(opened);
136
 
                        DBG(DEBUG_SAVE,
137
 
                            printf("unlinked temp cache %s\n", opened));
138
 
                } else {
139
 
                        char *backup;
140
 
 
141
 
                        backup = malloc(strlen(filename) + 5);
142
 
                        if (backup) {
143
 
                                sprintf(backup, "%s.old", filename);
144
 
                                unlink(backup);
145
 
                                if (link(filename, backup)) {
146
 
                                        DBG(DEBUG_SAVE,
147
 
                                                printf("can't link %s to %s\n",
148
 
                                                        filename, backup));
149
 
                                }
150
 
                                free(backup);
151
 
                        }
152
 
                        rename(opened, filename);
153
 
                        DBG(DEBUG_SAVE,
154
 
                            printf("moved temp cache %s\n", opened));
155
 
                }
156
 
        }
157
 
 
158
 
errout:
159
 
        free(tmp);
160
 
        return ret;
161
 
}
162
 
 
163
 
#ifdef TEST_PROGRAM
164
 
int main(int argc, char **argv)
165
 
{
166
 
        blkid_cache cache = NULL;
167
 
        int ret;
168
 
 
169
 
        blkid_init_debug(DEBUG_ALL);
170
 
        if (argc > 2) {
171
 
                fprintf(stderr, "Usage: %s [filename]\n"
172
 
                        "Test loading/saving a cache (filename)\n", argv[0]);
173
 
                exit(1);
174
 
        }
175
 
 
176
 
        if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
177
 
                fprintf(stderr, "%s: error creating cache (%d)\n",
178
 
                        argv[0], ret);
179
 
                exit(1);
180
 
        }
181
 
        if ((ret = blkid_probe_all(cache)) < 0) {
182
 
                fprintf(stderr, "error (%d) probing devices\n", ret);
183
 
                exit(1);
184
 
        }
185
 
        cache->bic_filename = blkid_strdup(argv[1]);
186
 
 
187
 
        if ((ret = blkid_flush_cache(cache)) < 0) {
188
 
                fprintf(stderr, "error (%d) saving cache\n", ret);
189
 
                exit(1);
190
 
        }
191
 
 
192
 
        blkid_put_cache(cache);
193
 
 
194
 
        return ret;
195
 
}
196
 
#endif