Skip to contentSkip to conversation

conga/ricci common/Makefile common/XML.cpp com ...

Cluster-devel [email protected] 1 participant 1 message Oct 23, 2006

lore
Share & tools
lore
#0 lore raw

CVSROOT: /cvs/cluster
Module name: conga
Branch: RHEL5
Changes by: kupcevic at sourceware.org 2006-10-23 21:13:22

Modified files:
ricci/common : Makefile XML.cpp utils.cpp
ricci/modules/cluster/clumon/src/daemon: Monitor.cpp ricci/modules/log: LogParser.cpp ricci/modules/rpm: PackageHandler.cpp ricci/modules/service: ServiceManager.cpp ricci/modules/storage: MountHandler.cpp
ricci/ricci : Ricci.cpp SSLInstance.cpp
Added files:
ricci/common : File.cpp
ricci/include : File.h

Log message:
ricci: file handling - agregate file IO into single routine (bz211564)

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/File.cpp.diff?cvsroot=cluster&only_with_tag=RHEL5&r1=NONE&r2=1.1.2.1
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/Makefile.diff?cvsroot=cluster&only_with_tag=RHEL5&r1=1.6&r2=1.6.2.1
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/XML.cpp.diff?cvsroot=cluster&only_with_tag=RHEL5&r1=1.6&r2=1.6.2.1
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/utils.cpp.diff?cvsroot=cluster&only_with_tag=RHEL5&r1=1.6&r2=1.6.2.1
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/include/File.h.diff?cvsroot=cluster&only_with_tag=RHEL5&r1=NONE&r2=1.1.2.1
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/cluster/clumon/src/daemon/Monitor.cpp.diff?cvsroot=cluster&only_with_tag=RHEL5&r1=1.10&r2=1.10.2.1
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/log/LogParser.cpp.diff?cvsroot=cluster&only_with_tag=RHEL5&r1=1.6&r2=1.6.2.1
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/rpm/PackageHandler.cpp.diff?cvsroot=cluster&only_with_tag=RHEL5&r1=1.9&r2=1.9.2.1
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/service/ServiceManager.cpp.diff?cvsroot=cluster&only_with_tag=RHEL5&r1=1.5&r2=1.5.2.1
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/modules/storage/MountHandler.cpp.diff?cvsroot=cluster&only_with_tag=RHEL5&r1=1.5&r2=1.5.2.1
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/ricci/Ricci.cpp.diff?cvsroot=cluster&only_with_tag=RHEL5&r1=1.18&r2=1.18.2.1
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/ricci/SSLInstance.cpp.diff?cvsroot=cluster&only_with_tag=RHEL5&r1=1.5&r2=1.5.2.1

/cvs/cluster/conga/ricci/common/File.cpp,v --> standard output revision 1.1.2.1

- renamed from ricci/common/File.cpp +214 -0
@@ -0,0 +1,214 @@
+/*
+  Copyright Red Hat, Inc. 2006
+
+  This program is free software; you can redistribute it and/or modify it
+  under the terms of the GNU General Public License as published by the
+  Free Software Foundation; either version 2, or (at your option) any
+  later version.
+
+  This program is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program; see the file COPYING.  If not, write to the
+  Free Software Foundation, Inc.,  675 Mass Ave, Cambridge, 
+  MA 02139, USA.
+*/
+/*
+ * Author: Stanko Kupcevic <[email protected]>
+ */
+
+
+#include "File.h"
+#include <fstream>
+#include <fcntl.h>
+#include <errno.h>
+
+using namespace std;
+
+
+File_pimpl::File_pimpl(void* fs, 
+		       bool& owner) :
+  fs(fs)
+{
+  if (fs == 0)
+    throw String("fs_ptr is null!!!");
+  owner = true;
+}
+
+File_pimpl::~File_pimpl()
+{
+  fstream* ptr = (fstream*) fs;
+  delete ptr;
+}
+
+
+
+File
+File::open(const String& filepath, 
+	   bool rw)
+{
+  if (access(filepath.c_str(), R_OK))
+    throw String("missing file ") + filepath;
+  ios_base::openmode mode = ios_base::in;
+  if (rw)
+    mode |= ios_base::out;
+  
+  counting_auto_ptr<File_pimpl> pimpl;
+  bool ownership_taken = false;
+  fstream* fs = new fstream(filepath.c_str(), mode);
+  try {
+    pimpl = counting_auto_ptr<File_pimpl>(new File_pimpl(fs, ownership_taken));
+  } catch ( ... ) {
+    if (!ownership_taken)
+      delete fs;
+    throw;
+  }
+  return File(pimpl, filepath, rw);
+}
+
+File 
+File::create(const String& filepath, 
+	     bool truncate)
+{
+  int t = ::open(filepath.c_str(), 
+		 O_CREAT|O_RDWR, 
+		 S_IRUSR|S_IWUSR|S_IRGRP);
+  if (t != -1)
+    while (close(t) && errno == EINTR)
+      ;
+  
+  ios_base::openmode mode = ios_base::in;
+  mode |= ios_base::out;
+  if (truncate)
+    mode |= ios_base::trunc;
+  
+  counting_auto_ptr<File_pimpl> pimpl;
+  bool ownership_taken = false;
+  fstream* fs = new fstream(filepath.c_str(), mode);
+  try {
+    pimpl = counting_auto_ptr<File_pimpl>(new File_pimpl(fs, ownership_taken));
+  } catch ( ... ) {
+    if (!ownership_taken)
+      delete fs;
+    throw;
+  }
+  return File(pimpl, filepath, true);
+}
+
+File::File(counting_auto_ptr<File_pimpl> pimpl, 
+	   const String& path,
+	   bool writable) :
+  _mutex(counting_auto_ptr<Mutex>(new Mutex())), 
+  _pimpl(pimpl),
+  _path(path),
+  _writable(writable)
+{
+  if (!((fstream*) _pimpl->fs)->is_open())
+    throw String("unable to open ") + _path;
+  check_failed();
+}
+
+File::~File()
+{
+  if (_writable)
+    ((fstream*) _pimpl->fs)->flush();
+}
+
+String 
+File::path() const
+{
+  return _path;
+}
+
+long 
+File::size() const
+{
+  MutexLocker l(*_mutex);
+  ((fstream*) _pimpl->fs)->seekg(0, ios::end);
+  check_failed();
+  long s = ((fstream*) _pimpl->fs)->tellg();
+  check_failed();
+  if (s < 0)
+    throw String("size of file ") + _path + " is negative!!!";
+  return s;
+}
+
+String 
+File::read() const
+{
+  MutexLocker l(*_mutex);
+  
+  long len = size();
+  auto_ptr<char> buff(new char[len]);
+  ((fstream*) _pimpl->fs)->seekg(0, ios::beg);
+  check_failed();
+  ((fstream*) _pimpl->fs)->read(buff.get(), len);
+  ::shred(buff.get(), len);
+  check_failed();
+  return String(buff.get(), len);
+}
+
+File& 
+File::append(const String& data)
+{
+  MutexLocker l(*_mutex);
+  if (!_writable)
+    throw String("not writable");
+  ((fstream*) _pimpl->fs)->seekp(0, ios::end);
+  check_failed();
+  ((fstream*) _pimpl->fs)->write(data.c_str(), data.size());
+  check_failed();
+  ((fstream*) _pimpl->fs)->flush();
+  check_failed();
+  return *this;
+}
+
+String 
+File::replace(const String& data)
+{
+  MutexLocker l(*_mutex);
+  if (!_writable)
+    throw String("not writable");
+  String old(read());
+  create(_path, true);
+  append(data);
+  return old;
+}
+
+void 
+File::shred()
+{
+  MutexLocker l(*_mutex);
+  if (!_writable)
+    throw String("not writable");
+  unsigned int len = size();
+  ((fstream*) _pimpl->fs)->seekp(0, ios::beg);
+  check_failed();
+  // should use random source (paranoid)
+  // doesn't work on journaled fss anyways
+  ((fstream*) _pimpl->fs)->write(String(len, 'o').c_str(), len);
+  check_failed();
+}
+
+void 
+File::unlink()
+{
+  MutexLocker l(*_mutex);
+  if (::unlink(_path.c_str()))
+    throw String("unlink failed");
+}
+
+File::operator const String () const
+{
+  return read();
+}
+
+void
+File::check_failed() const
+{
+  if (((fstream*) _pimpl->fs)->fail())
+    throw String("IO error");
+}
ricci/common/Makefile +1 -0
@@ -22,6 +22,7 @@
 	Thread.o \
 	Time.o \
 	utils.o \
+	File.o \
 	XML.o \
 	Socket.o \
 	ServerSocket.o \
ricci/common/XML.cpp +2 -20
@@ -24,12 +24,12 @@
 #include "XML.h"
 #include "Mutex.h"
 #include "utils.h"
+#include "File.h"
 
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 
 #include <algorithm>
-#include <fstream>
 
 //#include <iostream>
 using namespace std;
@@ -229,25 +229,7 @@
 XMLObject 
 readXML(const String& filename)
 {
-  char* buff = 0;
-  try {
-    if (access(filename.c_str(), R_OK))
-      throw String("missing ") + filename;
-    ifstream is(filename.c_str());
-    is.seekg(0, ios::end);
-    unsigned int length = is.tellg();
-    is.seekg(0, ios::beg);
-    buff = new char[length];
-    is.read(buff, length);
-    String xml_data(buff, length);
-    
-    XMLObject xml(parseXML(xml_data));
-    delete [] buff; buff = 0;
-    return xml;
-  } catch ( ... ) {
-    delete [] buff;
-    throw;
-  }
+  return parseXML(File::open(filename));
 }
 
 
ricci/common/utils.cpp +1 -6
@@ -27,8 +27,7 @@
 #include <openssl/md5.h>
 #include <stdlib.h>
 
-
-#include <iostream>
+//#include <iostream>
 
 
 using namespace std;
@@ -298,7 +297,3 @@
 std::map<String, exec_cache> 
 utils::cache;
 
-
-
-
-

/cvs/cluster/conga/ricci/include/File.h,v --> standard output revision 1.1.2.1

- renamed from ricci/include/File.h +84 -0

84 lines not on this page · raw message

ricci/modules/cluster/clumon/src/daemon/Monitor.cpp +0 -1
@@ -31,7 +31,6 @@
 #include <sys/sysinfo.h>
 
 #include <algorithm>
-#include <fstream>
 
 
 using namespace ClusterMonitoring;
ricci/modules/log/LogParser.cpp +2 -0
@@ -386,6 +386,8 @@
        iter != files.end();
        iter++) {
     ifstream log(iter->c_str());
+    if (!log.is_open())
+      throw String("failed to open ") + *iter;
     while (log.good()) {
       char buff[1024];
       log.getline(buff, sizeof(buff));
ricci/modules/rpm/PackageHandler.cpp +2 -19
@@ -23,9 +23,9 @@
 
 #include "PackageHandler.h"
 #include "utils.h"
+#include "File.h"
 
 #include <unistd.h>
-#include <fstream>
 
 
 using namespace std;
@@ -645,24 +645,7 @@
 {
   static bool release_set = false;
   if (!release_set) {
-    char* buff = 0;
-    String release;
-    try {
-      ifstream is("/etc/redhat-release");
-      is.seekg(0, ios::end);
-      unsigned int length = is.tellg();
-      is.seekg(0, ios::beg);
-      buff = new char[length];
-      is.read(buff, length);
-      
-      String rel(buff, length);
-      delete [] buff; buff = 0;
-      
-      release = utils::strip(rel);
-    } catch ( ... ) {
-      delete [] buff;
-      throw;
-    }
+    String release(utils::strip(File::open("/etc/redhat-release")));
     
     if (release.find("Nahant") != release.npos)
       RHEL4 = true;
ricci/modules/service/ServiceManager.cpp +39 -70

137 lines not on this page · raw message

ricci/modules/storage/MountHandler.cpp +2 -2
@@ -561,8 +561,8 @@
     int ret;
     while ((ret = flock(locker_fd, LOCK_UN)) && errno == EINTR)
       ;
-    if (ret)
-      throw String("unable to unflock fstab");
+    //if (ret)
+    //  throw String("unable to unflock fstab");
     while (close(locker_fd) == -1 && errno == EINTR)
       ;
   }
ricci/ricci/Ricci.cpp +8 -43

84 lines not on this page · raw message

ricci/ricci/SSLInstance.cpp +4 -16
@@ -27,6 +27,7 @@
 #include "Time.h"
 #include "Random.h"
 #include "utils.h"
+#include "File.h"
 
 #include <errno.h>
 #include <sys/types.h>
@@ -35,7 +36,6 @@
 #include <unistd.h>
 
 #include <iostream>
-#include <fstream>
 #include <list>
 #include <set>
 
@@ -119,23 +119,11 @@
   for (set<String>::const_iterator iter = files.begin();
        iter != files.end();
        iter++) {
-    char* buff = 0;
     try {
-      ifstream is(iter->c_str());
-      is.seekg(0, ios::end);
-      streamoff length = is.tellg();
-      is.seekg(0, ios::beg);
-      if (length > 10 * 1024)
-	throw String("file too large to be a certificate");
-      buff = new char[length];
-      is.read(buff, length);
-      String cert(buff, length);
-      if (cert.size())
+      String cert(File::open(*iter).read());
+      if (cert.size() && cert.size() < 10 * 1024)
 	authorized_certs.push_back(file_cert(*iter, cert));
-      delete [] buff;
-    } catch ( ... ) {
-      delete [] buff;
-    }
+    } catch ( ... ) {}
   }
 }
 static void