2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2004-11-14 08:29:32 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
2003-11-22 23:45:13 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2004-02-29 14:00:22 +00:00
|
|
|
|
2010-05-11 19:10:29 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__)
|
2005-07-14 16:46:38 +00:00
|
|
|
#include "boinc_win.h"
|
2010-05-11 19:10:29 +00:00
|
|
|
#elif defined(_WIN32) && defined(__STDWX_H__)
|
|
|
|
#include "stdwx.h"
|
2009-08-22 17:00:19 +00:00
|
|
|
#endif
|
|
|
|
|
2010-05-14 16:22:57 +00:00
|
|
|
#if defined(__MINGW32__)
|
2009-10-07 19:17:52 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2010-05-11 19:10:29 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define getcwd _getcwd
|
|
|
|
#endif
|
|
|
|
|
2005-12-16 03:35:15 +00:00
|
|
|
#if !defined(_WIN32) || defined(__CYGWIN32__)
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2008-09-09 19:10:42 +00:00
|
|
|
#ifdef _USING_FCGI_
|
|
|
|
#include "boinc_fcgi.h"
|
|
|
|
#else
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
2008-09-09 19:10:42 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <fcntl.h>
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cerrno>
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <sys/stat.h>
|
2004-04-11 20:07:53 +00:00
|
|
|
#include <sys/file.h>
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
2005-12-18 02:00:15 +00:00
|
|
|
|
2011-09-27 19:45:27 +00:00
|
|
|
#if HAVE_SYS_RESOURCE_H
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
2011-09-27 19:45:27 +00:00
|
|
|
#if HAVE_SYS_MOUNT_H
|
|
|
|
#if HAVE_SYS_PARAM_H
|
2003-08-13 21:35:26 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
2003-06-24 21:16:51 +00:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#endif
|
2002-07-03 22:37:24 +00:00
|
|
|
|
2011-09-27 19:45:27 +00:00
|
|
|
#if HAVE_SYS_STATVFS_H
|
2003-06-14 20:24:29 +00:00
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#define STATFS statvfs
|
2003-12-01 23:28:46 +00:00
|
|
|
#elif defined(HAVE_SYS_STATFS_H)
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
#define STATFS statfs
|
2003-06-14 20:24:29 +00:00
|
|
|
#else
|
|
|
|
#define STATFS statfs
|
|
|
|
#endif
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
2003-06-14 20:24:29 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#ifdef _WIN32
|
2009-04-23 03:40:49 +00:00
|
|
|
typedef BOOL (CALLBACK* FreeFn)(LPCSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
|
2002-04-30 22:22:54 +00:00
|
|
|
#endif
|
|
|
|
|
2002-06-06 18:42:01 +00:00
|
|
|
#include "util.h"
|
2007-02-21 16:26:51 +00:00
|
|
|
#include "str_util.h"
|
2009-06-16 20:54:44 +00:00
|
|
|
#include "str_replace.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "error_numbers.h"
|
|
|
|
#include "filesys.h"
|
|
|
|
|
2007-03-20 01:42:36 +00:00
|
|
|
|
2004-06-30 18:17:21 +00:00
|
|
|
using std::string;
|
|
|
|
|
2004-05-24 19:00:26 +00:00
|
|
|
char boinc_failed_file[256];
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
// routines for enumerating the entries in a directory
|
|
|
|
|
2006-01-18 23:04:05 +00:00
|
|
|
int is_file(const char* path) {
|
2002-12-11 23:09:37 +00:00
|
|
|
struct stat sbuf;
|
2011-05-13 12:42:06 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
int retval = stat(path, &sbuf);
|
|
|
|
#else
|
2011-05-12 05:26:37 +00:00
|
|
|
int retval = lstat(path, &sbuf);
|
2011-05-13 12:42:06 +00:00
|
|
|
#endif
|
2008-04-16 09:04:12 +00:00
|
|
|
return (!retval && (((sbuf.st_mode) & S_IFMT) == S_IFREG));
|
2002-12-11 23:09:37 +00:00
|
|
|
}
|
|
|
|
|
2006-01-18 23:04:05 +00:00
|
|
|
int is_dir(const char* path) {
|
2002-12-11 23:09:37 +00:00
|
|
|
struct stat sbuf;
|
2011-05-13 12:42:06 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
int retval = stat(path, &sbuf);
|
|
|
|
#else
|
2011-05-12 05:26:37 +00:00
|
|
|
int retval = lstat(path, &sbuf);
|
2011-05-13 12:42:06 +00:00
|
|
|
#endif
|
2008-04-16 09:04:12 +00:00
|
|
|
return (!retval && (((sbuf.st_mode) & S_IFMT) == S_IFDIR));
|
2002-12-11 23:09:37 +00:00
|
|
|
}
|
|
|
|
|
2008-04-03 21:59:05 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
int is_symlink(const char* path) {
|
|
|
|
struct stat sbuf;
|
2008-04-14 04:04:45 +00:00
|
|
|
int retval = lstat(path, &sbuf);
|
2008-04-14 09:15:01 +00:00
|
|
|
return (!retval && S_ISLNK(sbuf.st_mode));
|
2008-04-03 21:59:05 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Open a directory
|
2002-08-05 00:29:34 +00:00
|
|
|
//
|
2003-11-08 00:56:55 +00:00
|
|
|
DIRREF dir_open(const char* p) {
|
2002-08-09 21:43:19 +00:00
|
|
|
DIRREF dirp;
|
2007-07-08 03:30:06 +00:00
|
|
|
#ifdef _WIN32
|
2004-08-12 10:13:01 +00:00
|
|
|
if (!is_dir(p)) return NULL;
|
2002-12-06 07:33:45 +00:00
|
|
|
dirp = (DIR_DESC*) calloc(sizeof(DIR_DESC), 1);
|
2007-10-09 22:58:49 +00:00
|
|
|
if (!dirp) {
|
|
|
|
fprintf(stderr, "calloc() failed in dir_open()\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-08-09 21:43:19 +00:00
|
|
|
dirp->first = true;
|
2003-04-03 18:35:40 +00:00
|
|
|
safe_strcpy(dirp->path, p);
|
2002-12-06 07:33:45 +00:00
|
|
|
strcat(dirp->path, "\\*");
|
|
|
|
dirp->handle = INVALID_HANDLE_VALUE;
|
2007-07-08 03:30:06 +00:00
|
|
|
#else
|
|
|
|
dirp = opendir(p);
|
|
|
|
if (!dirp) return NULL;
|
2002-04-30 22:22:54 +00:00
|
|
|
#endif
|
2002-07-31 18:31:22 +00:00
|
|
|
return dirp;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Scan through a directory and return the next file name in it
|
|
|
|
//
|
2003-03-11 00:49:07 +00:00
|
|
|
int dir_scan(char* p, DIRREF dirp, int p_len) {
|
2007-07-08 03:30:06 +00:00
|
|
|
#ifdef _WIN32
|
2009-04-23 03:40:49 +00:00
|
|
|
WIN32_FIND_DATAA data;
|
2002-04-30 22:22:54 +00:00
|
|
|
while (1) {
|
2003-02-27 09:21:22 +00:00
|
|
|
if (dirp->first) {
|
|
|
|
dirp->first = false;
|
2009-04-23 03:40:49 +00:00
|
|
|
dirp->handle = FindFirstFileA(dirp->path, &data);
|
2003-02-27 09:21:22 +00:00
|
|
|
if (dirp->handle == INVALID_HANDLE_VALUE) {
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_READDIR;
|
2003-02-27 09:21:22 +00:00
|
|
|
} else {
|
2005-05-12 22:39:44 +00:00
|
|
|
// does Windows have "." and ".."? well, just in case.
|
|
|
|
//
|
|
|
|
if (!strcmp(data.cFileName, ".")) continue;
|
|
|
|
if (!strcmp(data.cFileName, "..")) continue;
|
2006-01-05 06:42:45 +00:00
|
|
|
if (p) strlcpy(p, data.cFileName, p_len);
|
2003-02-27 09:21:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
2009-04-23 03:40:49 +00:00
|
|
|
if (FindNextFileA(dirp->handle, &data)) {
|
2005-05-12 22:39:44 +00:00
|
|
|
if (!strcmp(data.cFileName, ".")) continue;
|
|
|
|
if (!strcmp(data.cFileName, "..")) continue;
|
2006-01-05 06:42:45 +00:00
|
|
|
if (p) strlcpy(p, data.cFileName, p_len);
|
2003-02-27 09:21:22 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
FindClose(dirp->handle);
|
|
|
|
dirp->handle = INVALID_HANDLE_VALUE;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2007-07-08 03:30:06 +00:00
|
|
|
#else
|
|
|
|
while (1) {
|
|
|
|
dirent* dp = readdir(dirp);
|
|
|
|
if (dp) {
|
|
|
|
if (!strcmp(dp->d_name, ".")) continue;
|
|
|
|
if (!strcmp(dp->d_name, "..")) continue;
|
|
|
|
if (p) strlcpy(p, dp->d_name, p_len);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return ERR_READDIR;
|
|
|
|
}
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Close a directory
|
|
|
|
//
|
2002-08-09 21:43:19 +00:00
|
|
|
void dir_close(DIRREF dirp) {
|
2007-07-08 03:30:06 +00:00
|
|
|
#ifdef _WIN32
|
2002-08-09 21:43:19 +00:00
|
|
|
if (dirp->handle != INVALID_HANDLE_VALUE) {
|
2003-02-27 09:21:22 +00:00
|
|
|
FindClose(dirp->handle);
|
|
|
|
dirp->handle = INVALID_HANDLE_VALUE;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2002-12-06 07:33:45 +00:00
|
|
|
free(dirp);
|
2007-07-08 03:30:06 +00:00
|
|
|
#else
|
|
|
|
if (dirp) {
|
|
|
|
closedir(dirp);
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-08-11 23:03:09 +00:00
|
|
|
bool is_dir_empty(const char *p) {
|
|
|
|
char file[256];
|
2010-08-13 18:43:24 +00:00
|
|
|
|
2010-08-11 23:03:09 +00:00
|
|
|
DIRREF dir = dir_open(p);
|
2010-08-13 18:43:24 +00:00
|
|
|
if (!dir) return true;
|
|
|
|
|
2011-10-05 19:20:53 +00:00
|
|
|
bool retval = (dir_scan(file, dir, sizeof(file)) != 0);
|
|
|
|
dir_close(dir);
|
|
|
|
return retval;
|
2010-08-11 23:03:09 +00:00
|
|
|
}
|
|
|
|
|
2003-06-24 00:17:36 +00:00
|
|
|
DirScanner::DirScanner(string const& path) {
|
2007-07-08 03:30:06 +00:00
|
|
|
#ifdef _WIN32
|
2003-06-24 00:17:36 +00:00
|
|
|
first = true;
|
|
|
|
handle = INVALID_HANDLE_VALUE;
|
2004-08-12 10:13:01 +00:00
|
|
|
if (!is_dir((char*)path.c_str())) {
|
2003-06-24 00:17:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
dir = path + "\\*";
|
2007-07-08 03:30:06 +00:00
|
|
|
#else
|
|
|
|
dirp = opendir(path.c_str());
|
2003-06-24 00:17:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan through a directory and return the next file name in it
|
|
|
|
//
|
|
|
|
bool DirScanner::scan(string& s) {
|
2007-07-08 03:30:06 +00:00
|
|
|
#ifdef _WIN32
|
2009-04-23 03:40:49 +00:00
|
|
|
WIN32_FIND_DATAA data;
|
2003-06-24 00:17:36 +00:00
|
|
|
while (1) {
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
2009-04-23 03:40:49 +00:00
|
|
|
handle = FindFirstFileA(dir.c_str(), &data);
|
2003-06-24 00:17:36 +00:00
|
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (data.cFileName[0] == '.') continue;
|
|
|
|
s = data.cFileName;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
2009-04-23 03:40:49 +00:00
|
|
|
if (FindNextFileA(handle, &data)) {
|
2003-06-24 00:17:36 +00:00
|
|
|
if (data.cFileName[0] == '.') continue;
|
|
|
|
s = data.cFileName;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
FindClose(handle);
|
|
|
|
handle = INVALID_HANDLE_VALUE;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-08 03:30:06 +00:00
|
|
|
#else
|
|
|
|
if (!dirp) return false;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
dirent* dp = readdir(dirp);
|
|
|
|
if (dp) {
|
|
|
|
if (dp->d_name[0] == '.') continue;
|
|
|
|
s = dp->d_name;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2003-06-24 00:17:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
DirScanner::~DirScanner() {
|
2007-07-08 03:30:06 +00:00
|
|
|
#ifdef _WIN32
|
2003-06-24 00:17:36 +00:00
|
|
|
if (handle != INVALID_HANDLE_VALUE) {
|
|
|
|
FindClose(handle);
|
|
|
|
}
|
2007-07-08 03:30:06 +00:00
|
|
|
#else
|
|
|
|
if (dirp) {
|
|
|
|
closedir(dirp);
|
|
|
|
}
|
2003-06-24 00:17:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-05-23 03:55:53 +00:00
|
|
|
static int boinc_delete_file_aux(const char* path) {
|
|
|
|
#ifdef _WIN32
|
2009-04-23 03:40:49 +00:00
|
|
|
if (!DeleteFileA(path)) {
|
2007-10-05 16:47:07 +00:00
|
|
|
return ERR_UNLINK;
|
|
|
|
}
|
2007-05-23 03:55:53 +00:00
|
|
|
#else
|
|
|
|
int retval = unlink(path);
|
2007-10-05 16:47:07 +00:00
|
|
|
if (retval) return ERR_UNLINK;
|
2007-05-23 03:55:53 +00:00
|
|
|
#endif
|
2007-10-05 16:47:07 +00:00
|
|
|
return 0;
|
2007-05-23 03:55:53 +00:00
|
|
|
}
|
2003-06-24 00:17:36 +00:00
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Delete the file located at path
|
|
|
|
//
|
2004-01-04 06:48:40 +00:00
|
|
|
int boinc_delete_file(const char* path) {
|
2005-07-07 22:26:49 +00:00
|
|
|
int retval = 0;
|
2002-08-05 00:29:34 +00:00
|
|
|
|
2004-03-29 22:57:43 +00:00
|
|
|
if (!boinc_file_exists(path)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2007-05-23 03:55:53 +00:00
|
|
|
retval = boinc_delete_file_aux(path);
|
|
|
|
if (retval) {
|
|
|
|
double start = dtime();
|
|
|
|
do {
|
|
|
|
boinc_sleep(drand()*2); // avoid lockstep
|
|
|
|
retval = boinc_delete_file_aux(path);
|
|
|
|
if (!retval) break;
|
2007-10-09 00:34:38 +00:00
|
|
|
} while (dtime() < start + FILE_RETRY_INTERVAL);
|
2006-08-01 12:36:19 +00:00
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
if (retval) {
|
2004-05-24 19:00:26 +00:00
|
|
|
safe_strcpy(boinc_failed_file, path);
|
2003-02-27 09:21:22 +00:00
|
|
|
return ERR_UNLINK;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get file size
|
2002-12-06 07:33:45 +00:00
|
|
|
//
|
2003-11-08 00:56:55 +00:00
|
|
|
int file_size(const char* path, double& size) {
|
2002-04-30 22:22:54 +00:00
|
|
|
struct stat sbuf;
|
|
|
|
int retval;
|
2002-08-05 00:29:34 +00:00
|
|
|
|
2008-03-27 20:30:19 +00:00
|
|
|
retval = stat(path, &sbuf);
|
2005-04-28 23:19:58 +00:00
|
|
|
if (retval) return ERR_NOT_FOUND;
|
2002-12-06 07:33:45 +00:00
|
|
|
size = (double)sbuf.st_size;
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-01-18 22:12:55 +00:00
|
|
|
int boinc_truncate(const char* path, double size) {
|
2006-01-18 22:54:24 +00:00
|
|
|
int retval;
|
2006-06-30 20:45:16 +00:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN32__)
|
2006-01-18 22:54:24 +00:00
|
|
|
// the usual Windows nightmare.
|
|
|
|
// There's another function, SetEndOfFile(),
|
|
|
|
// that supposedly works with files over 2GB,
|
|
|
|
// but it uses HANDLES
|
|
|
|
//
|
|
|
|
int fd = _open(path, _O_RDWR, 0);
|
|
|
|
if (fd == -1) return ERR_TRUNCATE;
|
|
|
|
retval = _chsize(fd, (long)size);
|
|
|
|
_close(fd);
|
|
|
|
#else
|
|
|
|
retval = truncate(path, (off_t)size);
|
|
|
|
#endif
|
2006-01-18 22:12:55 +00:00
|
|
|
if (retval) return ERR_TRUNCATE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-01-14 18:32:20 +00:00
|
|
|
// remove everything from specified directory
|
|
|
|
//
|
|
|
|
int clean_out_dir(const char* dirpath) {
|
|
|
|
char filename[256], path[256];
|
|
|
|
int retval;
|
|
|
|
DIRREF dirp;
|
|
|
|
|
|
|
|
dirp = dir_open(dirpath);
|
|
|
|
if (!dirp) return 0; // if dir doesn't exist, it's empty
|
|
|
|
while (1) {
|
|
|
|
strcpy(filename, "");
|
|
|
|
retval = dir_scan(filename, dirp, sizeof(filename));
|
|
|
|
if (retval) break;
|
|
|
|
sprintf(path, "%s/%s", dirpath, filename);
|
|
|
|
clean_out_dir(path);
|
|
|
|
boinc_rmdir(path);
|
|
|
|
retval = boinc_delete_file(path);
|
|
|
|
if (retval) {
|
|
|
|
dir_close(dirp);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dir_close(dirp);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-07-30 00:02:21 +00:00
|
|
|
|
2008-01-15 22:22:08 +00:00
|
|
|
// return total size of files in directory and optionally its subdirectories
|
2008-04-01 21:10:03 +00:00
|
|
|
// Win: use special version because stat() is slow, can be avoided
|
2008-04-03 21:59:05 +00:00
|
|
|
// Unix: follow symbolic links
|
2002-07-30 00:02:21 +00:00
|
|
|
//
|
2007-05-08 23:12:40 +00:00
|
|
|
int dir_size(const char* dirpath, double& size, bool recurse) {
|
2007-07-08 03:30:06 +00:00
|
|
|
#ifdef WIN32
|
2007-07-08 04:03:11 +00:00
|
|
|
char path2[_MAX_PATH];
|
|
|
|
sprintf(path2, "%s/*", dirpath);
|
2007-07-08 03:30:06 +00:00
|
|
|
size = 0.0;
|
2009-04-23 03:40:49 +00:00
|
|
|
WIN32_FIND_DATAA findData;
|
|
|
|
HANDLE hFind = ::FindFirstFileA(path2, &findData);
|
2008-11-02 20:09:59 +00:00
|
|
|
if (INVALID_HANDLE_VALUE == hFind) return ERR_OPENDIR;
|
|
|
|
do {
|
|
|
|
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
|
|
|
if (!recurse) continue;
|
|
|
|
if (!strcmp(findData.cFileName, ".")) continue;
|
|
|
|
if (!strcmp(findData.cFileName, "..")) continue;
|
|
|
|
|
|
|
|
double dsize = 0;
|
|
|
|
char buf[_MAX_PATH];
|
|
|
|
::sprintf(buf, "%s/%s", dirpath, findData.cFileName);
|
|
|
|
dir_size(buf, dsize, true);
|
|
|
|
size += dsize;
|
|
|
|
} else {
|
|
|
|
size += findData.nFileSizeLow + ((__int64)(findData.nFileSizeHigh) << 32);
|
|
|
|
}
|
2009-04-23 03:40:49 +00:00
|
|
|
} while (FindNextFileA(hFind, &findData));
|
2008-11-02 20:09:59 +00:00
|
|
|
::FindClose(hFind);
|
2007-07-08 03:30:06 +00:00
|
|
|
#else
|
2011-05-12 04:11:40 +00:00
|
|
|
char filename[1024], subdir[1024];
|
2002-12-06 07:33:45 +00:00
|
|
|
int retval=0;
|
2002-08-09 21:43:19 +00:00
|
|
|
DIRREF dirp;
|
2002-12-06 07:33:45 +00:00
|
|
|
double x;
|
2002-07-30 00:02:21 +00:00
|
|
|
|
2003-02-27 09:21:22 +00:00
|
|
|
size = 0;
|
2002-07-31 18:31:22 +00:00
|
|
|
dirp = dir_open(dirpath);
|
2003-10-21 04:06:55 +00:00
|
|
|
if (!dirp) return ERR_OPENDIR;
|
2002-07-30 00:02:21 +00:00
|
|
|
while (1) {
|
2003-03-11 00:49:07 +00:00
|
|
|
retval = dir_scan(filename, dirp, sizeof(filename));
|
2002-07-30 00:02:21 +00:00
|
|
|
if (retval) break;
|
2006-03-03 21:34:03 +00:00
|
|
|
sprintf(subdir, "%s/%s", dirpath, filename);
|
2002-12-06 07:33:45 +00:00
|
|
|
|
2007-05-08 23:12:40 +00:00
|
|
|
if (is_dir(subdir)) {
|
|
|
|
if (recurse) {
|
|
|
|
retval = dir_size(subdir, x);
|
|
|
|
if (retval) continue;
|
|
|
|
size += x;
|
|
|
|
}
|
2011-05-12 04:11:40 +00:00
|
|
|
} else if (is_file(subdir)) {
|
2002-12-06 07:33:45 +00:00
|
|
|
retval = file_size(subdir, x);
|
2002-12-11 23:09:37 +00:00
|
|
|
if (retval) continue;
|
2002-12-06 07:33:45 +00:00
|
|
|
size += x;
|
2002-07-30 00:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
2002-07-31 18:31:22 +00:00
|
|
|
dir_close(dirp);
|
2007-07-08 03:30:06 +00:00
|
|
|
#endif
|
2007-07-08 04:38:12 +00:00
|
|
|
return 0;
|
2002-07-30 00:02:21 +00:00
|
|
|
}
|
2002-08-22 21:44:00 +00:00
|
|
|
|
2003-12-24 00:50:51 +00:00
|
|
|
FILE* boinc_fopen(const char* path, const char* mode) {
|
2004-05-19 23:16:19 +00:00
|
|
|
// if opening for read, and file isn't there,
|
|
|
|
// leave now (avoid 5-second delay!!)
|
|
|
|
//
|
|
|
|
if (strchr(mode, 'r')) {
|
|
|
|
if (!boinc_file_exists(path)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2008-09-09 19:10:42 +00:00
|
|
|
#ifndef _USING_FCGI_
|
|
|
|
FILE *f = fopen(path, mode);
|
|
|
|
#else
|
|
|
|
FCGI_FILE *f = FCGI::fopen(path,mode);
|
|
|
|
#endif
|
|
|
|
|
2003-12-24 00:50:51 +00:00
|
|
|
#ifdef _WIN32
|
2004-04-23 00:05:16 +00:00
|
|
|
// on Windows: if fopen fails, try again for 5 seconds
|
|
|
|
// (since the file might be open by FastFind, Diskeeper etc.)
|
|
|
|
//
|
2004-03-01 19:11:12 +00:00
|
|
|
if (!f) {
|
2007-01-19 19:45:29 +00:00
|
|
|
double start = dtime();
|
|
|
|
do {
|
|
|
|
boinc_sleep(drand()*2);
|
2006-04-12 23:05:33 +00:00
|
|
|
f = _fsopen(path, mode, _SH_DENYNO);
|
2007-01-19 19:45:29 +00:00
|
|
|
// _SH_DENYNO makes the file sharable while open
|
2004-04-23 00:05:16 +00:00
|
|
|
if (f) break;
|
2007-10-09 00:34:38 +00:00
|
|
|
} while (dtime() < start + FILE_RETRY_INTERVAL);
|
2004-04-23 00:05:16 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
// Unix - if call was interrupted, retry a few times
|
|
|
|
//
|
|
|
|
if (!f) {
|
|
|
|
for (int i=0; i<5; i++) {
|
2007-05-23 03:55:53 +00:00
|
|
|
boinc_sleep(drand());
|
2004-04-23 00:05:16 +00:00
|
|
|
if (errno != EINTR) break;
|
2008-09-09 19:10:42 +00:00
|
|
|
#ifndef _USING_FCGI_
|
2004-04-23 00:05:16 +00:00
|
|
|
f = fopen(path, mode);
|
2008-09-09 19:10:42 +00:00
|
|
|
#else
|
|
|
|
f = FCGI::fopen(path, mode);
|
|
|
|
#endif
|
2004-04-23 00:05:16 +00:00
|
|
|
if (f) break;
|
2004-03-01 19:11:12 +00:00
|
|
|
}
|
2003-12-24 00:50:51 +00:00
|
|
|
}
|
2005-12-18 02:00:15 +00:00
|
|
|
if (f) {
|
|
|
|
fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
|
|
|
|
}
|
2003-12-24 00:50:51 +00:00
|
|
|
#endif
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2004-03-01 19:11:12 +00:00
|
|
|
|
2006-01-18 23:04:05 +00:00
|
|
|
int boinc_file_exists(const char* path) {
|
2010-11-30 05:27:05 +00:00
|
|
|
struct stat buf;
|
|
|
|
if (stat(path, &buf)) {
|
|
|
|
return false; // stat() returns zero on success
|
|
|
|
}
|
|
|
|
return true;
|
2008-03-27 20:30:19 +00:00
|
|
|
}
|
2004-03-01 19:11:12 +00:00
|
|
|
|
2008-03-27 20:30:19 +00:00
|
|
|
// same, but doesn't traverse symlinks
|
|
|
|
//
|
|
|
|
int boinc_file_or_symlink_exists(const char* path) {
|
2010-11-30 05:27:05 +00:00
|
|
|
struct stat buf;
|
2008-03-27 20:30:19 +00:00
|
|
|
#ifdef _WIN32
|
2010-11-30 05:27:05 +00:00
|
|
|
if (stat(path, &buf)) {
|
2008-03-27 20:30:19 +00:00
|
|
|
#else
|
2010-11-30 05:27:05 +00:00
|
|
|
if (lstat(path, &buf)) {
|
2008-03-27 20:30:19 +00:00
|
|
|
#endif
|
2010-11-30 05:27:05 +00:00
|
|
|
return false; // stat() returns zero on success
|
|
|
|
}
|
|
|
|
return true;
|
2004-03-01 19:11:12 +00:00
|
|
|
}
|
|
|
|
|
2005-06-27 16:31:49 +00:00
|
|
|
// returns zero on success, nonzero if didn't touch file
|
|
|
|
//
|
|
|
|
int boinc_touch_file(const char *path) {
|
2007-04-09 02:17:26 +00:00
|
|
|
|
2005-06-27 16:31:49 +00:00
|
|
|
if (boinc_file_exists(path)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2008-09-09 19:10:42 +00:00
|
|
|
#ifndef _USING_FCGI_
|
|
|
|
FILE *fp = fopen(path, "w");
|
|
|
|
#else
|
|
|
|
FCGI_FILE *fp = FCGI::fopen(path, "w");
|
|
|
|
#endif
|
2006-09-07 18:19:25 +00:00
|
|
|
if (fp) {
|
2005-06-27 16:31:49 +00:00
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
2007-04-09 02:17:26 +00:00
|
|
|
}
|
2005-06-27 16:31:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2004-03-01 19:11:12 +00:00
|
|
|
|
2003-11-08 00:56:55 +00:00
|
|
|
int boinc_copy(const char* orig, const char* newf) {
|
2003-07-01 18:27:08 +00:00
|
|
|
#ifdef _WIN32
|
2009-04-23 03:40:49 +00:00
|
|
|
if (!CopyFileA(orig, newf, FALSE)) { // FALSE means overwrite OK
|
2005-04-09 02:21:11 +00:00
|
|
|
return GetLastError();
|
2004-02-29 14:00:22 +00:00
|
|
|
}
|
2006-10-20 01:46:46 +00:00
|
|
|
return 0;
|
2005-12-02 22:29:35 +00:00
|
|
|
#elif defined(__EMX__)
|
2008-08-19 22:36:14 +00:00
|
|
|
char cmd[1024];
|
|
|
|
sprintf(cmd, "copy \"%s\" \"%s\"", orig, newf);
|
2008-08-20 16:49:37 +00:00
|
|
|
return system(cmd);
|
2003-07-01 18:27:08 +00:00
|
|
|
#else
|
2009-01-16 06:41:52 +00:00
|
|
|
// POSIX requires that shells run from an application will use the
|
|
|
|
// real UID and GID if different from the effective UID and GID.
|
|
|
|
// Mac OS 10.4 did not enforce this, but OS 10.5 does. Since
|
|
|
|
// system() invokes a shell, it may not properly copy the file's
|
|
|
|
// ownership or permissions when called from the BOINC Client
|
|
|
|
// under sandbox security, so we copy the file directly.
|
|
|
|
FILE *src, *dst;
|
|
|
|
int m, n;
|
|
|
|
int retval = 0;
|
|
|
|
struct stat sbuf;
|
|
|
|
unsigned char buf[65536];
|
|
|
|
src = boinc_fopen(orig, "r");
|
|
|
|
if (!src) return ERR_FOPEN;
|
|
|
|
dst = boinc_fopen(newf, "w");
|
|
|
|
if (!dst) {
|
|
|
|
fclose(src);
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
n = fread(buf, 1, sizeof(buf), src);
|
|
|
|
if (n <= 0) break;
|
|
|
|
m = fwrite(buf, 1, n, dst);
|
|
|
|
if (m != n) {
|
|
|
|
retval = ERR_FWRITE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(src);
|
|
|
|
fclose(dst);
|
|
|
|
// Copy file's ownership, permissions to the extent we are allowed
|
|
|
|
lstat(orig, &sbuf); // Get source file's info
|
|
|
|
chown(newf, sbuf.st_uid, sbuf.st_gid);
|
|
|
|
chmod(newf, sbuf.st_mode);
|
|
|
|
return retval;
|
2003-07-01 18:27:08 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-05-23 03:55:53 +00:00
|
|
|
static int boinc_rename_aux(const char* old, const char* newf) {
|
2002-08-22 21:44:00 +00:00
|
|
|
#ifdef _WIN32
|
2010-09-16 04:35:54 +00:00
|
|
|
if (MoveFileExA(old, newf, MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH)) return 0;
|
2007-05-23 03:55:53 +00:00
|
|
|
return GetLastError();
|
2004-06-09 20:04:09 +00:00
|
|
|
#else
|
2009-05-31 16:38:37 +00:00
|
|
|
int retval = rename(old, newf);
|
|
|
|
if (retval) return ERR_RENAME;
|
|
|
|
return 0;
|
2004-06-09 20:04:09 +00:00
|
|
|
#endif
|
2002-08-22 21:44:00 +00:00
|
|
|
}
|
2002-08-24 00:41:25 +00:00
|
|
|
|
2007-05-23 03:55:53 +00:00
|
|
|
int boinc_rename(const char* old, const char* newf) {
|
|
|
|
int retval=0;
|
|
|
|
|
|
|
|
retval = boinc_rename_aux(old, newf);
|
|
|
|
if (retval) {
|
|
|
|
double start = dtime();
|
|
|
|
do {
|
|
|
|
boinc_sleep(drand()*2); // avoid lockstep
|
|
|
|
retval = boinc_rename_aux(old, newf);
|
|
|
|
if (!retval) break;
|
2007-10-09 00:34:38 +00:00
|
|
|
} while (dtime() < start + FILE_RETRY_INTERVAL);
|
2007-05-23 03:55:53 +00:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2007-06-26 21:14:58 +00:00
|
|
|
// make a dir that's owner and group RWX
|
|
|
|
//
|
2004-04-15 20:42:15 +00:00
|
|
|
int boinc_mkdir(const char* path) {
|
|
|
|
if (is_dir(path)) return 0;
|
2003-05-13 18:55:07 +00:00
|
|
|
#ifdef _WIN32
|
2009-04-23 03:40:49 +00:00
|
|
|
if (!CreateDirectoryA(path, NULL)) {
|
2006-10-20 01:46:46 +00:00
|
|
|
return GetLastError();
|
|
|
|
}
|
2003-05-13 18:55:07 +00:00
|
|
|
#else
|
2006-06-07 00:09:05 +00:00
|
|
|
mode_t old_mask = umask(0);
|
2006-07-01 05:32:02 +00:00
|
|
|
int retval = mkdir(path, 0771);
|
2006-06-07 00:09:05 +00:00
|
|
|
umask(old_mask);
|
2009-05-31 16:38:37 +00:00
|
|
|
if (retval) return ERR_MKDIR;
|
2003-05-13 18:55:07 +00:00
|
|
|
#endif
|
2009-05-31 16:38:37 +00:00
|
|
|
return 0;
|
2003-05-13 18:55:07 +00:00
|
|
|
}
|
|
|
|
|
2003-11-08 00:56:55 +00:00
|
|
|
int boinc_rmdir(const char* name) {
|
2003-05-13 18:55:07 +00:00
|
|
|
#ifdef _WIN32
|
2009-04-23 03:40:49 +00:00
|
|
|
if (!RemoveDirectoryA(name)) {
|
2007-10-05 16:47:07 +00:00
|
|
|
return ERR_RMDIR;
|
2006-10-20 01:46:46 +00:00
|
|
|
}
|
2003-05-13 18:55:07 +00:00
|
|
|
#else
|
2007-10-05 16:47:07 +00:00
|
|
|
int retval = rmdir(name);
|
|
|
|
if (retval) return ERR_RMDIR;
|
2003-05-13 18:55:07 +00:00
|
|
|
#endif
|
2007-10-05 16:47:07 +00:00
|
|
|
return 0;
|
2003-05-13 18:55:07 +00:00
|
|
|
}
|
|
|
|
|
2006-06-12 04:05:10 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
int boinc_chown(const char* path, gid_t gid) {
|
2006-10-20 15:56:22 +00:00
|
|
|
if (gid) {
|
2006-06-16 13:40:30 +00:00
|
|
|
if (chown(path, (uid_t)-1, gid)) {
|
|
|
|
return ERR_CHOWN;
|
2006-10-20 15:56:22 +00:00
|
|
|
}
|
2006-06-12 04:05:10 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-04-14 23:32:17 +00:00
|
|
|
// if "filepath" is of the form a/b/c,
|
|
|
|
// create directories dirpath/a, dirpath/a/b etc.
|
|
|
|
//
|
|
|
|
int boinc_make_dirs(const char* dirpath, const char* filepath) {
|
|
|
|
char buf[1024], oldpath[1024], newpath[1024];
|
|
|
|
int retval;
|
|
|
|
char *p, *q;
|
|
|
|
|
|
|
|
if (strlen(filepath) + strlen(dirpath) > 1023) return ERR_BUFFER_OVERFLOW;
|
|
|
|
strcpy(buf, filepath);
|
2011-05-25 21:16:45 +00:00
|
|
|
strcpy(oldpath, dirpath);
|
2004-04-14 23:32:17 +00:00
|
|
|
|
|
|
|
q = buf;
|
|
|
|
while(*q) {
|
|
|
|
p = strchr(q, '/');
|
|
|
|
if (!p) break;
|
|
|
|
*p = 0;
|
2006-03-03 21:34:03 +00:00
|
|
|
sprintf(newpath, "%s/%s", oldpath, q);
|
2004-04-14 23:32:17 +00:00
|
|
|
retval = boinc_mkdir(newpath);
|
|
|
|
if (retval) return retval;
|
|
|
|
strcpy(oldpath, newpath);
|
|
|
|
q = p+1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-19 15:54:04 +00:00
|
|
|
|
2006-12-14 20:44:39 +00:00
|
|
|
FILE_LOCK::FILE_LOCK() {
|
|
|
|
#ifndef _WIN32
|
|
|
|
fd = -1;
|
|
|
|
#endif
|
2009-03-23 17:00:34 +00:00
|
|
|
locked = false;
|
2006-12-14 20:44:39 +00:00
|
|
|
}
|
2009-03-23 17:00:34 +00:00
|
|
|
|
2006-12-14 20:44:39 +00:00
|
|
|
FILE_LOCK::~FILE_LOCK() {
|
|
|
|
#ifndef _WIN32
|
|
|
|
if (fd >= 0) close(fd);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-03-05 23:12:31 +00:00
|
|
|
int FILE_LOCK::lock(const char* filename) {
|
2005-12-16 03:35:15 +00:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN32__)
|
2009-04-23 03:40:49 +00:00
|
|
|
handle = CreateFileA(
|
2004-11-11 19:33:51 +00:00
|
|
|
filename, GENERIC_WRITE,
|
|
|
|
0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
|
2005-04-09 02:21:11 +00:00
|
|
|
);
|
2005-03-05 23:12:31 +00:00
|
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
2009-03-23 17:26:20 +00:00
|
|
|
return GetLastError();
|
2005-01-20 20:16:48 +00:00
|
|
|
}
|
2005-01-19 15:54:04 +00:00
|
|
|
#else
|
2006-12-14 20:44:39 +00:00
|
|
|
if (fd<0) {
|
2009-06-09 18:46:47 +00:00
|
|
|
fd = open(filename, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
|
2006-12-14 20:44:39 +00:00
|
|
|
}
|
2006-07-06 17:45:58 +00:00
|
|
|
if (fd<0) {
|
2009-03-23 17:26:20 +00:00
|
|
|
return ERR_OPEN;
|
2006-07-06 17:45:58 +00:00
|
|
|
}
|
2005-01-20 20:16:48 +00:00
|
|
|
|
2006-07-06 17:45:58 +00:00
|
|
|
struct flock fl;
|
2009-12-31 02:15:26 +00:00
|
|
|
fl.l_type = F_WRLCK;
|
|
|
|
fl.l_whence = SEEK_SET;
|
|
|
|
fl.l_start = 0;
|
|
|
|
fl.l_len = 0;
|
2009-03-23 17:00:34 +00:00
|
|
|
if (fcntl(fd, F_SETLK, &fl) == -1) {
|
2009-03-23 17:26:20 +00:00
|
|
|
return ERR_FCNTL;
|
2009-03-23 17:00:34 +00:00
|
|
|
}
|
2006-07-06 17:45:58 +00:00
|
|
|
#endif
|
2009-03-23 17:00:34 +00:00
|
|
|
locked = true;
|
|
|
|
return 0;
|
2005-03-05 23:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int FILE_LOCK::unlock(const char* filename) {
|
2005-12-16 03:35:15 +00:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN32__)
|
2011-06-06 05:36:28 +00:00
|
|
|
CloseHandle(handle);
|
2005-03-05 23:12:31 +00:00
|
|
|
#else
|
2011-06-06 05:36:28 +00:00
|
|
|
close(fd);
|
2009-06-08 07:20:17 +00:00
|
|
|
fd = -1;
|
2005-03-05 23:12:31 +00:00
|
|
|
#endif
|
2006-12-14 20:44:39 +00:00
|
|
|
boinc_delete_file(filename);
|
2009-03-23 17:00:34 +00:00
|
|
|
locked = false;
|
2005-03-05 23:12:31 +00:00
|
|
|
return 0;
|
2004-04-07 06:51:42 +00:00
|
|
|
}
|
|
|
|
|
2006-12-14 20:44:39 +00:00
|
|
|
void boinc_getcwd(char* path) {
|
2008-08-13 17:26:26 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
getcwd(path, 256);
|
|
|
|
#else
|
2009-02-26 00:23:23 +00:00
|
|
|
char* p
|
|
|
|
#ifdef __GNUC__
|
|
|
|
__attribute__ ((unused))
|
|
|
|
#endif
|
|
|
|
= getcwd(path, 256);
|
2008-08-13 17:26:26 +00:00
|
|
|
#endif
|
2006-12-14 20:44:39 +00:00
|
|
|
}
|
|
|
|
|
2005-02-16 23:17:43 +00:00
|
|
|
void relative_to_absolute(const char* relname, char* path) {
|
2008-08-13 17:26:26 +00:00
|
|
|
boinc_getcwd(path);
|
2004-07-06 04:01:15 +00:00
|
|
|
if (strlen(relname)) {
|
2006-03-03 21:34:03 +00:00
|
|
|
strcat(path, "/");
|
2004-07-06 04:01:15 +00:00
|
|
|
strcat(path, relname);
|
|
|
|
}
|
2002-08-24 00:41:25 +00:00
|
|
|
}
|
2003-06-14 20:24:29 +00:00
|
|
|
|
|
|
|
// get total and free space on current filesystem (in bytes)
|
|
|
|
//
|
|
|
|
#ifdef _WIN32
|
2009-01-13 20:02:50 +00:00
|
|
|
int get_filesystem_info(double &total_space, double &free_space, char*) {
|
2007-05-24 17:04:54 +00:00
|
|
|
char buf[256];
|
|
|
|
boinc_getcwd(buf);
|
2005-04-09 02:21:11 +00:00
|
|
|
FreeFn pGetDiskFreeSpaceEx;
|
2007-09-12 08:25:22 +00:00
|
|
|
pGetDiskFreeSpaceEx = (FreeFn)GetProcAddress(
|
2009-04-23 03:40:49 +00:00
|
|
|
GetModuleHandleA("kernel32.dll"), "GetDiskFreeSpaceExA"
|
2007-09-12 08:25:22 +00:00
|
|
|
);
|
2005-04-09 02:21:11 +00:00
|
|
|
if (pGetDiskFreeSpaceEx) {
|
|
|
|
ULARGE_INTEGER TotalNumberOfFreeBytes;
|
|
|
|
ULARGE_INTEGER TotalNumberOfBytes;
|
2011-02-21 07:37:49 +00:00
|
|
|
ULARGE_INTEGER FreeBytesAvailable;
|
2007-09-12 08:25:22 +00:00
|
|
|
pGetDiskFreeSpaceEx(
|
2011-02-21 07:37:49 +00:00
|
|
|
buf, &FreeBytesAvailable, &TotalNumberOfBytes,
|
2007-09-12 08:25:22 +00:00
|
|
|
&TotalNumberOfFreeBytes
|
|
|
|
);
|
2005-04-09 02:21:11 +00:00
|
|
|
signed __int64 uMB;
|
2011-02-21 07:37:49 +00:00
|
|
|
uMB = FreeBytesAvailable.QuadPart / (1024 * 1024);
|
2005-04-09 02:21:11 +00:00
|
|
|
free_space = uMB * 1024.0 * 1024.0;
|
|
|
|
uMB = TotalNumberOfBytes.QuadPart / (1024 * 1024);
|
|
|
|
total_space = uMB * 1024.0 * 1024.0;
|
|
|
|
} else {
|
|
|
|
DWORD dwSectPerClust;
|
|
|
|
DWORD dwBytesPerSect;
|
|
|
|
DWORD dwFreeClusters;
|
|
|
|
DWORD dwTotalClusters;
|
2009-04-23 03:40:49 +00:00
|
|
|
GetDiskFreeSpaceA(
|
2007-09-12 08:25:22 +00:00
|
|
|
buf, &dwSectPerClust, &dwBytesPerSect, &dwFreeClusters,
|
|
|
|
&dwTotalClusters
|
|
|
|
);
|
2005-04-09 02:21:11 +00:00
|
|
|
free_space = (double)dwFreeClusters * dwSectPerClust * dwBytesPerSect;
|
|
|
|
total_space = (double)dwTotalClusters * dwSectPerClust * dwBytesPerSect;
|
|
|
|
}
|
2003-06-14 20:24:29 +00:00
|
|
|
#else
|
2009-01-13 20:02:50 +00:00
|
|
|
int get_filesystem_info(double &total_space, double &free_space, char* path) {
|
2003-06-14 20:24:29 +00:00
|
|
|
#ifdef STATFS
|
|
|
|
struct STATFS fs_info;
|
2003-06-24 00:17:36 +00:00
|
|
|
|
2007-09-12 08:25:22 +00:00
|
|
|
STATFS(path, &fs_info);
|
2011-09-27 19:45:27 +00:00
|
|
|
#if HAVE_SYS_STATVFS_H
|
2003-11-07 23:26:17 +00:00
|
|
|
total_space = (double)fs_info.f_frsize * (double)fs_info.f_blocks;
|
|
|
|
free_space = (double)fs_info.f_frsize * (double)fs_info.f_bavail;
|
2003-11-10 00:03:37 +00:00
|
|
|
#else
|
|
|
|
total_space = (double)fs_info.f_bsize * (double)fs_info.f_blocks;
|
|
|
|
free_space = (double)fs_info.f_bsize * (double)fs_info.f_bavail;
|
|
|
|
#endif
|
2003-06-14 20:24:29 +00:00
|
|
|
#else
|
|
|
|
#error Need to specify a method to obtain free/total disk space
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2004-12-10 19:17:32 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
int get_file_dir(char* filename, char* dir) {
|
|
|
|
char buf[8192], *p, path[256];
|
|
|
|
struct stat sbuf;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
p = getenv("PATH");
|
|
|
|
if (!p) return ERR_NOT_FOUND;
|
|
|
|
strcpy(buf, p);
|
|
|
|
|
|
|
|
p = strtok(buf, ":");
|
|
|
|
while (p) {
|
|
|
|
sprintf(path, "%s/%s", p, filename);
|
|
|
|
retval = stat(path, &sbuf);
|
|
|
|
if (!retval && (sbuf.st_mode & 0111)) {
|
|
|
|
strcpy(dir, p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
p = strtok(0, ":");
|
|
|
|
}
|
|
|
|
return ERR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|