File: filebuf.c

package info (click to toggle)
unsort 1.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 532 kB
  • ctags: 134
  • sloc: ansic: 1,401; sh: 1,042; makefile: 5
file content (158 lines) | stat: -rw-r--r-- 4,071 bytes parent folder | download
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
/*****************************************************************************

	unsort - reorder files semi-randomly
	Copyright (C) 2007, 2008  Wessel Dankers <wsl@fruit.je>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

*****************************************************************************/

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/mman.h>

#include "error.h"
#include "filebuf.h"

const filebuf_t filebuf_0 = {0};

#define filebuf_off(buf, off) (((uint8_t *)(buf)) + (size_t)(off))

static int filebuf_tmp(void) {
	FILE *fh;
	int fd;
	fh = tmpfile();
	if(!fh)
		exit_perror(ERROR_SYSTEM, "Can't create temporary file");
	fd = dup(fileno(fh));
	if(fd == -1)
		exit_perror(ERROR_SYSTEM, "Can't dup(%d)", fileno(fh));
	fclose(fh);
	return fd;
}

static void write_all(int fd, void *buf, size_t len) {
	ssize_t r;
	while(len) {
		r = write(fd, buf, len);
		if(!r)
			exit_error(ERROR_SYSTEM, "Can't write to fd %d", fd);
		if(r < 0)
			exit_perror(ERROR_SYSTEM, "Can't write to fd %d", fd);
		buf = filebuf_off(buf, r);
		len -= (size_t)r;
	}
}

static bool filebuf_mmap(int fd, filebuf_t *fb, size_t len) {
	void *buf;
	buf = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, (ssize_t)0);
	if(buf == MAP_FAILED)
		return true;
	fb->buf = buf;
	fb->len = len;
	return false;
}

#define BUFSIZE 1048576
#define MINREAD 4096

static void *streambuf = NULL;
static size_t streamleft = BUFSIZE;

static void filebuf_stream(int fd, filebuf_t *fb) {
	void *buf;
	size_t len = 0, left, edge, offset = 0;
	ssize_t r;
	int tmp;
	static long pg = -1;

	if(!streambuf)
		streambuf = xalloc(streamleft);

	buf = streambuf;
	left = streamleft;

	while(left >= MINREAD) {
		r = read(fd, filebuf_off(buf, len), left);
		if(r < 0)
			exit_perror(ERROR_SYSTEM, "Can't read from fd %d", fd);
		if(!r) {
			if(len) {
				streambuf = filebuf_off(streambuf, len);
				streamleft -= len;
			} else {
				buf = NULL;
			}
			fb->buf = buf;
			fb->len = len;
			return;
		}
		len += (size_t)r;
		left -= (size_t)r;
	}

	tmp = filebuf_tmp();
	write_all(tmp, buf, len);
	buf = NULL;

	if(pg == -1)
		pg = sysconf(_SC_PAGESIZE);

	left = 0;
	for(;;) {
		if(left < MINREAD) {
			if(buf && munmap(buf, (size_t)BUFSIZE) == -1)
				exit_perror(ERROR_SYSTEM, "Can't munmap %d bytes", BUFSIZE);
			edge = len % (size_t)pg;
			offset = len - edge;
			left = BUFSIZE - edge;
			if(ftruncate(tmp, (off_t)(offset + BUFSIZE)) == -1)
				exit_perror(ERROR_SYSTEM, "Can't extend fd %d to %ld bytes", tmp, (long)(offset + BUFSIZE));
			buf = mmap(NULL, (size_t)BUFSIZE, PROT_WRITE, MAP_SHARED, tmp, (off_t)offset);
			if(buf == MAP_FAILED)
				exit_perror(ERROR_SYSTEM, "Can't mmap fd %d for writing", fd);
		}
		r = read(fd, (char *)buf + len - offset, left);
		if(!r)
			break;
		if(r < 0)
			exit_perror(ERROR_SYSTEM, "Can't read from fd %d", tmp);
		len += (size_t)r;
		left -= (size_t)r;
	}

	if(buf && munmap(buf, (size_t)BUFSIZE) == -1)
		exit_perror(ERROR_SYSTEM, "Can't munmap %d bytes", BUFSIZE);
	if(ftruncate(tmp, (off_t)len) == -1)
		exit_perror(ERROR_SYSTEM, "Can't truncate fd %d to %ld bytes", tmp, (long)len);

	if(filebuf_mmap(tmp, fb, len))
		exit_perror(ERROR_SYSTEM, "Can't mmap fd %d", tmp);

	close(tmp);
}

void filebuf_init(filebuf_t *fb, int fd) {
	struct stat st;

	if(fstat(fd, &st) || !st.st_size || filebuf_mmap(fd, fb, (size_t)st.st_size))
		filebuf_stream(fd, fb);
}