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