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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
/* Part of publib.
Copyright (c) 1994-2006 Lars Wirzenius. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* File: file_io.c
* Purpose: File I/O.
* Author: Lars Wirzenius
* Version: "@(#)publib:$Id: file_io.c,v 1.1 1996/11/05 21:15:46 liw Exp $"
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "publib/alloc.h"
#include "publib/files.h"
#include "publib/errormsg.h"
#define MAX_TRIES 1024
static int create_save_temp_file(const char *, char *, int *);
static int copy_modes(const char *, const char *);
static int make_backup_name(char *, const char *, size_t);
int file_read_open(FILE *f, void **data, size_t *size) {
struct dynarr da;
int c;
dynarr_init(&da, 1);
while ((c = getc(f)) != EOF && !ferror(stdout)) {
if (dynarr_resize(&da, da.used + 2) == -1) {
__publib_error("out of memory");
dynarr_free(&da);
(void) fclose(f);
return -1;
}
((char *) da.data)[da.used++] = c;
}
if (ferror(f)) {
__publib_error("error reading from file");
return -1;
}
*data = da.data;
*size = da.used;
return 0;
}
int file_read(const char *pathname, void **data, size_t *size) {
FILE *f;
int ret;
f = fopen(pathname, "r");
if (f == NULL) {
__publib_error("error opening file for reading");
return -1;
}
ret = file_read_open(f, data, size);
if (fclose(f) == EOF) {
__publib_error("error closing file after reading");
ret = -1;
}
return ret;
}
int file_write(const char *pathname, void *data, size_t n) {
FILE *f;
int c;
f = fopen(pathname, "w");
if (f == NULL) {
__publib_error("couldn't open file for writing");
return -1;
}
(void) fwrite(data ? data : "", 1, n, f);
c = ferror(f);
if (fclose(f) == EOF || c) {
__publib_error("error writing to file");
return -1;
}
return 0;
}
int file_save(const char *name, void *data, size_t size, int keep_backup) {
FILE *f;
int c, fd;
char temp_name[FILENAME_MAX];
char back_name[sizeof(temp_name) + 4];
if (strlen(name) + 64 > sizeof(temp_name)) {
__publib_error("filename is too long (can't save)");
return -1;
}
if (make_backup_name(back_name, name, sizeof(back_name)) == -1)
return -1;
if (create_save_temp_file(name, temp_name, &fd) == -1)
return -1;
f = fdopen(fd, "w");
if (f == NULL) {
__publib_error("error opening file");
return -1;
}
(void) fwrite(data ? data : "", 1, size, f);
c = ferror(f);
if (fclose(f) == EOF || c) {
__publib_error("error closing file");
return -1;
}
if (rename(name, back_name) == -1 && errno != ENOENT) {
__publib_error("rename of original failed (can't save)");
return -1;
}
if (rename(temp_name, name) == -1) {
__publib_error("rename of new failed (can't save)");
return -1;
}
if (copy_modes(back_name, name) == -1)
return -1;
if (!keep_backup) {
if (remove(back_name) == -1 && errno != ENOENT) {
__publib_error("remove of backup failed (can't save)");
return -1;
}
}
return 0;
}
/**********************************************************************/
/*
* Function: create_save_temp_file
* Purpose: Create a new file in same directory as the file to be saved.
* Arguments: original the name of the original file
* buf where the new file's name is stored
* fd where file descriptor of new file is stored
* Return: -1 for failure, 0 for success.
* Note: buf should be big enough; strlen(original) + 64 should
* be enough (unless you have _really_ big unsigned longs).
*/
static int create_save_temp_file(const char *original, char *buf, int *fd) {
char *p;
unsigned long i;
time_t random;
strcpy(buf, original);
p = strrchr(buf, '/');
if (p != NULL)
p[1] = '\0';
else
buf[0] = '\0';
time(&random);
*fd = -1;
p = strchr(buf, '\0');
for (i = 0; i < MAX_TRIES && *fd == -1; ++i) {
sprintf(p, "#%lu-%lu#", (unsigned long) random, i);
*fd = open(buf, O_WRONLY | O_CREAT | O_EXCL, 0600);
if (*fd == -1 && errno != EEXIST) {
__publib_error("can't create temporary file");
return -1;
}
}
if (*fd == -1)
return -1;
return 0;
}
/*
* Function: copy_modes
* Purpose: Copy the permissions from one file to another.
* Arguments: src original file
* tgt target file
* Return: -1 for error, 0 for OK.
*/
static int copy_modes(const char *src, const char *tgt) {
struct stat st;
if (stat(src, &st) == -1) {
if (errno == ENOENT)
return 0;
__publib_error("Can't stat file (can't complete save)");
return -1;
}
if (chmod(tgt, st.st_mode) == -1) {
__publib_error("Can't chmod file (can't complete save)");
return -1;
}
return 0;
}
/*
* Function: make_backup_name
* Purpose: Create name of backup file from original file.
* Note: The target buffer must be big enough.
*/
static int make_backup_name(char *back, const char *orig, size_t max) {
if (strlen(orig) + 2 > max)
return -1;
sprintf(back, "%s~", orig);
return 0;
}
|