irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

rsFileChksum.cpp
Go to the documentation of this file.
1 
3 /* This is script-generated code (for the most part). */
4 /* See fileChksum.h for a description of this API call.*/
5 
6 #include "fileChksum.h"
7 #include "miscServerFunct.hpp"
8 #include "rsFileChksum.hpp"
9 
10 // =-=-=-=-=-=-=-
11 #include "irods_log.hpp"
12 #include "irods_file_object.hpp"
13 #include "irods_stacktrace.hpp"
15 #include "irods_hasher_factory.hpp"
17 #include "MD5Strategy.hpp"
18 
19 #define SVR_MD5_BUF_SZ (1024*1024)
20 
21 int
23  rsComm_t *rsComm,
24  fileChksumInp_t *fileChksumInp,
25  char **chksumStr ) {
27  int remoteFlag;
28  int status;
29  irods::error ret = irods::get_host_for_hier_string( fileChksumInp->rescHier, remoteFlag, rodsServerHost );
30  if ( !ret.ok() ) {
31  irods::log( PASSMSG( "failed in call to irods::get_host_for_hier_string", ret ) );
32  return -1;
33  }
34 
35 
36  if ( remoteFlag == LOCAL_HOST ) {
37  status = _rsFileChksum( rsComm, fileChksumInp, chksumStr );
38  }
39  else if ( remoteFlag == REMOTE_HOST ) {
40  status = remoteFileChksum( rsComm, fileChksumInp, chksumStr,
42  }
43  else {
44  if ( remoteFlag < 0 ) {
45  return remoteFlag;
46  }
47  else {
49  "rsFileChksum: resolveHost returned unrecognized value %d",
50  remoteFlag );
52  }
53  }
54 
55  return status;
56 }
57 
58 int
59 remoteFileChksum( rsComm_t *rsComm, fileChksumInp_t *fileChksumInp,
60  char **chksumStr, rodsServerHost_t *rodsServerHost ) {
61  int status;
62 
63  if ( rodsServerHost == NULL ) {
65  "remoteFileChksum: Invalid rodsServerHost" );
67  }
68 
69  if ( ( status = svrToSvrConnect( rsComm, rodsServerHost ) ) < 0 ) {
70  return status;
71  }
72 
73 
74  status = rcFileChksum( rodsServerHost->conn, fileChksumInp, chksumStr );
75 
76  if ( status < 0 ) {
78  "remoteFileChksum: rcFileChksum failed for %s",
79  fileChksumInp->fileName );
80  }
81 
82  return status;
83 }
84 
85 int
87  rsComm_t *rsComm,
88  fileChksumInp_t *fileChksumInp,
89  char **chksumStr ) {
90  int status;
91  if ( !*chksumStr ) {
92  *chksumStr = ( char* )malloc( sizeof( char ) * NAME_LEN );
93  }
94 
96  rsComm,
97  fileChksumInp->objPath,
98  fileChksumInp->fileName,
99  fileChksumInp->rescHier,
100  fileChksumInp->orig_chksum,
101  *chksumStr );
102  if ( status < 0 ) {
104  "_rsFileChksum: fileChksum for %s, status = %d",
105  fileChksumInp->fileName, status );
106  free( *chksumStr );
107  *chksumStr = NULL;
108  }
109 
110  return status;
111 }
112 
114  rsComm_t* rsComm,
115  char* objPath,
116  char* fileName,
117  char* rescHier,
118  char* orig_chksum,
119  char* chksumStr ) {
120  // =-=-=-=-=-=-=-
121  // capture server hashing settings
122  std::string hash_scheme( irods::MD5_NAME );
123  try {
124  hash_scheme = irods::get_server_property<const std::string>(irods::CFG_DEFAULT_HASH_SCHEME_KW);
125  } catch ( const irods::exception& ) {}
126 
127  // make sure the read parameter is lowercased
129  hash_scheme.begin(),
130  hash_scheme.end(),
131  hash_scheme.begin(),
132  ::tolower );
133 
134  std::string hash_policy;
135  try {
136  hash_policy = irods::get_server_property<const std::string>(irods::CFG_MATCH_HASH_POLICY_KW);
137  } catch ( const irods::exception& ) {}
138 
139  // =-=-=-=-=-=-=-
140  // extract scheme from checksum string
141  std::string chkstr_scheme;
142  if ( orig_chksum ) {
144  orig_chksum,
145  chkstr_scheme );
146  if ( !ret.ok() ) {
147  //irods::log( PASS( ret ) );
148  }
149  }
150 
151  // =-=-=-=-=-=-=-
152  // check the hash scheme against the policy
153  // if necessary
154  std::string final_scheme( hash_scheme );
155  if ( !chkstr_scheme.empty() ) {
156  if ( !hash_policy.empty() ) {
157  if ( irods::STRICT_HASH_POLICY == hash_policy ) {
158  if ( hash_scheme != chkstr_scheme ) {
160  }
161  }
162  }
163  final_scheme = chkstr_scheme;
164  }
165 
166  rodsLog(
167  LOG_DEBUG,
168  "fileChksum :: final_scheme [%s] chkstr_scheme [%s] svr_hash_policy [%s] hash_policy [%s]",
169  final_scheme.c_str(),
170  chkstr_scheme.c_str(),
171  hash_policy.c_str() );
172 
173  // =-=-=-=-=-=-=-
174  // call resource plugin to open file
175  irods::file_object_ptr file_obj(
176  new irods::file_object(
177  rsComm,
178  objPath,
179  fileName,
180  rescHier,
181  -1, 0, O_RDONLY ) ); // FIXME :: hack until this is better abstracted - JMC
182  irods::error ret = fileOpen( rsComm, file_obj );
183  if ( !ret.ok() ) {
184  int status = UNIX_FILE_OPEN_ERR - errno;
185  if ( ret.code() != DIRECT_ARCHIVE_ACCESS ) {
186  std::stringstream msg;
187  msg << "fileOpen failed for [";
188  msg << fileName;
189  msg << "]";
190  irods::log( PASSMSG( msg.str(), ret ) );
191  }
192  else {
193  status = ret.code();
194  }
195  return status;
196  }
197 
198  // =-=-=-=-=-=-=-
199  // create a hasher object and init given a scheme
200  // if it is unsupported then default to md5
201  irods::Hasher hasher;
202  ret = irods::getHasher( final_scheme, hasher );
203  if ( !ret.ok() ) {
204  irods::log( PASS( ret ) );
206  }
207 
208  // =-=-=-=-=-=-=-
209  // do an inital read of the file
210  char buffer[SVR_MD5_BUF_SZ];
211  irods::error read_err = fileRead(
212  rsComm,
213  file_obj,
214  buffer,
215  SVR_MD5_BUF_SZ );
216  if (!read_err.ok()) {
217  std::stringstream msg;
218  msg << __FUNCTION__;
219  msg << " - Failed to read buffer from file: \"";
220  msg << fileName;
221  msg << "\"";
222  irods::error result = PASSMSG( msg.str(), read_err );
223  irods::log( result );
224  return result.code();
225  }
226  int bytes_read = read_err.code();
227 
228  // RTS - Issue #3275
229  if ( bytes_read == 0 ) {
230  std::string buffer_read;
231  buffer_read.resize( SVR_MD5_BUF_SZ );
232  }
233 
234  // =-=-=-=-=-=-=-
235  // loop and update while there are still bytes to be read
236  while ( read_err.ok() && bytes_read > 0 ) {
237  // =-=-=-=-=-=-=-
238  // update hasher
239  hasher.update( std::string( buffer, bytes_read ) );
240 
241  // =-=-=-=-=-=-=-
242  // read some more
243  read_err = fileRead( rsComm, file_obj, buffer, SVR_MD5_BUF_SZ );
244  if ( read_err.ok() ) {
245  bytes_read = read_err.code();
246  }
247  else {
248  std::stringstream msg;
249  msg << __FUNCTION__;
250  msg << " - Failed to read buffer from file: \"";
251  msg << fileName;
252  msg << "\"";
253  irods::error result = PASSMSG( msg.str(), read_err );
254  irods::log( result );
255  return result.code();
256  }
257 
258  } // while
259 
260  // =-=-=-=-=-=-=-
261  // close out the file
262  ret = fileClose( rsComm, file_obj );
263  if ( !ret.ok() ) {
264  irods::error err = PASSMSG( "error on close", ret );
265  irods::log( err );
266  }
267 
268  // =-=-=-=-=-=-=-
269  // extract the digest from the hasher object
270  // and copy to outgoing string
271  std::string digest;
272  hasher.digest( digest );
273  strncpy( chksumStr, digest.c_str(), NAME_LEN );
274 
275  return 0;
276 }
rodsLog
void rodsLog(int level, const char *formatStr,...)
Definition: rodsLog.cpp:86
NULL
#define NULL
Definition: rodsDef.h:70
rsComm_t
Definition: rcConnect.h:145
fileChksum.h
FileChksumInp::objPath
char objPath[(1024+64)]
Definition: fileChksum.h:11
irods_server_properties.hpp
irods::CFG_DEFAULT_HASH_SCHEME_KW
const std::string CFG_DEFAULT_HASH_SCHEME_KW("default_hash_scheme")
irods::transform
void transform(const InputC &ic, OutputC &oc, UnaryOperation func)
Definition: irods_re_plugin.hpp:822
rodsServerHost::conn
rcComm_t * conn
Definition: rodsConnect.h:64
SYS_UNRECOGNIZED_REMOTE_FLAG
@ SYS_UNRECOGNIZED_REMOTE_FLAG
Definition: rodsErrorTable.h:88
irods_file_object.hpp
irods_stacktrace.hpp
PASS
#define PASS(prev_error_)
Definition: irods_error.hpp:118
REMOTE_HOST
#define REMOTE_HOST
Definition: rodsConnect.h:45
irods::getHasher
error getHasher(const std::string &name, Hasher &hasher)
Definition: irods_hasher_factory.cpp:27
LOCAL_HOST
#define LOCAL_HOST
Definition: rodsConnect.h:44
irods_resource_backport.hpp
irods::get_hash_scheme_from_checksum
error get_hash_scheme_from_checksum(const std::string &checksum, std::string &scheme)
Definition: irods_hasher_factory.cpp:39
rcFileChksum
int rcFileChksum(rcComm_t *conn, fileChksumInp_t *fileChksumInp, char **chksumStr)
Definition: rcFileChksum.cpp:24
PASSMSG
#define PASSMSG(message_, prev_error_)
Definition: irods_error.hpp:119
FileChksumInp::rescHier
char rescHier[(1024+64)]
Definition: fileChksum.h:10
USER_HASH_TYPE_MISMATCH
@ USER_HASH_TYPE_MISMATCH
Definition: rodsErrorTable.h:284
FileChksumInp::orig_chksum
char orig_chksum[64]
Definition: fileChksum.h:14
UNIX_FILE_OPEN_ERR
@ UNIX_FILE_OPEN_ERR
Definition: rodsErrorTable.h:300
irods::error::code
long long code() const
Definition: irods_error.cpp:194
_rsFileChksum
int _rsFileChksum(rsComm_t *rsComm, fileChksumInp_t *fileChksumInp, char **chksumStr)
Definition: rsFileChksum.cpp:86
LOG_DEBUG
#define LOG_DEBUG
Definition: rodsLog.h:23
irods::get_host_for_hier_string
error get_host_for_hier_string(const std::string &, int &, rodsServerHost_t *&)
Definition: irods_resource_backport.cpp:581
irods::Hasher
Definition: Hasher.hpp:15
FileChksumInp
Definition: fileChksum.h:7
rsFileChksum.hpp
SYS_INVALID_SERVER_HOST
@ SYS_INVALID_SERVER_HOST
Definition: rodsErrorTable.h:89
irods.pypyodbc.buffer
buffer
Definition: pypyodbc.py:46
svrToSvrConnect
int svrToSvrConnect(rsComm_t *rsComm, rodsServerHost_t *rodsServerHost)
Definition: miscServerFunct.cpp:106
irods.pypyodbc.status
status
Definition: pypyodbc.py:467
irods::log
void log(const error &)
Definition: irods_log.cpp:13
irods::Hasher::update
error update(const std::string &)
Definition: Hasher.cpp:22
irods::CFG_MATCH_HASH_POLICY_KW
const std::string CFG_MATCH_HASH_POLICY_KW("match_hash_policy")
LOG_NOTICE
#define LOG_NOTICE
Definition: rodsLog.h:33
irods::STRICT_HASH_POLICY
const std::string STRICT_HASH_POLICY("strict")
fileOpen
irods::error fileOpen(rsComm_t *, irods::first_class_object_ptr)
Definition: fileDriver.cpp:49
irods::error
Definition: irods_error.hpp:23
miscServerFunct.hpp
FileChksumInp::fileName
char fileName[(1024+64)]
Definition: fileChksum.h:9
fileClose
irods::error fileClose(rsComm_t *, irods::first_class_object_ptr)
Definition: fileDriver.cpp:147
irods::file_object
Definition: irods_file_object.hpp:19
irods::Hasher::digest
error digest(std::string &messageDigest)
Definition: Hasher.cpp:35
rodsServerHost
Definition: rodsConnect.h:62
irods::file_object_ptr
boost::shared_ptr< file_object > file_object_ptr
Definition: irods_file_object.hpp:145
irods_hasher_factory.hpp
rsFileChksum
int rsFileChksum(rsComm_t *rsComm, fileChksumInp_t *fileChksumInp, char **chksumStr)
Definition: rsFileChksum.cpp:22
irods::exception
Definition: irods_exception.hpp:15
irods::MD5_NAME
const std::string MD5_NAME("md5")
SVR_MD5_BUF_SZ
#define SVR_MD5_BUF_SZ
Definition: rsFileChksum.cpp:19
remoteFileChksum
int remoteFileChksum(rsComm_t *rsComm, fileChksumInp_t *fileChksumInp, char **chksumStr, rodsServerHost_t *rodsServerHost)
Definition: rsFileChksum.cpp:59
NAME_LEN
#define NAME_LEN
Definition: rodsDef.h:55
irods::error::ok
bool ok()
Definition: irods_error.cpp:258
fileChksum
int fileChksum(rsComm_t *rsComm, char *objPath, char *fileName, char *rescHier, char *orig_chksum, char *chksumStr)
Definition: rsFileChksum.cpp:113
DIRECT_ARCHIVE_ACCESS
@ DIRECT_ARCHIVE_ACCESS
Definition: rodsErrorTable.h:763
irods_log.hpp
fileRead
irods::error fileRead(rsComm_t *, irods::first_class_object_ptr, void *, int)
Definition: fileDriver.cpp:81
MD5Strategy.hpp