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"
|
2012-03-20 02:17:37 +00:00
|
|
|
#include "utf_winfunc.h"
|
|
|
|
#include "utfconv.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>
|
2012-03-14 09:39:43 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
2002-10-12 11:37:38 +00:00
|
|
|
#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"
|
2012-03-14 09:39:43 +00:00
|
|
|
#include "BLI_utildefines.h"
|
2002-10-12 11:37:38 +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
|
|
|
#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".
|
2012-03-03 20:19:11 +00:00
|
|
|
* 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 */
|
2012-03-20 02:17:37 +00:00
|
|
|
gzfile = BLI_gzopen(to, "wb1");
|
2012-03-24 06:18:31 +00:00
|
|
|
if (gzfile == NULL)
|
2010-01-29 11:26:17 +00:00
|
|
|
return -1;
|
2012-03-20 02:17:37 +00:00
|
|
|
file = BLI_open(from, O_BINARY|O_RDONLY,0);
|
2012-03-24 06:18:31 +00:00
|
|
|
if (file < 0)
|
2010-01-29 11:26:17 +00:00
|
|
|
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
|
|
|
|
2012-03-24 06:18:31 +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
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (readsize < 0) {
|
2010-01-29 11:26:17 +00:00
|
|
|
rval= -2; /* error happened in reading */
|
|
|
|
fprintf(stderr, "Error reading file %s: %s.\n", from, strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
2012-03-24 06:18:31 +00:00
|
|
|
else if (readsize == 0)
|
2010-01-29 11:26:17 +00:00
|
|
|
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
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (gzwrite(gzfile, buffer, readsize) <= 0) {
|
2010-01-29 11:26:17 +00:00
|
|
|
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.
|
2012-03-03 20:19:11 +00:00
|
|
|
* 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
|
|
|
|
2012-03-20 02:17:37 +00:00
|
|
|
gzfile = BLI_gzopen( from_file, "rb" );
|
2012-03-24 06:18:31 +00:00
|
|
|
for (;;) {
|
|
|
|
if (mem==NULL) {
|
2011-09-17 20:50:22 +00:00
|
|
|
mem= MEM_callocN(chunk_size, "BLI_ungzip_to_mem");
|
|
|
|
alloc_size= chunk_size;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-09-17 20:50:22 +00:00
|
|
|
mem= MEM_reallocN(mem, size+chunk_size);
|
|
|
|
alloc_size+= chunk_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
readsize= gzread(gzfile, mem+size, chunk_size);
|
2012-03-24 06:18:31 +00:00
|
|
|
if (readsize>0) {
|
2011-09-17 20:50:22 +00:00
|
|
|
size+= readsize;
|
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (size==0) {
|
2011-09-20 14:49:12 +00:00
|
|
|
MEM_freeN(mem);
|
|
|
|
mem= NULL;
|
|
|
|
}
|
2012-03-24 06:18:31 +00:00
|
|
|
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 */
|
2012-03-20 02:17:37 +00:00
|
|
|
file = BLI_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? */
|
2012-03-20 02:17:37 +00:00
|
|
|
file = BLI_open(filename, O_BINARY | O_RDWR | O_CREAT, 0666);
|
2009-07-13 18:47:08 +00:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (file < 0) {
|
2009-07-13 18:47:08 +00:00
|
|
|
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
|
|
|
{
|
2012-04-12 02:15:33 +00:00
|
|
|
FILE *f = BLI_fopen(file, "r+b");
|
2011-04-21 15:53:30 +00:00
|
|
|
if (f != NULL) {
|
2008-01-20 21:27:16 +00:00
|
|
|
char c = getc(f);
|
|
|
|
rewind(f);
|
2012-04-12 02:15:33 +00:00
|
|
|
putc(c, f);
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-04-12 02:15:33 +00:00
|
|
|
f = BLI_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
|
|
|
|
|
2012-04-12 00:15:02 +00:00
|
|
|
static char str[MAXPATHLEN + 12];
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-03-20 03:48:32 +00:00
|
|
|
FILE *BLI_fopen(const char *filename, const char *mode)
|
2012-03-20 02:17:37 +00:00
|
|
|
{
|
|
|
|
return ufopen(filename, mode);
|
|
|
|
}
|
|
|
|
|
2012-03-20 03:48:32 +00:00
|
|
|
gzFile BLI_gzopen(const char *filename, const char *mode)
|
2012-03-20 02:17:37 +00:00
|
|
|
{
|
|
|
|
gzFile gzfile;
|
|
|
|
|
2012-04-12 00:15:02 +00:00
|
|
|
if (!filename || !mode) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
wchar_t short_name_16[256];
|
|
|
|
char short_name[256];
|
|
|
|
int i = 0;
|
2012-04-06 21:11:10 +00:00
|
|
|
|
|
|
|
/* xxx Creates file before transcribing the path */
|
2012-04-12 00:15:02 +00:00
|
|
|
if(mode[0] == 'w')
|
2012-04-06 21:11:10 +00:00
|
|
|
fclose(ufopen(filename,"a"));
|
|
|
|
|
2012-03-20 02:17:37 +00:00
|
|
|
UTF16_ENCODE(filename);
|
|
|
|
|
2012-04-12 00:15:02 +00:00
|
|
|
GetShortPathNameW(filename_16,short_name_16, 256);
|
2012-03-20 02:17:37 +00:00
|
|
|
|
2012-04-12 00:15:02 +00:00
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
short_name[i] = short_name_16[i];
|
|
|
|
}
|
2012-03-20 02:17:37 +00:00
|
|
|
|
|
|
|
gzfile = gzopen(short_name,mode);
|
|
|
|
|
|
|
|
UTF16_UN_ENCODE(filename);
|
|
|
|
}
|
2012-04-12 00:15:02 +00:00
|
|
|
|
2012-03-20 02:17:37 +00:00
|
|
|
return gzfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BLI_open(const char *filename, int oflag, int pmode)
|
|
|
|
{
|
|
|
|
return uopen(filename, oflag, pmode);
|
|
|
|
}
|
|
|
|
|
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;
|
2012-03-20 02:17:37 +00:00
|
|
|
|
2012-04-12 02:15:33 +00:00
|
|
|
UTF16_ENCODE(file);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
if (recursive) {
|
|
|
|
callLocalErrorCallBack("Recursive delete is unsupported on Windows");
|
|
|
|
err= 1;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else if (dir) {
|
2012-03-20 02:17:37 +00:00
|
|
|
err= !RemoveDirectoryW(file_16);
|
2002-10-12 11:37:38 +00:00
|
|
|
if (err) printf ("Unable to remove directory");
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-03-20 02:17:37 +00:00
|
|
|
err= !DeleteFileW(file_16);
|
2002-10-12 11:37:38 +00:00
|
|
|
if (err) callLocalErrorCallBack("Unable to delete file");
|
|
|
|
}
|
|
|
|
|
2012-04-12 02:15:33 +00:00
|
|
|
UTF16_UN_ENCODE(file);
|
2012-03-20 02:17:37 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2012-03-20 02:17:37 +00:00
|
|
|
|
2012-04-12 02:15:33 +00:00
|
|
|
UTF16_ENCODE(file);
|
|
|
|
UTF16_ENCODE(str);
|
2012-03-20 02:17:37 +00:00
|
|
|
err= !MoveFileW(file_16, str_16);
|
2012-04-12 02:15:33 +00:00
|
|
|
UTF16_UN_ENCODE(str);
|
|
|
|
UTF16_UN_ENCODE(file);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-12 02:15:33 +00:00
|
|
|
UTF16_ENCODE(file);
|
|
|
|
UTF16_ENCODE(str);
|
|
|
|
err = !CopyFileW(file_16, str_16, FALSE);
|
|
|
|
UTF16_UN_ENCODE(str);
|
|
|
|
UTF16_UN_ENCODE(file);
|
2012-03-20 02:17:37 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
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];
|
|
|
|
|
2012-04-12 02:15:33 +00:00
|
|
|
/* 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) */
|
2002-10-12 11:37:38 +00:00
|
|
|
|
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);
|
2012-03-20 02:17:37 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if (lslash == tmp + strlen(tmp) - 1) {
|
|
|
|
*lslash = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BLI_exists(tmp)) return;
|
2012-04-12 02:15:33 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
lslash= BLI_last_slash(tmp);
|
|
|
|
if (lslash) {
|
2012-04-12 02:15:33 +00:00
|
|
|
/* Split about the last slash and recurse */
|
2002-10-12 11:37:38 +00:00
|
|
|
*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
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (dirname[0]) /* patch, this recursive loop tries to create a nameless directory */
|
2012-03-20 02:17:37 +00:00
|
|
|
if (umkdir(dirname)==-1)
|
|
|
|
printf("Unable to create directory %s\n",dirname);
|
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))
|
2012-03-24 06:18:31 +00:00
|
|
|
if (BLI_delete(to, 0, 0)) return 1;
|
2012-03-20 02:17:37 +00:00
|
|
|
|
|
|
|
return urename(from, to);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2010-12-16 19:05:47 +00:00
|
|
|
#else /* The UNIX world */
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-03-14 09:39:43 +00:00
|
|
|
enum {
|
|
|
|
/* operation succeeded succeeded */
|
|
|
|
recursiveOp_Callback_OK = 0,
|
|
|
|
|
|
|
|
/* operation requested not to perform recursive digging for current path */
|
|
|
|
recursiveOp_Callback_StopRecurs = 1,
|
|
|
|
|
|
|
|
/* error occured in callback and recursive walking should stop immediately */
|
|
|
|
recursiveOp_Callback_Error = 2
|
|
|
|
} recuresiveOp_Callback_Result;
|
|
|
|
|
|
|
|
typedef int (*recursiveOp_Callback) (const char *from, const char *to);
|
|
|
|
|
|
|
|
/* appending of filename to dir (ensures for buffer size before appending) */
|
|
|
|
static void join_dirfile_alloc(char **dst, size_t *alloc_len, const char *dir, const char *file)
|
|
|
|
{
|
|
|
|
size_t len = strlen(dir) + strlen(file) + 1;
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (!*dst)
|
2012-03-14 09:39:43 +00:00
|
|
|
*dst = MEM_callocN(len + 1, "join_dirfile_alloc path");
|
2012-03-24 06:18:31 +00:00
|
|
|
else if (*alloc_len < len)
|
2012-03-14 09:39:43 +00:00
|
|
|
*dst = MEM_reallocN(*dst, len + 1);
|
|
|
|
|
|
|
|
*alloc_len = len;
|
|
|
|
|
|
|
|
BLI_join_dirfile(*dst, len + 1, dir, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *strip_last_slash(const char *dir)
|
|
|
|
{
|
|
|
|
char *result = BLI_strdup(dir);
|
|
|
|
BLI_del_slash(result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int recursive_operation(const char *startfrom, const char *startto, recursiveOp_Callback callback_dir_pre,
|
|
|
|
recursiveOp_Callback callback_file, recursiveOp_Callback callback_dir_post)
|
|
|
|
{
|
2012-03-19 20:47:17 +00:00
|
|
|
struct dirent **dirlist;
|
2012-03-14 09:39:43 +00:00
|
|
|
struct stat st;
|
|
|
|
char *from = NULL, *to = NULL;
|
|
|
|
char *from_path = NULL, *to_path = NULL;
|
|
|
|
size_t from_alloc_len = -1, to_alloc_len = -1;
|
2012-03-19 20:47:17 +00:00
|
|
|
int i, n, ret = 0;
|
2012-03-14 09:39:43 +00:00
|
|
|
|
|
|
|
/* ensure there's no trailing slash in file path */
|
|
|
|
from = strip_last_slash(startfrom);
|
2012-03-24 06:18:31 +00:00
|
|
|
if (startto)
|
2012-03-14 09:39:43 +00:00
|
|
|
to = strip_last_slash(startto);
|
|
|
|
|
|
|
|
ret = lstat(from, &st);
|
2012-03-24 06:18:31 +00:00
|
|
|
if (ret < 0) {
|
2012-03-14 09:39:43 +00:00
|
|
|
/* source wasn't found, nothing to operate with */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (!S_ISDIR(st.st_mode)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
/* source isn't a directory, can't do recursive walking for it,
|
|
|
|
* so just call file callback and leave */
|
2012-03-24 06:18:31 +00:00
|
|
|
if (callback_file) {
|
2012-03-14 09:39:43 +00:00
|
|
|
ret = callback_file(from, to);
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (ret != recursiveOp_Callback_OK)
|
2012-03-14 09:39:43 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
MEM_freeN(from);
|
2012-03-24 06:18:31 +00:00
|
|
|
if (to) MEM_freeN(to);
|
2012-03-14 09:39:43 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-19 20:47:17 +00:00
|
|
|
|
|
|
|
n = scandir(startfrom, &dirlist, 0, alphasort);
|
|
|
|
if (n < 0) {
|
|
|
|
/* error opening directory for listing */
|
|
|
|
perror("scandir");
|
|
|
|
|
|
|
|
MEM_freeN(from);
|
2012-03-24 06:18:31 +00:00
|
|
|
if (to) MEM_freeN(to);
|
2012-03-19 20:47:17 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (callback_dir_pre) {
|
2012-03-14 09:39:43 +00:00
|
|
|
/* call pre-recursive walking directory callback */
|
|
|
|
ret = callback_dir_pre(from, to);
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (ret != recursiveOp_Callback_OK) {
|
2012-03-14 09:39:43 +00:00
|
|
|
MEM_freeN(from);
|
2012-03-24 06:18:31 +00:00
|
|
|
if (to) free(to);
|
2012-03-14 09:39:43 +00:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (ret == recursiveOp_Callback_StopRecurs) {
|
2012-03-14 09:39:43 +00:00
|
|
|
/* callback requested not to perform recursive walking, not an error */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-19 20:47:17 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
struct dirent *dirent = dirlist[i];
|
2012-03-14 09:39:43 +00:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, "..")) {
|
2012-03-19 20:47:17 +00:00
|
|
|
free(dirent);
|
2012-03-14 09:39:43 +00:00
|
|
|
continue;
|
2012-03-19 20:47:17 +00:00
|
|
|
}
|
2012-03-14 09:39:43 +00:00
|
|
|
|
|
|
|
join_dirfile_alloc(&from_path, &from_alloc_len, from, dirent->d_name);
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (to)
|
2012-03-14 09:39:43 +00:00
|
|
|
join_dirfile_alloc(&to_path, &to_alloc_len, to, dirent->d_name);
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (dirent->d_type == DT_DIR) {
|
2012-03-14 09:39:43 +00:00
|
|
|
/* recursively dig into a folder */
|
|
|
|
ret = recursive_operation(from_path, to_path, callback_dir_pre, callback_file, callback_dir_post);
|
|
|
|
}
|
2012-03-24 06:18:31 +00:00
|
|
|
else if (callback_file) {
|
2012-03-14 09:39:43 +00:00
|
|
|
/* call file callback for current path */
|
|
|
|
ret = callback_file(from_path, to_path);
|
2012-03-24 06:18:31 +00:00
|
|
|
if (ret != recursiveOp_Callback_OK)
|
2012-03-14 09:39:43 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (ret != 0) {
|
2012-03-19 20:47:17 +00:00
|
|
|
while (i < n)
|
|
|
|
free(dirlist[i]);
|
2012-03-14 09:39:43 +00:00
|
|
|
break;
|
2012-03-19 20:47:17 +00:00
|
|
|
}
|
2012-03-14 09:39:43 +00:00
|
|
|
}
|
|
|
|
|
2012-03-19 20:47:17 +00:00
|
|
|
free(dirlist);
|
2012-03-14 09:39:43 +00:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (ret == 0) {
|
|
|
|
if (callback_dir_post) {
|
2012-03-14 09:39:43 +00:00
|
|
|
/* call post-recursive directory callback */
|
|
|
|
ret = callback_dir_post(from, to);
|
2012-03-24 06:18:31 +00:00
|
|
|
if (ret != recursiveOp_Callback_OK)
|
2012-03-14 09:39:43 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (from_path) MEM_freeN(from_path);
|
|
|
|
if (to_path) MEM_freeN(to_path);
|
2012-03-14 09:39:43 +00:00
|
|
|
|
|
|
|
MEM_freeN(from);
|
2012-03-24 06:18:31 +00:00
|
|
|
if (to) MEM_freeN(to);
|
2012-03-14 09:39:43 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int delete_callback_post(const char *from, const char *UNUSED(to))
|
|
|
|
{
|
2012-03-24 06:18:31 +00:00
|
|
|
if (rmdir(from)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
perror("rmdir");
|
|
|
|
|
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return recursiveOp_Callback_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int delete_single_file(const char *from, const char *UNUSED(to))
|
|
|
|
{
|
2012-03-24 06:18:31 +00:00
|
|
|
if (unlink(from)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
perror("unlink");
|
|
|
|
|
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return recursiveOp_Callback_OK;
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-03-20 03:48:32 +00:00
|
|
|
FILE *BLI_fopen(const char *filename, const char *mode)
|
2012-03-20 02:17:37 +00:00
|
|
|
{
|
|
|
|
return fopen(filename, mode);
|
|
|
|
}
|
|
|
|
|
2012-03-20 03:48:32 +00:00
|
|
|
void *BLI_gzopen(const char *filename, const char *mode)
|
2012-03-20 02:17:37 +00:00
|
|
|
{
|
|
|
|
return gzopen(filename, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
int BLI_open(const char *filename, int oflag, int pmode)
|
|
|
|
{
|
|
|
|
return open(filename, oflag, pmode);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-03-24 06:18:31 +00:00
|
|
|
if (strchr(file, '"')) {
|
2004-12-16 14:40:25 +00:00
|
|
|
printf("Error: not deleted file %s because of quote!\n", file);
|
|
|
|
}
|
|
|
|
else {
|
2008-04-11 12:29:29 +00:00
|
|
|
if (recursive) {
|
2012-03-14 09:39:43 +00:00
|
|
|
return recursive_operation(file, NULL, NULL, delete_single_file, delete_callback_post);
|
2008-04-11 12:29:29 +00:00
|
|
|
}
|
|
|
|
else if (dir) {
|
2012-03-14 09:39:43 +00:00
|
|
|
return rmdir(file);
|
2008-04-11 12:29:29 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2012-03-14 09:39:43 +00:00
|
|
|
static int check_the_same(const char *path_a, const char *path_b)
|
|
|
|
{
|
|
|
|
struct stat st_a, st_b;
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (lstat(path_a, &st_a))
|
2012-03-14 09:39:43 +00:00
|
|
|
return 0;
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (lstat(path_b, &st_b))
|
2012-03-14 09:39:43 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return st_a.st_dev == st_b.st_dev && st_a.st_ino == st_b.st_ino;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_permissions(const char *file, struct stat *st)
|
|
|
|
{
|
2012-03-24 06:18:31 +00:00
|
|
|
if (chown(file, st->st_uid, st->st_gid)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
perror("chown");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (chmod(file, st->st_mode)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
perror("chmod");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pre-recursive callback for copying operation
|
|
|
|
* creates a destination directory where all source content fill be copied to */
|
|
|
|
static int copy_callback_pre(const char *from, const char *to)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (check_the_same(from, to)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
fprintf(stderr, "%s: '%s' is the same as '%s'\n", __func__, from, to);
|
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (lstat(from, &st)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
perror("stat");
|
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create a directory */
|
2012-03-24 06:18:31 +00:00
|
|
|
if (mkdir(to, st.st_mode)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
perror("mkdir");
|
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set proper owner and group on new directory */
|
2012-03-24 06:18:31 +00:00
|
|
|
if (chown(to, st.st_uid, st.st_gid)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
perror("chown");
|
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return recursiveOp_Callback_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int copy_single_file(const char *from, const char *to)
|
|
|
|
{
|
|
|
|
FILE *from_stream, *to_stream;
|
|
|
|
struct stat st;
|
|
|
|
char buf[4096];
|
|
|
|
size_t len;
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (check_the_same(from, to)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
fprintf(stderr, "%s: '%s' is the same as '%s'\n", __func__, from, to);
|
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (lstat(from, &st)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
perror("lstat");
|
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (S_ISLNK(st.st_mode)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
/* symbolic links should be copied in special way */
|
|
|
|
char *link_buffer;
|
|
|
|
int need_free;
|
|
|
|
ssize_t link_len;
|
|
|
|
|
|
|
|
/* get large enough buffer to read link content */
|
2012-03-24 06:18:31 +00:00
|
|
|
if (st.st_size < sizeof(buf)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
link_buffer = buf;
|
|
|
|
need_free = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
link_buffer = MEM_callocN(st.st_size+2, "copy_single_file link_buffer");
|
|
|
|
need_free = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
link_len = readlink(from, link_buffer, st.st_size+1);
|
2012-03-24 06:18:31 +00:00
|
|
|
if (link_len < 0) {
|
2012-03-14 09:39:43 +00:00
|
|
|
perror("readlink");
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (need_free) MEM_freeN(link_buffer);
|
2012-03-14 09:39:43 +00:00
|
|
|
|
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
link_buffer[link_len] = 0;
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (symlink(link_buffer, to)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
perror("symlink");
|
2012-03-24 06:18:31 +00:00
|
|
|
if (need_free) MEM_freeN(link_buffer);
|
2012-03-14 09:39:43 +00:00
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (need_free)
|
2012-03-14 09:39:43 +00:00
|
|
|
MEM_freeN(link_buffer);
|
|
|
|
|
|
|
|
return recursiveOp_Callback_OK;
|
|
|
|
}
|
2012-03-24 06:18:31 +00:00
|
|
|
else if (S_ISCHR (st.st_mode) ||
|
2012-03-14 09:39:43 +00:00
|
|
|
S_ISBLK (st.st_mode) ||
|
|
|
|
S_ISFIFO (st.st_mode) ||
|
|
|
|
S_ISSOCK (st.st_mode))
|
|
|
|
{
|
|
|
|
/* copy special type of file */
|
2012-03-24 06:18:31 +00:00
|
|
|
if (mknod(to, st.st_mode, st.st_rdev)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
perror("mknod");
|
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (set_permissions(to, &st))
|
2012-03-14 09:39:43 +00:00
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
|
|
|
|
return recursiveOp_Callback_OK;
|
|
|
|
}
|
2012-03-24 06:18:31 +00:00
|
|
|
else if (!S_ISREG(st.st_mode)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
fprintf(stderr, "Copying of this kind of files isn't supported yet\n");
|
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
from_stream = fopen(from, "rb");
|
2012-03-24 06:18:31 +00:00
|
|
|
if (!from_stream) {
|
2012-03-14 09:39:43 +00:00
|
|
|
perror("fopen");
|
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
to_stream = fopen(to, "wb");
|
2012-03-24 06:18:31 +00:00
|
|
|
if (!to_stream) {
|
2012-03-14 09:39:43 +00:00
|
|
|
perror("fopen");
|
|
|
|
fclose(from_stream);
|
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
while ((len = fread(buf, 1, sizeof(buf), from_stream)) > 0) {
|
2012-03-14 09:39:43 +00:00
|
|
|
fwrite(buf, 1, len, to_stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(to_stream);
|
|
|
|
fclose(from_stream);
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (set_permissions(to, &st))
|
2012-03-14 09:39:43 +00:00
|
|
|
return recursiveOp_Callback_Error;
|
|
|
|
|
|
|
|
return recursiveOp_Callback_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int move_callback_pre(const char *from, const char *to)
|
|
|
|
{
|
|
|
|
int ret = rename(from, to);
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (ret)
|
2012-03-14 09:39:43 +00:00
|
|
|
return copy_callback_pre(from, to);
|
|
|
|
|
|
|
|
return recursiveOp_Callback_StopRecurs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int move_single_file(const char *from, const char *to)
|
|
|
|
{
|
|
|
|
int ret = rename(from, to);
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (ret)
|
2012-03-14 09:39:43 +00:00
|
|
|
return copy_single_file(from, to);
|
|
|
|
|
|
|
|
return recursiveOp_Callback_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-22 15:35:49 +00:00
|
|
|
int BLI_move(const char *file, const char *to)
|
|
|
|
{
|
2012-03-14 09:39:43 +00:00
|
|
|
int ret = recursive_operation(file, to, move_callback_pre, move_single_file, NULL);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (ret) {
|
2012-03-14 09:39:43 +00:00
|
|
|
return recursive_operation(file, NULL, NULL, delete_single_file, delete_callback_post);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *check_destination(const char *file, const char *to)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (!stat(to, &st)) {
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2012-03-14 09:39:43 +00:00
|
|
|
char *str, *filename, *path;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
str = strip_last_slash(file);
|
|
|
|
filename = BLI_last_slash(str);
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (!filename) {
|
2012-03-14 09:39:43 +00:00
|
|
|
MEM_freeN(str);
|
|
|
|
return (char*)to;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip slash */
|
|
|
|
filename += 1;
|
|
|
|
|
|
|
|
len = strlen(to) + strlen(filename) + 1;
|
|
|
|
path = MEM_callocN(len + 1, "check_destination path");
|
|
|
|
BLI_join_dirfile(path, len + 1, to, filename);
|
|
|
|
|
|
|
|
MEM_freeN(str);
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (char*)to;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 15:35:49 +00:00
|
|
|
int BLI_copy(const char *file, const char *to)
|
|
|
|
{
|
2012-03-14 09:39:43 +00:00
|
|
|
char *actual_to = check_destination(file, to);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = recursive_operation(file, actual_to, copy_callback_pre, copy_single_file, NULL);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (actual_to!=to)
|
2012-03-14 09:39:43 +00:00
|
|
|
MEM_freeN(actual_to);
|
|
|
|
|
|
|
|
return ret;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 15:35:49 +00:00
|
|
|
int BLI_create_symlink(const char *file, const char *to)
|
|
|
|
{
|
2012-03-14 09:39:43 +00:00
|
|
|
return symlink(to, file);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2012-03-14 09:39:43 +00:00
|
|
|
size_t size;
|
|
|
|
#ifdef MAXPATHLEN
|
|
|
|
char static_buf[MAXPATHLEN];
|
|
|
|
#endif
|
|
|
|
char *tmp;
|
|
|
|
int needs_free;
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if (BLI_exists(dirname)) return;
|
|
|
|
|
2012-03-14 09:39:43 +00:00
|
|
|
#ifdef MAXPATHLEN
|
|
|
|
size = MAXPATHLEN;
|
|
|
|
tmp = static_buf;
|
|
|
|
needs_free = 0;
|
|
|
|
#else
|
|
|
|
size = strlen(dirname)+1;
|
|
|
|
tmp = MEM_callocN(size, "BLI_dir_create_recursive tmp");
|
|
|
|
needs_free = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
BLI_strncpy(tmp, dirname, size);
|
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
|
|
|
}
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (needs_free)
|
2012-03-14 09:39:43 +00:00
|
|
|
MEM_freeN(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;
|
|
|
|
|
2012-03-14 09:39:43 +00:00
|
|
|
if (BLI_exists(to))
|
2012-03-24 06:18:31 +00:00
|
|
|
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
|
|
|
|