]> git.sesse.net Git - freerainbowtables/blob - BOINC software/BOINC server apps/index_calculator_assimilator/index_calculator_assimilator.cpp
initial
[freerainbowtables] / BOINC software / BOINC server apps / index_calculator_assimilator / index_calculator_assimilator.cpp
1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
4 //
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.
9 //
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.
14 //
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/>.
17
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
21
22 #include <vector>
23 #include <string>
24 #include <cstdlib>
25 #include <sys/types.h>
26 #include <pwd.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include "boinc_db.h"
31 #include "error_numbers.h"
32 #include "filesys.h"
33 #include "sched_msgs.h"
34 #include "validate_util.h"
35 #include "sched_config.h"
36 using std::vector;
37 using std::string;
38
39 int write_error(char* p) {
40     static FILE* f = 0;
41     if (!f) {
42         f = fopen("../../results/errors", "a");
43         if (!f) return ERR_FOPEN;
44     }
45     fprintf(f, "%s", p);
46     fflush(f);
47     return 0;
48 }
49
50 int GetNumFilesInDir(char *dir)
51 {
52         DIR *partsdir;
53         struct dirent *part;
54         if((partsdir  = opendir(dir)) == NULL) {
55                 return 0;
56         }
57         int numfiles = 0;
58         while ((part = readdir(partsdir)) != NULL) 
59         {
60                 char *partname = part->d_name;
61                 if(partname != "." && partname != "..")
62                 {
63                         numfiles++;
64                 }
65         }
66         return numfiles;
67
68 }
69 int assimilate_handler(
70     WORKUNIT& wu, vector<RESULT>& /*results*/, RESULT& canonical_result
71 ) {
72     DB_CONN frt;
73     int retval;
74     char buf[1024];
75     char query[1024];
76     unsigned int i;
77     int tableid;
78     MYSQL_RES* resp;
79     MYSQL_ROW row;
80     retval = frt.open("rainbowtables-distrrtgen", config.db_host, config.db_user, config.db_passwd);
81     if (retval) {
82         log_messages.printf(MSG_CRITICAL, "can't open db rainbowtables-distrrtgen\n");
83         exit(1);
84     }
85
86     
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);    
92
93     
94     if (wu.canonical_resultid) {
95          char resdir[512];
96 struct passwd *pwd;
97 struct group *grp;
98 pwd = getpwnam("rainbow");
99 grp = getgrnam("rainbow");
100
101         sprintf(query, "UPDATE rainbowcrack_cracker_lookups SET hasindices = 2 WHERE lookupid = %i", lookupid);
102 //      log_messages.printf(MSG_DEBUG, "%s\n", query);
103         frt.do_query(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);
108         if(retval) {
109                 log_messages.printf(MSG_DEBUG, "Query returned %i\n", retval);
110                 goto cleanup;
111         }
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;
122         char copy_path[256];
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];
128                 if (n==1) {
129                      sprintf(copy_path, "../../indices/%i/%i.index", tableid, lookupid);
130                 } else {
131                     sprintf(copy_path, "../../indices/%i/%i_%d.index", tableid, lookupid, i);
132                 }
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());
140                     write_error(buf);
141                     goto cleanup;
142                 }
143         }
144     } else {
145         sprintf(buf, "%s: 0x%x\n", wu.name, wu.error_mask);
146         return write_error(buf);
147     }
148     retval = 0;
149 cleanup:
150     frt.close();        
151     return retval;
152 }