Project

General

Profile

Bug #2588 » chunk.c.patch

Patch to ensure that the space to store the chunk data is available - dirk4000, 2014-08-08 08:53

View differences:

chunk_new.c 2017-08-28 01:47:48.000000000 +0200
275 275
	return 0;
276 276
}
277 277

  
278

  
279

  
280
#ifdef __linux__
281
#include <linux/version.h>
282
#endif
283
#include <stdlib.h> /* to get retrieved the POSIX_SOURCE defines  */
284

  
285
int os_fallocate(int fd, off_t filesize) {
286
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
287
        return fallocate(fd, 0, 0, filesize);
288
#elif _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L
289
        return posix_fallocate(fd, 0, filesize);
290
#else
291
	/* fprintf(stderr, "unsupported platform, old upload bug can still occur\n"); */
292
#endif
293
}
294

  
278 295
chunk *chunkqueue_get_append_tempfile(chunkqueue *cq) {
279 296
	chunk *c;
280 297
	buffer *template = buffer_init_string("/var/tmp/lighttpd-upload-XXXXXX");
......
298 315

  
299 316
			if (-1 != (c->file.fd = mkstemp(template->ptr))) {
300 317
				/* only trigger the unlink if we created the temp-file successfully */
301
				c->file.is_temp = 1;
302
				break;
318
				/* ensure that there is enough storage for our data */
319
				const int MAX_CHUNK_SIZE = 1 * 1024 * 1024; 
320
				if (os_fallocate(c->file.fd, MAX_CHUNK_SIZE + 1) ) {
321
				/*	fprintf(stderr, "not enough space on storage: %d\n", i); */
322
					close(c->file.fd);
323
					c->file.fd = -1;
324
					unlink(template->ptr);
325
				}
326
				else {
327
					lseek(c->file.fd, 0, SEEK_SET);
328
					c->file.is_temp = 1;
329
					break;
330
				} 
303 331
			}
304 332
		}
305 333
	} else {
(1-1/2)