e6319a22ba89a4a5820d53ec564d0fd376396a0c
[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>     
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__) || defined(__sun)
60 #include <sys/statfs.h>
61 #endif
62
63 #if defined (__FreeBSD__) || defined (__OpenBSD__) || defined (__NetBSD__)
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 #if !defined(__FreeBSD__) && !defined(__APPLE__)
99 #include <malloc.h>
100 #endif
101
102 /* lib includes */
103 #include "MEM_guardedalloc.h"
104
105 #include "DNA_listBase.h"
106 #include "BLI_blenlib.h"
107 #include "BLI_storage.h"
108 #include "BLI_storage_types.h"
109
110 #include "BLI_util.h"
111 #include "BLI_linklist.h"
112
113 /* functions */
114 void  BLI_buildpwtable(struct passwd **pwtable);
115 void  BLI_freepwtable(struct passwd *pwtable);
116 char *BLI_findpwtable(struct passwd *pwtable, unsigned short user);
117  
118 /* vars: */
119 static int totnum,actnum;
120 static struct direntry *files;
121
122 static struct ListBase dirbase_={
123         0,0};
124 static struct ListBase *dirbase = &dirbase_;
125
126
127 char *BLI_getwdN(char *dir)
128 {
129         char *pwd;
130
131         if (dir) {
132                 pwd = getenv("PWD");
133                 if (pwd){
134                         strcpy(dir, pwd);
135                         return(dir);
136                 }
137                 /* 160 is FILE_MAXDIR in filesel.c */
138                 return( getcwd(dir, 160) );
139         }
140         return(0);
141 }
142
143
144 int BLI_compare(struct direntry *entry1, struct direntry *entry2)
145 {
146         /* type is equal to stat.st_mode */
147
148         if (S_ISDIR(entry1->type)){
149                 if (S_ISDIR(entry2->type)==0) return (-1);
150         } else{
151                 if (S_ISDIR(entry2->type)) return (1);
152         }
153         if (S_ISREG(entry1->type)){
154                 if (S_ISREG(entry2->type)==0) return (-1);
155         } else{
156                 if (S_ISREG(entry2->type)) return (1);
157         }
158         if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
159         if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
160         return (strcasecmp(entry1->relname,entry2->relname));
161 }
162
163
164 double BLI_diskfree(char *dir)
165 {
166 #ifdef WIN32
167         DWORD sectorspc, bytesps, freec, clusters;
168         char tmp[4];
169         
170         tmp[0]='\\'; tmp[1]=0; /* Just a failsafe */
171         if (dir[0]=='/' || dir[0]=='\\') {
172                 tmp[0]='\\';
173                 tmp[1]=0;
174         } else if (dir[1]==':') {
175                 tmp[0]=dir[0];
176                 tmp[1]=':';
177                 tmp[2]='\\';
178                 tmp[3]=0;
179         }
180
181         GetDiskFreeSpace(tmp,&sectorspc, &bytesps, &freec, &clusters);
182
183         return (double) (freec*bytesps*sectorspc);
184 #else
185         struct statfs disk;
186         char name[100],*slash;
187
188
189         strcpy(name,dir);
190
191         if(strlen(name)){
192                 slash = strrchr(name,'/');
193                 if (slash) slash[1] = 0;
194         } else strcpy(name,"/");
195
196 #if defined (__FreeBSD__) || defined (linux) || defined (__OpenBSD__) 
197         if (statfs(name, &disk)) return(-1);
198 #endif
199 #ifdef __BeOS
200         return -1;
201 #endif
202 #if defined __sgi || defined __sun__
203
204         if (statfs(name, &disk, sizeof(struct statfs), 0)){
205                 /* printf("diskfree: Couldn't get information about %s.\n",dir); */
206                 return(-1);
207         }
208 #endif
209
210         return ( ((double) disk.f_bsize) * ((double) disk.f_bfree));
211 #endif
212 }
213
214 static int hide_dot= 0;
215
216 void BLI_hide_dot_files(int set)
217 {
218         if(set) hide_dot= 1;
219         else hide_dot= 0;
220 }
221
222 void BLI_builddir(char *dirname, char *relname)
223 {
224         struct dirent *fname;
225         struct dirlink *dlink;
226         int rellen, newnum = 0, seen_ = 0, seen__ = 0;
227         char buf[256];
228         DIR *dir;
229
230         strcpy(buf,relname);
231         rellen=strlen(relname);
232
233         if (rellen){
234                 buf[rellen]='/';
235                 rellen++;
236         }
237
238         if (chdir(dirname) == -1){
239                 perror(dirname);
240                 return;
241         }
242
243         if (dir = (DIR *)opendir(".")){
244                 while ((fname = (struct dirent*) readdir(dir)) != NULL) {
245                         
246                         if(hide_dot && fname->d_name[0]=='.' && fname->d_name[1]!='.' && fname->d_name[1]!=0);
247                         else {
248                                 
249                                 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
250                                 if (dlink){
251                                         strcpy(buf+rellen,fname->d_name);
252         
253                                         dlink->name = strdup(buf);
254         
255                                         if (dlink->name[0] == '.') {
256                                                 if (dlink->name[1] == 0) seen_ = 1;
257                                                 else if (dlink->name[1] == '.') {
258                                                         if (dlink->name[2] == 0) seen__ = 1;
259                                                 }
260                                         }
261                                         BLI_addhead(dirbase,dlink);
262                                         newnum++;
263                                 }
264                         }
265                 }
266                 
267                 if (newnum){
268 #ifndef WIN32           
269                         if (seen_ == 0) {       /* Cachefs PATCH */
270                                 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
271                                 strcpy(buf+rellen,"./.");
272                                 dlink->name = strdup(buf);
273                                 BLI_addhead(dirbase,dlink);
274                                 newnum++;
275                         }
276                         if (seen__ == 0) {      /* MAC PATCH */
277                                 dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
278                                 strcpy(buf+rellen,"./..");
279                                 dlink->name = strdup(buf);
280                                 BLI_addhead(dirbase,dlink);
281                                 newnum++;
282                         }
283 #endif                  
284
285                         if (files) files=(struct direntry *)realloc(files,(totnum+newnum) * sizeof(struct direntry));
286                         else files=(struct direntry *)malloc(newnum * sizeof(struct direntry));
287
288                         if (files){
289                                 dlink = (struct dirlink *) dirbase->first;
290                                 while(dlink){
291                                         memset(&files[actnum], 0 , sizeof(struct direntry));
292                                         files[actnum].relname = dlink->name;
293                                         stat(dlink->name,&files[actnum].s);
294                                         files[actnum].type=files[actnum].s.st_mode;
295                                         files[actnum].flags = 0;
296                                         totnum++;
297                                         actnum++;
298                                         dlink = dlink->next;
299                                 }
300                         } else{
301                                 printf("Couldn't get memory for dir\n");
302                                 exit(1);
303                         }
304
305                         BLI_freelist(dirbase);
306                         if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *,const void*))BLI_compare);
307                 } else {
308                         printf("%s empty directory\n",dirname);
309                 }
310
311                 closedir(dir);
312         } else {
313                 printf("%s non-existant directory\n",dirname);
314         }
315 }
316
317 #ifndef WIN32
318 void BLI_buildpwtable(struct passwd **pwtable)
319 {
320         int count=0,slen;
321         struct passwd *pw,*pwtab;
322
323         do{
324                 pw=getpwent();
325                 if (pw){
326                         count++;
327                 }
328         }while(pw);
329         endpwent();
330
331         pwtab = (struct passwd *)calloc(count+1,sizeof(struct passwd));
332         count=0;
333         do{
334                 pw=getpwent();
335                 if (pw){
336                         pwtab[count] = *pw;
337                         slen = strlen(pw->pw_name);
338                         pwtab[count].pw_name = malloc(slen+1);
339                         strcpy(pwtab[count].pw_name,pw->pw_name);
340                         count ++;
341                 }
342         }while(pw);
343         pwtab[count].pw_name = 0;
344         endpwent();
345
346         *(pwtable) = pwtab;
347 }
348
349 void BLI_freepwtable(struct passwd *pwtable)
350 {
351         int count=0;
352
353         do{
354                 if (pwtable[count].pw_name) free(pwtable[count].pw_name);
355                 else break;
356                 count++;
357         }while (1);
358
359         free(pwtable);
360 }
361
362
363 char *BLI_findpwtable(struct passwd *pwtable, ushort user)
364 {
365         static char string[32];
366         
367         while (pwtable->pw_name){
368                 if (pwtable->pw_uid == user){
369                         return (pwtable->pw_name);
370                 }
371                 pwtable++;
372         }
373         sprintf(string, "%d", user);
374         return (string);
375 }
376 #endif
377
378
379 void BLI_adddirstrings()
380 {
381         char datum[100];
382         char buf[250];
383         char size[250];
384         static char * types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
385         int num, mode;
386         int num1, num2, num3, num4, st_size;
387 #ifndef WIN32   
388         struct passwd *pwtable;
389 #endif
390         struct direntry * file;
391         struct tm *tm;
392         
393 #ifndef WIN32
394         BLI_buildpwtable(&pwtable);
395 #endif
396
397         file = &files[0];
398         
399         for(num=0;num<actnum;num++){
400 #ifdef WIN32
401                 mode = 0;
402                 strcpy(file->mode1, types[0]);
403                 strcpy(file->mode2, types[0]);
404                 strcpy(file->mode3, types[0]);
405 #else
406                 mode = file->s.st_mode;
407
408                 strcpy(file->mode1, types[(mode & 0700) >> 6]);
409                 strcpy(file->mode2, types[(mode & 0070) >> 3]);
410                 strcpy(file->mode3, types[(mode & 0007)]);
411                 
412                 if (((mode & S_ISGID) == S_ISGID) && (file->mode2[2]=='-'))file->mode2[2]='l';
413
414                 if (mode & (S_ISUID | S_ISGID)){
415                         if (file->mode1[2]=='x') file->mode1[2]='s';
416                         else file->mode1[2]='S';
417
418                         if (file->mode2[2]=='x')file->mode2[2]='s';
419                 }
420
421                 if (mode & S_ISVTX){
422                         if (file->mode3[2] == 'x') file->mode3[2] = 't';
423                         else file->mode3[2] = 'T';
424                 }
425 #endif
426
427 #ifdef WIN32
428                 strcpy(files[num].owner,"user");
429 #else
430                 strcpy(files[num].owner, BLI_findpwtable(pwtable,files[num].s.st_uid));
431 #endif
432
433                 tm= localtime(&files[num].s.st_mtime);
434                 strftime(files[num].time, 8, "%H:%M", tm);
435                 strftime(files[num].date, 16, "%d-%b-%y", tm);
436                 
437                 st_size= (int)files[num].s.st_size;
438                 
439                 num1= st_size % 1000;
440                 num2= st_size/1000;
441                 num2= num2 % 1000;
442                 num3= st_size/(1000*1000);
443                 num3= num3 % 1000;
444                 num4= st_size/(1000*1000*1000);
445                 num4= num4 % 1000;
446
447                 if(num4) sprintf(files[num].size, "%3d %03d %03d %03d", num4, num3, num2, num1);
448                 else if(num3) sprintf(files[num].size, "%7d %03d %03d", num3, num2, num1);
449                 else if(num2) sprintf(files[num].size, "%11d %03d", num2, num1);
450                 else if(num1) sprintf(files[num].size, "%15d", num1);
451                 else sprintf(files[num].size, "");
452
453                 strftime(datum, 32, "%d-%b-%y %R", tm);
454
455                 if (st_size < 1000) {
456                         sprintf(size, "%10d", st_size);
457                 } else if (st_size < 1000 * 1000) {
458                         sprintf(size, "%6d %03d", st_size / 1000, st_size % 1000);
459                 } else if (st_size < 100 * 1000 * 1000) {
460                         sprintf(size, "%2d %03d %03d", st_size / (1000 * 1000), (st_size / 1000) % 1000, st_size % 1000);
461                 } else {
462                         sprintf(size, "> %4.1f M", st_size / (1024.0 * 1024.0));
463                         sprintf(size, "%10d", st_size);
464                 }
465
466                 sprintf(buf,"%s %s %10s %s", files[num].date, files[num].time, size,
467                         files[num].relname);
468
469                 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,
470                         files[num].relname);
471
472                 files[num].string=malloc(strlen(buf)+1);
473                 if (files[num].string){
474                         strcpy(files[num].string,buf);
475                 }
476
477                 file++;
478         }
479 #ifndef WIN32
480         BLI_freepwtable(pwtable);
481 #endif
482 }
483
484 unsigned int BLI_getdir(char *dirname,  struct direntry **filelist)
485 {
486         // reset global variables
487         // memory stored in files is free()'d in
488         // filesel.c:freefilelist()
489
490         actnum = totnum = 0;
491         files = 0;
492
493         BLI_builddir(dirname,"");
494         BLI_adddirstrings();
495
496         if (files) {
497                 *(filelist) = files;
498         } else {
499                 // keep blender happy. Blender stores this in a variable
500                 // where 0 has special meaning.....
501                 *(filelist) = files = malloc(sizeof(struct direntry));
502         }
503
504         return(actnum);
505 }
506
507
508 int BLI_filesize(int file)
509 {
510         struct stat buf;
511
512         if (file <= 0) return (-1);
513         fstat(file, &buf);
514         return (buf.st_size);
515 }
516
517
518
519 int BLI_exist(char *name)
520 {
521         struct stat st;
522
523         if (stat(name,&st)) return(0);
524         return(st.st_mode);
525 }
526
527 LinkNode *BLI_read_file_as_lines(char *name)
528 {
529         FILE *fp= fopen(name, "r");
530         LinkNode *lines= NULL;
531         char *buf;
532         int size;
533
534         if (!fp) return NULL;
535                 
536         fseek(fp, 0, SEEK_END);
537         size= ftell(fp);
538         fseek(fp, 0, SEEK_SET);
539
540         buf= malloc(size);
541         if (buf) {
542                 int i, last= 0;
543                 
544                         /* 
545                          * size = because on win32 reading
546                          * all the bytes in the file will return
547                          * less bytes because of crnl changes.
548                          */
549                 size= fread(buf, 1, size, fp);
550                 for (i=0; i<=size; i++) {
551                         if (i==size || buf[i]=='\n') {
552                                 char *line= BLI_strdupn(&buf[last], i-last);
553
554                                 BLI_linklist_prepend(&lines, line);
555                                 last= i+1;
556                         }
557                 }
558                 
559                 free(buf);
560         }
561         
562         fclose(fp);
563         
564         BLI_linklist_reverse(&lines);
565         return lines;
566 }
567
568 void BLI_free_file_lines(LinkNode *lines)
569 {
570         BLI_linklist_free(lines, (void(*)(void*)) MEM_freeN);
571 }