4 * cleaned up (a bit) mar-01 nzc
6 * More low-level file things.
10 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version. The Blender
16 * Foundation also sells licenses for use in proprietary software under
17 * the Blender License. See http://www.blender.org/BL/ for information
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software Foundation,
27 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
30 * All rights reserved.
32 * The Original Code is: all of this file.
34 * Contributor(s): none yet.
36 * ***** END GPL/BL DUAL LICENSE BLOCK *****
47 #include "BLI_winstuff.h"
49 #include <sys/param.h>
52 #include "BLI_blenlib.h"
53 #include "BLI_storage.h"
54 #include "BLI_fileops.h"
55 #include "BLI_callbacks.h"
57 /* implementations: */
58 char *first_slash(char *string) {
59 char *ffslash, *fbslash;
61 ffslash= strchr(string, '/');
62 fbslash= strchr(string, '\\');
64 if (!ffslash) return fbslash;
65 else if (!fbslash) return ffslash;
67 if ((int)ffslash < (int)fbslash) return ffslash;
71 char *BLI_last_slash(char *string) {
72 char *lfslash, *lbslash;
74 lfslash= strrchr(string, '/');
75 lbslash= strrchr(string, '\\');
77 if (!lfslash) return lbslash;
78 else if (!lbslash) return lfslash;
80 if ((int)lfslash < (int)lbslash) return lbslash;
86 static char str[MAXPATHLEN+12];
88 int BLI_delete(char *file, int dir, int recursive) {
92 callLocalErrorCallBack("Recursive delete is unsupported on Windows");
95 err= !RemoveDirectory(file);
96 if (err) printf ("Unable to remove directory");
98 err= !DeleteFile(file);
99 if (err) callLocalErrorCallBack("Unable to delete file");
105 int BLI_touch(char *file) {
106 callLocalErrorCallBack("Touching files is unsupported on Windows");
111 int BLI_move(char *file, char *to) {
114 // windows doesn't support moveing to a directory
115 // it has to be 'mv filename filename' and not
116 // 'mv filename destdir'
119 // points 'to' to a directory ?
120 if (BLI_last_slash(str) == (str + strlen(str) - 1)) {
121 if (BLI_last_slash(file) != NULL) {
122 strcat(str, BLI_last_slash(file) + 1);
126 err= !MoveFile(file, str);
128 callLocalErrorCallBack("Unable to move file");
129 printf(" Move from '%s' to '%s' failed\n", file, str);
136 int BLI_copy_fileops(char *file, char *to) {
139 // windows doesn't support copying to a directory
140 // it has to be 'cp filename filename' and not
141 // 'cp filename destdir'
144 // points 'to' to a directory ?
145 if (BLI_last_slash(str) == (str + strlen(str) - 1)) {
146 if (BLI_last_slash(file) != NULL) {
147 strcat(str, BLI_last_slash(file) + 1);
151 err= !CopyFile(file,str,FALSE);
154 callLocalErrorCallBack("Unable to copy file!");
155 printf(" Copy from '%s' to '%s' failed\n", file, str);
161 int BLI_link(char *file, char *to) {
162 callLocalErrorCallBack("Linking files is unsupported on Windows");
167 int BLI_backup(char *file, char *from, char *to) {
168 callLocalErrorCallBack("Backing up files is unsupported on Windows");
173 int BLI_exists(char *file) {
174 return (GetFileAttributes(file) != 0xFFFFFFFF);
177 void BLI_recurdir_fileops(char *dirname) {
179 char tmp[MAXPATHLEN];
181 // First remove possible slash at the end of the dirname.
182 // This routine otherwise tries to create
183 // blah1/blah2/ (with slash) after creating
184 // blah1/blah2 (without slash)
186 strcpy(tmp, dirname);
187 lslash= BLI_last_slash(tmp);
189 if (lslash == tmp + strlen(tmp) - 1) {
193 if (BLI_exists(tmp)) return;
195 lslash= BLI_last_slash(tmp);
197 /* Split about the last slash and recurse */
199 BLI_recurdir_fileops(tmp);
202 if (!CreateDirectory(dirname, NULL))
203 callLocalErrorCallBack("Unable to create directory\n");
206 int BLI_rename(char *from, char *to) {
207 if (!BLI_exists(from)) return 0;
210 if(BLI_delete(to, 0, 0)) return 1;
212 return rename(from, to);
215 #else /* The sane UNIX world */
218 * but the sane UNIX world is tied to the interface, and the system
219 * timer, and... We implement a callback mechanism. The system will
220 * have to initialise the callback before the functions will work!
222 static char str[MAXPATHLEN+12];
224 int BLI_delete(char *file, int dir, int recursive) {
225 if (recursive) sprintf(str, "/bin/rm -rf %s", file);
226 else if (dir) sprintf(str, "/bin/rmdir \"%s\"", file);
227 else sprintf(str, "/bin/rm -f \"%s\"", file);
232 int BLI_touch(char *file)
235 if( BLI_exists("/bin/touch") )
236 sprintf(str, "/bin/touch %s", file);
238 sprintf(str, "/usr/bin/touch %s", file);
243 int BLI_move(char *file, char *to) {
244 sprintf(str, "/bin/mv -f %s %s", file, to);
249 int BLI_copy_fileops(char *file, char *to) {
250 sprintf(str, "/bin/cp -rf \"%s\" %s", file, to);
255 int BLI_link(char *file, char *to) {
256 sprintf(str, "/bin/ln -f %s %s", file, to);
261 int BLI_backup(char *file, char *from, char *to) {
262 sprintf(str, "/bin/su root -c 'cd %s; /bin/tar cf - \"%s\" | (/bin/cd %s; /bin/tar xf -)'", from, file, to);
267 int BLI_exists(char *file) {
268 return BLI_exist(file);
271 void BLI_recurdir_fileops(char *dirname) {
273 char tmp[MAXPATHLEN];
275 if (BLI_exists(dirname)) return;
277 strcpy(tmp, dirname);
279 lslash= BLI_last_slash(tmp);
281 /* Split about the last slash and recurse */
283 BLI_recurdir_fileops(tmp);
286 mkdir(dirname, 0777);
289 int BLI_rename(char *from, char *to) {
290 if (!BLI_exists(from)) return 0;
292 if (BLI_exists(to)) if(BLI_delete(to, 0, 0)) return 1;
294 return rename(from, to);