File: filebuffer.c

package info (click to toggle)
pqiv 2.11-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 700 kB
  • sloc: ansic: 9,781; sh: 360; makefile: 13
file content (256 lines) | stat: -rw-r--r-- 7,659 bytes parent folder | download | duplicates (2)
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
/**
 * pqiv
 *
 * Copyright (c) 2013-2014, Phillip Berndt
 *
 * 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 "filebuffer.h"
#include <errno.h>
#include <string.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#ifdef _POSIX_VERSION
#define HAS_MMAP
#endif

#ifdef HAS_MMAP
#include <sys/mman.h>
#endif

struct buffered_file {
	GBytes *data;
	char *file_name;
	int ref_count;
	gboolean file_name_is_temporary;
};

GHashTable *file_buffer_table = NULL;
GRecMutex file_buffer_table_mutex;

#ifdef HAS_MMAP
extern GFile *gfile_for_commandline_arg(const char *);

struct buffered_file_mmap_info {
	void *ptr;
	int fd;
	size_t size;
};

static void buffered_file_mmap_free_helper(struct buffered_file_mmap_info *info) {
	munmap(info->ptr, info->size);
	close(info->fd);
	g_slice_free(struct buffered_file_mmap_info, info);
}
#endif

GBytes *buffered_file_as_bytes(file_t *file, GInputStream *data, GError **error_pointer) {
	g_rec_mutex_lock(&file_buffer_table_mutex);
	if(!file_buffer_table) {
		file_buffer_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
	}
	struct buffered_file *buffer = g_hash_table_lookup(file_buffer_table, file->file_name);
	if(!buffer) {
		GBytes *data_bytes = NULL;

		if((file->file_flags & FILE_FLAGS_MEMORY_IMAGE)) {
			if(file->file_data_loader) {
				data_bytes = file->file_data_loader(file, error_pointer);
			}
			else {
				data_bytes = g_bytes_ref(file->file_data);
			}

			if(!data_bytes) {
				g_rec_mutex_unlock(&file_buffer_table_mutex);
				return NULL;
			}
		}
		else {

#ifdef HAS_MMAP
			// If this is a local file, try to mmap() it first instead of loading it completely
			GFile *input_file = gfile_for_commandline_arg(file->file_name);
			char *input_file_abspath = g_file_get_path(input_file);
			if(input_file_abspath) {
				GFileInfo *file_info = g_file_query_info(input_file, G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, error_pointer);
				if(!file_info) {
					g_object_unref(input_file);
					g_rec_mutex_unlock(&file_buffer_table_mutex);
					return NULL;
				}
				goffset input_file_size = g_file_info_get_size(file_info);
				g_object_unref(file_info);

				int fd = open(input_file_abspath, O_RDONLY);
				g_free(input_file_abspath);
				if(fd < 0) {
					g_object_unref(input_file);
					g_rec_mutex_unlock(&file_buffer_table_mutex);
					*error_pointer = g_error_new(g_quark_from_static_string("pqiv-filebuffer-error"), 1, "Opening the file failed with errno=%d: %s", errno, strerror(errno));
					return NULL;
				}
				void *input_file_data = mmap(NULL, input_file_size, PROT_READ, MAP_SHARED, fd, 0);

				if(input_file_data != MAP_FAILED) {
					struct buffered_file_mmap_info *mmap_info = g_slice_new(struct buffered_file_mmap_info);
					mmap_info->ptr = input_file_data;
					mmap_info->fd = fd;
					mmap_info->size = input_file_size;

					data_bytes = g_bytes_new_with_free_func(input_file_data, input_file_size, (GDestroyNotify)buffered_file_mmap_free_helper, mmap_info);
				}
				else {
					close(fd);
				}
			}
			g_object_unref(input_file);
#endif

			if(data_bytes) {
				// mmap() above worked
			}
			else if(!data) {
				data = image_loader_stream_file(file, error_pointer);
				if(!data) {
					g_rec_mutex_unlock(&file_buffer_table_mutex);
					return NULL;
				}
				data_bytes = g_input_stream_read_completely(data, image_loader_cancellable, error_pointer);
				g_object_unref(data);
			}
			else {
				data_bytes = g_input_stream_read_completely(data, image_loader_cancellable, error_pointer);
			}

			if(!data_bytes) {
				g_rec_mutex_unlock(&file_buffer_table_mutex);
				return NULL;
			}
		}
		buffer = g_new0(struct buffered_file, 1);
		g_hash_table_insert(file_buffer_table, g_strdup(file->file_name), buffer);
		buffer->data = data_bytes;
	}
	buffer->ref_count++;
	g_rec_mutex_unlock(&file_buffer_table_mutex);
	return buffer->data;
}

char *buffered_file_as_local_file(file_t *file, GInputStream *data, GError **error_pointer) {
	g_rec_mutex_lock(&file_buffer_table_mutex);
	if(!file_buffer_table) {
		file_buffer_table = g_hash_table_new(g_str_hash, g_str_equal);
	}
	struct buffered_file *buffer = g_hash_table_lookup(file_buffer_table, file->file_name);
	if(buffer) {
		buffer->ref_count++;
		g_rec_mutex_unlock(&file_buffer_table_mutex);
		return buffer->file_name;
	}

	buffer = g_new0(struct buffered_file, 1);
	g_hash_table_insert(file_buffer_table, g_strdup(file->file_name), buffer);

	gchar *path = NULL;
	if(!(file->file_flags & FILE_FLAGS_MEMORY_IMAGE)) {
		GFile *input_file = g_file_new_for_commandline_arg(file->file_name);
		path = g_file_get_path(input_file);
		g_object_unref(input_file);
	}
	if(path) {
		buffer->file_name = path;
		buffer->file_name_is_temporary = FALSE;
	}
	else {
		gboolean local_data = FALSE;
		if(!data) {
			data = image_loader_stream_file(file, error_pointer);
			if(!data) {
				g_hash_table_remove(file_buffer_table, file->file_name);
				g_rec_mutex_unlock(&file_buffer_table_mutex);
				return NULL;
			}
			local_data = TRUE;
		}

		GFile *temporary_file;
		GFileIOStream *iostream = NULL;
		gchar *extension = strrchr(file->file_name, '.');
		if(extension) {
			gchar *name_template = g_strdup_printf("pqiv-XXXXXX%s", extension);
			temporary_file = g_file_new_tmp(name_template, &iostream, error_pointer);
			g_free(name_template);
		}
		else {
			temporary_file = g_file_new_tmp("pqiv-XXXXXX.ps", &iostream, error_pointer);
		}
		if(!temporary_file) {
			g_printerr("Failed to buffer %s: Could not create a temporary file in %s\n", file->file_name, g_get_tmp_dir());
			if(local_data) {
				g_object_unref(data);
			}
			g_hash_table_remove(file_buffer_table, file->file_name);
			g_rec_mutex_unlock(&file_buffer_table_mutex);
			return NULL;
		}

		if(g_output_stream_splice(g_io_stream_get_output_stream(G_IO_STREAM(iostream)), data, G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET, image_loader_cancellable, error_pointer) < 0) {
			g_hash_table_remove(file_buffer_table, file->file_name);
			if(local_data) {
				g_object_unref(data);
			}
			g_rec_mutex_unlock(&file_buffer_table_mutex);
			return NULL;
		}

		buffer->file_name = g_file_get_path(temporary_file);
		buffer->file_name_is_temporary = TRUE;

		g_object_unref(iostream);
		g_object_unref(temporary_file);
		if(local_data) {
			g_object_unref(data);
		}
	}

	buffer->ref_count++;
	g_rec_mutex_unlock(&file_buffer_table_mutex);
	return buffer->file_name;
}

void buffered_file_unref(file_t *file) {
	g_rec_mutex_lock(&file_buffer_table_mutex);
	struct buffered_file *buffer = g_hash_table_lookup(file_buffer_table, file->file_name);
	if(!buffer) {
		g_rec_mutex_unlock(&file_buffer_table_mutex);
		return;
	}
	if(--buffer->ref_count == 0) {
		if(buffer->data) {
			g_bytes_unref(buffer->data);
		}
		if(buffer->file_name) {
			if(buffer->file_name_is_temporary) {
				g_unlink(buffer->file_name);
			}
			g_free(buffer->file_name);
		}
		g_hash_table_remove(file_buffer_table, file->file_name);
	}
	g_rec_mutex_unlock(&file_buffer_table_mutex);
}