This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/blenlib/intern/storage.c

533 lines
13 KiB
C
Raw Normal View History

/*
2002-10-12 11:37:38 +00:00
* $Id$
*
* ***** 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
* 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.
*
* ***** END GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
* Reorganised mar-01 nzc
* Some really low-level file thingies.
*/
2011-02-27 20:37:56 +00:00
/** \file blender/blenlib/intern/storage.c
* \ingroup bli
*/
2002-10-12 11:37:38 +00:00
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
2002-10-12 11:37:38 +00:00
#ifndef WIN32
#include <dirent.h>
#endif
#include <time.h>
#include <sys/stat.h>
2011-09-21 08:40:30 +00:00
#if defined (__sun__) || defined (__sun) || defined (__NetBSD__)
#include <sys/statvfs.h> /* Other modern unix os's should probably use this also */
#elif !defined(__FreeBSD__) && !defined(linux) && (defined(__sparc) || defined(__sparc__))
2002-10-12 11:37:38 +00:00
#include <sys/statfs.h>
#endif
#if defined (__FreeBSD__) || defined (__OpenBSD__)
2002-10-12 11:37:38 +00:00
#include <sys/param.h>
#include <sys/mount.h>
#endif
#if defined(linux) || defined(__CYGWIN32__) || defined(__hpux)
2002-10-12 11:37:38 +00:00
#include <sys/vfs.h>
#endif
#ifdef __APPLE__
/* For statfs */
#include <sys/param.h>
#include <sys/mount.h>
#endif /* __APPLE__ */
#include <fcntl.h>
#include <string.h> /* strcpy etc.. */
#ifndef WIN32
#include <sys/ioctl.h>
#include <unistd.h> /* */
#include <pwd.h>
#endif
#ifdef WIN32
#include <io.h>
#include <direct.h>
#include "BLI_winstuff.h"
#endif
2002-10-12 11:37:38 +00:00
/* lib includes */
#include "MEM_guardedalloc.h"
#include "DNA_listBase.h"
#include "BLI_listbase.h"
2002-10-12 11:37:38 +00:00
#include "BLI_linklist.h"
#include "BLI_storage.h"
#include "BLI_storage_types.h"
#include "BLI_string.h"
#include "BKE_utildefines.h"
2002-10-12 11:37:38 +00:00
/* vars: */
static int totnum,actnum;
static struct direntry *files;
static struct ListBase dirbase_={NULL, NULL};
2002-10-12 11:37:38 +00:00
static struct ListBase *dirbase = &dirbase_;
/* can return NULL when the size is not big enough */
char *BLI_getwdN(char *dir, const int maxncpy)
2002-10-12 11:37:38 +00:00
{
const char *pwd= getenv("PWD");
if (pwd){
BLI_strncpy(dir, pwd, maxncpy);
return dir;
2002-10-12 11:37:38 +00:00
}
return getcwd(dir, maxncpy);
2002-10-12 11:37:38 +00:00
}
int BLI_compare(struct direntry *entry1, struct direntry *entry2)
{
/* type is equal to stat.st_mode */
2002-10-12 11:37:38 +00:00
if (S_ISDIR(entry1->type)){
if (S_ISDIR(entry2->type)==0) return (-1);
} else{
if (S_ISDIR(entry2->type)) return (1);
}
if (S_ISREG(entry1->type)){
if (S_ISREG(entry2->type)==0) return (-1);
} else{
if (S_ISREG(entry2->type)) return (1);
}
if ((entry1->type & S_IFMT) < (entry2->type & S_IFMT)) return (-1);
if ((entry1->type & S_IFMT) > (entry2->type & S_IFMT)) return (1);
/* make sure "." and ".." are always first */
if( strcmp(entry1->relname, ".")==0 ) return (-1);
if( strcmp(entry2->relname, ".")==0 ) return (1);
if( strcmp(entry1->relname, "..")==0 ) return (-1);
if( strcmp(entry2->relname, "..")==0 ) return (1);
return (BLI_natstrcmp(entry1->relname,entry2->relname));
2002-10-12 11:37:38 +00:00
}
double BLI_diskfree(const char *dir)
2002-10-12 11:37:38 +00:00
{
#ifdef WIN32
DWORD sectorspc, bytesps, freec, clusters;
char tmp[4];
tmp[0]='\\'; tmp[1]=0; /* Just a failsafe */
if (dir[0]=='/' || dir[0]=='\\') {
tmp[0]='\\';
tmp[1]=0;
} else if (dir[1]==':') {
tmp[0]=dir[0];
tmp[1]=':';
tmp[2]='\\';
tmp[3]=0;
}
GetDiskFreeSpace(tmp,&sectorspc, &bytesps, &freec, &clusters);
return (double) (freec*bytesps*sectorspc);
#else
2011-09-21 08:40:30 +00:00
#if defined (__sun__) || defined (__sun) || defined (__NetBSD__)
struct statvfs disk;
#else
2002-10-12 11:37:38 +00:00
struct statfs disk;
#endif
char name[FILE_MAXDIR],*slash;
int len = strlen(dir);
if (len >= FILE_MAXDIR) /* path too long */
return -1;
2002-10-12 11:37:38 +00:00
strcpy(name,dir);
if(len){
2002-10-12 11:37:38 +00:00
slash = strrchr(name,'/');
if (slash) slash[1] = 0;
} else strcpy(name,"/");
#if defined (__FreeBSD__) || defined (linux) || defined (__OpenBSD__) || defined (__APPLE__)
2002-10-12 11:37:38 +00:00
if (statfs(name, &disk)) return(-1);
#endif
2011-09-21 08:40:30 +00:00
#if defined (__sun__) || defined (__sun) || defined (__NetBSD__)
if (statvfs(name, &disk)) return(-1);
#elif !defined(__FreeBSD__) && !defined(linux) && (defined(__sparc) || defined(__sparc__))
/* WARNING - This may not be supported by geeneric unix os's - Campbell */
if (statfs(name, &disk, sizeof(struct statfs), 0)) return(-1);
2002-10-12 11:37:38 +00:00
#endif
return ( ((double) disk.f_bsize) * ((double) disk.f_bfree));
#endif
}
void BLI_builddir(const char *dirname, const char *relname)
2002-10-12 11:37:38 +00:00
{
struct dirent *fname;
struct dirlink *dlink;
int rellen, newnum = 0;
2002-10-12 11:37:38 +00:00
char buf[256];
DIR *dir;
strcpy(buf,relname);
rellen=strlen(relname);
if (rellen){
buf[rellen]='/';
rellen++;
}
if (chdir(dirname) == -1){
perror(dirname);
return;
}
if ( (dir = (DIR *)opendir(".")) ){
2002-10-12 11:37:38 +00:00
while ((fname = (struct dirent*) readdir(dir)) != NULL) {
dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
if (dlink){
strcpy(buf+rellen,fname->d_name);
dlink->name = BLI_strdup(buf);
BLI_addhead(dirbase,dlink);
newnum++;
2002-10-12 11:37:38 +00:00
}
}
if (newnum){
if(files) {
void *tmp= realloc(files, (totnum+newnum) * sizeof(struct direntry));
if(tmp) {
files= (struct direntry *)tmp;
}
else { /* realloc fail */
free(files);
files= NULL;
}
}
if(files==NULL)
files=(struct direntry *)malloc(newnum * sizeof(struct direntry));
2002-10-12 11:37:38 +00:00
if (files){
dlink = (struct dirlink *) dirbase->first;
while(dlink){
memset(&files[actnum], 0 , sizeof(struct direntry));
files[actnum].relname = dlink->name;
Drag and drop 2.5 integration! Finally, slashdot regulars can use Blender too now! :) ** Drag works as follows: - drag-able items are defined by the standard interface ui toolkit - each button can get this feature, via uiButSetDragXXX(but, ...). There are calls to define drag-able images, ID blocks, RNA paths, file paths, and so on. By default you drag an icon, exceptionally an ImBuf - Drag items are registered centrally in the WM, it allows more drag items simultaneous too, but not implemented ** Drop works as follows: - On mouse release, and if drag items exist in the WM, it converts the mouse event to an EVT_DROP type. This event then gets the full drag info as customdata - drop regions are defined with WM_dropbox_add(), similar to keymaps you can make a "drop map" this way, which become 'drop map handlers' in the queues. - next to that the UI kit handles some common button types (like accepting ID or names) to be catching a drop event too. - Every "drop box" has two callbacks: - poll() = check if the event drag data is relevant for this box - copy() = fill in custom properties in the dropbox to initialize an operator - The dropbox handler then calls its standard Operator with its dropbox properties. ** Currently implemented Drag items: - ID icons in browse buttons - ID icons in context menu of properties region - ID icons in outliner and rna viewer - FileBrowser icons - FileBrowser preview images Drag-able icons are subtly visualized by making them brighter a bit on mouse-over. In case the icon is a button or UI element too (most cases), the drag-able feature will make the item react to mouse-release instead of mouse-press. Drop options: - UI buttons: ID and text buttons (paste name) - View3d: Object ID drop copies object - View3d: Material ID drop assigns to object under cursor - View3d: Image ID drop assigns to object UV texture under cursor - Sequencer: Path drop will add either Image or Movie strip - Image window: Path drop will open image ** Drag and drop Notes: - Dropping into another Blender window (from same application) works too. I've added code that passes on mousemoves and clicks to other windows, without activating them though. This does make using multi-window Blender a bit friendler. - Dropping a file path to an image, is not the same as dropping an Image ID... keep this in mind. Sequencer for example wants paths to be dropped, textures in 3d window wants an Image ID. - Although drop boxes could be defined via Python, I suggest they're part of the UI and editor design (= how we want an editor to work), and not default offered configurable like keymaps. - At the moment only one item can be dragged at a time. This is for several reasons.... For one, Blender doesn't have a well defined uniform way to define "what is selected" (files, outliner items, etc). Secondly there's potential conflicts on what todo when you drop mixed drag sets on spots. All undefined stuff... nice for later. - Example to bypass the above: a collection of images that form a strip, should be represented in filewindow as a single sequence anyway. This then will fit well and gets handled neatly by design. - Another option to check is to allow multiple options per drop... it could show the operator as a sort of menu, allowing arrow or scrollwheel to choose. For time being I'd prefer to try to design a singular drop though, just offer only one drop action per data type on given spots. - What does work already, but a tad slow, is to use a function that detects an object (type) under cursor, so a drag item's option can be further refined (like drop object on object = parent). (disabled) ** More notes - Added saving for Region layouts (like split points for toolbar) - Label buttons now handle mouse over - File list: added full path entry for drop feature. - Filesel bugfix: wm_operator_exec() got called there and fully handled, while WM event code tried same. Added new OPERATOR_HANDLED flag for this. Maybe python needs it too? - Cocoa: added window move event, so multi-win setups work OK (didnt save). - Interface_handlers.c: removed win->active - Severe area copy bug: area handlers were not set to NULL - Filesel bugfix: next/prev folder list was not copied on area copies ** Leftover todos - Cocoa windows seem to hang on cases still... needs check - Cocoa 'draw overlap' swap doesn't work - Cocoa window loses focus permanently on using Spotlight (for these reasons, makefile building has Carbon as default atm) - ListView templates in UI cannot become dragged yet, needs review... it consists of two overlapping UI elements, preventing handling icon clicks. - There's already Ghost library code to handle dropping from OS into Blender window. I've noticed this code is unfinished for Macs, but seems to be complete for Windows. Needs test... currently, an external drop event will print in console when succesfully delivered to Blender's WM.
2010-01-26 18:18:21 +00:00
files[actnum].path = BLI_strdupcat(dirname, dlink->name);
// use 64 bit file size, only needed for WIN32 and WIN64.
// Excluding other than current MSVC compiler until able to test.
#if (defined(WIN32) || defined(WIN64)) && (_MSC_VER>=1500)
_stat64(dlink->name,&files[actnum].s);
#elif defined(__MINGW32__)
_stati64(dlink->name,&files[actnum].s);
#else
2002-10-12 11:37:38 +00:00
stat(dlink->name,&files[actnum].s);
#endif
2002-10-12 11:37:38 +00:00
files[actnum].type=files[actnum].s.st_mode;
files[actnum].flags = 0;
totnum++;
actnum++;
dlink = dlink->next;
}
} else{
printf("Couldn't get memory for dir\n");
exit(1);
}
BLI_freelist(dirbase);
if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *,const void*))BLI_compare);
} else {
printf("%s empty directory\n",dirname);
}
closedir(dir);
} else {
printf("%s non-existant directory\n",dirname);
}
}
void BLI_adddirstrings(void)
2002-10-12 11:37:38 +00:00
{
char datum[100];
char buf[512];
2002-10-12 11:37:38 +00:00
char size[250];
static const char * types[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};
2002-10-12 11:37:38 +00:00
int num, mode;
#ifdef WIN32
__int64 st_size;
#else
off_t st_size;
#endif
2002-10-12 11:37:38 +00:00
struct direntry * file;
struct tm *tm;
time_t zero= 0;
2002-10-12 11:37:38 +00:00
for(num=0, file= files; num<actnum; num++, file++){
2002-10-12 11:37:38 +00:00
#ifdef WIN32
mode = 0;
BLI_strncpy(file->mode1, types[0], sizeof(file->mode1));
BLI_strncpy(file->mode2, types[0], sizeof(file->mode2));
BLI_strncpy(file->mode3, types[0], sizeof(file->mode3));
2002-10-12 11:37:38 +00:00
#else
mode = file->s.st_mode;
BLI_strncpy(file->mode1, types[(mode & 0700) >> 6], sizeof(file->mode1));
BLI_strncpy(file->mode2, types[(mode & 0070) >> 3], sizeof(file->mode2));
BLI_strncpy(file->mode3, types[(mode & 0007)], sizeof(file->mode3));
2002-10-12 11:37:38 +00:00
if (((mode & S_ISGID) == S_ISGID) && (file->mode2[2]=='-'))file->mode2[2]='l';
if (mode & (S_ISUID | S_ISGID)){
if (file->mode1[2]=='x') file->mode1[2]='s';
else file->mode1[2]='S';
if (file->mode2[2]=='x')file->mode2[2]='s';
}
if (mode & S_ISVTX){
if (file->mode3[2] == 'x') file->mode3[2] = 't';
else file->mode3[2] = 'T';
}
#endif
#ifdef WIN32
strcpy(file->owner,"user");
2002-10-12 11:37:38 +00:00
#else
{
struct passwd *pwuser;
pwuser = getpwuid(file->s.st_uid);
if ( pwuser ) {
BLI_strncpy(file->owner, pwuser->pw_name, sizeof(file->owner));
} else {
BLI_snprintf(file->owner, sizeof(file->owner), "%d", file->s.st_uid);
}
}
2002-10-12 11:37:38 +00:00
#endif
tm= localtime(&file->s.st_mtime);
// prevent impossible dates in windows
if(tm==NULL) tm= localtime(&zero);
strftime(file->time, 8, "%H:%M", tm);
strftime(file->date, 16, "%d-%b-%y", tm);
/*
* Seems st_size is signed 32-bit value in *nix and Windows. This
* will buy us some time until files get bigger than 4GB or until
* everyone starts using __USE_FILE_OFFSET64 or equivalent.
*/
st_size= file->s.st_size;
if (st_size > 1024*1024*1024) {
sprintf(file->size, "%.2f GB", ((double)st_size)/(1024*1024*1024));
}
else if (st_size > 1024*1024) {
sprintf(file->size, "%.1f MB", ((double)st_size)/(1024*1024));
}
else if (st_size > 1024) {
sprintf(file->size, "%d KB", (int)(st_size/1024));
}
else {
sprintf(file->size, "%d B", (int)st_size);
}
2002-10-12 11:37:38 +00:00
strftime(datum, 32, "%d-%b-%y %H:%M", tm);
2002-10-12 11:37:38 +00:00
if (st_size < 1000) {
sprintf(size, "%10d", (int) st_size);
2002-10-12 11:37:38 +00:00
} else if (st_size < 1000 * 1000) {
sprintf(size, "%6d %03d", (int) (st_size / 1000), (int) (st_size % 1000));
2002-10-12 11:37:38 +00:00
} else if (st_size < 100 * 1000 * 1000) {
sprintf(size, "%2d %03d %03d", (int) (st_size / (1000 * 1000)), (int) ((st_size / 1000) % 1000), (int) ( st_size % 1000));
2002-10-12 11:37:38 +00:00
} else {
sprintf(size, "> %4.1f M", (double) (st_size / (1024.0 * 1024.0)));
sprintf(size, "%10d", (int) st_size);
2002-10-12 11:37:38 +00:00
}
sprintf(buf,"%s %s %s %7s %s %s %10s %s", file->mode1, file->mode2, file->mode3, file->owner, file->date, file->time, size,
file->relname);
2002-10-12 11:37:38 +00:00
file->string=MEM_mallocN(strlen(buf)+1, "filestring");
if (file->string){
strcpy(file->string,buf);
2002-10-12 11:37:38 +00:00
}
}
}
unsigned int BLI_getdir(const char *dirname, struct direntry **filelist)
2002-10-12 11:37:38 +00:00
{
// reset global variables
// memory stored in files is free()'d in
// filesel.c:freefilelist()
actnum = totnum = 0;
files = NULL;
2002-10-12 11:37:38 +00:00
BLI_builddir(dirname,"");
BLI_adddirstrings();
if (files) {
*(filelist) = files;
} else {
// keep blender happy. Blender stores this in a variable
// where 0 has special meaning.....
*(filelist) = files = malloc(sizeof(struct direntry));
}
return(actnum);
}
size_t BLI_filesize(int file)
2002-10-12 11:37:38 +00:00
{
struct stat buf;
if (file <= 0) return (-1);
fstat(file, &buf);
return (buf.st_size);
}
size_t BLI_filepathsize(const char *path)
{
int size, file = open(path, O_BINARY|O_RDONLY);
if (file == -1)
return -1;
size = BLI_filesize(file);
close(file);
return size;
}
2002-10-12 11:37:38 +00:00
int BLI_exist(const char *name)
2002-10-12 11:37:38 +00:00
{
#if defined(WIN32) && !defined(__MINGW32__)
struct _stat64i32 st;
/* in Windows stat doesn't recognize dir ending on a slash
To not break code where the ending slash is expected we
don't mess with the argument name directly here - elubie */
char tmp[FILE_MAXDIR+FILE_MAXFILE];
int len, res;
BLI_strncpy(tmp, name, FILE_MAXDIR+FILE_MAXFILE);
len = strlen(tmp);
if (len > 3 && ( tmp[len-1]=='\\' || tmp[len-1]=='/') ) tmp[len-1] = '\0';
res = _stat(tmp, &st);
if (res == -1) return(0);
#elif defined(__MINGW32__)
struct _stati64 st;
char tmp[FILE_MAXDIR+FILE_MAXFILE];
int len, res;
BLI_strncpy(tmp, name, FILE_MAXDIR+FILE_MAXFILE);
len = strlen(tmp);
if (len > 3 && ( tmp[len-1]=='\\' || tmp[len-1]=='/') ) tmp[len-1] = '\0';
res = _stati64(tmp, &st);
if (res) return(0);
#else
struct stat st;
if (stat(name,&st)) return(0);
#endif
2002-10-12 11:37:38 +00:00
return(st.st_mode);
}
/* would be better in fileops.c except that it needs stat.h so add here */
int BLI_is_dir(const char *file)
{
return S_ISDIR(BLI_exist(file));
}
LinkNode *BLI_read_file_as_lines(const char *name)
2002-10-12 11:37:38 +00:00
{
FILE *fp= fopen(name, "r");
LinkNode *lines= NULL;
char *buf;
size_t size;
2002-10-12 11:37:38 +00:00
if (!fp) return NULL;
fseek(fp, 0, SEEK_END);
size= (size_t)ftell(fp);
2002-10-12 11:37:38 +00:00
fseek(fp, 0, SEEK_SET);
buf= MEM_mallocN(size, "file_as_lines");
2002-10-12 11:37:38 +00:00
if (buf) {
size_t i, last= 0;
2002-10-12 11:37:38 +00:00
/*
* size = because on win32 reading
* all the bytes in the file will return
* less bytes because of crnl changes.
*/
size= fread(buf, 1, size, fp);
for (i=0; i<=size; i++) {
if (i==size || buf[i]=='\n') {
char *line= BLI_strdupn(&buf[last], i-last);
BLI_linklist_prepend(&lines, line);
last= i+1;
}
}
MEM_freeN(buf);
2002-10-12 11:37:38 +00:00
}
fclose(fp);
BLI_linklist_reverse(&lines);
return lines;
}
void BLI_free_file_lines(LinkNode *lines)
{
BLI_linklist_free(lines, (void(*)(void*)) MEM_freeN);
}
int BLI_file_older(const char *file1, const char *file2)
{
struct stat st1, st2;
if(stat(file1, &st1)) return 0;
if(stat(file2, &st2)) return 0;
return (st1.st_mtime < st2.st_mtime);
}