| 
									
										
										
										
											2011-02-23 10:52:22 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2008-04-16 22:40:48 +00:00
										 |  |  |  * ***** BEGIN GPL LICENSE BLOCK ***** | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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 2 | 
					
						
							| 
									
										
										
										
											2008-04-16 22:40:48 +00:00
										 |  |  |  * of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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, write to the Free Software Foundation, | 
					
						
							| 
									
										
										
										
											2010-02-12 13:34:04 +00:00
										 |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is Copyright (C) 2004 Blender Foundation | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is: all of this file. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): none yet. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2008-04-16 22:40:48 +00:00
										 |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  |  * .blend file reading entry point | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-27 20:35:41 +00:00
										 |  |  | /** \file blender/blenloader/intern/undofile.c
 | 
					
						
							|  |  |  |  *  \ingroup blenloader | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <math.h>
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | #include <fcntl.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* open/close */ | 
					
						
							|  |  |  | #ifndef _WIN32
 | 
					
						
							|  |  |  | #  include <unistd.h>
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #  include <io.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-05 14:18:45 +00:00
										 |  |  | #include "DNA_listBase.h"
 | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_blenlib.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-05 17:56:54 +00:00
										 |  |  | #include "BLO_undofile.h"
 | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | #include "BLO_readfile.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BKE_main.h"
 | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-27 14:44:17 +02:00
										 |  |  | /* keep last */ | 
					
						
							|  |  |  | #include "BLI_strict_flags.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | /* **************** support for memory-write, for undo buffers *************** */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* not memfile itself */ | 
					
						
							| 
									
										
										
										
											2015-04-18 17:33:04 +02:00
										 |  |  | void BLO_memfile_free(MemFile *memfile) | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	MemFileChunk *chunk; | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-08-26 23:37:08 +00:00
										 |  |  | 	while ((chunk = BLI_pophead(&memfile->chunks))) { | 
					
						
							| 
									
										
										
										
											2018-03-27 14:44:17 +02:00
										 |  |  | 		if (chunk->is_identical == false) { | 
					
						
							|  |  |  | 			MEM_freeN((void *)chunk->buf); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | 		MEM_freeN(chunk); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-05-12 15:02:10 +00:00
										 |  |  | 	memfile->size = 0; | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-26 14:00:13 +00:00
										 |  |  | /* to keep list of memfiles consistent, 'first' is always first in list */ | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | /* result is that 'first' is being freed */ | 
					
						
							| 
									
										
										
										
											2015-04-18 17:33:04 +02:00
										 |  |  | void BLO_memfile_merge(MemFile *first, MemFile *second) | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	MemFileChunk *fc, *sc; | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2012-05-12 15:02:10 +00:00
										 |  |  | 	fc = first->chunks.first; | 
					
						
							|  |  |  | 	sc = second->chunks.first; | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | 	while (fc || sc) { | 
					
						
							| 
									
										
										
										
											2012-03-24 06:18:31 +00:00
										 |  |  | 		if (fc && sc) { | 
					
						
							| 
									
										
										
										
											2018-03-27 14:44:17 +02:00
										 |  |  | 			if (sc->is_identical) { | 
					
						
							|  |  |  | 				sc->is_identical = false; | 
					
						
							|  |  |  | 				fc->is_identical = true; | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2012-05-12 15:02:10 +00:00
										 |  |  | 		if (fc) fc = fc->next; | 
					
						
							|  |  |  | 		if (sc) sc = sc->next; | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2015-04-18 17:33:04 +02:00
										 |  |  | 	BLO_memfile_free(first); | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-18 17:33:04 +02:00
										 |  |  | void memfile_chunk_add(MemFile *compare, MemFile *current, const char *buf, unsigned int size) | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-05-12 15:02:10 +00:00
										 |  |  | 	static MemFileChunk *compchunk = NULL; | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | 	MemFileChunk *curchunk; | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2012-08-04 12:30:16 +00:00
										 |  |  | 	/* this function inits when compare != NULL or when current == NULL  */ | 
					
						
							| 
									
										
										
										
											2012-03-24 06:18:31 +00:00
										 |  |  | 	if (compare) { | 
					
						
							| 
									
										
										
										
											2012-05-12 15:02:10 +00:00
										 |  |  | 		compchunk = compare->chunks.first; | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-05-12 15:02:10 +00:00
										 |  |  | 	if (current == NULL) { | 
					
						
							|  |  |  | 		compchunk = NULL; | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2012-05-12 15:02:10 +00:00
										 |  |  | 	curchunk = MEM_mallocN(sizeof(MemFileChunk), "MemFileChunk"); | 
					
						
							|  |  |  | 	curchunk->size = size; | 
					
						
							|  |  |  | 	curchunk->buf = NULL; | 
					
						
							| 
									
										
										
										
											2018-03-27 14:44:17 +02:00
										 |  |  | 	curchunk->is_identical = false; | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | 	BLI_addtail(¤t->chunks, curchunk); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/* we compare compchunk with buf */ | 
					
						
							| 
									
										
										
										
											2012-03-24 06:18:31 +00:00
										 |  |  | 	if (compchunk) { | 
					
						
							|  |  |  | 		if (compchunk->size == curchunk->size) { | 
					
						
							| 
									
										
										
										
											2016-07-08 15:42:50 +10:00
										 |  |  | 			if (memcmp(compchunk->buf, buf, size) == 0) { | 
					
						
							| 
									
										
										
										
											2012-05-12 15:02:10 +00:00
										 |  |  | 				curchunk->buf = compchunk->buf; | 
					
						
							| 
									
										
										
										
											2018-03-27 14:44:17 +02:00
										 |  |  | 				curchunk->is_identical = true; | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2012-05-12 15:02:10 +00:00
										 |  |  | 		compchunk = compchunk->next; | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-03-27 14:44:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | 	/* not equal... */ | 
					
						
							| 
									
										
										
										
											2012-05-12 15:02:10 +00:00
										 |  |  | 	if (curchunk->buf == NULL) { | 
					
						
							| 
									
										
										
										
											2018-03-27 14:44:17 +02:00
										 |  |  | 		char *buf_new = MEM_mallocN(size, "Chunk buffer"); | 
					
						
							|  |  |  | 		memcpy(buf_new, buf, size); | 
					
						
							|  |  |  | 		curchunk->buf = buf_new; | 
					
						
							| 
									
										
										
										
											2004-09-05 13:59:08 +00:00
										 |  |  | 		current->size += size; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct Main *BLO_memfile_main_get(struct MemFile *memfile, struct Main *oldmain, struct Scene **r_scene) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct Main *bmain_undo = NULL; | 
					
						
							|  |  |  | 	BlendFileData *bfd = BLO_read_from_memfile(oldmain, oldmain->name, memfile, NULL, BLO_READ_SKIP_NONE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (bfd) { | 
					
						
							|  |  |  | 		bmain_undo = bfd->main; | 
					
						
							|  |  |  | 		if (r_scene) { | 
					
						
							|  |  |  | 			*r_scene = bfd->curscene; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		MEM_freeN(bfd); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return bmain_undo; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Saves .blend using undo buffer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \return success. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | bool BLO_memfile_write_file(struct MemFile *memfile, const char *filename) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	MemFileChunk *chunk; | 
					
						
							|  |  |  | 	int file, oflags; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* note: This is currently used for autosave and 'quit.blend', where _not_ following symlinks is OK,
 | 
					
						
							|  |  |  | 	 * however if this is ever executed explicitly by the user, we may want to allow writing to symlinks. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	oflags = O_BINARY | O_WRONLY | O_CREAT | O_TRUNC; | 
					
						
							|  |  |  | #ifdef O_NOFOLLOW
 | 
					
						
							|  |  |  | 	/* use O_NOFOLLOW to avoid writing to a symlink - use 'O_EXCL' (CVE-2008-1103) */ | 
					
						
							|  |  |  | 	oflags |= O_NOFOLLOW; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 	/* TODO(sergey): How to deal with symlinks on windows? */ | 
					
						
							|  |  |  | #  ifndef _MSC_VER
 | 
					
						
							|  |  |  | #    warning "Symbolic links will be followed on undo save, possibly causing CVE-2008-1103"
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 	file = BLI_open(filename,  oflags, 0666); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (file == -1) { | 
					
						
							|  |  |  | 		fprintf(stderr, "Unable to save '%s': %s\n", | 
					
						
							|  |  |  | 		        filename, errno ? strerror(errno) : "Unknown error opening file"); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (chunk = memfile->chunks.first; chunk; chunk = chunk->next) { | 
					
						
							| 
									
										
										
										
											2018-04-01 07:41:23 +02:00
										 |  |  | 		if ((size_t)write(file, chunk->buf, chunk->size) != chunk->size) { | 
					
						
							| 
									
										
										
										
											2018-03-19 14:17:59 +01:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	close(file); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (chunk) { | 
					
						
							|  |  |  | 		fprintf(stderr, "Unable to save '%s': %s\n", | 
					
						
							|  |  |  | 		        filename, errno ? strerror(errno) : "Unknown error writing file"); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } |