2002-10-12 11:37:38 +00:00
|
|
|
/*
|
2007-12-29 14:31:26 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +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.
|
2002-10-12 11:37:38 +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.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): none yet.
|
|
|
|
*
|
2007-12-29 14:31:26 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2002-10-12 11:37:38 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-27 20:37:56 +00:00
|
|
|
/** \file blender/blenlib/intern/fileops.c
|
|
|
|
* \ingroup bli
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2009-09-06 13:20:05 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
2005-07-27 21:31:44 +00:00
|
|
|
#include "zlib.h"
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#ifdef WIN32
|
2005-07-27 21:31:44 +00:00
|
|
|
#include <io.h>
|
2009-09-06 13:20:05 +00:00
|
|
|
#include "BLI_winstuff.h"
|
2010-09-15 16:13:32 +00:00
|
|
|
#include "BLI_callbacks.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
#else
|
2009-01-17 14:56:12 +00:00
|
|
|
#include <unistd.h> // for read close
|
2002-10-12 11:37:38 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
|
2011-09-17 20:50:22 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "BLI_blenlib.h"
|
|
|
|
|
Patch provided by Shaul Kedem: Compressed files are back!
He even made a nice doc in wiki:
http://wiki.blender.org/bin/view.pl/Blenderdev/Blendgz
Usage: set the option "Compress File" in the main "File" pulldown menu.
This setting is a user-def, meaning it is not changed on reading files.
If you want it default, save it with CTRL+U.
The longest debate went over the file naming convention. Shaul started
with .blend.gz files, which gave issues in Blender because of the code
hanging out everywhere that detects blender files, and that appends the
.blend extension if needed.
Daniel Dunbar proposed to just save it as .blend, and not bother users
with such details. This is indeed the most elegant solution, with as
only drawback that old Blender executables cannot read it.
This drawback isn't very relevant at the moment, since we're heading
towards a release that isn't upward compatible anyway... the recode
going on on Meshes, Modfiers, Armatures, Poses, Actions, NLA already
have upward compatibility issues.
We might check - during the next month(s) - on a builtin system to
warn users in the future when we change things that make a file risky
to read in an older release.
2005-07-27 19:46:06 +00:00
|
|
|
#include "BKE_utildefines.h"
|
|
|
|
|
2008-08-17 17:08:00 +00:00
|
|
|
#include "BLO_sys_types.h" // for intptr_t support
|
|
|
|
|
2008-02-27 09:48:43 +00:00
|
|
|
|
Patch provided by Shaul Kedem: Compressed files are back!
He even made a nice doc in wiki:
http://wiki.blender.org/bin/view.pl/Blenderdev/Blendgz
Usage: set the option "Compress File" in the main "File" pulldown menu.
This setting is a user-def, meaning it is not changed on reading files.
If you want it default, save it with CTRL+U.
The longest debate went over the file naming convention. Shaul started
with .blend.gz files, which gave issues in Blender because of the code
hanging out everywhere that detects blender files, and that appends the
.blend extension if needed.
Daniel Dunbar proposed to just save it as .blend, and not bother users
with such details. This is indeed the most elegant solution, with as
only drawback that old Blender executables cannot read it.
This drawback isn't very relevant at the moment, since we're heading
towards a release that isn't upward compatible anyway... the recode
going on on Meshes, Modfiers, Armatures, Poses, Actions, NLA already
have upward compatibility issues.
We might check - during the next month(s) - on a builtin system to
warn users in the future when we change things that make a file risky
to read in an older release.
2005-07-27 19:46:06 +00:00
|
|
|
/* 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
|
|
|
|
*/
|
2011-10-22 15:35:49 +00:00
|
|
|
int BLI_file_gzip(const char *from, const char *to)
|
|
|
|
{
|
Patch provided by Shaul Kedem: Compressed files are back!
He even made a nice doc in wiki:
http://wiki.blender.org/bin/view.pl/Blenderdev/Blendgz
Usage: set the option "Compress File" in the main "File" pulldown menu.
This setting is a user-def, meaning it is not changed on reading files.
If you want it default, save it with CTRL+U.
The longest debate went over the file naming convention. Shaul started
with .blend.gz files, which gave issues in Blender because of the code
hanging out everywhere that detects blender files, and that appends the
.blend extension if needed.
Daniel Dunbar proposed to just save it as .blend, and not bother users
with such details. This is indeed the most elegant solution, with as
only drawback that old Blender executables cannot read it.
This drawback isn't very relevant at the moment, since we're heading
towards a release that isn't upward compatible anyway... the recode
going on on Meshes, Modfiers, Armatures, Poses, Actions, NLA already
have upward compatibility issues.
We might check - during the next month(s) - on a builtin system to
warn users in the future when we change things that make a file risky
to read in an older release.
2005-07-27 19:46:06 +00:00
|
|
|
char buffer[10240];
|
|
|
|
int file;
|
|
|
|
int readsize = 0;
|
2010-01-29 11:26:17 +00:00
|
|
|
int rval= 0, err;
|
|
|
|
gzFile gzfile;
|
2011-08-23 07:59:25 +00:00
|
|
|
|
|
|
|
/* 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 = gzopen(to, "wb1");
|
2010-01-29 11:26:17 +00:00
|
|
|
if(gzfile == NULL)
|
|
|
|
return -1;
|
Patch provided by Shaul Kedem: Compressed files are back!
He even made a nice doc in wiki:
http://wiki.blender.org/bin/view.pl/Blenderdev/Blendgz
Usage: set the option "Compress File" in the main "File" pulldown menu.
This setting is a user-def, meaning it is not changed on reading files.
If you want it default, save it with CTRL+U.
The longest debate went over the file naming convention. Shaul started
with .blend.gz files, which gave issues in Blender because of the code
hanging out everywhere that detects blender files, and that appends the
.blend extension if needed.
Daniel Dunbar proposed to just save it as .blend, and not bother users
with such details. This is indeed the most elegant solution, with as
only drawback that old Blender executables cannot read it.
This drawback isn't very relevant at the moment, since we're heading
towards a release that isn't upward compatible anyway... the recode
going on on Meshes, Modfiers, Armatures, Poses, Actions, NLA already
have upward compatibility issues.
We might check - during the next month(s) - on a builtin system to
warn users in the future when we change things that make a file risky
to read in an older release.
2005-07-27 19:46:06 +00:00
|
|
|
|
2010-01-29 11:26:17 +00:00
|
|
|
file = open(from, O_BINARY|O_RDONLY);
|
|
|
|
if(file < 0)
|
|
|
|
return -2;
|
Patch provided by Shaul Kedem: Compressed files are back!
He even made a nice doc in wiki:
http://wiki.blender.org/bin/view.pl/Blenderdev/Blendgz
Usage: set the option "Compress File" in the main "File" pulldown menu.
This setting is a user-def, meaning it is not changed on reading files.
If you want it default, save it with CTRL+U.
The longest debate went over the file naming convention. Shaul started
with .blend.gz files, which gave issues in Blender because of the code
hanging out everywhere that detects blender files, and that appends the
.blend extension if needed.
Daniel Dunbar proposed to just save it as .blend, and not bother users
with such details. This is indeed the most elegant solution, with as
only drawback that old Blender executables cannot read it.
This drawback isn't very relevant at the moment, since we're heading
towards a release that isn't upward compatible anyway... the recode
going on on Meshes, Modfiers, Armatures, Poses, Actions, NLA already
have upward compatibility issues.
We might check - during the next month(s) - on a builtin system to
warn users in the future when we change things that make a file risky
to read in an older release.
2005-07-27 19:46:06 +00:00
|
|
|
|
2010-01-29 11:26:17 +00:00
|
|
|
while(1) {
|
2012-01-12 13:16:30 +00:00
|
|
|
readsize = read(file, buffer, sizeof(buffer));
|
2010-01-29 11:26:17 +00:00
|
|
|
|
|
|
|
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 */
|
Patch provided by Shaul Kedem: Compressed files are back!
He even made a nice doc in wiki:
http://wiki.blender.org/bin/view.pl/Blenderdev/Blendgz
Usage: set the option "Compress File" in the main "File" pulldown menu.
This setting is a user-def, meaning it is not changed on reading files.
If you want it default, save it with CTRL+U.
The longest debate went over the file naming convention. Shaul started
with .blend.gz files, which gave issues in Blender because of the code
hanging out everywhere that detects blender files, and that appends the
.blend extension if needed.
Daniel Dunbar proposed to just save it as .blend, and not bother users
with such details. This is indeed the most elegant solution, with as
only drawback that old Blender executables cannot read it.
This drawback isn't very relevant at the moment, since we're heading
towards a release that isn't upward compatible anyway... the recode
going on on Meshes, Modfiers, Armatures, Poses, Actions, NLA already
have upward compatibility issues.
We might check - during the next month(s) - on a builtin system to
warn users in the future when we change things that make a file risky
to read in an older release.
2005-07-27 19:46:06 +00:00
|
|
|
|
2010-01-29 11:26:17 +00:00
|
|
|
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;
|
|
|
|
}
|
Patch provided by Shaul Kedem: Compressed files are back!
He even made a nice doc in wiki:
http://wiki.blender.org/bin/view.pl/Blenderdev/Blendgz
Usage: set the option "Compress File" in the main "File" pulldown menu.
This setting is a user-def, meaning it is not changed on reading files.
If you want it default, save it with CTRL+U.
The longest debate went over the file naming convention. Shaul started
with .blend.gz files, which gave issues in Blender because of the code
hanging out everywhere that detects blender files, and that appends the
.blend extension if needed.
Daniel Dunbar proposed to just save it as .blend, and not bother users
with such details. This is indeed the most elegant solution, with as
only drawback that old Blender executables cannot read it.
This drawback isn't very relevant at the moment, since we're heading
towards a release that isn't upward compatible anyway... the recode
going on on Meshes, Modfiers, Armatures, Poses, Actions, NLA already
have upward compatibility issues.
We might check - during the next month(s) - on a builtin system to
warn users in the future when we change things that make a file risky
to read in an older release.
2005-07-27 19:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gzclose(gzfile);
|
|
|
|
close(file);
|
2005-07-27 21:31:44 +00:00
|
|
|
|
2010-01-29 11:26:17 +00:00
|
|
|
return rval;
|
Patch provided by Shaul Kedem: Compressed files are back!
He even made a nice doc in wiki:
http://wiki.blender.org/bin/view.pl/Blenderdev/Blendgz
Usage: set the option "Compress File" in the main "File" pulldown menu.
This setting is a user-def, meaning it is not changed on reading files.
If you want it default, save it with CTRL+U.
The longest debate went over the file naming convention. Shaul started
with .blend.gz files, which gave issues in Blender because of the code
hanging out everywhere that detects blender files, and that appends the
.blend extension if needed.
Daniel Dunbar proposed to just save it as .blend, and not bother users
with such details. This is indeed the most elegant solution, with as
only drawback that old Blender executables cannot read it.
This drawback isn't very relevant at the moment, since we're heading
towards a release that isn't upward compatible anyway... the recode
going on on Meshes, Modfiers, Armatures, Poses, Actions, NLA already
have upward compatibility issues.
We might check - during the next month(s) - on a builtin system to
warn users in the future when we change things that make a file risky
to read in an older release.
2005-07-27 19:46:06 +00:00
|
|
|
}
|
|
|
|
|
2011-06-02 11:22:22 +00:00
|
|
|
/* gzip the file in from_file and write it to memery to_mem, at most size bytes.
|
|
|
|
return the unziped size
|
|
|
|
*/
|
2011-10-22 15:35:49 +00:00
|
|
|
char *BLI_file_ungzip_to_mem(const char *from_file, int *size_r)
|
2011-06-02 11:22:22 +00:00
|
|
|
{
|
|
|
|
gzFile gzfile;
|
2011-09-17 20:50:22 +00:00
|
|
|
int readsize, size, alloc_size=0;
|
|
|
|
char *mem= NULL;
|
|
|
|
const int chunk_size= 512*1024;
|
|
|
|
|
|
|
|
size= 0;
|
2011-06-02 11:22:22 +00:00
|
|
|
|
|
|
|
gzfile = gzopen( from_file, "rb" );
|
|
|
|
|
2011-09-17 20:50:22 +00:00
|
|
|
for(;;) {
|
|
|
|
if(mem==NULL) {
|
|
|
|
mem= MEM_callocN(chunk_size, "BLI_ungzip_to_mem");
|
|
|
|
alloc_size= chunk_size;
|
|
|
|
} else {
|
|
|
|
mem= MEM_reallocN(mem, size+chunk_size);
|
|
|
|
alloc_size+= chunk_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
readsize= gzread(gzfile, mem+size, chunk_size);
|
|
|
|
if(readsize>0) {
|
|
|
|
size+= readsize;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
|
2011-09-20 14:49:12 +00:00
|
|
|
if(size==0) {
|
|
|
|
MEM_freeN(mem);
|
|
|
|
mem= NULL;
|
|
|
|
}
|
|
|
|
else if(alloc_size!=size)
|
2011-09-17 20:50:22 +00:00
|
|
|
mem= MEM_reallocN(mem, size);
|
|
|
|
|
|
|
|
*size_r= size;
|
2011-06-02 11:22:22 +00:00
|
|
|
|
2011-09-17 20:50:22 +00:00
|
|
|
return mem;
|
2011-06-02 11:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-19 13:53:00 +00:00
|
|
|
/* return 1 when file can be written */
|
2011-10-22 15:35:49 +00:00
|
|
|
int BLI_file_is_writable(const char *filename)
|
2006-06-19 13:53:00 +00:00
|
|
|
{
|
|
|
|
int file;
|
|
|
|
|
2009-07-13 18:47:08 +00:00
|
|
|
/* first try to open without creating */
|
2009-05-17 16:10:39 +00:00
|
|
|
file = open(filename, O_BINARY | O_RDWR, 0666);
|
2006-06-19 13:53:00 +00:00
|
|
|
|
2009-07-13 18:47:08 +00:00
|
|
|
if (file < 0) {
|
|
|
|
/* now try to open and create. a test without actually
|
|
|
|
* creating a file would be nice, but how? */
|
|
|
|
file = open(filename, O_BINARY | O_RDWR | O_CREAT, 0666);
|
|
|
|
|
|
|
|
if(file < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* success, delete the file we create */
|
|
|
|
close(file);
|
|
|
|
BLI_delete(filename, 0, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2006-06-19 13:53:00 +00:00
|
|
|
else {
|
|
|
|
close(file);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-22 15:35:49 +00:00
|
|
|
int BLI_file_touch(const char *file)
|
2008-01-20 21:27:16 +00:00
|
|
|
{
|
2011-04-21 15:53:30 +00:00
|
|
|
FILE *f = fopen(file,"r+b");
|
|
|
|
if (f != NULL) {
|
2008-01-20 21:27:16 +00:00
|
|
|
char c = getc(f);
|
|
|
|
rewind(f);
|
|
|
|
putc(c,f);
|
|
|
|
} else {
|
2011-04-21 15:53:30 +00:00
|
|
|
f = fopen(file,"wb");
|
2008-01-20 21:27:16 +00:00
|
|
|
}
|
|
|
|
if (f) {
|
|
|
|
fclose(f);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
|
|
|
|
static char str[MAXPATHLEN+12];
|
|
|
|
|
2011-10-22 15:35:49 +00:00
|
|
|
int BLI_delete(const char *file, int dir, int recursive)
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (recursive) {
|
|
|
|
callLocalErrorCallBack("Recursive delete is unsupported on Windows");
|
|
|
|
err= 1;
|
|
|
|
} else if (dir) {
|
|
|
|
err= !RemoveDirectory(file);
|
|
|
|
if (err) printf ("Unable to remove directory");
|
|
|
|
} else {
|
|
|
|
err= !DeleteFile(file);
|
|
|
|
if (err) callLocalErrorCallBack("Unable to delete file");
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-10-22 15:35:49 +00:00
|
|
|
int BLI_move(const char *file, const char *to)
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
// windows doesn't support moveing to a directory
|
|
|
|
// it has to be 'mv filename filename' and not
|
|
|
|
// 'mv filename destdir'
|
|
|
|
|
2011-10-19 23:10:54 +00:00
|
|
|
BLI_strncpy(str, to, sizeof(str));
|
2002-10-12 11:37:38 +00:00
|
|
|
// points 'to' to a directory ?
|
|
|
|
if (BLI_last_slash(str) == (str + strlen(str) - 1)) {
|
|
|
|
if (BLI_last_slash(file) != NULL) {
|
|
|
|
strcat(str, BLI_last_slash(file) + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err= !MoveFile(file, str);
|
|
|
|
if (err) {
|
|
|
|
callLocalErrorCallBack("Unable to move file");
|
|
|
|
printf(" Move from '%s' to '%s' failed\n", file, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-22 15:35:49 +00:00
|
|
|
int BLI_copy(const char *file, const char *to)
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
// windows doesn't support copying to a directory
|
|
|
|
// it has to be 'cp filename filename' and not
|
|
|
|
// 'cp filename destdir'
|
|
|
|
|
2011-10-19 23:10:54 +00:00
|
|
|
BLI_strncpy(str, to, sizeof(str));
|
2002-10-12 11:37:38 +00:00
|
|
|
// points 'to' to a directory ?
|
|
|
|
if (BLI_last_slash(str) == (str + strlen(str) - 1)) {
|
|
|
|
if (BLI_last_slash(file) != NULL) {
|
|
|
|
strcat(str, BLI_last_slash(file) + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err= !CopyFile(file,str,FALSE);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
callLocalErrorCallBack("Unable to copy file!");
|
|
|
|
printf(" Copy from '%s' to '%s' failed\n", file, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-10-22 15:35:49 +00:00
|
|
|
int BLI_create_symlink(const char *file, const char *to)
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
callLocalErrorCallBack("Linking files is unsupported on Windows");
|
2011-04-10 09:37:04 +00:00
|
|
|
(void)file;
|
|
|
|
(void)to;
|
2002-10-12 11:37:38 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-10-22 15:35:49 +00:00
|
|
|
void BLI_dir_create_recursive(const char *dirname)
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
char *lslash;
|
|
|
|
char tmp[MAXPATHLEN];
|
|
|
|
|
|
|
|
// First remove possible slash at the end of the dirname.
|
|
|
|
// This routine otherwise tries to create
|
|
|
|
// blah1/blah2/ (with slash) after creating
|
|
|
|
// blah1/blah2 (without slash)
|
|
|
|
|
2011-10-19 23:10:54 +00:00
|
|
|
BLI_strncpy(tmp, dirname, sizeof(tmp));
|
2002-10-12 11:37:38 +00:00
|
|
|
lslash= BLI_last_slash(tmp);
|
|
|
|
|
|
|
|
if (lslash == tmp + strlen(tmp) - 1) {
|
|
|
|
*lslash = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BLI_exists(tmp)) return;
|
|
|
|
|
|
|
|
lslash= BLI_last_slash(tmp);
|
|
|
|
if (lslash) {
|
|
|
|
/* Split about the last slash and recurse */
|
|
|
|
*lslash = 0;
|
2011-10-22 15:35:49 +00:00
|
|
|
BLI_dir_create_recursive(tmp);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2004-12-11 15:56:01 +00:00
|
|
|
|
|
|
|
if(dirname[0]) /* patch, this recursive loop tries to create a nameless directory */
|
|
|
|
if (!CreateDirectory(dirname, NULL))
|
|
|
|
callLocalErrorCallBack("Unable to create directory\n");
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 15:35:49 +00:00
|
|
|
int BLI_rename(const char *from, const char *to)
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
if (!BLI_exists(from)) return 0;
|
|
|
|
|
2009-07-28 18:07:00 +00:00
|
|
|
/* make sure the filenames are different (case insensitive) before removing */
|
|
|
|
if (BLI_exists(to) && BLI_strcasecmp(from, to))
|
|
|
|
if(BLI_delete(to, 0, 0)) return 1;
|
2009-07-26 18:52:27 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
return rename(from, to);
|
|
|
|
}
|
|
|
|
|
2010-12-16 19:05:47 +00:00
|
|
|
#else /* The UNIX world */
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
/*
|
2010-07-14 23:39:23 +00:00
|
|
|
* but the UNIX world is tied to the interface, and the system
|
2002-10-12 11:37:38 +00:00
|
|
|
* timer, and... We implement a callback mechanism. The system will
|
2012-03-02 16:05:54 +00:00
|
|
|
* have to initialize the callback before the functions will work!
|
2002-10-12 11:37:38 +00:00
|
|
|
* */
|
2011-02-12 16:54:24 +00:00
|
|
|
static char str[12 + (MAXPATHLEN * 2)];
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2010-11-19 02:14:18 +00:00
|
|
|
int BLI_delete(const char *file, int dir, int recursive)
|
2004-12-16 14:40:25 +00:00
|
|
|
{
|
|
|
|
if(strchr(file, '"')) {
|
|
|
|
printf("Error: not deleted file %s because of quote!\n", file);
|
|
|
|
}
|
|
|
|
else {
|
2008-04-11 12:29:29 +00:00
|
|
|
if (recursive) {
|
2011-02-12 16:54:24 +00:00
|
|
|
BLI_snprintf(str, sizeof(str), "/bin/rm -rf \"%s\"", file);
|
2008-04-11 12:29:29 +00:00
|
|
|
return system(str);
|
|
|
|
}
|
|
|
|
else if (dir) {
|
2011-02-12 16:54:24 +00:00
|
|
|
BLI_snprintf(str, sizeof(str), "/bin/rmdir \"%s\"", file);
|
2008-04-11 12:29:29 +00:00
|
|
|
return system(str);
|
|
|
|
}
|
2008-04-12 10:55:10 +00:00
|
|
|
else {
|
2011-02-12 16:54:24 +00:00
|
|
|
return remove(file); //BLI_snprintf(str, sizeof(str), "/bin/rm -f \"%s\"", file);
|
2008-04-12 10:55:10 +00:00
|
|
|
}
|
2004-12-16 14:40:25 +00:00
|
|
|
}
|
|
|
|
return -1;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 15:35:49 +00:00
|
|
|
int BLI_move(const char *file, const char *to)
|
|
|
|
{
|
2011-02-12 16:54:24 +00:00
|
|
|
BLI_snprintf(str, sizeof(str), "/bin/mv -f \"%s\" \"%s\"", file, to);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
return system(str);
|
|
|
|
}
|
|
|
|
|
2011-10-22 15:35:49 +00:00
|
|
|
int BLI_copy(const char *file, const char *to)
|
|
|
|
{
|
2011-02-12 16:54:24 +00:00
|
|
|
BLI_snprintf(str, sizeof(str), "/bin/cp -rf \"%s\" \"%s\"", file, to);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
return system(str);
|
|
|
|
}
|
|
|
|
|
2011-10-22 15:35:49 +00:00
|
|
|
int BLI_create_symlink(const char *file, const char *to)
|
|
|
|
{
|
2011-02-12 16:54:24 +00:00
|
|
|
BLI_snprintf(str, sizeof(str), "/bin/ln -f \"%s\" \"%s\"", file, to);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
return system(str);
|
|
|
|
}
|
|
|
|
|
2011-10-22 15:35:49 +00:00
|
|
|
void BLI_dir_create_recursive(const char *dirname)
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
char *lslash;
|
|
|
|
char tmp[MAXPATHLEN];
|
|
|
|
|
|
|
|
if (BLI_exists(dirname)) return;
|
|
|
|
|
2011-10-19 23:10:54 +00:00
|
|
|
BLI_strncpy(tmp, dirname, sizeof(tmp));
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
lslash= BLI_last_slash(tmp);
|
|
|
|
if (lslash) {
|
|
|
|
/* Split about the last slash and recurse */
|
|
|
|
*lslash = 0;
|
2011-10-22 15:35:49 +00:00
|
|
|
BLI_dir_create_recursive(tmp);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mkdir(dirname, 0777);
|
|
|
|
}
|
|
|
|
|
2011-10-22 15:35:49 +00:00
|
|
|
int BLI_rename(const char *from, const char *to)
|
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
if (!BLI_exists(from)) return 0;
|
|
|
|
|
2009-07-28 18:07:00 +00:00
|
|
|
if (BLI_exists(to)) if(BLI_delete(to, 0, 0)) return 1;
|
2009-07-26 18:52:27 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
return rename(from, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2011-10-22 15:35:49 +00:00
|
|
|
|