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-07-13 13:54:09 +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.
|
2004-07-13 13:54:09 +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
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-10-16 19:03:49 +00:00
|
|
|
#include "cpp.h"
|
2002-06-06 18:42:01 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2002-06-06 18:42:01 +00:00
|
|
|
#endif
|
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifndef _WIN32
|
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 <sys/stat.h>
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cctype>
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2002-08-24 00:41:25 +00:00
|
|
|
#include "filesys.h"
|
2002-07-11 01:09:53 +00:00
|
|
|
#include "error_numbers.h"
|
2004-04-08 08:15:23 +00:00
|
|
|
#include "client_msgs.h"
|
2005-10-09 21:03:11 +00:00
|
|
|
#include "client_state.h"
|
2003-03-08 23:48:05 +00:00
|
|
|
#include "util.h"
|
2002-06-21 06:52:47 +00:00
|
|
|
|
2003-07-03 05:01:29 +00:00
|
|
|
#include "file_names.h"
|
|
|
|
|
2002-12-19 00:37:19 +00:00
|
|
|
void escape_project_url(char *in, char* out) {
|
2003-05-20 00:03:39 +00:00
|
|
|
escape_url_readable(in, out);
|
2003-06-24 00:17:36 +00:00
|
|
|
char& last = out[strlen(out)-1];
|
|
|
|
// remove trailing _
|
|
|
|
if (last == '_') {
|
|
|
|
last = '\0';
|
|
|
|
}
|
2002-09-25 18:33:26 +00:00
|
|
|
}
|
|
|
|
|
2004-01-04 06:48:40 +00:00
|
|
|
void get_project_dir(PROJECT* p, char* path) {
|
|
|
|
char buf[256];
|
|
|
|
escape_project_url(p->master_url, buf);
|
2006-03-03 21:34:03 +00:00
|
|
|
sprintf(path, "%s/%s", PROJECTS_DIR, buf);
|
2004-01-04 06:48:40 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Gets the pathname of a file
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
void get_pathname(FILE_INFO* fip, char* path) {
|
|
|
|
PROJECT* p = fip->project;
|
2002-06-21 06:52:47 +00:00
|
|
|
char buf[256];
|
2002-08-05 00:29:34 +00:00
|
|
|
|
2002-07-05 05:33:40 +00:00
|
|
|
// for testing purposes, it's handy to allow a FILE_INFO without
|
|
|
|
// an associated PROJECT.
|
|
|
|
//
|
|
|
|
if (p) {
|
2004-01-04 06:48:40 +00:00
|
|
|
get_project_dir(p, buf);
|
2006-03-03 21:34:03 +00:00
|
|
|
sprintf(path, "%s/%s", buf, fip->name);
|
2002-07-05 05:33:40 +00:00
|
|
|
} else {
|
2004-01-04 06:48:40 +00:00
|
|
|
strcpy(path, fip->name);
|
2002-07-05 05:33:40 +00:00
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2004-09-22 22:09:54 +00:00
|
|
|
void get_sched_request_filename(PROJECT& project, char* buf) {
|
|
|
|
char url[256];
|
|
|
|
|
2004-09-22 22:28:25 +00:00
|
|
|
escape_project_url(project.master_url, url);
|
|
|
|
sprintf(buf, "%s%s.xml", SCHED_OP_REQUEST_BASE, url);
|
2004-09-22 22:09:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void get_sched_reply_filename(PROJECT& project, char* buf) {
|
|
|
|
char url[256];
|
|
|
|
|
2004-09-22 22:28:25 +00:00
|
|
|
escape_project_url(project.master_url, url);
|
|
|
|
sprintf(buf, "%s%s.xml", SCHED_OP_REPLY_BASE, url);
|
2004-09-22 22:09:54 +00:00
|
|
|
}
|
|
|
|
|
2005-03-10 00:43:22 +00:00
|
|
|
void get_master_filename(PROJECT& project, char* buf) {
|
|
|
|
char url[256];
|
|
|
|
|
|
|
|
escape_project_url(project.master_url, url);
|
|
|
|
sprintf(buf, "%s%s.xml", MASTER_BASE, url);
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Returns the location of a numbered slot directory
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
void get_slot_dir(int slot, char* path) {
|
2006-03-03 21:34:03 +00:00
|
|
|
sprintf(path, "%s/%d", SLOTS_DIR, slot);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Create the directory for the project p
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int make_project_dir(PROJECT& p) {
|
2004-01-04 06:48:40 +00:00
|
|
|
char buf[256];
|
2004-04-20 23:04:05 +00:00
|
|
|
int retval;
|
2002-06-21 06:52:47 +00:00
|
|
|
|
2003-05-13 18:55:07 +00:00
|
|
|
boinc_mkdir(PROJECTS_DIR);
|
2006-06-12 04:05:10 +00:00
|
|
|
#ifdef SANDBOX
|
2006-06-13 09:08:00 +00:00
|
|
|
chmod(PROJECTS_DIR,
|
|
|
|
S_IRUSR|S_IWUSR|S_IXUSR
|
2006-06-16 13:40:30 +00:00
|
|
|
|S_IRGRP|S_IWGRP|S_IXGRP
|
2006-06-14 07:36:44 +00:00
|
|
|
|S_IROTH|S_IXOTH
|
2006-06-13 09:08:00 +00:00
|
|
|
);
|
2006-06-12 04:05:10 +00:00
|
|
|
#endif
|
2004-01-04 06:48:40 +00:00
|
|
|
get_project_dir(&p, buf);
|
2004-04-20 23:04:05 +00:00
|
|
|
retval = boinc_mkdir(buf);
|
2006-06-12 04:05:10 +00:00
|
|
|
#ifdef SANDBOX
|
2006-06-13 09:08:00 +00:00
|
|
|
boinc_chown(buf, gstate.boinc_project_gid);
|
2006-06-12 04:05:10 +00:00
|
|
|
#endif
|
2004-04-20 23:04:05 +00:00
|
|
|
return retval;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2002-12-02 20:52:22 +00:00
|
|
|
int remove_project_dir(PROJECT& p) {
|
2004-01-04 06:48:40 +00:00
|
|
|
char buf[256];
|
2004-05-24 19:00:26 +00:00
|
|
|
int retval;
|
2002-12-02 20:52:22 +00:00
|
|
|
|
2004-01-04 06:48:40 +00:00
|
|
|
get_project_dir(&p, buf);
|
2004-05-24 19:00:26 +00:00
|
|
|
retval = clean_out_dir(buf);
|
|
|
|
if (retval) {
|
2006-01-17 22:48:09 +00:00
|
|
|
msg_printf(&p, MSG_ERROR, "Can't delete file %s", boinc_failed_file);
|
2004-05-24 19:00:26 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2004-04-14 23:32:17 +00:00
|
|
|
return boinc_rmdir(buf);
|
2002-12-02 20:52:22 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Create the slot directory for the specified slot #
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int make_slot_dir(int slot) {
|
|
|
|
char buf[256];
|
2004-04-14 23:32:17 +00:00
|
|
|
if (slot<0) {
|
2006-01-17 22:48:09 +00:00
|
|
|
msg_printf(NULL, MSG_ERROR, "Bad slot number %d", slot);
|
2002-07-11 01:09:53 +00:00
|
|
|
return ERR_NEG;
|
|
|
|
}
|
2003-05-13 18:55:07 +00:00
|
|
|
boinc_mkdir(SLOTS_DIR);
|
2006-06-12 04:05:10 +00:00
|
|
|
#ifdef SANDBOX
|
2006-06-13 09:08:00 +00:00
|
|
|
chmod(SLOTS_DIR,
|
|
|
|
S_IRUSR|S_IWUSR|S_IXUSR
|
2006-06-21 03:10:24 +00:00
|
|
|
|S_IRGRP|S_IWGRP|S_IXGRP
|
2006-06-14 07:36:44 +00:00
|
|
|
|S_IROTH|S_IXOTH
|
2006-06-13 09:08:00 +00:00
|
|
|
);
|
2006-06-12 04:05:10 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
get_slot_dir(slot, buf);
|
2006-06-12 04:05:10 +00:00
|
|
|
int retval = boinc_mkdir(buf);
|
|
|
|
#ifdef SANDBOX
|
2006-06-13 09:08:00 +00:00
|
|
|
boinc_chown(buf, gstate.boinc_project_gid);
|
2006-06-12 04:05:10 +00:00
|
|
|
#endif
|
|
|
|
return retval;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2002-06-06 18:42:01 +00:00
|
|
|
|
2005-10-09 21:03:11 +00:00
|
|
|
// A slot dir can't be cleaned out
|
|
|
|
// (e.g. a virus-checker has a file locked).
|
|
|
|
// Rename it to DELETE_ME_x
|
|
|
|
//
|
|
|
|
int rename_slot_dir(int slot) {
|
|
|
|
char oldname[256], newname[256];
|
|
|
|
sprintf(oldname, "%s/%d", SLOTS_DIR, slot);
|
|
|
|
sprintf(newname, "%s/DELETE_ME_%d_%d", SLOTS_DIR, slot, (int)gstate.now);
|
|
|
|
int retval = rename(oldname, newname);
|
|
|
|
if (retval) return ERR_RENAME;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-10-10 03:21:52 +00:00
|
|
|
// delete directories created by the above
|
|
|
|
//
|
|
|
|
void delete_old_slot_dirs() {
|
|
|
|
char filename[256], path[256];
|
|
|
|
DIRREF dirp;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
dirp = dir_open(SLOTS_DIR);
|
2005-10-11 01:42:55 +00:00
|
|
|
if (!dirp) return;
|
2005-10-10 03:21:52 +00:00
|
|
|
while (1) {
|
|
|
|
strcpy(filename, "");
|
|
|
|
retval = dir_scan(filename, dirp, sizeof(filename));
|
|
|
|
if (retval) break;
|
|
|
|
if (strstr(filename, "DELETE_ME")) {
|
|
|
|
sprintf(path, "%s/%s", SLOTS_DIR, filename);
|
|
|
|
clean_out_dir(path);
|
|
|
|
boinc_rmdir(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-26 05:57:10 +00:00
|
|
|
void get_account_filename(char* master_url, char* path) {
|
|
|
|
char buf[256];
|
|
|
|
escape_project_url(master_url, buf);
|
|
|
|
sprintf(path, "account_%s.xml", buf);
|
|
|
|
}
|
|
|
|
|
2005-02-27 20:06:51 +00:00
|
|
|
static bool bad_account_filename(const char* filename) {
|
2005-06-26 23:13:48 +00:00
|
|
|
msg_printf(NULL, MSG_ERROR, "Invalid account filename: %s", filename);
|
2005-02-27 20:06:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// account filenames are of the form
|
|
|
|
// account_URL.xml
|
|
|
|
// where URL is master URL with slashes replaced by underscores
|
|
|
|
//
|
2005-02-16 23:17:43 +00:00
|
|
|
bool is_account_file(const char* filename) {
|
2005-02-27 20:06:51 +00:00
|
|
|
const char* p, *q;
|
|
|
|
p = strstr(filename, "account_");
|
|
|
|
if (p != filename) return false;
|
|
|
|
|
2005-04-12 18:08:51 +00:00
|
|
|
q = filename + strlen("account_");
|
2005-02-27 20:06:51 +00:00
|
|
|
p = strstr(q, ".xml");
|
|
|
|
if (!p) return bad_account_filename(filename);
|
|
|
|
if (p == q) return bad_account_filename(filename);
|
|
|
|
|
|
|
|
q = p + strlen(".xml");
|
|
|
|
if (strlen(q)) return bad_account_filename(filename);
|
|
|
|
return true;
|
2002-09-26 05:57:10 +00:00
|
|
|
}
|
2004-09-06 20:30:22 +00:00
|
|
|
|
2005-04-04 06:02:36 +00:00
|
|
|
// statistics filenames are of the form
|
|
|
|
// statistics_URL.xml
|
|
|
|
// where URL is master URL with slashes replaced by underscores
|
|
|
|
//
|
|
|
|
bool is_statistics_file(const char* filename) {
|
|
|
|
const char* p, *q;
|
|
|
|
p = strstr(filename, "statistics_");
|
|
|
|
if (p != filename) return false;
|
|
|
|
q = filename + strlen("statistics_");
|
|
|
|
|
|
|
|
p = strstr(q, ".");
|
|
|
|
if (!p) return bad_account_filename(filename);
|
|
|
|
if (p == q) return bad_account_filename(filename);
|
|
|
|
|
|
|
|
q = p+1;
|
|
|
|
p = strstr(q, ".xml");
|
|
|
|
if (!p) return bad_account_filename(filename);
|
|
|
|
if (p == q) return bad_account_filename(filename);
|
|
|
|
|
|
|
|
q = p + strlen(".xml");
|
|
|
|
if (strlen(q)) return bad_account_filename(filename);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void get_statistics_filename(char* master_url, char* path) {
|
|
|
|
char buf[256];
|
|
|
|
escape_project_url(master_url, buf);
|
|
|
|
sprintf(path, "statistics_%s.xml", buf);
|
|
|
|
}
|
|
|
|
|
2005-04-06 19:41:31 +00:00
|
|
|
bool is_image_file(const char* filename) {
|
|
|
|
std::string fn = filename;
|
|
|
|
downcase_string(fn);
|
2005-06-06 19:18:01 +00:00
|
|
|
if (ends_with(fn, std::string(".jpg"))) return true;
|
|
|
|
if (ends_with(fn, std::string(".jpeg"))) return true;
|
|
|
|
if (ends_with(fn, std::string(".png"))) return true;
|
2005-04-06 19:41:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-04-04 06:02:36 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_7d362a6a52 = "$Id$";
|