// dial_ws_file_imp.cxx // Here we define the gsoap service functions for the filesystem // service. #include #include #include "dataset_util/FileStatus.h" #include "dataset_util/Text.h" #include "dataset_util/ssystem.h" #include "dataset_util/XmlElement.h" #include "dial_ws/GsoapRegistry.h" #include "dial_ws/DialWs.h" #include "../gsoap/dial_ws_fileH.h" using std::string; using std::ostream; using std::cout; using std::endl; using std::ofstream; using std::auto_ptr; using dial::DialWs; extern SOAP_NMAC struct Namespace dial_ws_file_namespaces[]; //********************************************************************** // Helper functions. //********************************************************************** namespace { //********************************************************************** // Initialization. // This function is called when the service is started. int init() { return 0; } //********************************************************************** // Registration. // The "filesystem" service is associated with the listed // initialization fundtion, gsoap server function and gsoap namespace. int dial_ws_file_reg = dial::GsoapRegistry::insert( "filesystem", init, dial_ws_file_serve, dial_ws_file_namespaces); //********************************************************************** } // end unnamed namespace //********************************************************************** // SOAP functions for Scheduler. //********************************************************************** // File existence. int dial__file_exists(soap*, string fname, bool& exist) { DialWs::lout() << ">>>Received file existence request" << endl; FileStatus fstat(fname); exist = fstat.exists(); DialWs::lout() << "File " << fname << " "; if ( exist ) DialWs::lout() << "exists."; else DialWs::lout() << "does not exist."; DialWs::lout() << endl; return SOAP_OK; } //********************************************************************** // File description. int dial__file_listing(soap* psoap, string fname, char*& listing) { listing = 0; DialWs::lout() << ">>>Received file listing request" << endl; const char* emsg1 = "Error in DIAL filesystem file_listing"; FileStatus fstat(fname); if ( ! fstat.exists() ) { DialWs::lout() << "File does not exist" << endl;; soap_receiver_fault(psoap, emsg1, "File does not exist"); return SOAP_OK; } char tmpfile[] = "/tmp/dial_ws_file.XXXXXX"; int fd = mkstemp(tmpfile); if ( fd == -1 ) { DialWs::lout() << "Unable top create temporary file from " << tmpfile << endl; soap_receiver_fault(psoap, emsg1, "Internal error"); return SOAP_OK; } ssystem("ls -lsd " + fname + " >" + tmpfile); Text txt(tmpfile); unlink(tmpfile); if ( txt.size() != 1 ) { DialWs::lout() << "Error creating listing" << endl; DialWs::lout() << txt << endl; soap_receiver_fault(psoap, emsg1, "Internal error"); return SOAP_OK; } static string slist; slist = txt.line(0); listing = const_cast(slist.c_str()); DialWs::lout() << "File " << fname << endl; DialWs::lout() << listing << endl; return SOAP_OK; } //********************************************************************** // Directory listing. int dial__directory_listing(soap* psoap, string fname, string& sxlist) { // Write initial message to log. DialWs::lout() << ">>>Received directory listing request" << endl; // Define primary error string. const char* emsg1 = "Error in DIAL filesystem directory_listing"; // Verify that the input name is a directory. FileStatus fstat(fname); if ( ! fstat.is_directory() ) { DialWs::lout() << "File is not a directory" << endl;; soap_receiver_fault(psoap, emsg1, "Not a directory"); return SOAP_OK; } // Use ls command to fetch directory contents. char tmpfile[] = "/tmp/dial_ws_file.XXXXXX"; int fd = mkstemp(tmpfile); if ( fd == -1 ) { DialWs::lout() << "Unable top create temporary file from " << tmpfile << endl; soap_receiver_fault(psoap, emsg1, "Internal error"); return SOAP_OK; } ssystem("ls " + fname + " >" + tmpfile); // Store directory contents in a text object. Text txt(tmpfile); unlink(tmpfile); // Extract the XML description of text object and copy it to the // static string. auto_ptr pxlist(txt.xml()); sxlist = pxlist->to_xml_text(); // Log result and exit. DialWs::lout() << "Directory " << fname << endl; DialWs::lout() << txt << endl; return SOAP_OK; } //********************************************************************** // Get a file. int dial__file_get(soap* psoap, string fname, std::string& sxfile) { // Write initial message to log. DialWs::lout() << ">>>Received file get request" << endl; // Define primary error string. const char* emsg1 = "Error in DIAL filesystem file_get"; // Verify that the input name is an existing file. FileStatus fstat(fname); if ( ! fstat.exists() ) { DialWs::lout() << "File does not exist" << endl;; soap_receiver_fault(psoap, emsg1, "File does not exist"); return SOAP_OK; } Text txt(fname); if ( ! txt.size() ) { DialWs::lout() << "Error reading file" << endl;; soap_receiver_fault(psoap, emsg1, "File read error"); return SOAP_OK; } // Extract the XML description of text object and copy it to the // static string. auto_ptr pxfile(txt.xml()); sxfile = pxfile->to_xml_text(); // Log result and exit. DialWs::lout() << "File " << fname << endl; DialWs::lout() << txt << endl; return SOAP_OK; } //**********************************************************************