Author: kpvdr
Date: 2009-12-03 10:42:14 -0500 (Thu, 03 Dec 2009)
New Revision: 3734
Modified:
store/trunk/cpp/lib/jrnl/jdir.cpp
store/trunk/cpp/lib/jrnl/jdir.hpp
store/trunk/cpp/lib/jrnl/jerrno.cpp
store/trunk/cpp/lib/jrnl/jerrno.hpp
Log:
Tidied up some common code in jdir class, fixed up some of its documentation.
Modified: store/trunk/cpp/lib/jrnl/jdir.cpp
===================================================================
--- store/trunk/cpp/lib/jrnl/jdir.cpp 2009-12-03 10:00:25 UTC (rev 3733)
+++ store/trunk/cpp/lib/jrnl/jdir.cpp 2009-12-03 15:42:14 UTC (rev 3734)
@@ -35,7 +35,6 @@
#include <cstdlib>
#include <cstring>
-#include <dirent.h>
#include <cerrno>
#include <iomanip>
#include "jrnl/jcfg.hpp"
@@ -164,20 +163,9 @@
}
// FIXME: Find out why this fails with false alarms/errors from time to time...
// While commented out, there is no error capture from reading dir entries.
-// if (errno)
-// {
-// std::ostringstream oss;
-// oss << "dir=\"" << dirname <<
"\"" << FORMAT_SYSERR(errno);
-// ::closedir(dir); // Try to close, it makes no sense to trap errors here...
-// throw jexception(jerrno::JERR_JDIR_READDIR, oss.str(), "jdir",
"clear_dir");
-// }
+// check_err(errno, dir, dirname, "clear_dir");
#endif
- if (::closedir(dir))
- {
- std::ostringstream oss;
- oss << "dir=\"" << dirname <<
"\"" << FORMAT_SYSERR(errno);
- throw jexception(jerrno::JERR_JDIR_CLOSEDIR, oss.str(), "jdir",
"clear_dir");
- }
+ close_dir(dir, dirname, "clear_dir");
}
@@ -287,23 +275,13 @@
}
}
}
+
// FIXME: Find out why this fails with false alarms/errors from time to time...
// While commented out, there is no error capture from reading dir entries.
-// if (errno)
-// {
-// std::ostringstream oss;
-// oss << "dir=\"" << dirname <<
"\"" << FORMAT_SYSERR(errno);
-// ::closedir(dir); // Try to close, it makes no sense to trap errors
here...
-// throw jexception(jerrno::JERR_JDIR_READDIR, oss.str(), "jdir",
"delete_dir");
-// }
+// check_err(errno, dir, dirname, "delete_dir");
}
// Now dir is empty, close and delete it
- if (::closedir(dir))
- {
- std::ostringstream oss;
- oss << "dir=\"" << dirname <<
"\"" << FORMAT_SYSERR(errno);
- throw jexception(jerrno::JERR_JDIR_CLOSEDIR, oss.str(), "jdir",
"delete_dir");
- }
+ close_dir(dir, dirname, "delete_dir");
if (!children_only)
if (::rmdir(dirname.c_str()))
@@ -347,19 +325,9 @@
}
// FIXME: Find out why this fails with false alarms/errors from time to time...
// While commented out, there is no error capture from reading dir entries.
-// if (errno)
-// {
-// std::ostringstream oss;
-// oss << "dir=\"" << dirname <<
"\"" << FORMAT_SYSERR(errno);
-// ::closedir(dir); // Try to close, it makes no sense to trap errors here...
-// throw jexception(jerrno::JERR_JDIR_READDIR, oss.str(), "jdir",
"clear_dir");
-// }
- if (::closedir(dir))
- {
- std::ostringstream oss;
- oss << "dir=\"" << dirname <<
"\"" << FORMAT_SYSERR(errno);
- throw jexception(jerrno::JERR_JDIR_CLOSEDIR, oss.str(), "jdir",
"create_bak_dir");
- }
+// check_err(errno, dir, dirname, "create_bak_dir");
+ close_dir(dir, dirname, "create_bak_dir");
+
std::ostringstream dn;
dn << dirname << "/_" << base_filename <<
".bak." << std::hex << std::setw(4) <<
std::setfill('0') << ++dir_num;
@@ -413,6 +381,29 @@
return exists(name.c_str());
}
+void
+jdir::check_err(const int err_num, DIR* dir, const std::string& dir_name, const
std::string& fn_name)
+{
+ if (err_num)
+ {
+ std::ostringstream oss;
+ oss << "dir=\"" << dir_name <<
"\"" << FORMAT_SYSERR(err_num);
+ ::closedir(dir); // Try to close, it makes no sense to trap errors here...
+ throw jexception(jerrno::JERR_JDIR_READDIR, oss.str(), "jdir",
fn_name);
+ }
+}
+
+void
+jdir::close_dir(DIR* dir, const std::string& dir_name, const std::string&
fn_name)
+{
+ if (::closedir(dir))
+ {
+ std::ostringstream oss;
+ oss << "dir=\"" << dir_name <<
"\"" << FORMAT_SYSERR(errno);
+ throw jexception(jerrno::JERR_JDIR_CLOSEDIR, oss.str(), "jdir",
fn_name);
+ }
+}
+
std::ostream&
operator<<(std::ostream& os, const jdir& jdir)
{
Modified: store/trunk/cpp/lib/jrnl/jdir.hpp
===================================================================
--- store/trunk/cpp/lib/jrnl/jdir.hpp 2009-12-03 10:00:25 UTC (rev 3733)
+++ store/trunk/cpp/lib/jrnl/jdir.hpp 2009-12-03 15:42:14 UTC (rev 3734)
@@ -3,8 +3,8 @@
*
* Qpid asynchronous store plugin library
*
- * File containing code for class mrg::journal::jdir (journal data
- * directory), used for controlling and manipulating journal data
+ * File containing code for class mrg::journal::jdir (%journal data
+ * directory), used for controlling and manipulating %journal data
* directories and files. See class documentation for details.
*
* \author Kim van der Riet
@@ -43,6 +43,7 @@
}
#include "jrnl/jinf.hpp"
+#include <dirent.h>
namespace mrg
{
@@ -51,7 +52,7 @@
/**
* \class jdir
- * \brief Class to manage the journal directory
+ * \brief Class to manage the %journal directory
*/
class jdir
{
@@ -65,7 +66,7 @@
* \brief Sole constructor
*
* \param dirname Name of directory to be managed.
- * \param base_filename Filename root used in the creation of journal files
+ * \param base_filename Filename root used in the creation of %journal files
* and sub-directories.
*/
jdir(const std::string& dirname, const std::string& base_filename);
@@ -74,10 +75,10 @@
/**
- * \brief Create journal directory as set in the dirname parameter of the
constructor.
+ * \brief Create %journal directory as set in the dirname parameter of the
constructor.
* Recursive creation is supported.
*
- * \exception ??
+ * \exception jerrno::JERR_JDIR_MKDIR The creation of dirname failed.
*/
void create_dir();
@@ -86,7 +87,7 @@
*
* \param dirname C-string containing name of directory.
*
- * \exception ??
+ * \exception jerrno::JERR_JDIR_MKDIR The creation of dirname failed.
*/
static void create_dir(const char* dirname);
@@ -95,153 +96,180 @@
*
* \param dirname String containing name of directory.
*
- * \exception ??
+ * \exception jerrno::JERR_JDIR_MKDIR The creation of dirname failed.
*/
static void create_dir(const std::string& dirname);
/**
- * \brief Clear the journal directory of files matching the base filename
+ * \brief Clear the %journal directory of files matching the base filename
* by moving them into a subdirectory. This fn uses the dirname and
base_filename
* that were set on construction.
*
* \param create_flag If set, create dirname if it is non-existent, otherwise
throw
* exception.
*
- * \exception The journal directory could not be opened.
- * \exception The move of files from the journal directory to the created backup
+ * \exception jerrno::JERR_JDIR_OPENDIR The %journal directory could not be
opened.
+ * \exception jerrno::JERR_JDIR_FMOVE Moving the files from the %journal directory
to the created backup
* directory failed.
- * \exception The directory handle could not be closed.
+ * \exception jerrno::JERR_JDIR_CLOSEDIR The directory handle could not be
closed.
*/
void clear_dir(const bool create_flag = true);
/**
- * \brief Clear the directory dirname of journal files matching base_filename
+ * \brief Clear the directory dirname of %journal files matching base_filename
* by moving them into a subdirectory.
*
- * \param dirname C-string containing name of journal directory.
- * \param base_filename C-string containing base filename of journal files to be
matched
+ * \param dirname C-string containing name of %journal directory.
+ * \param base_filename C-string containing base filename of %journal files to be
matched
* for moving into subdirectory.
* \param create_flag If set, create dirname if it is non-existent, otherwise
throw
* exception
*
- * \exception The journal directory could not be opened.
- * \exception The move of files from the journal directory to the created backup
+ * \exception jerrno::JERR_JDIR_OPENDIR The %journal directory could not be
opened.
+ * \exception jerrno::JERR_JDIR_FMOVE Moving the files from the %journal directory
to the created backup
* directory failed.
- * \exception The directory handle could not be closed.
+ * \exception jerrno::JERR_JDIR_CLOSEDIR The directory handle could not be
closed.
*/
static void clear_dir(const char* dirname, const char* base_filename,
const bool create_flag = true);
/**
- * \brief Clear the directory dirname of journal files matching base_filename
+ * \brief Clear the directory dirname of %journal files matching base_filename
* by moving them into a subdirectory.
*
- * \param dirname String containing name of journal directory.
- * \param base_filename String containing base filename of journal files to be
matched
+ * \param dirname String containing name of %journal directory.
+ * \param base_filename String containing base filename of %journal files to be
matched
* for moving into subdirectory.
* \param create_flag If set, create dirname if it is non-existent, otherwise
throw
* exception
*
- * \exception The journal directory could not be opened.
- * \exception The move of files from the journal directory to the created backup
+ * \exception jerrno::JERR_JDIR_OPENDIR The %journal directory could not be
opened.
+ * \exception jerrno::JERR_JDIR_FMOVE Moving the files from the %journal directory
to the created backup
* directory failed.
- * \exception The directory handle could not be closed.
+ * \exception jerrno::JERR_JDIR_CLOSEDIR The directory handle could not be
closed.
*/
static void clear_dir(const std::string& dirname, const std::string&
base_filename,
const bool create_flag = true);
/**
- * \brief ??
+ * \brief Verify that dirname is a valid %journal directory.
*
- * \exception ??
+ * The validation reads the .%jinf file, and using this information verifies that
all the expected %journal
+ * (.jdat) files are present.
+ *
+ * \exception jerrno::JERR_JDIR_NOTDIR dirname is not a directory
+ * \exception jerrno::JERR_JDIR_STAT Could not stat dirname
+ * \exception jerrno::JERR__FILEIO Error reading %jinf file
+ * \exception jerrno::JERR_JINF_CVALIDFAIL Error validating %jinf file
+ * \exception jerrno::JERR_JDIR_NOSUCHFILE Expected jdat file is missing
*/
void verify_dir();
/**
- * \brief ??
+ * \brief Verify that dirname is a valid %journal directory.
*
- * \param dirname C-string containing name of journal directory.
- * \param base_filename C-string containing base filename of journal files to be
matched
- * for moving into subdirectory.
+ * The validation reads the .%jinf file, and using this information verifies that
all the expected %journal
+ * (.jdat) files are present.
*
- * \exception ??
+ * \param dirname C-string containing name of %journal directory.
+ * \param base_filename C-string containing base filename of %journal files to be
matched for moving into sub-directory.
+ *
+ * \exception jerrno::JERR_JDIR_NOTDIR dirname is not a directory
+ * \exception jerrno::JERR_JDIR_STAT Could not stat dirname
+ * \exception jerrno::JERR__FILEIO Error reading %jinf file
+ * \exception jerrno::JERR_JINF_CVALIDFAIL Error validating %jinf file
+ * \exception jerrno::JERR_JDIR_NOSUCHFILE Expected jdat file is missing
*/
static void verify_dir(const char* dirname, const char* base_filename);
/**
- * \brief ??
+ * \brief Verify that dirname is a valid %journal directory.
*
- * \param dirname String containing name of journal directory.
- * \param base_filename String containing base filename of journal files to be
matched
- * for moving into subdirectory.
+ * The validation reads the .%jinf file, and using this information verifies that
all the expected %journal
+ * (.jdat) files are present.
*
- * \exception ??
+ * \param dirname String containing name of %journal directory.
+ * \param base_filename String containing base filename of %journal files to be
matched for moving into sub-directory.
+ *
+ * \exception jerrno::JERR_JDIR_NOTDIR dirname is not a directory
+ * \exception jerrno::JERR_JDIR_STAT Could not stat dirname
+ * \exception jerrno::JERR__FILEIO Error reading %jinf file
+ * \exception jerrno::JERR_JINF_CVALIDFAIL Error validating %jinf file
+ * \exception jerrno::JERR_JDIR_NOSUCHFILE Expected jdat file is missing
*/
static void verify_dir(const std::string& dirname, const std::string&
base_filename);
/**
- * \brief Delete the journal directory and all files and sub--directories that it
may
+ * \brief Delete the %journal directory and all files and sub--directories that it
may
* contain. This is equivilent of rm -rf.
*
* FIXME: links are not handled correctly.
*
- * \param children_only To only delete children.
+ * \param children_only If true, delete only children of dirname, but leave
dirname itself.
*
- * \exception The journal directory could not be opened.
- * \exception The move of files from the journal directory to the created backup
- * directory failed.
- * \exception The directory handle could not be closed.
+ * \exception jerrno::JERR_JDIR_OPENDIR The %journal directory could not be
opened.
+ * \exception jerrno::JERR_JDIR_STAT Could not stat dirname.
+ * \exception jerrno::JERR_JDIR_UNLINK A file could not be deleted.
+ * \exception jerrno::JERR_JDIR_BADFTYPE A dir entry is neiter a file nor a dir.
+ * \exception jerrno::JERR_JDIR_CLOSEDIR The directory handle could not be
closed.
+ * \exception jerrno::JERR_JDIR_RMDIR A directory could not be deleted.
*/
void delete_dir(bool children_only = false );
/**
- * \brief Delete the journal directory and all files and sub--directories that it
may
+ * \brief Delete the %journal directory and all files and sub--directories that it
may
* contain. This is equivilent of rm -rf.
*
* FIXME: links are not handled correctly.
*
* \param dirname C-string containing name of directory to be deleted.
- * \param children_only To only delete children.
+ * \param children_only If true, delete only children of dirname, but leave
dirname itself.
*
- * \exception The journal directory could not be opened.
- * \exception The move of files from the journal directory to the created backup
- * directory failed.
- * \exception The directory handle could not be closed.
+ * \exception jerrno::JERR_JDIR_OPENDIR The %journal directory could not be
opened.
+ * \exception jerrno::JERR_JDIR_STAT Could not stat dirname.
+ * \exception jerrno::JERR_JDIR_UNLINK A file could not be deleted.
+ * \exception jerrno::JERR_JDIR_BADFTYPE A dir entry is neiter a file nor a dir.
+ * \exception jerrno::JERR_JDIR_CLOSEDIR The directory handle could not be
closed.
+ * \exception jerrno::JERR_JDIR_RMDIR A directory could not be deleted.
*/
static void delete_dir(const char* dirname, bool children_only = false);
/**
- * \brief Delete the journal directory and all files and sub--directories that it
may
+ * \brief Delete the %journal directory and all files and sub--directories that it
may
* contain. This is equivilent of rm -rf.
*
* FIXME: links are not handled correctly.
*
* \param dirname String containing name of directory to be deleted.
- * \param children_only To only delete children.
+ * \param children_only If true, delete only children of dirname, but leave
dirname itself.
*
- * \exception The journal directory could not be opened.
- * \exception The move of files from the journal directory to the created backup
- * directory failed.
- * \exception The directory handle could not be closed.
+ * \exception jerrno::JERR_JDIR_OPENDIR The %journal directory could not be
opened.
+ * \exception jerrno::JERR_JDIR_STAT Could not stat dirname.
+ * \exception jerrno::JERR_JDIR_UNLINK A file could not be deleted.
+ * \exception jerrno::JERR_JDIR_BADFTYPE A dir entry is neiter a file nor a dir.
+ * \exception jerrno::JERR_JDIR_CLOSEDIR The directory handle could not be
closed.
+ * \exception jerrno::JERR_JDIR_RMDIR A directory could not be deleted.
*/
static void delete_dir(const std::string& dirname, bool children_only =
false);
/**
- * \brief Create bakup directory that is next in sequence and move all journal
files
+ * \brief Create bakup directory that is next in sequence and move all %journal
files
* matching base_filename into it.
*
* In directory dirname, search for existing backup directory using pattern
* "_basename.bak.XXXX" where XXXX is a hexadecimal sequence, and create
next directory
- * based on highest number found. Move all journal files which match the
base_fileaname
+ * based on highest number found. Move all %journal files which match the
base_fileaname
* parameter into this new backup directory.
*
- * \param dirname String containing name of journal directory.
- * \param base_filename String containing base filename of journal files to be
matched
+ * \param dirname String containing name of %journal directory.
+ * \param base_filename String containing base filename of %journal files to be
matched
* for moving into subdirectory.
*
- * \exception ??
+ * \exception jerrno::JERR_JDIR_OPENDIR The %journal directory could not be
opened.
+ * \exception jerrno::JERR_JDIR_CLOSEDIR The directory handle could not be
closed.
+ * \exception jerrno::JERR_JDIR_MKDIR The backup directory could not be deleted.
*/
static std::string create_bak_dir(const std::string& dirname,
const std::string& base_filename);
@@ -252,7 +280,7 @@
inline const std::string& dirname() const { return _dirname; }
/**
- * \brief Return the journal base filename name as a string.
+ * \brief Return the %journal base filename name as a string.
*/
inline const std::string& base_filename() const { return _base_filename; }
@@ -262,7 +290,7 @@
* \param name Name of file to be tested.
* \return <b><i>true</i></b> if the named file is a
directory; <b><i>false</i></b>
* otherwise.
- * \exception The stat() operation failed on the named file.
+ * \exception jerrno::JERR_JDIR_STAT Could not stat name.
*/
static bool is_dir(const char* name);
@@ -272,7 +300,7 @@
* \param name Name of file to be tested.
* \return <b><i>true</i></b> if the named file is a
directory; <b><i>false</i></b>
* otherwise.
- * \exception The stat() operation failed on the named file.
+ * \exception jerrno::JERR_JDIR_STAT Could not stat name.
*/
static bool is_dir(const std::string& name);
@@ -287,7 +315,7 @@
* \param name Name of entity to be tested.
* \return <b><i>true</i></b> if the named entity exists;
<b><i>false</i></b>
* otherwise.
- * \exception The stat() operation failed on the named entity.
+ * \exception jerrno::JERR_JDIR_STAT Could not stat name.
*/
static bool exists(const char* name);
@@ -301,12 +329,10 @@
* \param name Name of entity to be tested.
* \return <b><i>true</i></b> if the named entity exists;
<b><i>false</i></b>
* otherwise.
- * \exception The stat() operation failed on the named entity.
+ * \exception jerrno::JERR_JDIR_STAT Could not stat name.
*/
static bool exists(const std::string& name);
- public:
-
/**
* \brief Stream operator
*/
@@ -316,6 +342,21 @@
* \brief Stream operator
*/
friend std::ostream& operator<<(std::ostream& os, const jdir*
jdirPtr);
+
+ private:
+ /**
+ * \brief Check for error, if non-zero close DIR handle and throw
JERR_JDIR_READDIR
+ *
+ * \exception jerrno::JERR_JDIR_READDIR Error while reading contents of dir.
+ */
+ static void check_err(const int err_num, DIR* dir, const std::string&
dir_name, const std::string& fn_name);
+
+ /**
+ * \brief Close a DIR handle, throw JERR_JDIR_CLOSEDIR if error occurs during
close
+ *
+ * \exception jerrno::JERR_JDIR_CLOSEDIR The directory handle could not be
closed.
+ */
+ static void close_dir(DIR* dir, const std::string& dir_name, const
std::string& fn_name);
};
} // namespace journal
Modified: store/trunk/cpp/lib/jrnl/jerrno.cpp
===================================================================
--- store/trunk/cpp/lib/jrnl/jerrno.cpp 2009-12-03 10:00:25 UTC (rev 3733)
+++ store/trunk/cpp/lib/jrnl/jerrno.cpp 2009-12-03 15:42:14 UTC (rev 3734)
@@ -68,7 +68,7 @@
const u_int32_t jerrno::JERR_JDIR_NOTDIR = 0x0300;
const u_int32_t jerrno::JERR_JDIR_MKDIR = 0x0301;
const u_int32_t jerrno::JERR_JDIR_OPENDIR = 0x0302;
-//const u_int32_t jerrno::JERR_JDIR_READDIR = 0x0303;
+const u_int32_t jerrno::JERR_JDIR_READDIR = 0x0303;
const u_int32_t jerrno::JERR_JDIR_CLOSEDIR = 0x0304;
const u_int32_t jerrno::JERR_JDIR_RMDIR = 0x0305;
const u_int32_t jerrno::JERR_JDIR_NOSUCHFILE = 0x0306;
@@ -158,7 +158,7 @@
_err_map[JERR_JDIR_NOTDIR] = "JERR_JDIR_NOTDIR: Directory name exists but is not
a directory.";
_err_map[JERR_JDIR_MKDIR] = "JERR_JDIR_MKDIR: Directory creation failed.";
_err_map[JERR_JDIR_OPENDIR] = "JERR_JDIR_OPENDIR: Directory open failed.";
- //_err_map[JERR_JDIR_READDIR] = "JERR_JDIR_READDIR: Directory read
failed.";
+ _err_map[JERR_JDIR_READDIR] = "JERR_JDIR_READDIR: Directory read failed.";
_err_map[JERR_JDIR_CLOSEDIR] = "JERR_JDIR_CLOSEDIR: Directory close
failed.";
_err_map[JERR_JDIR_RMDIR] = "JERR_JDIR_RMDIR: Directory delete failed.";
_err_map[JERR_JDIR_NOSUCHFILE] = "JERR_JDIR_NOSUCHFILE: File does not
exist.";
Modified: store/trunk/cpp/lib/jrnl/jerrno.hpp
===================================================================
--- store/trunk/cpp/lib/jrnl/jerrno.hpp 2009-12-03 10:00:25 UTC (rev 3733)
+++ store/trunk/cpp/lib/jrnl/jerrno.hpp 2009-12-03 15:42:14 UTC (rev 3734)
@@ -86,7 +86,7 @@
static const u_int32_t JERR_JDIR_NOTDIR; ///< Exists but is not a
directory
static const u_int32_t JERR_JDIR_MKDIR; ///< Directory creation
failed
static const u_int32_t JERR_JDIR_OPENDIR; ///< Directory open failed
- //static const u_int32_t JERR_JDIR_READDIR; ///< Directory read failed
+ static const u_int32_t JERR_JDIR_READDIR; ///< Directory read failed
static const u_int32_t JERR_JDIR_CLOSEDIR; ///< Directory close failed
static const u_int32_t JERR_JDIR_RMDIR; ///< Directory delete failed
static const u_int32_t JERR_JDIR_NOSUCHFILE; ///< File does not exist