opps had a typo...
[blender-staging.git] / source / blender / blenlib / intern / storage.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * Reorganised mar-01 nzc
32  * Some really low-level file thingies.
33  */
34
35 #include <sys/types.h>
36 #include <stdio.h>
37 #include <stdlib.h>                     /* voorkomt dat je bij malloc type moet aangeven */
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 #ifdef WIN32
44
45 #include <windows.h>
46 #include "BLI_winstuff.h"
47 #include <sys/types.h>
48 #include <io.h>
49 #include <direct.h>
50 #endif
51
52 #ifndef WIN32
53 #include <dirent.h>
54 #endif
55
56 #include <time.h>
57 #include <sys/stat.h>
58
59 #if defined(__sgi) || defined(__sun__)
60 #include <sys/statfs.h>
61 #endif
62
63 #if defined (__FreeBSD__) || defined (__OpenBSD__) 
64 #include <sys/param.h>
65 #include <sys/mount.h>
66 #endif
67
68 #if defined(linux) || defined(__CYGWIN32__)
69 #include <sys/vfs.h>
70 #endif
71
72 #ifdef __BeOS
73 struct statfs {
74         int f_bsize;
75         int f_bfree;
76 };
77 #endif
78
79 #ifdef __APPLE__
80 /* For statfs */
81 #include <sys/param.h>
82 #include <sys/mount.h>
83 #endif /* __APPLE__ */
84
85
86 #include <fcntl.h>
87 #if !defined(__BeOS) && !defined(WIN32)
88 #include <sys/mtio.h>                   /* tape comando's */
89 #endif
90 #include <string.h>                     /* strcpy etc.. */
91
92 #ifndef WIN32
93 #include <sys/ioctl.h>
94 #include <unistd.h>                     /*  */
95 #include <pwd.h>
96 #endif
97
98 /* MAART: #ifndef __FreeBSD__ */
99 #if !defined(__FreeBSD__) && !defined(__APPLE__)
100 #include <malloc.h>
101 #endif
102
103 /* lib includes */
104 #include "MEM_guardedalloc.h"
105
106 #include "DNA_listBase.h"
107 #include "BLI_blenlib.h"
108 #include "BLI_storage.h"
109 #include "BLI_storage_types.h"
110
111 #include "BLI_util.h"
112 #include "BLI_linklist.h"
113
114 /* functions */
115 void  BLI_buildpwtable(struct passwd **pwtable);
116 void  BLI_freepwtable(struct passwd *pwtable);
117 char *BLI_findpwtable(struct passwd *pwtable, unsigned short user);
118  
119 /* vars: */
120 static int totnum,actnum;
121 static struct direntry *files;
122
123 static struct ListBase dirbase_={
124         0,0};
125 static struct ListBase *dirbase = &dirbase_;
126
127
128 char *BLI_getwdN(char *dir)
129 {
130         char *pwd;
131
132         if (dir) {
133                 pwd = getenv("PWD");
134                 if (pwd){
135                         strcpy(dir, pwd);
136                         return(dir);
137                 }
138                 /* 160 is FILE_MAXDIR in filesel.c */
139                 return( getcwd(dir, 160) );
140         }
141         return(0);
142 }
143
144
145 int BLI_compare(struct direntry *entry1, struct direntry *entry2)
146 {
147         /* type is gelijk aan stat.st_mode */
148
149         if (S_ISDIR(entry1->type)){
150                 if (S_ISDIR(entry2->type)==0) return (-1);
151         } else{
152                 if (S_ISDIR(entry2->type)) return (1);
153         }
154         if (S_ISREG(entry1->type)){
155                 if (S_ISREG(entry2->type)==0) return (-1);
156         } else{
157                 if (S_ISREG(entry2->type)) return (1);
158         }
159         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
160         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
161         return (strcasecmp(entry1->relname,entry2->relname));
162 }
163
164
165 double BLI_diskfree(char *dir)
166 {
167 #ifdef WIN32
168         DWORD sectorspc, bytesps, freec, clusters;
169         char tmp[4];
170         
171         tmp[0]='\\'; tmp[1]=0; /* Just a failsafe */
172         if (dir[0]=='/' || dir[0]=='\\') {
173                 tmp[0]='\\';
174                 tmp[1]=0;
175         } else if (dir[1]==':') {
176                 tmp[0]=dir[0];
177                 tmp[1]=':';
178                 tmp[2]='\\';
179                 tmp[3]=0;
180         }
181
182         GetDiskFreeSpace(tmp,&sectorspc, &bytesps, &freec, &clusters);
183
184         return (double) (freec*bytesps*sectorspc);
185 #else
186         struct statfs disk;
187         char name[100],*slash;
188
189
190         strcpy(name,dir);
191
192         if(strlen(name)){
193                 slash = strrchr(name,'/');
194                 if (slash) slash[1] = 0;
195         } else strcpy(name,"/");
196
197 #if defined (__FreeBSD__) || defined (linux) || defined (__OpenBSD__) 
198         if (statfs(name, &disk)) return(-1);
199 #endif
200 #ifdef __BeOS
201         return -1;
202 #endif
203 #if defined __sgi || defined __sun__
204
205         if (statfs(name, &disk, sizeof(struct statfs), 0)){
206                 /* printf("diskfree: Couldn't get information about %s.\n",dir); */
207                 return(-1);
208         }
209 #endif
210
211         return ( ((double) disk.f_bsize) * ((double) disk.f_bfree));
212 #endif
213 }
214
215 static int hide_dot= 0;
216
217 void BLI_hide_dot_files(int set)
218 {
219         if(set) hide_dot= 1;
220         else hide_dot= 0;
221 }
222
223 void BLI_builddir(char *dirname, char *relname)
224 {
225         struct dirent *fname;
226         struct dirlink *dlink;
227         int rellen, newnum = 0, seen_ = 0, seen__ = 0;
228         char buf[256];
229         DIR *dir;
230
231         strcpy(buf,relname);
232         rellen=strlen(relname);
233
234         if (rellen){
235                 buf[rellen]='/';
236                 rellen++;
237         }
238
239         if (chdir(dirname) == -1){
240                 perror(dirname);
241                 return;
242         }
243
244         if (dir = (DIR *)opendir(".")){
245                 while ((fname = (struct dirent*) readdir(dir)) != NULL) {
246                         
247                         if(hide_dot && fname->d_name[0]=='.' && fname->d_name[1]!='.' && fname->d_name[1]!=0);
248                         else {
249                                 
250                                 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
251                                 if (dlink){
252                                         strcpy(buf+rellen,fname->d_name);
253         
254                                         dlink->name = strdup(buf);
255         
256                                         if (dlink->name[0] == '.') {
257                                                 if (dlink->name[1] == 0) seen_ = 1;
258                                                 else if (dlink->name[1] == '.') {
259                                                         if (dlink->name[2] == 0) seen__ = 1;
260                                                 }
261                                         }
262                                         BLI_addhead(dirbase,dlink);
263                                         newnum++;
264                                 }
265                         }
266                 }
267                 
268                 if (newnum){
269 #ifndef WIN32           
270                         if (seen_ == 0) {       /* Cachefs PATCH */
271                                 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
272                                 strcpy(buf+rellen,"./.");
273                                 dlink->name = strdup(buf);
274                                 BLI_addhead(dirbase,dlink);
275                                 newnum++;
276                         }
277                         if (seen__ == 0) {      /* MAC PATCH */
278                                 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
279                                 strcpy(buf+rellen,"./..");
280                                 dlink->name = strdup(buf);
281                                 BLI_addhead(dirbase,dlink);
282                                 newnum++;
283                         }
284 #endif                  
285
286                         if (files) files=(struct direntry *)realloc(files,(totnum+newnum) * sizeof(struct direntry));
287                         else files=(struct direntry *)malloc(newnum * sizeof(struct direntry));
288
289                         if (files){
290                                 dlink = (struct dirlink *) dirbase->first;
291                                 while(dlink){
292                                         memset(&files[actnum], 0 , sizeof(struct direntry));
293                                         files[actnum].relname = dlink->name;
294                                         stat(dlink->name,&files[actnum].s);
295                                         files[actnum].type=files[actnum].s.st_mode;
296                                         files[actnum].flags = 0;
297                                         totnum++;
298                                         actnum++;
299                                         dlink = dlink->next;
300                                 }
301                         } else{
302                                 printf("Couldn't get memory for dir\n");
303                                 exit(1);
304                         }
305
306                         BLI_freelist(dirbase);
307                         if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *,const void*))BLI_compare);
308                 } else {
309                         printf("%s empty directory\n",dirname);
310                 }
311
312                 closedir(dir);
313         } else {
314                 printf("%s non-existant directory\n",dirname);
315         }
316 }
317
318 #ifndef WIN32
319 void BLI_buildpwtable(struct passwd **pwtable)
320 {
321         int count=0,slen;
322         struct passwd *pw,*pwtab;
323
324         do{
325                 pw=getpwent();
326                 if (pw){
327                         count++;
328                 }
329         }while(pw);
330         endpwent();
331
332         pwtab = (struct passwd *)calloc(count+1,sizeof(struct passwd));
333         count=0;
334         do{
335                 pw=getpwent();
336                 if (pw){
337                         pwtab[count] = *pw;
338                         slen = strlen(pw->pw_name);
339                         pwtab[count].pw_name = malloc(slen+1);
340                         strcpy(pwtab[count].pw_name,pw->pw_name);
341                         count ++;
342                 }
343         }while(pw);
344         pwtab[count].pw_name = 0;
345         endpwent();
346
347         *(pwtable) = pwtab;
348 }
349
350 void BLI_freepwtable(struct passwd *pwtable)
351 {
352         int count=0;
353
354         do{
355                 if (pwtable[count].pw_name) free(pwtable[count].pw_name);
356                 else break;
357                 count++;
358         }while (1);
359
360         free(pwtable);
361 }
362
363
364 char *BLI_findpwtable(struct passwd *pwtable, ushort user)
365 {
366         static char string[32];
367         
368         while (pwtable->pw_name){
369                 if (pwtable->pw_uid == user){
370                         return (pwtable->pw_name);
371                 }
372                 pwtable++;
373         }
374         sprintf(string, "%d", user);
375         return (string);
376 }
377 #endif
378
379
380 void BLI_adddirstrings()
381 {
382         char datum[100];
383         char buf[250];
384         char size[250];
385         static char * types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
386         int num, mode;
387         int num1, num2, num3, num4, st_size;
388 #ifndef WIN32   
389         struct passwd *pwtable;
390 #endif
391         struct direntry * file;
392         struct tm *tm;
393         
394 #ifndef WIN32
395         BLI_buildpwtable(&pwtable);
396 #endif
397
398         file = &files[0];
399         
400         for(num=0;num<actnum;num++){
401 #ifdef WIN32
402                 mode = 0;
403                 strcpy(file->mode1, types[0]);
404                 strcpy(file->mode2, types[0]);
405                 strcpy(file->mode3, types[0]);
406 #else
407                 mode = file->s.st_mode;
408
409                 strcpy(file->mode1, types[(mode & 0700) >> 6]);
410                 strcpy(file->mode2, types[(mode & 0070) >> 3]);
411                 strcpy(file->mode3, types[(mode & 0007)]);
412                 
413                 if (((mode & S_ISGID) == S_ISGID) && (file->mode2[2]=='-'))file->mode2[2]='l';
414
415                 if (mode & (S_ISUID | S_ISGID)){
416                         if (file->mode1[2]=='x') file->mode1[2]='s';
417                         else file->mode1[2]='S';
418
419                         if (file->mode2[2]=='x')file->mode2[2]='s';
420                 }
421
422                 if (mode & S_ISVTX){
423                         if (file->mode3[2] == 'x') file->mode3[2] = 't';
424                         else file->mode3[2] = 'T';
425                 }
426 #endif
427
428 #ifdef WIN32
429                 strcpy(files[num].owner,"user");
430 #else
431                 strcpy(files[num].owner, BLI_findpwtable(pwtable,files[num].s.st_uid));
432 #endif
433
434                 tm= localtime(&files[num].s.st_mtime);
435                 strftime(files[num].time, 8, "%H:%M", tm);
436                 strftime(files[num].date, 16, "%d-%b-%y", tm);
437                 
438                 st_size= (int)files[num].s.st_size;
439                 
440                 num1= st_size % 1000;
441                 num2= st_size/1000;
442                 num2= num2 % 1000;
443                 num3= st_size/(1000*1000);
444                 num3= num3 % 1000;
445                 num4= st_size/(1000*1000*1000);
446                 num4= num4 % 1000;
447
448                 if(num4) sprintf(files[num].size, "%3d %03d %03d %03d", num4, num3, num2, num1);
449                 else if(num3) sprintf(files[num].size, "%7d %03d %03d", num3, num2, num1);
450                 else if(num2) sprintf(files[num].size, "%11d %03d", num2, num1);
451                 else if(num1) sprintf(files[num].size, "%15d", num1);
452                 else sprintf(files[num].size, "");
453
454                 strftime(datum, 32, "%d-%b-%y %R", tm);
455
456                 if (st_size < 1000) {
457                         sprintf(size, "%10d", st_size);
458                 } else if (st_size < 1000 * 1000) {
459                         sprintf(size, "%6d %03d", st_size / 1000, st_size % 1000);
460                 } else if (st_size < 100 * 1000 * 1000) {
461                         sprintf(size, "%2d %03d %03d", st_size / (1000 * 1000), (st_size / 1000) % 1000, st_size % 1000);
462                 } else {
463                         sprintf(size, "> %4.1f M", st_size / (1024.0 * 1024.0));
464                         sprintf(size, "%10d", st_size);
465                 }
466
467                 sprintf(buf,"%s %s %10s %s", files[num].date, files[num].time, size,
468                         files[num].relname);
469
470                 sprintf(buf,"%s %s %s %7s %s %s %10s %s", file->mode1, file->mode2, file->mode3, files[num].owner, files[num].date, files[num].time, size,
471                         files[num].relname);
472
473                 files[num].string=malloc(strlen(buf)+1);
474                 if (files[num].string){
475                         strcpy(files[num].string,buf);
476                 }
477
478                 file++;
479         }
480 #ifndef WIN32
481         BLI_freepwtable(pwtable);
482 #endif
483 }
484
485 unsigned int BLI_getdir(char *dirname,  struct direntry **filelist)
486 {
487         // reset global variables
488         // memory stored in files is free()'d in
489         // filesel.c:freefilelist()
490
491         actnum = totnum = 0;
492         files = 0;
493
494         BLI_builddir(dirname,"");
495         BLI_adddirstrings();
496
497         if (files) {
498                 *(filelist) = files;
499         } else {
500                 // keep blender happy. Blender stores this in a variable
501                 // where 0 has special meaning.....
502                 *(filelist) = files = malloc(sizeof(struct direntry));
503         }
504
505         return(actnum);
506 }
507
508
509 int BLI_filesize(int file)
510 {
511         struct stat buf;
512
513         if (file <= 0) return (-1);
514         fstat(file, &buf);
515         return (buf.st_size);
516 }
517
518
519
520 int BLI_exist(char *name)
521 {
522         struct stat st;
523
524         if (stat(name,&st)) return(0);
525         return(st.st_mode);
526 }
527
528 LinkNode *BLI_read_file_as_lines(char *name)
529 {
530         FILE *fp= fopen(name, "r");
531         LinkNode *lines= NULL;
532         char *buf;
533         int size;
534
535         if (!fp) return NULL;
536                 
537         fseek(fp, 0, SEEK_END);
538         size= ftell(fp);
539         fseek(fp, 0, SEEK_SET);
540
541         buf= malloc(size);
542         if (buf) {
543                 int i, last= 0;
544                 
545                         /* 
546                          * size = because on win32 reading
547                          * all the bytes in the file will return
548                          * less bytes because of crnl changes.
549                          */
550                 size= fread(buf, 1, size, fp);
551                 for (i=0; i<=size; i++) {
552                         if (i==size || buf[i]=='\n') {
553                                 char *line= BLI_strdupn(&buf[last], i-last);
554
555                                 BLI_linklist_prepend(&lines, line);
556                                 last= i+1;
557                         }
558                 }
559                 
560                 free(buf);
561         }
562         
563         fclose(fp);
564         
565         BLI_linklist_reverse(&lines);
566         return lines;
567 }
568
569 void BLI_free_file_lines(LinkNode *lines)
570 {
571         BLI_linklist_free(lines, (void(*)(void*)) MEM_freeN);
572 }