2002-04-30 22:22:54 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// 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
|
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2002-06-06 18:42:01 +00:00
|
|
|
#include "windows_cpp.h"
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#ifdef macintosh
|
|
|
|
#include "seti_mac.h"
|
|
|
|
#else
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MAC_OS_X
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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
|
2002-07-03 22:37:24 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#include <winsock.h>
|
|
|
|
#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"
|
|
|
|
|
2002-07-03 22:37:24 +00:00
|
|
|
#ifdef HAVE_DIRENT_H
|
2002-04-30 22:22:54 +00:00
|
|
|
DIR* dirp;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
static char path[256];
|
|
|
|
static HANDLE handle;
|
|
|
|
static int first;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char failed_file[256];
|
|
|
|
|
|
|
|
// routines for enumerating the entries in a directory
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Open a directory
|
2002-04-30 22:22:54 +00:00
|
|
|
int dir_open(char* p) {
|
2002-07-11 01:09:53 +00:00
|
|
|
if(p==NULL) {
|
|
|
|
fprintf(stderr, "error: dir_open: unexpected NULL pointer p\n");
|
|
|
|
return ERR_NULL;
|
|
|
|
}
|
2002-07-03 22:37:24 +00:00
|
|
|
#ifdef HAVE_DIRENT_H
|
2002-04-30 22:22:54 +00:00
|
|
|
dirp = opendir(p);
|
|
|
|
if (!dirp) return ERR_OPENDIR;
|
|
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
|
|
|
strcpy(path, p);
|
|
|
|
strcat(path, "\\*");
|
|
|
|
first = 1;
|
|
|
|
handle = INVALID_HANDLE_VALUE;
|
2002-07-15 23:21:20 +00:00
|
|
|
// This is probably incomplete
|
2002-04-30 22:22:54 +00:00
|
|
|
#endif
|
|
|
|
#ifdef macintosh
|
|
|
|
SayErr("\pdir_open called (empty function)"); /* CAF Temp */
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Scan through a directory and return the next file name in it
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int dir_scan(char* p) {
|
2002-07-11 01:09:53 +00:00
|
|
|
if(p==NULL) {
|
|
|
|
fprintf(stderr, "error: dir_scan: unexpected NULL pointer p\n");
|
|
|
|
return ERR_NULL;
|
|
|
|
}
|
2002-07-03 22:37:24 +00:00
|
|
|
#ifdef HAVE_DIRENT_H
|
2002-04-30 22:22:54 +00:00
|
|
|
while (1) {
|
|
|
|
dirent* dp = readdir(dirp);
|
|
|
|
if (dp) {
|
|
|
|
if (dp->d_name[0] == '.') continue;
|
|
|
|
if (p) strcpy(p, dp->d_name);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
closedir(dirp);
|
|
|
|
dirp = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
|
|
|
WIN32_FIND_DATA data;
|
|
|
|
while (1) {
|
|
|
|
if (first) {
|
|
|
|
first = 0;
|
|
|
|
handle = FindFirstFile(path, &data);
|
|
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if (data.cFileName[0] == '.') continue;
|
|
|
|
if (p) strcpy(p, data.cFileName);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (FindNextFile(handle, &data)) {
|
|
|
|
if (data.cFileName[0] == '.') continue;
|
|
|
|
if (p) strcpy(p, data.cFileName);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
FindClose(handle);
|
|
|
|
handle = INVALID_HANDLE_VALUE;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef macintosh
|
|
|
|
SayErr("\pdir_scan called (empty function)"); /* CAF Temp */
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Close a directory
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
void dir_close() {
|
2002-07-03 22:37:24 +00:00
|
|
|
#ifdef HAVE_DIRENT_H
|
2002-04-30 22:22:54 +00:00
|
|
|
if (dirp) {
|
|
|
|
closedir(dirp);
|
|
|
|
dirp = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
|
|
|
FindClose(handle);
|
|
|
|
handle = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef macintosh
|
|
|
|
SayErr("\pdir_close called (empty function)"); /* CAF Temp */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Delete the file located at path
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int file_delete(char* path) {
|
|
|
|
int retval,i;
|
2002-07-11 01:09:53 +00:00
|
|
|
if(path==NULL) {
|
|
|
|
fprintf(stderr, "error: file_delete: unexpected NULL pointer path\n");
|
|
|
|
return ERR_NULL;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
for (i=0; i<2; i++) {
|
2002-07-03 22:37:24 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2002-04-30 22:22:54 +00:00
|
|
|
retval = unlink(path);
|
|
|
|
#endif
|
|
|
|
#if ( defined(_WIN32) || defined(macintosh) )
|
|
|
|
retval = remove(path);
|
|
|
|
#endif
|
|
|
|
if (!retval) break;
|
2002-06-06 18:42:01 +00:00
|
|
|
if (i==0) boinc_sleep(3);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
if (retval) {
|
|
|
|
strcpy(failed_file, path);
|
|
|
|
return ERR_UNLINK;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get file size
|
|
|
|
int file_size(char* path, int& size) {
|
|
|
|
struct stat sbuf;
|
|
|
|
int retval;
|
2002-07-11 01:09:53 +00:00
|
|
|
if(path==NULL) {
|
|
|
|
fprintf(stderr, "error: file_size: unexpected NULL pointer path\n");
|
|
|
|
return ERR_NULL;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
retval = stat(path, &sbuf);
|
|
|
|
if (retval) return retval;
|
|
|
|
size = sbuf.st_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
/* boinc_link creates a file (new_link) which contains an XML
|
|
|
|
reference (soft link) to existing. */
|
2002-06-20 23:46:51 +00:00
|
|
|
|
|
|
|
int boinc_link( char *existing, char *new_link ) {
|
|
|
|
FILE *fp;
|
2002-07-11 01:09:53 +00:00
|
|
|
if(existing==NULL) {
|
|
|
|
fprintf(stderr, "error: boinc_link: unexpected NULL pointer existing\n");
|
|
|
|
return ERR_NULL;
|
|
|
|
}
|
|
|
|
if(new_link==NULL) {
|
|
|
|
fprintf(stderr, "error: boinc_link: unexpected NULL pointer new_link\n");
|
|
|
|
return ERR_NULL;
|
|
|
|
}
|
|
|
|
fp = fopen( new_link, "wb" );
|
|
|
|
if (!fp) return ERR_FOPEN;
|
|
|
|
rewind( fp );
|
|
|
|
fprintf( fp, "<soft_link>%s</soft_link>\n", existing );
|
|
|
|
fclose( fp );
|
2002-06-20 23:46:51 +00:00
|
|
|
|
2002-07-11 01:09:53 +00:00
|
|
|
return 0;
|
2002-06-20 23:46:51 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Goes through directory specified by dirpath and removes all files from it
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int clean_out_dir(char* dirpath) {
|
|
|
|
char filename[256], path[256];
|
|
|
|
int retval;
|
2002-07-11 01:09:53 +00:00
|
|
|
if(dirpath==NULL) {
|
|
|
|
fprintf(stderr, "error: clean_out_dir: unexpected NULL pointer dirpath\n");
|
|
|
|
return ERR_NULL;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
retval = dir_open(dirpath);
|
|
|
|
if (retval) return retval;
|
|
|
|
while (1) {
|
|
|
|
retval = dir_scan(filename);
|
|
|
|
if (retval) break;
|
|
|
|
sprintf(path, "%s/%s", dirpath, filename);
|
|
|
|
retval = file_delete(path);
|
|
|
|
if (retval) {
|
|
|
|
dir_close();
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dir_close();
|
|
|
|
return 0;
|
|
|
|
}
|