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"
|
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
|
|
|
|
|
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;
|
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;
|
2005-12-16 03:35:15 +00:00
|
|
|
#elif defined(_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) {
|
2005-05-12 22:39:44 +00:00
|
|
|
if (!strcmp(dp->d_name, ".")) continue;
|
|
|
|
if (!strcmp(dp->d_name, "..")) continue;
|
2006-01-05 06:42:45 +00:00
|
|
|
if (p) strlcpy(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
|
|
|
}
|
2005-12-16 03:35:15 +00:00
|
|
|
#elif defined(_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
|
|
|
}
|
|
|
|
#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
|
|
|
}
|
2005-12-16 03:35:15 +00:00
|
|
|
#elif defined(_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());
|
2005-12-16 03:35:15 +00:00
|
|
|
#elif defined(_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 + "\\*";
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
}
|
2005-12-16 03:35:15 +00:00
|
|
|
#elif defined(_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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close a directory
|
|
|
|
DirScanner::~DirScanner() {
|
|
|
|
#ifdef HAVE_DIRENT_H
|
|
|
|
if (dirp) {
|
|
|
|
closedir(dirp);
|
|
|
|
}
|
2005-12-16 03:35:15 +00:00
|
|
|
#elif defined(_WIN32)
|
2003-06-24 00:17:36 +00:00
|
|
|
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) {
|
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;
|
|
|
|
}
|
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);
|
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;
|
|
|
|
#ifdef _WIN32
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
if (boinc_file_exists(path)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fp=fopen(path, "w"))) {
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2005-04-09 02:21:11 +00:00
|
|
|
if (CopyFile(orig, newf, FALSE)) {
|
|
|
|
return 0;
|
2004-02-29 14:00:22 +00:00
|
|
|
} else {
|
2005-04-09 02:21:11 +00:00
|
|
|
return GetLastError();
|
2004-02-29 14:00:22 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-01-19 15:54:04 +00:00
|
|
|
|
2005-03-05 23:12:31 +00:00
|
|
|
int FILE_LOCK::lock(const char* filename) {
|
2005-01-20 20:16:48 +00:00
|
|
|
int retval=0;
|
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) {
|
|
|
|
retval = 1;
|
2005-01-20 20:16:48 +00:00
|
|
|
}
|
|
|
|
|
2005-01-19 15:54:04 +00:00
|
|
|
// some systems have both!
|
2004-11-11 19:33:51 +00:00
|
|
|
#elif defined(HAVE_LOCKF) && !defined(__APPLE__)
|
2005-03-05 23:12:31 +00:00
|
|
|
fd = open(filename, O_WRONLY|O_CREAT, 0644);
|
|
|
|
retval = lockf(fd, F_TLOCK, 0);
|
2005-01-19 15:54:04 +00:00
|
|
|
#elif HAVE_FLOCK
|
2005-03-05 23:12:31 +00:00
|
|
|
fd = open(filename, O_WRONLY|O_CREAT, 0644);
|
|
|
|
retval = flock(fd, LOCK_EX|LOCK_NB);
|
2005-01-19 15:54:04 +00:00
|
|
|
// must leave file-handle open
|
|
|
|
#else
|
|
|
|
no file lock mechanism;
|
|
|
|
#endif
|
2005-01-20 20:16:48 +00:00
|
|
|
|
2005-03-05 23:12:31 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
if (boinc_delete_file(filename) != 0) {
|
|
|
|
perror("FILE_LOCK::unlock: delete failed.");
|
|
|
|
}
|
|
|
|
return 0;
|
2004-04-07 06:51:42 +00:00
|
|
|
}
|
|
|
|
|
2005-02-16 23:17:43 +00:00
|
|
|
void relative_to_absolute(const char* relname, char* path) {
|
2005-12-16 03:35:15 +00:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN32__)
|
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
|
2005-04-09 02:21:11 +00:00
|
|
|
FreeFn pGetDiskFreeSpaceEx;
|
|
|
|
pGetDiskFreeSpaceEx = (FreeFn)GetProcAddress(GetModuleHandle("kernel32.dll"),
|
2003-11-22 23:45:13 +00:00
|
|
|
"GetDiskFreeSpaceExA");
|
2005-04-09 02:21:11 +00:00
|
|
|
if (pGetDiskFreeSpaceEx) {
|
|
|
|
ULARGE_INTEGER TotalNumberOfFreeBytes;
|
|
|
|
ULARGE_INTEGER TotalNumberOfBytes;
|
|
|
|
ULARGE_INTEGER TotalNumberOfBytesFreeToCaller;
|
|
|
|
pGetDiskFreeSpaceEx(NULL, &TotalNumberOfBytesFreeToCaller, &TotalNumberOfBytes, &TotalNumberOfFreeBytes);
|
|
|
|
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;
|
|
|
|
GetDiskFreeSpace(NULL, &dwSectPerClust, &dwBytesPerSect, &dwFreeClusters, &dwTotalClusters);
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|
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$";
|