File: sbuf_io.c

package info (click to toggle)
publib 0.40-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,448 kB
  • sloc: ansic: 6,708; sh: 395; makefile: 369
file content (329 lines) | stat: -rw-r--r-- 7,528 bytes parent folder | download | duplicates (3)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* 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.
*/
/*
 * sbuf_io.c -- reading and writing buffers and marks
 *
 * Part of Publib.  See manpage for more information.
 * "@(#)publib-sbuf:$Id: sbuf_io.c,v 1.7 1997/04/30 22:41:15 liw Exp $"
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>

#include "publib/sbuf.h"
#include "publib/errormsg.h"


#define IO_BUFFER_SIZE 8*1024
#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 *, char *, size_t);


int sbuf_load(Sbuf *buf) {
	FILE *f;
	int ret;

	sbuf_set_flags(buf, SBUF_LOADED_FLAG);
	ret = 0;

	if (buf->name == NULL)
		goto ok;

	sbuf_remark(buf->aux, 0, sbuf_length(buf));
	if (sbuf_strchange(buf->aux, "", 0) == -1) {
		__publib_error("error clearing old contents of file");
		return -1;
	}

	f = fopen(buf->name, "r");
	if (f == NULL) {
		if (errno == ENOENT)
			goto ok;
		__publib_error("error loading file (no permission?)");
		return -1;
	}

	ret = sbuf_insert_file(buf->aux, f);

	if (fclose(f) == EOF) {
		__publib_error("error closing file while loading buffer");
		ret = -1;
	}

ok:
	sbuf_clear_flags(buf, SBUF_DIRTY_FLAG);
	return ret;
}


int sbuf_save(Sbuf *buf, int keep_backup) {
	FILE *f;
	int e, fd, ret;
	char temp_name[FILENAME_MAX];
	char back_name[sizeof(temp_name) + 4];

	if (buf->name == NULL) {
		__publib_error("file has no name (can't save)");
		return -1;
	}

	if (strlen(buf->name) + 64 > sizeof(temp_name)) {
		__publib_error("filename is too long (can't save)");
		return -1;
	}
		
	if (make_backup_name(back_name, buf->name, sizeof(back_name)) == -1) {
		__publib_error("backup name too long");
		return -1;
	}

	if (create_save_temp_file(buf->name, temp_name, &fd) == -1)
		return -1;
		
	f = fdopen(fd, "w");
	if (f == NULL) {
		__publib_error("error opening file");
		return -1;
	}

	sbuf_remark(buf->aux, 0, sbuf_length(buf));
	ret = sbuf_write_to_file(buf->aux, f);
	e = errno;

	if (fclose(f) == EOF) {
		if (ret == 0) {
			e = errno;
			__publib_error("error closing file");
		}
		ret = -1;
	}

	if (rename(buf->name, back_name) == -1 && errno != ENOENT) {
		__publib_error("rename of original failed (can't save)");
		return -1;
	}
	if (rename(temp_name, buf->name) == -1) {
		__publib_error("rename of new failed (can't save)");
		return -1;
	}
	if (copy_modes(back_name, buf->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;
		}
	}

	if (ret == 0)
		sbuf_set_dirty(buf, 0);
	else
		errno = e;

	return ret;
}


int sbuf_insert_file(Sbufmark *mark, FILE *f) {
	char iobuf[IO_BUFFER_SIZE];
	size_t n;
	long begin;
	
	begin = sbuf_mark_begin(mark);
	while ((n = fread(iobuf, 1, IO_BUFFER_SIZE, f)) > 0 && !ferror(f)) {
		sbuf_remark(mark, sbuf_mark_end(mark), 0);
		if (sbuf_strchange(mark, iobuf, n) == -1)
			goto error;
	}
	if (!ferror(f))
		return 0;
	/*FALLTHROUGH*/
	__publib_error("error reading file");
error:
	sbuf_remark(mark, begin, sbuf_mark_end(mark) - begin);
	(void) sbuf_strchange(mark, "", 0);
	return -1;
}



int sbuf_write_to(Sbufmark *mark, const char *filename) {
	FILE *f;
	int ret, e;

	f = fopen(filename, "w");
	if (f == NULL) {
		__publib_error("couldn't open file for writing");
		return -1;
	}

	e = 0;
	ret = sbuf_write_to_file(mark, f);
	if (ret == -1) {
		e = errno;
		ret = -1;
	}
	
	if (fclose(f) == EOF) {
		if (e == 0) 
			e = errno;
		ret = -1;
	}

	errno = e;
	return ret;
}


int sbuf_write_to_file(Sbufmark *m, FILE *f) {
	char iobuf[IO_BUFFER_SIZE];
	char *p;
	long i, n, nn, pos;
	Sbuf *buf;

	n = sbuf_mark_length(m);
	pos = sbuf_mark_begin(m);
	buf = m->buf;

	while (n > 0) {
		nn = (n < IO_BUFFER_SIZE) ? n : IO_BUFFER_SIZE;
		n -= nn;
		for (i = 0, p = iobuf; i < nn; ++i, ++p, ++pos)
			*p = sbuf_charat(buf, pos);
		if (fwrite(iobuf, nn, 1, f) != 1) {
			__publib_error("error writing to file");
			return -1;
		}
	}

	return 0;
}


/**********************************************************************
 * Local functions.
 */


/*
 * 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, or invent
 *		them if the file is new (use umask).
 * 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;
	mode_t mode;
	
	if (stat(src, &st) == -1) {
		if (errno != ENOENT) {
			__publib_error("Can't stat file (can't complete save)");
			return -1;
		}
		mode = umask(0);
		(void) umask(mode);
		mode = 0666 & ~mode;
	} else
		mode = st.st_mode;
	if (chmod(tgt, 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, char *orig, size_t max) {
	if (strlen(orig) + 2 > max)
		return -1;

	sprintf(back, "%s~", orig);
	return 0;
}