2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2004-11-14 08:29:32 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This 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 2.1 of the License, or (at your option) any later version.
|
2003-11-22 23:45:13 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software 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 Lesser General Public License for more details.
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2004-02-29 14:00:22 +00:00
|
|
|
|
2005-07-14 16:46:38 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__) && !defined(_BOINC_WIN_) && !defined(_AFX_STDAFX_H_)
|
|
|
|
#include "boinc_win.h"
|
|
|
|
#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"
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
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
|
|
|
|
2002-07-03 22:37:24 +00:00
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
2003-06-24 21:16:51 +00:00
|
|
|
#ifdef HAVE_SYS_MOUNT_H
|
2004-07-08 21:37:52 +00:00
|
|
|
#ifdef 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
|
|
|
|
2003-06-14 20:24:29 +00:00
|
|
|
#ifdef HAVE_SYS_STATVFS_H
|
|
|
|
#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
|
2003-06-19 18:19:35 +00:00
|
|
|
typedef BOOL (CALLBACK* FreeFn)(LPCTSTR, 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"
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "error_numbers.h"
|
|
|
|
#include "filesys.h"
|
|
|
|
|
2004-04-30 23:18:56 +00:00
|
|
|
#ifdef _USING_FCGI_
|
|
|
|
#include "fcgi_stdio.h"
|
|
|
|
#endif
|
|
|
|
|
2007-03-20 01:42:36 +00:00
|
|
|
|
2007-01-19 19:45:29 +00:00
|
|
|
#define RETRY_INTERVAL 5
|
|
|
|
// On Windows, retry for this period of time, since some other program
|
|
|
|
// (virus scan, defrag, index) may have the file open.
|
|
|
|
|
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;
|
2004-04-15 20:42:15 +00:00
|
|
|
int retval = stat(path, &sbuf);
|
2005-04-12 21:54:40 +00:00
|
|
|
return (!retval && (sbuf.st_mode & 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;
|
2004-04-15 20:42:15 +00:00
|
|
|
int retval = stat(path, &sbuf);
|
2005-04-12 21:54:40 +00:00
|
|
|
return (!retval && (sbuf.st_mode & S_IFDIR));
|
2002-12-11 23:09:37 +00:00
|
|
|
}
|
|
|
|
|
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);
|
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
|
2002-04-30 22:22:54 +00:00
|
|
|
WIN32_FIND_DATA data;
|
|
|
|
while (1) {
|
2003-02-27 09:21:22 +00:00
|
|
|
if (dirp->first) {
|
|
|
|
dirp->first = false;
|
|
|
|
dirp->handle = FindFirstFile(dirp->path, &data);
|
|
|
|
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 {
|
|
|
|
if (FindNextFile(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
|
|
|
|
}
|
|
|
|
|
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
|
2003-06-24 00:17:36 +00:00
|
|
|
WIN32_FIND_DATA data;
|
|
|
|
while (1) {
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
2003-06-24 20:13:02 +00:00
|
|
|
handle = FindFirstFile(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 {
|
|
|
|
if (FindNextFile(handle, &data)) {
|
|
|
|
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
|
2007-10-05 16:47:07 +00:00
|
|
|
if (!DeleteFile(path)) {
|
|
|
|
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;
|
|
|
|
} while (dtime() < start + 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
|
|
|
|
2002-04-30 22:22:54 +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;
|
|
|
|
}
|
|
|
|
|
2007-01-11 00:20:58 +00:00
|
|
|
// recursively delete everything in the specified directory
|
2007-03-15 23:01:51 +00:00
|
|
|
// (but not the directory itself).
|
|
|
|
// If an error occurs, delete as much as you can.
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2003-11-08 00:56:55 +00:00
|
|
|
int clean_out_dir(const char* dirpath) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char filename[256], path[256];
|
2007-03-15 23:01:51 +00:00
|
|
|
int retval, final_retval = 0;
|
2002-08-09 21:43:19 +00:00
|
|
|
DIRREF dirp;
|
2003-06-24 00:17:36 +00:00
|
|
|
|
2002-07-31 18:31:22 +00:00
|
|
|
dirp = dir_open(dirpath);
|
2006-08-01 12:36:19 +00:00
|
|
|
if (!dirp) {
|
2006-07-06 11:04:46 +00:00
|
|
|
return 0; // if dir doesn't exist, it's empty
|
2006-08-01 12:36:19 +00:00
|
|
|
}
|
2007-04-09 02:17:26 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
while (1) {
|
2003-04-03 18:35:40 +00:00
|
|
|
strcpy(filename, "");
|
2003-03-11 00:49:07 +00:00
|
|
|
retval = dir_scan(filename, dirp, sizeof(filename));
|
2002-04-30 22:22:54 +00:00
|
|
|
if (retval) break;
|
2006-03-03 21:34:03 +00:00
|
|
|
sprintf(path, "%s/%s", dirpath, filename);
|
2007-03-15 23:01:51 +00:00
|
|
|
if (is_dir(path)) {
|
|
|
|
retval = clean_out_dir(path);
|
|
|
|
if (retval) final_retval = retval;
|
|
|
|
retval = boinc_rmdir(path);
|
|
|
|
if (retval) final_retval = retval;
|
|
|
|
} else {
|
|
|
|
retval = boinc_delete_file(path);
|
|
|
|
if (retval) final_retval = retval;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
2002-07-31 18:31:22 +00:00
|
|
|
dir_close(dirp);
|
2007-03-15 23:01:51 +00:00
|
|
|
return final_retval;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2002-07-30 00:02:21 +00:00
|
|
|
|
2003-06-14 20:24:29 +00:00
|
|
|
// return total size of files in directory and its subdirectories
|
2007-07-08 03:30:06 +00:00
|
|
|
// Special version for Win because stat() is slow, can be avoided
|
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;
|
|
|
|
WIN32_FIND_DATA findData;
|
2007-07-08 04:03:11 +00:00
|
|
|
HANDLE hFind = ::FindFirstFile(path2, &findData);
|
2007-07-08 03:30:06 +00:00
|
|
|
if (INVALID_HANDLE_VALUE != hFind) {
|
|
|
|
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];
|
2007-07-08 04:03:11 +00:00
|
|
|
::sprintf(buf, "%s/%s", dirpath, findData.cFileName);
|
2007-07-08 03:30:06 +00:00
|
|
|
dir_size(buf, dsize, recurse);
|
|
|
|
size += dsize;
|
|
|
|
} else {
|
2007-07-17 20:34:37 +00:00
|
|
|
size += findData.nFileSizeLow + ((__int64)(findData.nFileSizeHigh) << 32);
|
2007-07-08 03:30:06 +00:00
|
|
|
}
|
|
|
|
} while (FindNextFile(hFind, &findData));
|
|
|
|
::FindClose(hFind);
|
|
|
|
} else {
|
|
|
|
return ERR_OPENDIR;
|
|
|
|
}
|
|
|
|
#else
|
2002-08-05 00:29:34 +00:00
|
|
|
char filename[256], subdir[256];
|
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;
|
|
|
|
}
|
2003-10-22 18:39:22 +00:00
|
|
|
} else {
|
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) {
|
|
|
|
FILE* f;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2003-12-24 00:50:51 +00:00
|
|
|
f = fopen(path, mode);
|
|
|
|
#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-01-19 19:45:29 +00:00
|
|
|
} while (dtime() < start + 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;
|
|
|
|
f = fopen(path, mode);
|
|
|
|
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) {
|
2004-03-01 19:11:12 +00:00
|
|
|
struct stat buf;
|
|
|
|
|
2004-03-29 22:57:43 +00:00
|
|
|
if (stat(path, &buf)) {
|
2004-04-20 02:47:51 +00:00
|
|
|
return false; // stat() returns zero on success
|
2004-03-01 19:11:12 +00:00
|
|
|
}
|
2004-03-29 22:57:43 +00:00
|
|
|
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) {
|
|
|
|
FILE *fp;
|
2007-04-09 02:17:26 +00:00
|
|
|
|
2005-06-27 16:31:49 +00:00
|
|
|
if (boinc_file_exists(path)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2007-04-09 02:17:26 +00:00
|
|
|
fp = fopen(path, "w");
|
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
|
2007-04-09 02:17:26 +00:00
|
|
|
if (!CopyFile(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__)
|
|
|
|
char cmd[256];
|
|
|
|
sprintf(cmd, "copy %s %s", orig, newf);
|
|
|
|
return system(cmd);
|
2003-07-01 18:27:08 +00:00
|
|
|
#else
|
2005-04-09 02:21:11 +00:00
|
|
|
char cmd[256];
|
|
|
|
sprintf(cmd, "cp %s %s", orig, newf);
|
|
|
|
return system(cmd);
|
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
|
2004-06-09 20:04:09 +00:00
|
|
|
boinc_delete_file(newf);
|
2007-05-23 03:55:53 +00:00
|
|
|
if (MoveFile(old, newf)) return 0;
|
|
|
|
return GetLastError();
|
2004-06-09 20:04:09 +00:00
|
|
|
#else
|
2002-08-22 21:44:00 +00:00
|
|
|
return rename(old, newf);
|
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;
|
|
|
|
} while (dtime() < start + RETRY_INTERVAL);
|
|
|
|
}
|
|
|
|
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
|
2006-10-20 01:46:46 +00:00
|
|
|
if (!CreateDirectory(path, NULL)) {
|
|
|
|
return GetLastError();
|
|
|
|
}
|
|
|
|
return 0;
|
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);
|
|
|
|
return retval;
|
2003-05-13 18:55:07 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-11-08 00:56:55 +00:00
|
|
|
int boinc_rmdir(const char* name) {
|
2003-05-13 18:55:07 +00:00
|
|
|
#ifdef _WIN32
|
2006-10-20 01:46:46 +00:00
|
|
|
if (!RemoveDirectory(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);
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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__)
|
2005-03-05 23:12:31 +00:00
|
|
|
handle = CreateFile(
|
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) {
|
2006-07-06 17:45:58 +00:00
|
|
|
return -1;
|
2005-01-20 20:16:48 +00:00
|
|
|
}
|
2006-07-06 17:45:58 +00:00
|
|
|
return 0;
|
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) {
|
|
|
|
fd = open(
|
|
|
|
filename, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
|
|
|
|
);
|
|
|
|
}
|
2006-07-06 17:45:58 +00:00
|
|
|
if (fd<0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2005-01-20 20:16:48 +00:00
|
|
|
|
2006-07-06 17:45:58 +00:00
|
|
|
struct flock fl;
|
|
|
|
fl.l_type=F_WRLCK;
|
|
|
|
fl.l_whence=SEEK_SET;
|
|
|
|
fl.l_start=0;
|
|
|
|
fl.l_len=0;
|
|
|
|
if (-1 != fcntl(fd, F_SETLK, &fl)) return 0;
|
|
|
|
return -1;
|
|
|
|
#endif
|
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__)
|
2005-03-05 23:12:31 +00:00
|
|
|
if (!CloseHandle(handle)) {
|
|
|
|
perror("FILE_LOCK::unlock(): close failed.");
|
2005-01-20 20:16:48 +00:00
|
|
|
}
|
2005-03-05 23:12:31 +00:00
|
|
|
#else
|
|
|
|
if (close(fd)) {
|
|
|
|
perror("FILE_LOCK::unlock(): close failed.");
|
2005-01-20 20:16:48 +00:00
|
|
|
}
|
2005-03-05 23:12:31 +00:00
|
|
|
#endif
|
2006-12-14 20:44:39 +00:00
|
|
|
boinc_delete_file(filename);
|
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) {
|
2007-03-20 00:07:16 +00:00
|
|
|
char* p __attribute__ ((unused)) = getcwd(path, 256);
|
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) {
|
2007-03-20 00:07:16 +00:00
|
|
|
char* p __attribute__ ((unused)) = getcwd(path, 256);
|
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)
|
|
|
|
//
|
2007-09-12 08:25:22 +00:00
|
|
|
int get_filesystem_info(double &total_space, double &free_space, char* path) {
|
2003-06-14 20:24:29 +00:00
|
|
|
#ifdef _WIN32
|
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(
|
|
|
|
GetModuleHandle("kernel32.dll"), "GetDiskFreeSpaceExA"
|
|
|
|
);
|
2005-04-09 02:21:11 +00:00
|
|
|
if (pGetDiskFreeSpaceEx) {
|
|
|
|
ULARGE_INTEGER TotalNumberOfFreeBytes;
|
|
|
|
ULARGE_INTEGER TotalNumberOfBytes;
|
|
|
|
ULARGE_INTEGER TotalNumberOfBytesFreeToCaller;
|
2007-09-12 08:25:22 +00:00
|
|
|
pGetDiskFreeSpaceEx(
|
|
|
|
buf, &TotalNumberOfBytesFreeToCaller, &TotalNumberOfBytes,
|
|
|
|
&TotalNumberOfFreeBytes
|
|
|
|
);
|
2005-04-09 02:21:11 +00:00
|
|
|
signed __int64 uMB;
|
|
|
|
uMB = TotalNumberOfFreeBytes.QuadPart / (1024 * 1024);
|
|
|
|
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;
|
2007-09-12 08:25:22 +00:00
|
|
|
GetDiskFreeSpace(
|
|
|
|
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
|
|
|
|
#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);
|
2003-11-10 00:03:37 +00:00
|
|
|
#ifdef 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
|
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_636c8d709b = "$Id$";
|