1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC. If not, see <http://www.gnu.org/licenses/>.
18 // A sample assimilator that:
19 // 1) if success, copy the output file(s) to a directory
20 // 2) if failure, append a message to an error log
25 #include <sys/types.h>
31 #include "error_numbers.h"
33 #include "sched_msgs.h"
34 #include "validate_util.h"
35 #include "sched_config.h"
39 int write_error(char* p) {
42 f = fopen("../../results/errors", "a");
43 if (!f) return ERR_FOPEN;
50 int GetNumFilesInDir(char *dir)
54 if((partsdir = opendir(dir)) == NULL) {
58 while ((part = readdir(partsdir)) != NULL)
60 char *partname = part->d_name;
61 if(partname != "." && partname != "..")
69 int assimilate_handler(
70 WORKUNIT& wu, vector<RESULT>& /*results*/, RESULT& canonical_result
80 retval = frt.open("rainbowtables-distrrtgen", config.db_host, config.db_user, config.db_passwd);
82 log_messages.printf(MSG_CRITICAL, "can't open db rainbowtables-distrrtgen\n");
87 const char *pos = strchr(wu.name, ' ');
88 char partnum[256] = {0};
89 strncpy(partnum, wu.name, pos - wu.name);
90 int lookupid = atoi(partnum);
91 log_messages.printf(MSG_DEBUG, "Handling lookupid %i\n", lookupid);
94 if (wu.canonical_resultid) {
98 pwd = getpwnam("rainbow");
99 grp = getgrnam("rainbow");
101 sprintf(query, "UPDATE rainbowcrack_cracker_lookups SET hasindices = 2 WHERE lookupid = %i", lookupid);
102 // log_messages.printf(MSG_DEBUG, "%s\n", query);
104 if(retval) goto cleanup;
105 sprintf(query, "SELECT tableid FROM rainbowcrack_cracker_lookups WHERE lookupid = %i", lookupid);
106 // log_messages.printf(MSG_DEBUG, "%s\n", query);
107 retval = frt.do_query(query);
109 log_messages.printf(MSG_DEBUG, "Query returned %i\n", retval);
112 resp = mysql_store_result(frt.mysql);
113 if (!resp) {retval = ERR_DB_NOT_FOUND; goto cleanup; }
114 row = mysql_fetch_row(resp);
115 mysql_free_result(resp);
116 if (!row) { retval = ERR_DB_NOT_FOUND; goto cleanup; }
117 int tableid = atoi(row[0]);
118 sprintf(resdir, "../../indices/%s", row[0]);
119 // log_messages.printf(MSG_DEBUG, "Result dir: %s\n", resdir);
120 retval = boinc_mkdir(resdir);
121 vector<FILE_INFO> output_files;
123 get_output_file_infos(canonical_result, output_files);
124 unsigned int n = output_files.size();
125 // log_messages.printf(MSG_DEBUG, "Number of output files: %i\n", n);
126 for (i=0; i<n; i++) {
127 FILE_INFO& fi = output_files[i];
129 sprintf(copy_path, "../../indices/%i/%i.index", tableid, lookupid);
131 sprintf(copy_path, "../../indices/%i/%i_%d.index", tableid, lookupid, i);
133 // log_messages.printf(MSG_DEBUG, "Copying from %s to %s\n", fi.path.c_str(), copy_path);
134 retval = boinc_copy(fi.path.c_str() , copy_path);
135 int fildes = open(copy_path, O_RDWR);
136 fchown(fildes, pwd->pw_uid, grp->gr_gid);
137 if (retval && !fi.optional) {
138 log_messages.printf(MSG_CRITICAL, "FAILED copy operation of %s to %s\n", fi.path.c_str(), copy_path);
139 sprintf(buf, "couldn't copy file %s\n", fi.path.c_str());
145 sprintf(buf, "%s: 0x%x\n", wu.name, wu.error_mask);
146 return write_error(buf);