2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * The Original Code is Copyright (C) 2004 Blender Foundation
17 * All rights reserved.
18 * .blend file reading entry point
39 #include "MEM_guardedalloc.h"
41 #include "DNA_listBase.h"
43 #include "BLI_blenlib.h"
45 #include "BLO_readfile.h"
46 #include "BLO_undofile.h"
51 #include "BLI_strict_flags.h"
53 /* **************** support for memory-write, for undo buffers *************** */
55 /* not memfile itself */
56 void BLO_memfile_free(MemFile *memfile)
60 while ((chunk = BLI_pophead(&memfile->chunks))) {
61 if (chunk->is_identical == false) {
62 MEM_freeN((void *)chunk->buf);
69 /* to keep list of memfiles consistent, 'first' is always first in list */
70 /* result is that 'first' is being freed */
71 void BLO_memfile_merge(MemFile *first, MemFile *second)
73 MemFileChunk *fc, *sc;
75 fc = first->chunks.first;
76 sc = second->chunks.first;
79 if (sc->is_identical) {
80 sc->is_identical = false;
81 fc->is_identical = true;
92 BLO_memfile_free(first);
95 /* Clear is_identical_future before adding next memfile. */
96 void BLO_memfile_clear_future(MemFile *memfile)
98 LISTBASE_FOREACH (MemFileChunk *, chunk, &memfile->chunks) {
99 chunk->is_identical_future = false;
103 void memfile_chunk_add(MemFile *memfile, const char *buf, uint size, MemFileChunk **compchunk_step)
105 MemFileChunk *curchunk = MEM_mallocN(sizeof(MemFileChunk), "MemFileChunk");
106 curchunk->size = size;
107 curchunk->buf = NULL;
108 curchunk->is_identical = false;
109 /* This is unsafe in the sense that an app handler or other code that does not
110 * perform an undo push may make changes after the last undo push that
111 * will then not be undo. Though it's not entirely clear that is wrong behavior. */
112 curchunk->is_identical_future = true;
113 BLI_addtail(&memfile->chunks, curchunk);
115 /* we compare compchunk with buf */
116 if (*compchunk_step != NULL) {
117 MemFileChunk *compchunk = *compchunk_step;
118 if (compchunk->size == curchunk->size) {
119 if (memcmp(compchunk->buf, buf, size) == 0) {
120 curchunk->buf = compchunk->buf;
121 curchunk->is_identical = true;
122 compchunk->is_identical_future = true;
125 *compchunk_step = compchunk->next;
129 if (curchunk->buf == NULL) {
130 char *buf_new = MEM_mallocN(size, "Chunk buffer");
131 memcpy(buf_new, buf, size);
132 curchunk->buf = buf_new;
133 memfile->size += size;
137 struct Main *BLO_memfile_main_get(struct MemFile *memfile,
138 struct Main *oldmain,
139 struct Scene **r_scene)
141 struct Main *bmain_undo = NULL;
142 BlendFileData *bfd = BLO_read_from_memfile(oldmain,
143 BKE_main_blendfile_path(oldmain),
145 &(const struct BlendFileReadParams){0},
149 bmain_undo = bfd->main;
151 *r_scene = bfd->curscene;
161 * Saves .blend using undo buffer.
165 bool BLO_memfile_write_file(struct MemFile *memfile, const char *filename)
170 /* note: This is currently used for autosave and 'quit.blend',
171 * where _not_ following symlinks is OK,
172 * however if this is ever executed explicitly by the user,
173 * we may want to allow writing to symlinks.
176 oflags = O_BINARY | O_WRONLY | O_CREAT | O_TRUNC;
178 /* use O_NOFOLLOW to avoid writing to a symlink - use 'O_EXCL' (CVE-2008-1103) */
179 oflags |= O_NOFOLLOW;
181 /* TODO(sergey): How to deal with symlinks on windows? */
183 # warning "Symbolic links will be followed on undo save, possibly causing CVE-2008-1103"
186 file = BLI_open(filename, oflags, 0666);
190 "Unable to save '%s': %s\n",
192 errno ? strerror(errno) : "Unknown error opening file");
196 for (chunk = memfile->chunks.first; chunk; chunk = chunk->next) {
197 if ((size_t)write(file, chunk->buf, chunk->size) != chunk->size) {
206 "Unable to save '%s': %s\n",
208 errno ? strerror(errno) : "Unknown error writing file");