Back to index

nordugrid-arc-nox  1.1.0~rc6
cachecheck.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include <arc/message/SOAPEnvelope.h>
00006 #include <arc/ws-addressing/WSA.h>
00007 #include <arc/data/FileCache.h>
00008 #include <sys/types.h>
00009 #include <pwd.h>
00010 #include <sys/stat.h> 
00011 #include <arc/data/DataHandle.h>
00012 #include <arc/URL.h>
00013 #include <arc/StringConv.h>
00014 #include <arc/UserConfig.h>
00015 
00016 #include "arex.h"
00017 #include "grid-manager/conf/conf_cache.h"
00018 #include "grid-manager/jobs/job.h"
00019 #include "grid-manager/jobs/users.h"
00020 
00021 #define CACHE_CHECK_SESSION_DIR_ID "9999999999999999999999999999999"
00022 #define inttostring Arc::tostring
00023 
00024 namespace ARex {
00025 
00026 Arc::MCC_Status ARexService::CacheCheck(ARexGMConfig& config,Arc::XMLNode in,Arc::XMLNode out) {
00027 
00028       // We are supporting only this cachedir format for checking: cachedir="/tmp/cache"
00029          //
00030          // The cachedir="/tmp/%U/cache" format cannot be implemented at the moment 
00031          // but maybe at the future 
00032 
00033 
00034   uid_t uid = getuid();
00035   gid_t gid = getgid();
00036  
00037   std::string file_owner_username = "";
00038 
00039   JobUser user(uid);
00040 
00041   std::vector<std::string> caches;
00042 
00043     struct passwd pw_;
00044     struct passwd *pw;
00045     char buf[BUFSIZ];
00046     getpwuid_r(getuid(),&pw_,buf,BUFSIZ,&pw);
00047     if(pw == NULL) {
00048      logger.msg(Arc::ERROR, "Error with cache configuration"); 
00049      Arc::SOAPFault fault(out.Parent(),Arc::SOAPFault::Sender,"Error with cache configuration");  
00050      fault.Detail(true).NewChild("CacheConfigurationFault");
00051         out.Destroy();
00052         return Arc::MCC_Status();
00053     }
00054     if(pw->pw_name) file_owner_username=pw->pw_name;
00055 
00056     // use cache dir(s) from conf file
00057     try {
00058       CacheConfig * cache_config = new CacheConfig(std::string(file_owner_username));
00059       std::vector<std::string> conf_caches = cache_config->getCacheDirs();
00060       // add each cache to our list
00061       for (std::vector<std::string>::iterator i = conf_caches.begin(); i != conf_caches.end(); i++) {
00062         user.substitute(*i);
00063         caches.push_back(*i);
00064       }
00065     }
00066     catch (CacheConfigException e) {
00067      logger.msg(Arc::ERROR, "Error with cache configuration: %s", e.what()); 
00068      Arc::SOAPFault fault(out.Parent(),Arc::SOAPFault::Sender,"Error with cache configuration");  
00069      fault.Detail(true).NewChild("CacheConfigurationFault");
00070         out.Destroy();
00071         return Arc::MCC_Status();
00072     }
00073 
00074   if (caches.empty()) {
00075      Arc::SOAPFault fault(out.Parent(),Arc::SOAPFault::Sender,"Cache is disabled");  
00076      fault.Detail(true).NewChild("CacheDisabledFault");
00077         out.Destroy();
00078         return Arc::MCC_Status();
00079   }
00080 
00081 
00082   Arc::FileCache * cache;
00083   if(!caches.empty()) {
00084 
00085     cache = new Arc::FileCache(caches, CACHE_CHECK_SESSION_DIR_ID ,uid,gid);
00086     if (!(*cache)) {
00087      logger.msg(Arc::ERROR, "Error with cache configuration"); 
00088      Arc::SOAPFault fault(out.Parent(),Arc::SOAPFault::Sender,"Error with cache configuration");  
00089      fault.Detail(true).NewChild("CacheConfigurationFault");
00090         out.Destroy();
00091         return Arc::MCC_Status();
00092     }
00093   }
00094 
00095   bool fileexist;
00096 
00097   Arc::XMLNode resp = out.NewChild("CacheCheckResponse");
00098 
00099   Arc::XMLNode results = resp.NewChild("CacheCheckResult");
00100 
00101    for(int n = 0;;++n) {
00102       Arc::XMLNode id = in["CacheCheck"]["TheseFilesNeedToCheck"]["FileURL"][n];
00103       
00104       if (!id) break;
00105     
00106       fileexist = false;
00107 
00108          std::string fileurl = (std::string)in["CacheCheck"]["TheseFilesNeedToCheck"]["FileURL"][n];
00109  
00110       std::string file_lfn;
00111 
00112       Arc::UserConfig usercfg(true);
00113       Arc::DataHandle d(fileurl, usercfg);
00114 
00115       file_lfn = (*cache).File(d->str());
00116 
00117          struct stat fileStat;
00118          fileexist = (stat(file_lfn.c_str(), &fileStat) == 0) ? true : false;
00119 
00120       Arc::XMLNode resultelement = results.NewChild("Result");
00121 
00122          resultelement.NewChild("FileURL") = fileurl;
00123          resultelement.NewChild("ExistInTheCache") = (fileexist ? "true": "false");
00124   
00125       if (fileexist) 
00126             resultelement.NewChild("FileSize") = inttostring(fileStat.st_size);
00127          else
00128             resultelement.NewChild("FileSize") = "0";
00129 
00130 
00131    }
00132   
00133    return Arc::MCC_Status(Arc::STATUS_OK);
00134 }
00135 
00136 } // namespace