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
|
|
|
|
#include "stdafx.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2004-04-11 20:07:53 +00:00
|
|
|
#include <sys/file.h>
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
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
|
2003-08-13 21:35:26 +00:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#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"
|
|
|
|
|
|
|
|
char failed_file[256];
|
|
|
|
|
|
|
|
// 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));
|
2002-12-11 23:09:37 +00:00
|
|
|
stat(path, &sbuf);
|
|
|
|
return sbuf.st_mode & S_IFREG;
|
|
|
|
}
|
|
|
|
|
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));
|
2002-12-11 23:09:37 +00:00
|
|
|
stat(path, &sbuf);
|
|
|
|
return sbuf.st_mode & S_IFDIR;
|
|
|
|
}
|
|
|
|
|
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
|
2003-02-27 09:21:22 +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;
|
|
|
|
if(!is_dir((char*)path.c_str())) {
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|
2002-07-03 22:37:24 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2002-12-06 07:33:45 +00:00
|
|
|
retval = unlink(path);
|
2002-04-30 22:22:54 +00:00
|
|
|
#endif
|
|
|
|
#if ( defined(_WIN32) || defined(macintosh) )
|
2002-12-06 07:33:45 +00:00
|
|
|
retval = remove(path);
|
2002-04-30 22:22:54 +00:00
|
|
|
#endif
|
|
|
|
if (retval) {
|
2003-04-03 18:35:40 +00:00
|
|
|
safe_strcpy(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);
|
2003-10-21 04:06:55 +00:00
|
|
|
if (!dirp) return ERR_OPENDIR;
|
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
|
|
|
|
2004-03-01 19:11:12 +00:00
|
|
|
// on Windows: if fopen fails, try again for 5 seconds
|
2003-12-24 00:50:51 +00:00
|
|
|
// (since the file might be open by FastFind, Diskeeper etc.)
|
|
|
|
//
|
|
|
|
FILE* boinc_fopen(const char* path, const char* mode) {
|
|
|
|
FILE* f;
|
|
|
|
|
|
|
|
f = fopen(path, mode);
|
|
|
|
#ifdef _WIN32
|
2004-03-01 19:11:12 +00:00
|
|
|
if (!f) {
|
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
boinc_sleep(1.0);
|
|
|
|
f = fopen(path, mode);
|
|
|
|
if (f) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-12-24 00:50:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2004-03-01 19:11:12 +00:00
|
|
|
|
|
|
|
int boinc_file_exists(const char* path) {
|
|
|
|
struct stat buf;
|
|
|
|
|
2004-03-29 22:57:43 +00:00
|
|
|
if (stat(path, &buf)) {
|
|
|
|
return false;
|
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-02-29 14:00:22 +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
|
|
|
|
unlink(newf);
|
|
|
|
#endif
|
|
|
|
return rename(old, newf);
|
|
|
|
}
|
2002-08-24 00:41:25 +00:00
|
|
|
|
2003-11-08 00:56:55 +00:00
|
|
|
int boinc_mkdir(const char* name) {
|
2003-05-13 18:55:07 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
return CreateDirectory(name, NULL);
|
|
|
|
#else
|
|
|
|
return mkdir(name, 0777);
|
|
|
|
#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
|
2003-05-13 23:04:49 +00:00
|
|
|
return RemoveDirectory(name);
|
2003-05-13 18:55:07 +00:00
|
|
|
#else
|
|
|
|
return rmdir(name);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-04-07 06:51:42 +00:00
|
|
|
int lock_file(char* filename) {
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
// some systems have both!
|
|
|
|
#ifdef HAVE_FLOCK
|
|
|
|
int lock = open(filename, O_WRONLY|O_CREAT, 0644);
|
|
|
|
retval = flock(lock, LOCK_EX|LOCK_NB);
|
|
|
|
#elif HAVE_LOCKF
|
|
|
|
int lock = open(filename, O_WRONLY|O_CREAT, 0644);
|
|
|
|
retval = lockf(lock, F_TLOCK, 1);
|
|
|
|
// must leave fd open
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2002-08-24 00:41:25 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
void full_path(char* relname, char* path) {
|
2002-08-25 07:54:33 +00:00
|
|
|
_getcwd(path, 256);
|
2002-08-24 00:41:25 +00:00
|
|
|
strcat(path, PATH_SEPARATOR);
|
|
|
|
strcat(path, relname);
|
|
|
|
}
|
|
|
|
#endif
|
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");
|
2003-06-14 20:24:29 +00:00
|
|
|
if(pGetDiskFreeSpaceEx) {
|
|
|
|
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;
|
|
|
|
}
|