Exemple #1
0
/* saves .blend using undo buffer, returns 1 == success */
int BKE_undo_save_file(const char *filename)
{
	UndoElem *uel;
	MemFileChunk *chunk;
	const int flag = O_BINARY + O_WRONLY + O_CREAT + O_TRUNC + O_EXCL;
	int file;

	if ((U.uiflag & USER_GLOBALUNDO) == 0) {
		return 0;
	}

	uel = curundo;
	if (uel == NULL) {
		fprintf(stderr, "No undo buffer to save recovery file\n");
		return 0;
	}

	/* first try create the file, if it exists call without 'O_CREAT',
	 * to avoid writing to a symlink - use 'O_EXCL' (CVE-2008-1103) */
	errno = 0;
	file = BLI_open(filename, flag, 0666);
	if (file == -1) {
		if (errno == EEXIST) {
			errno = 0;
			file = BLI_open(filename, flag & ~O_CREAT, 0666);
		}
	}

	if (file == -1) {
		fprintf(stderr, "Unable to save '%s': %s\n",
		        filename, errno ? strerror(errno) : "Unknown error opening file");
		return 0;
	}

	for (chunk = uel->memfile.chunks.first; chunk; chunk = chunk->next) {
		if (write(file, chunk->buf, chunk->size) != chunk->size) {
			break;
		}
	}
	
	close(file);
	
	if (chunk) {
		fprintf(stderr, "Unable to save '%s': %s\n",
		        filename, errno ? strerror(errno) : "Unknown error writing file");
		return 0;
	}
	return 1;
}
Exemple #2
0
ImBuf *IMB_loadiffname(const char *filepath, int flags, char colorspace[IM_MAX_SPACE])
{
	ImBuf *ibuf;
	int file, a;
	char filepath_tx[IB_FILENAME_SIZE];

	imb_cache_filename(filepath_tx, filepath, flags);

	file = BLI_open(filepath_tx, O_BINARY | O_RDONLY, 0);
	if (file < 0) return NULL;

	ibuf = IMB_loadifffile(file, filepath, flags, colorspace, filepath_tx);

	if (ibuf) {
		BLI_strncpy(ibuf->name, filepath, sizeof(ibuf->name));
		BLI_strncpy(ibuf->cachename, filepath_tx, sizeof(ibuf->cachename));
		for (a = 1; a < ibuf->miptot; a++)
			BLI_strncpy(ibuf->mipmap[a - 1]->cachename, filepath_tx, sizeof(ibuf->cachename));
		if (flags & IB_fields) IMB_de_interlace(ibuf);
	}

	close(file);

	return ibuf;
}
Exemple #3
0
/* checks if image was already loaded, then returns same image
 * otherwise creates new.
 * does not load ibuf itself
 * pass on optional frame for #name images */
MovieClip *BKE_movieclip_file_add(const char *name)
{
    MovieClip *clip;
    MovieClipUser user = {0};
    int file, len, width, height;
    const char *libname;
    char str[FILE_MAX], strtest[FILE_MAX];

    BLI_strncpy(str, name, sizeof(str));
    BLI_path_abs(str, G.main->name);

    /* exists? */
    file = BLI_open(str, O_BINARY | O_RDONLY, 0);
    if (file == -1)
        return NULL;
    close(file);

    /* ** first search an identical clip ** */
    for (clip = G.main->movieclip.first; clip; clip = clip->id.next) {
        BLI_strncpy(strtest, clip->name, sizeof(clip->name));
        BLI_path_abs(strtest, G.main->name);

        if (strcmp(strtest, str) == 0) {
            BLI_strncpy(clip->name, name, sizeof(clip->name));  /* for stringcode */
            clip->id.us++;  /* officially should not, it doesn't link here! */

            return clip;
        }
    }

    /* ** add new movieclip ** */

    /* create a short library name */
    len = strlen(name);

    while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\')
        len--;
    libname = name + len;

    clip = movieclip_alloc(libname);
    BLI_strncpy(clip->name, name, sizeof(clip->name));

    if (BLI_testextensie_array(name, imb_ext_movie))
        clip->source = MCLIP_SRC_MOVIE;
    else
        clip->source = MCLIP_SRC_SEQUENCE;

    user.framenr = 1;
    BKE_movieclip_get_size(clip, &user, &width, &height);
    if (width && height) {
        clip->tracking.camera.principal[0] = ((float)width) / 2.0f;
        clip->tracking.camera.principal[1] = ((float)height) / 2.0f;

        clip->tracking.camera.focal = 24.0f * width / clip->tracking.camera.sensor_width;
    }

    movieclip_calc_length(clip);

    return clip;
}
Exemple #4
0
int BLO_is_a_runtime(const char *path)
{
	int res = 0, fd = BLI_open(path, O_BINARY | O_RDONLY, 0);
	int datastart;
	char buf[8];

	if (fd == -1)
		goto cleanup;
	
	lseek(fd, -12, SEEK_END);
	
	datastart = handle_read_msb_int(fd);

	if (datastart == -1)
		goto cleanup;
	else if (read(fd, buf, 8) != 8)
		goto cleanup;
	else if (memcmp(buf, "BRUNTIME", 8) != 0)
		goto cleanup;
	else
		res = 1;

cleanup:
	if (fd != -1)
		close(fd);

	return res;
}
Exemple #5
0
ImBuf *IMB_testiffname(const char *filepath, int flags)
{
	ImBuf *ibuf;
	int file;
	char filepath_tx[IB_FILENAME_SIZE];
	char colorspace[IM_MAX_SPACE] = "\0";

	BLI_assert(!BLI_path_is_rel(filepath));

	imb_cache_filename(filepath_tx, filepath, flags);

	file = BLI_open(filepath_tx, O_BINARY | O_RDONLY, 0);
	if (file == -1)
		return NULL;

	ibuf = IMB_loadifffile(file, filepath, flags | IB_test | IB_multilayer, colorspace, filepath_tx);

	if (ibuf) {
		BLI_strncpy(ibuf->name, filepath, sizeof(ibuf->name));
		BLI_strncpy(ibuf->cachename, filepath_tx, sizeof(ibuf->cachename));
	}

	close(file);

	return ibuf;
}
Exemple #6
0
int writePackedFile(ReportList *reports, const char *filename, PackedFile *pf, int guimode)
{
	int file, number, remove_tmp = FALSE;
	int ret_value = RET_OK;
	char name[FILE_MAX];
	char tempname[FILE_MAX];
/*  	void *data; */
	
	if (guimode) {} //XXX  waitcursor(1);
	
	BLI_strncpy(name, filename, sizeof(name));
	BLI_path_abs(name, G.main->name);
	
	if (BLI_exists(name)) {
		for (number = 1; number <= 999; number++) {
			BLI_snprintf(tempname, sizeof(tempname), "%s.%03d_", name, number);
			if (! BLI_exists(tempname)) {
				if (BLI_copy(name, tempname) == RET_OK) {
					remove_tmp = TRUE;
				}
				break;
			}
		}
	}
	
	// make sure the path to the file exists...
	BLI_make_existing_file(name);
	
	file = BLI_open(name, O_BINARY + O_WRONLY + O_CREAT + O_TRUNC, 0666);
	if (file >= 0) {
		if (write(file, pf->data, pf->size) != pf->size) {
			BKE_reportf(reports, RPT_ERROR, "Error writing file: %s", name);
			ret_value = RET_ERROR;
		}
		close(file);
	}
	else {
		BKE_reportf(reports, RPT_ERROR, "Error creating file: %s", name);
		ret_value = RET_ERROR;
	}
	
	if (remove_tmp) {
		if (ret_value == RET_ERROR) {
			if (BLI_rename(tempname, name) != 0) {
				BKE_reportf(reports, RPT_ERROR, "Error restoring tempfile. Check files: '%s' '%s'", tempname, name);
			}
		}
		else {
			if (BLI_delete(tempname, 0, 0) != 0) {
				BKE_reportf(reports, RPT_ERROR, "Error deleting '%s' (ignored)", tempname);
			}
		}
	}
	
	if (guimode) {} //XXX waitcursor(0);

	return (ret_value);
}
Exemple #7
0
/* checks if image was already loaded, then returns same image
 * otherwise creates new.
 * does not load ibuf itself
 * pass on optional frame for #name images */
MovieClip *BKE_movieclip_file_add(Main *bmain, const char *name)
{
	MovieClip *clip;
	int file, len;
	const char *libname;
	char str[FILE_MAX], strtest[FILE_MAX];

	BLI_strncpy(str, name, sizeof(str));
	BLI_path_abs(str, bmain->name);

	/* exists? */
	file = BLI_open(str, O_BINARY | O_RDONLY, 0);
	if (file == -1)
		return NULL;
	close(file);

	/* ** first search an identical clip ** */
	for (clip = bmain->movieclip.first; clip; clip = clip->id.next) {
		BLI_strncpy(strtest, clip->name, sizeof(clip->name));
		BLI_path_abs(strtest, G.main->name);

		if (strcmp(strtest, str) == 0) {
			BLI_strncpy(clip->name, name, sizeof(clip->name));  /* for stringcode */
			clip->id.us++;  /* officially should not, it doesn't link here! */

			return clip;
		}
	}

	/* ** add new movieclip ** */

	/* create a short library name */
	len = strlen(name);

	while (len > 0 && name[len - 1] != '/' && name[len - 1] != '\\')
		len--;
	libname = name + len;

	clip = movieclip_alloc(bmain, libname);
	BLI_strncpy(clip->name, name, sizeof(clip->name));

	detect_clip_source(clip);

	movieclip_load_get_szie(clip);
	if (clip->lastsize[0]) {
		int width = clip->lastsize[0];

		clip->tracking.camera.focal = 24.0f * width / clip->tracking.camera.sensor_width;
	}

	movieclip_calc_length(clip);

	return clip;
}
Exemple #8
0
void imb_loadtile(ImBuf *ibuf, int tx, int ty, unsigned int *rect)
{
	int file;

	file = BLI_open(ibuf->cachename, O_BINARY | O_RDONLY, 0);
	if (file < 0) return;

	imb_loadtilefile(ibuf, file, tx, ty, rect);

	close(file);
}
Exemple #9
0
size_t BLI_file_size(const char *path)
{
	int size, file = BLI_open(path, O_BINARY|O_RDONLY, 0);
	
	if (file == -1)
		return -1;
	
	size = BLI_file_descriptor_size(file);
	close(file);
	return size;
}
Exemple #10
0
/**
 * Saves .blend using undo buffer.
 *
 * \return success.
 */
bool BKE_undo_save_file(const char *filename)
{
	UndoElem *uel;
	MemFileChunk *chunk;
	int file, oflags;

	if ((U.uiflag & USER_GLOBALUNDO) == 0) {
		return false;
	}

	uel = curundo;
	if (uel == NULL) {
		fprintf(stderr, "No undo buffer to save recovery file\n");
		return false;
	}

	/* 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 = uel->memfile.chunks.first; chunk; chunk = chunk->next) {
		if (write(file, chunk->buf, chunk->size) != chunk->size) {
			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;
}
Exemple #11
0
int IMB_ispic_type(const char *name)
{
	/* increased from 32 to 64 because of the bitmaps header size */
#define HEADER_SIZE 64

	unsigned char buf[HEADER_SIZE];
	const ImFileType *type;
	BLI_stat_t st;
	int fp;

	BLI_assert(!BLI_path_is_rel(name));

	if (UTIL_DEBUG) printf("%s: loading %s\n", __func__, name);
	
	if (BLI_stat(name, &st) == -1)
		return false;
	if (((st.st_mode) & S_IFMT) != S_IFREG)
		return false;

	if ((fp = BLI_open(name, O_BINARY | O_RDONLY, 0)) == -1)
		return false;

	memset(buf, 0, sizeof(buf));
	if (read(fp, buf, HEADER_SIZE) <= 0) {
		close(fp);
		return false;
	}

	close(fp);

	/* XXX move this exception */
	if ((BIG_LONG(((int *)buf)[0]) & 0xfffffff0) == 0xffd8ffe0)
		return IMB_FTYPE_JPG;

	for (type = IMB_FILE_TYPES; type < IMB_FILE_TYPES_LAST; type++) {
		if (type->is_a) {
			if (type->is_a(buf)) {
				return type->filetype;
			}
		}
		else if (type->is_a_filepath) {
			if (type->is_a_filepath(name)) {
				return type->filetype;
			}
		}
	}

	return 0;

#undef HEADER_SIZE
}
Exemple #12
0
int checkPackedFile(const char *filename, PackedFile *pf)
{
	struct stat st;
	int ret_val, i, len, file;
	char buf[4096];
	char name[FILE_MAX];
	
	BLI_strncpy(name, filename, sizeof(name));
	BLI_path_abs(name, G.main->name);
	
	if (stat(name, &st)) {
		ret_val = PF_NOFILE;
	}
	else if (st.st_size != pf->size) {
		ret_val = PF_DIFFERS;
	}
	else {
		// we'll have to compare the two...
		
		file = BLI_open(name, O_BINARY | O_RDONLY, 0);
		if (file < 0) {
			ret_val = PF_NOFILE;
		}
		else {
			ret_val = PF_EQUAL;
			
			for (i = 0; i < pf->size; i += sizeof(buf)) {
				len = pf->size - i;
				if (len > sizeof(buf)) {
					len = sizeof(buf);
				}
				
				if (read(file, buf, len) != len) {
					// read error ...
					ret_val = PF_DIFFERS;
					break;
				}
				else {
					if (memcmp(buf, ((char *)pf->data) + i, len)) {
						ret_val = PF_DIFFERS;
						break;
					}
				}
			}
			
			close(file);
		}
	}
	
	return(ret_val);
}
Exemple #13
0
PackedFile *newPackedFile(ReportList *reports, const char *filename, const char *basepath)
{
	PackedFile *pf = NULL;
	int file, filelen;
	char name[FILE_MAX];
	void *data;
	
	/* render result has no filename and can be ignored
	 * any other files with no name can be ignored too */
	if (filename[0] == '\0')
		return NULL;

	//XXX waitcursor(1);
	
	/* convert relative filenames to absolute filenames */

	BLI_strncpy(name, filename, sizeof(name));
	BLI_path_abs(name, basepath);

	/* open the file
	 * and create a PackedFile structure */

	file = BLI_open(name, O_BINARY | O_RDONLY, 0);
	if (file == -1) {
		BKE_reportf(reports, RPT_ERROR, "Unable to pack file, source path '%s' not found", name);
	}
	else {
		filelen = BLI_file_descriptor_size(file);

		if (filelen == 0) {
			/* MEM_mallocN complains about MEM_mallocN(0, "bla");
			 * we don't care.... */
			data = MEM_mallocN(1, "packFile");
		}
		else {
			data = MEM_mallocN(filelen, "packFile");
		}
		if (read(file, data, filelen) == filelen) {
			pf = newPackedFileMemory(data, filelen);
		}
		else {
			MEM_freeN(data);
		}

		close(file);
	}

	//XXX waitcursor(0);
		
	return (pf);
}
Exemple #14
0
static unsigned char *proxy_thread_next_frame(ProxyQueue *queue, MovieClip *clip, size_t *size_r, int *cfra_r)
{
	unsigned char *mem = NULL;

	BLI_spin_lock(&queue->spin);
	if (!*queue->stop && queue->cfra <= queue->efra) {
		MovieClipUser user = {0};
		char name[FILE_MAX];
		size_t size;
		int file;

		user.framenr = queue->cfra;

		BKE_movieclip_filename_for_frame(clip, &user, name);

		file = BLI_open(name, O_BINARY | O_RDONLY, 0);
		if (file < 0) {
			BLI_spin_unlock(&queue->spin);
			return NULL;
		}

		size = BLI_file_descriptor_size(file);
		if (size < 1) {
			close(file);
			BLI_spin_unlock(&queue->spin);
			return NULL;
		}

		mem = MEM_mallocN(size, "movieclip proxy memory file");

		if (read(file, mem, size) != size) {
			close(file);
			BLI_spin_unlock(&queue->spin);
			MEM_freeN(mem);
			return NULL;
		}

		*size_r = size;
		*cfra_r = queue->cfra;

		queue->cfra++;
		close(file);

		*queue->do_update = 1;
		*queue->progress = (float)(queue->cfra - queue->sfra) / (queue->efra - queue->sfra);
	}
	BLI_spin_unlock(&queue->spin);

	return mem;
}
Exemple #15
0
BlendFileData *BLO_read_runtime(const char *path, ReportList *reports)
{
	BlendFileData *bfd = NULL;
	size_t actualsize;
	int fd, datastart;
	char buf[8];

	fd = BLI_open(path, O_BINARY | O_RDONLY, 0);

	if (fd == -1) {
		BKE_reportf(reports, RPT_ERROR, "Unable to open '%s': %s", path, strerror(errno));
		goto cleanup;
	}
	
	actualsize = BLI_file_descriptor_size(fd);

	lseek(fd, -12, SEEK_END);

	datastart = handle_read_msb_int(fd);

	if (datastart == -1) {
		BKE_reportf(reports, RPT_ERROR, "Unable to read '%s' (problem seeking)", path);
		goto cleanup;
	}
	else if (read(fd, buf, 8) != 8) {
		BKE_reportf(reports, RPT_ERROR, "Unable to read '%s' (truncated header)", path);
		goto cleanup;
	}
	else if (memcmp(buf, "BRUNTIME", 8) != 0) {
		BKE_reportf(reports, RPT_ERROR, "Unable to read '%s' (not a blend file)", path);
		goto cleanup;
	}
	else {
		//printf("starting to read runtime from %s at datastart %d\n", path, datastart);
		lseek(fd, datastart, SEEK_SET);
		bfd = blo_read_blendafterruntime(fd, path, actualsize - datastart, reports);
		fd = -1; // file was closed in blo_read_blendafterruntime()
	}
	
cleanup:
	if (fd != -1)
		close(fd);
	
	return bfd;
}
Exemple #16
0
/* read file for specified frame number to the memory */
static unsigned char *prefetch_read_file_to_memory(
        MovieClip *clip, int current_frame, short render_size, short render_flag,
        size_t *r_size)
{
	MovieClipUser user = {0};
	char name[FILE_MAX];
	size_t size;
	int file;
	unsigned char *mem;

	user.framenr = current_frame;
	user.render_size = render_size;
	user.render_flag = render_flag;

	BKE_movieclip_filename_for_frame(clip, &user, name);

	file = BLI_open(name, O_BINARY | O_RDONLY, 0);
	if (file == -1) {
		return NULL;
	}

	size = BLI_file_descriptor_size(file);
	if (size < 1) {
		close(file);
		return NULL;
	}

	mem = MEM_mallocN(size, "movieclip prefetch memory file");

	if (read(file, mem, size) != size) {
		close(file);
		MEM_freeN(mem);
		return NULL;
	}

	*r_size = size;

	close(file);

	return mem;
}
Exemple #17
0
/* gzip the file in from and write it to "to". 
 * return -1 if zlib fails, -2 if the originating file does not exist
 * note: will remove the "from" file
 */
int BLI_file_gzip(const char *from, const char *to)
{
	char buffer[10240];
	int file;
	int readsize = 0;
	int rval = 0, err;
	gzFile gzfile;

	/* level 1 is very close to 3 (the default) in terms of file size,
	 * but about twice as fast, best use for speedy saving - campbell */
	gzfile = BLI_gzopen(to, "wb1");
	if (gzfile == NULL)
		return -1;
	file = BLI_open(from, O_BINARY | O_RDONLY, 0);
	if (file == -1)
		return -2;

	while (1) {
		readsize = read(file, buffer, sizeof(buffer));

		if (readsize < 0) {
			rval = -2; /* error happened in reading */
			fprintf(stderr, "Error reading file %s: %s.\n", from, strerror(errno));
			break;
		}
		else if (readsize == 0)
			break;  /* done reading */
		
		if (gzwrite(gzfile, buffer, readsize) <= 0) {
			rval = -1; /* error happened in writing */
			fprintf(stderr, "Error writing gz file %s: %s.\n", to, gzerror(gzfile, &err));
			break;
		}
	}
	
	gzclose(gzfile);
	close(file);

	return rval;
}