2003-07-01 20:37:09 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-04-30 22:22:54 +00:00
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
2003-07-01 20:37:09 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-11-22 23:45:13 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
2003-11-22 23:45:13 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
2003-07-01 20:37:09 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-11-22 23:45:13 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
2004-02-29 14:00:22 +00:00
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifdef _WIN32
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
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-07-03 22:37:24 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <sys/time.h>
|
2002-07-03 22:37:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <unistd.h>
|
2002-07-03 22:37:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_DIRENT_H
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <dirent.h>
|
2002-07-03 22:37:24 +00:00
|
|
|
#endif
|
|
|
|
#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"
|
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
|
|
|
|
|
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
|
|
|
|
|
2002-12-11 23:09:37 +00:00
|
|
|
int is_file(char* path) {
|
|
|
|
struct stat sbuf;
|
2003-02-27 09:21:22 +00:00
|
|
|
memset(&sbuf, 0, sizeof(struct stat));
|
2004-04-15 20:42:15 +00:00
|
|
|
int retval = stat(path, &sbuf);
|
|
|
|
return (retval==0) && (sbuf.st_mode & S_IFREG);
|
2002-12-11 23:09:37 +00:00
|
|
|
}
|
|
|
|
|
2003-11-09 23:45:31 +00:00
|
|
|
int is_dir(const char* path) {
|
2002-12-11 23:09:37 +00:00
|
|
|
struct stat sbuf;
|
2003-02-27 09:21:22 +00:00
|
|
|
memset(&sbuf, 0, sizeof(struct stat));
|
2004-04-15 20:42:15 +00:00
|
|
|
int retval = stat(path, &sbuf);
|
|
|
|
return (retval==0) && (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;
|
2002-07-31 18:31:22 +00:00
|
|
|
|
2002-07-03 22:37:24 +00:00
|
|
|
#ifdef HAVE_DIRENT_H
|
2002-07-30 23:44:22 +00:00
|
|
|
dirp = opendir(p);
|
2002-07-31 18:31:22 +00:00
|
|
|
if (!dirp) return NULL;
|
2002-04-30 22:22:54 +00:00
|
|
|
#endif
|
|
|
|
#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;
|
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) {
|
2002-07-03 22:37:24 +00:00
|
|
|
#ifdef HAVE_DIRENT_H
|
2002-04-30 22:22:54 +00:00
|
|
|
while (1) {
|
2003-02-27 09:21:22 +00:00
|
|
|
dirent* dp = readdir(dirp);
|
|
|
|
if (dp) {
|
|
|
|
if (dp->d_name[0] == '.') continue;
|
2003-03-11 00:49:07 +00:00
|
|
|
if (p) safe_strncpy(p, dp->d_name, p_len);
|
2003-02-27 09:21:22 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_READDIR;
|
2003-02-27 09:21:22 +00:00
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
|
|
|
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 {
|
|
|
|
if (data.cFileName[0] == '.') continue;
|
2003-03-11 00:49:07 +00:00
|
|
|
if (p) safe_strncpy(p, data.cFileName, p_len);
|
2003-02-27 09:21:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (FindNextFile(dirp->handle, &data)) {
|
|
|
|
if (data.cFileName[0] == '.') continue;
|
2003-03-11 00:49:07 +00:00
|
|
|
if (p) safe_strncpy(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
|
|
|
}
|
|
|
|
#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) {
|
2002-07-03 22:37:24 +00:00
|
|
|
#ifdef HAVE_DIRENT_H
|
2002-07-30 23:44:22 +00:00
|
|
|
if (dirp) {
|
2003-02-27 09:21:22 +00:00
|
|
|
closedir(dirp);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#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);
|
2002-04-30 22:22:54 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-06-24 00:17:36 +00:00
|
|
|
DirScanner::DirScanner(string const& path) {
|
|
|
|
#ifdef HAVE_DIRENT_H
|
|
|
|
dirp = opendir(path.c_str());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
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 + "\\*";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan through a directory and return the next file name in it
|
|
|
|
//
|
|
|
|
bool DirScanner::scan(string& s) {
|
|
|
|
#ifdef HAVE_DIRENT_H
|
2004-04-26 21:36:12 +00:00
|
|
|
if (!dirp) return false;
|
2004-07-13 13:54:09 +00:00
|
|
|
|
2003-06-24 00:17:36 +00:00
|
|
|
while (1) {
|
|
|
|
dirent* dp = readdir(dirp);
|
|
|
|
if (dp) {
|
|
|
|
if (dp->d_name[0] == '.') continue;
|
|
|
|
s = dp->d_name;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close a directory
|
|
|
|
DirScanner::~DirScanner() {
|
|
|
|
#ifdef HAVE_DIRENT_H
|
|
|
|
if (dirp) {
|
|
|
|
closedir(dirp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (handle != INVALID_HANDLE_VALUE) {
|
|
|
|
FindClose(handle);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
2002-12-06 07:33:45 +00:00
|
|
|
int retval;
|
2002-08-05 00:29:34 +00:00
|
|
|
|
2004-03-29 22:57:43 +00:00
|
|
|
if (!boinc_file_exists(path)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-24 19:00:26 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
for (int i=0; i<5; i++) {
|
|
|
|
retval = remove(path);
|
|
|
|
if (!retval) break;
|
2004-06-09 20:04:09 +00:00
|
|
|
boinc_sleep(drand()); // avoid lockstep
|
2004-05-24 19:00:26 +00:00
|
|
|
}
|
|
|
|
#else
|
2002-12-06 07:33:45 +00:00
|
|
|
retval = unlink(path);
|
2002-04-30 22:22:54 +00:00
|
|
|
#endif
|
|
|
|
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);
|
|
|
|
if (retval) return retval;
|
2002-12-06 07:33:45 +00:00
|
|
|
size = (double)sbuf.st_size;
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-06-14 20:24:29 +00:00
|
|
|
// removes all files from specified directory
|
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];
|
|
|
|
int retval;
|
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);
|
2004-06-01 22:19:24 +00:00
|
|
|
if (!dirp) return 0; // if dir doesn't exist, it's empty
|
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;
|
2002-12-11 23:09:37 +00:00
|
|
|
sprintf(path, "%s%s%s", dirpath, PATH_SEPARATOR, filename);
|
2003-02-27 09:21:22 +00:00
|
|
|
clean_out_dir(path);
|
2003-06-06 17:43:45 +00:00
|
|
|
boinc_rmdir(path);
|
2004-01-04 06:48:40 +00:00
|
|
|
retval = boinc_delete_file(path);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (retval) {
|
2002-07-31 18:31:22 +00:00
|
|
|
dir_close(dirp);
|
2002-04-30 22:22:54 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
2002-07-31 18:31:22 +00:00
|
|
|
dir_close(dirp);
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
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
|
2002-07-30 00:02:21 +00:00
|
|
|
//
|
2003-11-08 00:56:55 +00:00
|
|
|
int dir_size(const char* dirpath, double& size) {
|
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;
|
2002-12-11 23:09:37 +00:00
|
|
|
sprintf(subdir, "%s%s%s", dirpath, PATH_SEPARATOR, filename);
|
2002-12-06 07:33:45 +00:00
|
|
|
|
|
|
|
// We don't know if this entry is a file or a directory.
|
|
|
|
// dir_size() will return -1 if it's a file
|
|
|
|
//
|
|
|
|
retval = dir_size(subdir, x);
|
|
|
|
if (retval == 0) {
|
|
|
|
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);
|
2002-12-11 23:09:37 +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) {
|
2004-04-23 00:05:16 +00:00
|
|
|
for (int i=0; i<5; i++) {
|
2004-06-09 20:04:09 +00:00
|
|
|
boinc_sleep(drand());
|
2004-03-01 19:11:12 +00:00
|
|
|
f = fopen(path, mode);
|
2004-04-23 00:05:16 +00:00
|
|
|
if (f) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// Unix - if call was interrupted, retry a few times
|
|
|
|
//
|
|
|
|
if (!f) {
|
|
|
|
for (int i=0; i<5; i++) {
|
|
|
|
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
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2004-03-01 19:11:12 +00:00
|
|
|
|
2004-04-20 02:47:51 +00:00
|
|
|
bool 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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
2004-08-12 10:13:01 +00:00
|
|
|
if (CopyFile(orig, newf, FALSE)) {
|
2003-07-01 21:05:37 +00:00
|
|
|
return 0;
|
2004-02-29 14:00:22 +00:00
|
|
|
} else {
|
2003-07-01 21:05:37 +00:00
|
|
|
return GetLastError();
|
2004-02-29 14:00:22 +00:00
|
|
|
}
|
2003-07-01 18:27:08 +00:00
|
|
|
#else
|
|
|
|
char cmd[256];
|
|
|
|
sprintf(cmd, "cp %s %s", orig, newf);
|
|
|
|
return system(cmd);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-11-08 00:56:55 +00:00
|
|
|
int boinc_rename(const char* old, const char* newf) {
|
2002-08-22 21:44:00 +00:00
|
|
|
#ifdef _WIN32
|
2004-06-10 07:49:50 +00:00
|
|
|
int retval=0;
|
2004-06-09 20:04:09 +00:00
|
|
|
boinc_delete_file(newf);
|
|
|
|
for (int i=0; i<5; i++) {
|
|
|
|
retval = rename(old, newf);
|
|
|
|
if (!retval) break;
|
|
|
|
boinc_sleep(drand()); // avoid lockstep
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
#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
|
|
|
|
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
|
2004-04-20 23:04:05 +00:00
|
|
|
return !CreateDirectory(path, NULL);
|
2003-05-13 18:55:07 +00:00
|
|
|
#else
|
2004-04-15 20:42:15 +00:00
|
|
|
return mkdir(path, 0777);
|
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
|
2004-04-20 23:04:05 +00:00
|
|
|
return !RemoveDirectory(name);
|
2003-05-13 18:55:07 +00:00
|
|
|
#else
|
|
|
|
return rmdir(name);
|
|
|
|
#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;
|
|
|
|
sprintf(newpath, "%s%s%s", oldpath, PATH_SEPARATOR, q);
|
|
|
|
retval = boinc_mkdir(newpath);
|
|
|
|
if (retval) return retval;
|
|
|
|
strcpy(oldpath, newpath);
|
|
|
|
q = p+1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-07 06:51:42 +00:00
|
|
|
int lock_file(char* filename) {
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
// some systems have both!
|
2004-09-27 23:30:09 +00:00
|
|
|
#if defined(HAVE_LOCKF) && !defined(__APPLE__)
|
2004-04-07 06:51:42 +00:00
|
|
|
int lock = open(filename, O_WRONLY|O_CREAT, 0644);
|
|
|
|
retval = lockf(lock, F_TLOCK, 1);
|
2004-09-24 20:52:18 +00:00
|
|
|
#elif HAVE_FLOCK
|
|
|
|
int lock = open(filename, O_WRONLY|O_CREAT, 0644);
|
|
|
|
retval = flock(lock, LOCK_EX|LOCK_NB);
|
2004-04-07 06:51:42 +00:00
|
|
|
// must leave fd open
|
2004-11-11 19:24:02 +00:00
|
|
|
#else
|
|
|
|
no file lock mechanism
|
2004-04-07 06:51:42 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
HANDLE hfile = CreateFile(
|
|
|
|
filename, GENERIC_WRITE,
|
|
|
|
0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
|
|
|
|
);
|
|
|
|
if (hfile == INVALID_HANDLE_VALUE) retval = 1;
|
|
|
|
else retval = 0;
|
|
|
|
#endif
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2004-04-23 00:05:16 +00:00
|
|
|
void relative_to_absolute(char* relname, char* path) {
|
2002-08-24 00:41:25 +00:00
|
|
|
#ifdef _WIN32
|
2002-08-25 07:54:33 +00:00
|
|
|
_getcwd(path, 256);
|
2004-04-23 00:05:16 +00:00
|
|
|
#else
|
|
|
|
getcwd(path, 256);
|
|
|
|
#endif
|
2004-07-06 04:01:15 +00:00
|
|
|
if (strlen(relname)) {
|
|
|
|
strcat(path, PATH_SEPARATOR);
|
|
|
|
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)
|
|
|
|
//
|
|
|
|
int get_filesystem_info(double &total_space, double &free_space) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
FreeFn pGetDiskFreeSpaceEx;
|
2003-11-22 23:45:13 +00:00
|
|
|
pGetDiskFreeSpaceEx = (FreeFn)GetProcAddress(GetModuleHandle("kernel32.dll"),
|
|
|
|
"GetDiskFreeSpaceExA");
|
2004-08-12 10:13:01 +00:00
|
|
|
if (pGetDiskFreeSpaceEx) {
|
2003-06-14 20:24:29 +00:00
|
|
|
ULARGE_INTEGER TotalNumberOfFreeBytes;
|
|
|
|
ULARGE_INTEGER TotalNumberOfBytes;
|
2003-11-22 23:45:13 +00:00
|
|
|
ULARGE_INTEGER TotalNumberOfBytesFreeToCaller;
|
|
|
|
pGetDiskFreeSpaceEx(NULL, &TotalNumberOfBytesFreeToCaller, &TotalNumberOfBytes, &TotalNumberOfFreeBytes);
|
2003-06-19 18:19:35 +00:00
|
|
|
signed __int64 uMB;
|
2003-06-14 20:24:29 +00:00
|
|
|
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;
|
|
|
|
GetDiskFreeSpace(NULL, &dwSectPerClust, &dwBytesPerSect, &dwFreeClusters, &dwTotalClusters);
|
|
|
|
free_space = (double)dwFreeClusters * dwSectPerClust * dwBytesPerSect;
|
|
|
|
total_space = (double)dwTotalClusters * dwSectPerClust * dwBytesPerSect;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#ifdef STATFS
|
|
|
|
struct STATFS fs_info;
|
2003-06-24 00:17:36 +00:00
|
|
|
|
2003-06-14 20:24:29 +00:00
|
|
|
STATFS(".", &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;
|
|
|
|
}
|