List:Commits« Previous MessageNext Message »
From:Magnus Blaudd Date:September 9 2009 10:35am
Subject:bzr commit into mysql-5.1-telco-7.0 branch (magnus.blaudd:2995)
View as plain text  
#At file:///Users/magnus/mysql/7.0-test-mgmd/ based on revid:magnus.blaudd@stripped

 2995 Magnus Blaudd	2009-09-09 [merge]
      Merge

    renamed:
      storage/ndb/src/mgmsrv/DirIterator.cpp => storage/ndb/src/common/portlib/NdbDir.cpp
      storage/ndb/src/mgmsrv/DirIterator.hpp => storage/ndb/include/portlib/NdbDir.hpp
    modified:
      storage/ndb/src/common/portlib/CMakeLists.txt
      storage/ndb/src/common/portlib/Makefile.am
      storage/ndb/src/mgmsrv/CMakeLists.txt
      storage/ndb/src/mgmsrv/ConfigManager.cpp
      storage/ndb/src/mgmsrv/Makefile.am
      storage/ndb/src/common/portlib/NdbDir.cpp
      storage/ndb/include/portlib/NdbDir.hpp
=== renamed file 'storage/ndb/src/mgmsrv/DirIterator.hpp' => 'storage/ndb/include/portlib/NdbDir.hpp'
--- a/storage/ndb/src/mgmsrv/DirIterator.hpp	2008-10-24 12:41:10 +0000
+++ b/storage/ndb/include/portlib/NdbDir.hpp	2009-09-08 20:08:24 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2008 Sun Microsystems, Inc.
+/* Copyright (C) 2008-2009 Sun Microsystems, Inc.
 
    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
@@ -13,17 +13,53 @@
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
-#ifndef DirIterator_HPP
-#define DirIterator_HPP
+#ifndef NdbDir_HPP
+#define NdbDir_HPP
 
-class DirIterator {
-  class DirIteratorImpl& m_impl;
+class NdbDir {
 public:
-  DirIterator();
-  ~DirIterator();
+  class Iterator {
+    class DirIteratorImpl& m_impl;
+    Iterator(const Iterator&);  // not impl
+    Iterator& operator=(const Iterator&); // not impl
+  public:
+    Iterator();
+    ~Iterator();
+
+    int open(const char* path);
+    void close(void);
+    const char* next_file(void);
+    const char* next_entry(void);
+  };
+
+  class Temp {
+    const char* m_path;
+    Temp(const Temp&);  // not impl
+    Temp& operator=(const Temp&); // not impl
+  public:
+    Temp();
+    ~Temp();
+    const char* path(void) const;
+  };
+
+  /*
+    Create directory
+  */
+  static bool create(const char *path);
+
+  /*
+    Remove directory recursively
+      dir - path to directory that should be removed
+      only_contents - only remove the contents of the directory
+
+  */
+  static bool remove_recursive(const char* path, bool only_contents = false);
+
+  /*
+    Remove empty directory
+  */
+  static bool remove(const char* path);
 
-  int open(const char* path);
-  const char* next_file(void);
 };
 
 #endif

=== modified file 'storage/ndb/src/common/portlib/CMakeLists.txt'
--- a/storage/ndb/src/common/portlib/CMakeLists.txt	2009-01-23 12:50:40 +0000
+++ b/storage/ndb/src/common/portlib/CMakeLists.txt	2009-09-08 20:08:24 +0000
@@ -28,12 +28,19 @@ ENDIF(WIN32)
 ADD_LIBRARY(ndbportlib STATIC
             NdbCondition.c NdbMutex.c
             NdbEnv.c NdbThread.c NdbHost.c NdbTCP.cpp
-            NdbMem.c NdbConfig.c NdbTick.c
+            NdbMem.c NdbConfig.c NdbTick.c NdbDir.cpp
             my_daemon.cc ${CMAKE_SOURCE_DIR}/sql/nt_servc.cc
             ${EXTRA_SRC}
 )
 TARGET_LINK_LIBRARIES(ndbportlib mysys ${EXTRA_LIB})
 
+ADD_EXECUTABLE(NdbDir-t
+  NdbDir.cpp
+  ${CMAKE_SOURCE_DIR}/storage/ndb/src/common/util/basestring_vsnprintf.c)
+SET_TARGET_PROPERTIES(NdbDir-t
+                      PROPERTIES COMPILE_FLAGS "-DTEST_NDBDIR")
+TARGET_LINK_LIBRARIES(NdbDir-t ndbportlib)
+
 #ADD_EXECUTABLE(memtest memtest.c)
 #TARGET_LINK_LIBRARIES(memtest ndbportlib wsock32)
 

=== modified file 'storage/ndb/src/common/portlib/Makefile.am'
--- a/storage/ndb/src/common/portlib/Makefile.am	2009-05-27 15:21:45 +0000
+++ b/storage/ndb/src/common/portlib/Makefile.am	2009-09-08 20:08:24 +0000
@@ -22,7 +22,7 @@ libportlib_la_SOURCES = \
 	NdbCondition.c NdbMutex.c NdbTick.c \
 	NdbEnv.c NdbThread.c NdbHost.c NdbTCP.cpp	    \
 	NdbDaemon.c my_daemon.cc NdbMem.c \
-	NdbConfig.c
+	NdbConfig.c NdbDir.cpp
 
 include $(top_srcdir)/storage/ndb/config/common.mk.am
 include $(top_srcdir)/storage/ndb/config/type_util.mk.am
@@ -34,4 +34,14 @@ EXTRA_PROGRAMS = memtest PortLibTest mun
 PortLibTest_SOURCES = NdbPortLibTest.cpp
 munmaptest_SOURCES = munmaptest.cpp
 
+noinst_PROGRAMS = NdbDir-t
+
+NdbDir_t_SOURCES = NdbDir.cpp \
+       $(top_srcdir)/storage/ndb/src/common/util/basestring_vsnprintf.c
+NdbDir_t_CXXFLAGS = -DTEST_NDBDIR
+NdbDir_t_LDADD = \
+       libportlib.la \
+       $(top_builddir)/mysys/libmysyslt.la
+
+
 %::SCCS/s.%

=== renamed file 'storage/ndb/src/mgmsrv/DirIterator.cpp' => 'storage/ndb/src/common/portlib/NdbDir.cpp'
--- a/storage/ndb/src/mgmsrv/DirIterator.cpp	2008-11-06 17:17:12 +0000
+++ b/storage/ndb/src/common/portlib/NdbDir.cpp	2009-09-08 20:08:24 +0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 2008 Sun Microsystems, Inc.
+/* Copyright (C) 2008-2009 Sun Microsystems, Inc.
 
    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
@@ -13,17 +13,16 @@
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
-#include "DirIterator.hpp"
-#include <stdio.h>
+#include <ndb_global.h>
+#include <NdbDir.hpp>
+
+#include <util/basestring_vsnprintf.h>
 
 #ifndef __WIN__
 
 #include <dirent.h>
-
 #include <sys/stat.h>
 
-#include <util/BaseString.hpp>
-
 class DirIteratorImpl {
   DIR* m_dirp;
   const char *m_path;
@@ -38,11 +37,12 @@ class DirIteratorImpl {
       return (dp->d_type == DT_REG);
 #endif
     /* Using stat to read more info about the file */
-    BaseString fullpath;
-    fullpath.assfmt("%s/%s", m_path, dp->d_name);
+    char path_buf[PATH_MAX];
+    basestring_snprintf(path_buf, sizeof(path_buf),
+                        "%s/%s", m_path, dp->d_name);
 
     struct stat buf;
-    if (stat(fullpath.c_str(), &buf))
+    if (stat(path_buf, &buf))
       return false; // 'stat' failed
 
     return S_ISREG(buf.st_mode);
@@ -54,7 +54,7 @@ public:
     m_dirp(NULL) {};
 
   ~DirIteratorImpl() {
-    closedir(m_dirp);
+    close();
   }
 
   int open(const char* path){
@@ -65,19 +65,27 @@ public:
     return 0;
   }
 
-  const char* next_file(void){
-    struct dirent* dp;
-    while ((dp = readdir(m_dirp)) != NULL &&
-           !is_regular_file(dp))
-      ;
-    return dp ? dp->d_name : NULL;
+  void close(void)
+  {
+    if (m_dirp)
+      closedir(m_dirp);
+    m_dirp = NULL;
+  }
+
+  const char* next_entry(bool& is_directory)
+  {
+    struct dirent* dp = readdir(m_dirp);
+
+    if (dp == NULL)
+      return NULL;
+
+    is_directory = !is_regular_file(dp);
+    return dp->d_name;
   }
 };
 
 #else
 
-#include <BaseString.hpp>
-
 class DirIteratorImpl {
   bool m_first;
   WIN32_FIND_DATA m_find_data;
@@ -93,13 +101,14 @@ public:
     m_find_handle(INVALID_HANDLE_VALUE) {};
 
   ~DirIteratorImpl() {
-    FindClose(m_find_handle);
+    close();
   }
 
   int open(const char* path){
-    BaseString path_buf;
-    path_buf.assfmt("%s\\*", path);
-    m_find_handle = FindFirstFile(path_buf.c_str(), &m_find_data);
+    char path_buf[PATH_MAX+2];
+    m_first = true;
+    basestring_snprintf(path_buf, sizeof(path_buf), "%s\\*", path);
+    m_find_handle = FindFirstFile(path_buf, &m_find_data);
     if(m_find_handle == INVALID_HANDLE_VALUE)
     {
       if (GetLastError() == ERROR_FILE_NOT_FOUND)
@@ -110,40 +119,289 @@ public:
     return 0;
   }
 
-  const char* next_file(void){
-	  while(m_first || FindNextFile(m_find_handle, &m_find_data))
+  void close(void)
+  {
+    if (m_find_handle)
+      FindClose(m_find_handle);
+    m_find_handle = NULL;
+  }
+
+  const char* next_entry(bool& is_directory)
+  {
+    if (m_first || FindNextFile(m_find_handle, &m_find_data))
     {
-      m_first= false;
-      
-	    if (!is_dir(m_find_data))
-        return m_find_data.cFileName;
+      m_first = false;
+      is_directory = is_dir(m_find_data);
+      return m_find_data.cFileName;
     }
-    return NULL;    
+    return NULL;
   }
-
 };
 
 #endif
 
 
-DirIterator::DirIterator() :
+NdbDir::Iterator::Iterator() :
   m_impl(*new DirIteratorImpl())
 {
 }
 
-DirIterator::~DirIterator()
+NdbDir::Iterator::~Iterator()
 {
   delete &m_impl;
 }
 
 
-int DirIterator::open(const char* path)
+int NdbDir::Iterator::open(const char* path)
 {
   return m_impl.open(path);
 }
 
-const char* DirIterator::next_file(void)
+void NdbDir::Iterator::close(void)
+{
+  m_impl.close();
+}
+
+const char* NdbDir::Iterator::next_file(void)
+{
+  bool is_dir;
+  while(m_impl.next_entry(is_dir) != NULL && is_dir)
+    ;
+  return NULL;
+}
+
+const char* NdbDir::Iterator::next_entry(void)
+{
+  bool is_dir;
+  return m_impl.next_entry(is_dir);
+}
+
+
+bool
+NdbDir::create(const char *dir)
+{
+#ifdef _WIN32
+  if (CreateDirectory(dir, NULL) == 0)
+  {
+    fprintf(stderr,
+            "Failed to create directory '%s', error: %d",
+            dir, GetLastError());
+    return false;
+  }
+#else
+  if (mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR) != 0)
+  {
+    fprintf(stderr,
+            "Failed to create directory '%s', error: %d",
+            dir, errno);
+    return false;
+  }
+#endif
+  return true;
+}
+
+
+NdbDir::Temp::Temp()
 {
-  return m_impl.next_file();
+#ifdef _WIN32
+  DWORD len = GetTempPath(0, NULL);
+  m_path = new char[len];
+  if (GetTempPath(len, m_path) == 0)
+    abort();
+#else
+  char* tmp = getenv("TMPDIR");
+  if (tmp)
+    m_path = tmp;
+  else
+    m_path = "/tmp/";
+#endif
 }
 
+NdbDir::Temp::~Temp()
+{
+#ifdef _WIN32
+  delete [] m_path;
+#endif
+}
+
+
+const char*
+NdbDir::Temp::path(void) const {
+  return m_path;
+}
+
+
+bool
+NdbDir::remove(const char* path)
+{
+#ifdef _WIN32
+  if (RemoveDirectory(path) != 0)
+    return true; // Gone
+#else
+  if (rmdir(path) == 0)
+    return true; // Gone
+#endif
+  return false;
+}
+
+bool
+NdbDir::remove_recursive(const char* dir, bool only_contents)
+{
+  char path[PATH_MAX];
+  if (basestring_snprintf(path, sizeof(path),
+                          "%s%s", dir, DIR_SEPARATOR) < 0) {
+    fprintf(stderr, "Too long path to remove: '%s'\n", dir);
+    return false;
+  }
+  int start_len = strlen(path);
+
+  const char* name;
+  NdbDir::Iterator iter;
+loop:
+  {
+    if (iter.open(path) != 0)
+    {
+      fprintf(stderr, "Failed to open iterator for '%s'\n",
+              path);
+      return false;
+    }
+
+    while ((name = iter.next_entry()) != NULL)
+    {
+      if ((strcmp(".", name) == 0) || (strcmp("..", name) == 0))
+        continue;
+
+      int end_len, len = strlen(path);
+      if ((end_len = basestring_snprintf(path + len, sizeof(path) - len,
+                                         "%s", name)) < 0)
+      {
+        fprintf(stderr, "Too long path detected: '%s'+'%s'\n",
+                path, name);
+        return false;
+      }
+
+      if (unlink(path) == 0 || NdbDir::remove(path) == true)
+      {
+        path[len] = 0;
+        continue;
+      }
+
+      iter.close();
+
+      if (basestring_snprintf(path + end_len, sizeof(path) - end_len,
+                   "%s", DIR_SEPARATOR) < 0)
+      {
+        fprintf(stderr, "Too long path detected: '%s'+'%s'\n",
+                path, DIR_SEPARATOR);
+        return false;
+      }
+
+      goto loop;
+    }
+    iter.close();
+
+    int len = strlen(path);
+    path[len - 1] = 0; // remove ending slash
+
+    char * prev_slash = strrchr(path, IF_WIN('\\', '/'));
+    if (len > start_len && prev_slash)
+    {
+      // Not done yet, step up one dir level
+      assert(prev_slash > path && prev_slash < path + sizeof(path));
+      prev_slash[1] = 0;
+      goto loop;
+    }
+  }
+
+  if (only_contents == false && NdbDir::remove(dir) == false)
+  {
+    fprintf(stderr,
+            "Failed to remove directory '%s', error: %d\n",
+            dir, errno);
+    return false;
+  }
+
+  return true;
+}
+
+
+#ifdef TEST_NDBDIR
+#include <NdbTap.hpp>
+
+#define CHECK(x) \
+  if (!(x)) {					       \
+    fprintf(stderr, "failed at line %d\n",  __LINE__ );	       \
+    abort(); }
+
+static void
+build_tree(const char* path)
+{
+  char tmp[PATH_MAX];
+  CHECK(NdbDir::create(path));
+
+  // Create files in path/
+  for (int i = 8; i < 14; i++){
+    basestring_snprintf(tmp, sizeof(tmp), "%s%sfile%d", path, DIR_SEPARATOR, i);
+    fclose(fopen(tmp, "w"));
+  }
+
+  // Create directories
+  for (int i = 8; i < 14; i++){
+    basestring_snprintf(tmp, sizeof(tmp), "%s%sdir%d", path, DIR_SEPARATOR, i);
+    CHECK(NdbDir::create(tmp));
+
+    // Create files in dir
+    for (int j = 0; j < 6; j++){
+      basestring_snprintf(tmp, sizeof(tmp), "%s%sdir%d%sfile%d",
+	       path, DIR_SEPARATOR, i, DIR_SEPARATOR, j);
+      fclose(fopen(tmp, "w"));
+    }
+  }
+
+#ifndef _WIN32
+  // Symlink the last file created to path/symlink
+  char tmp2[PATH_MAX];
+  basestring_snprintf(tmp2, sizeof(tmp2), "%s%ssymlink", path, DIR_SEPARATOR);
+  CHECK(symlink(tmp, tmp2) == 0);
+#endif
+}
+
+static bool
+gone(const char *dir) {
+  return (access(dir, F_OK) == -1 && errno == ENOENT);
+}
+
+TAPTEST(DirIterator)
+{
+  NdbDir::Temp tempdir;
+  char path[PATH_MAX];
+  basestring_snprintf(path, sizeof(path),"%s%s%s",
+                      tempdir.path(), DIR_SEPARATOR, "ndbdir_test");
+
+  printf("Using directory '%s'\n", path);
+
+  // Build dir tree and remove all of it
+  build_tree(path);
+  CHECK(NdbDir::remove_recursive(path));
+  CHECK(gone(path));
+
+  // Remove non exisiting directory
+  CHECK(!NdbDir::remove_recursive(path));
+  CHECK(gone(path));
+
+  // Build dir tree and remove everything inside it
+  build_tree(path);
+  CHECK(NdbDir::remove_recursive(path, true));
+  CHECK(!gone(path));
+
+  // Remoe also the empty dir
+  CHECK(NdbDir::remove_recursive(path));
+  CHECK(gone(path));
+
+  // Remove non exisiting directory(again)
+  CHECK(!NdbDir::remove_recursive(path));
+  CHECK(gone(path));
+
+  return 1; // OK
+}
+#endif

=== modified file 'storage/ndb/src/mgmsrv/CMakeLists.txt'
--- a/storage/ndb/src/mgmsrv/CMakeLists.txt	2008-10-24 12:41:10 +0000
+++ b/storage/ndb/src/mgmsrv/CMakeLists.txt	2009-09-08 20:08:24 +0000
@@ -41,6 +41,5 @@ ADD_EXECUTABLE(ndb_mgmd
                MgmtSrvr.cpp
                main.cpp
                Services.cpp
-               ConfigManager.cpp
-               DirIterator.cpp)
+               ConfigManager.cpp)
 TARGET_LINK_LIBRARIES(ndb_mgmd ndbconf)

=== modified file 'storage/ndb/src/mgmsrv/ConfigManager.cpp'
--- a/storage/ndb/src/mgmsrv/ConfigManager.cpp	2009-06-10 10:16:30 +0000
+++ b/storage/ndb/src/mgmsrv/ConfigManager.cpp	2009-09-08 20:08:24 +0000
@@ -16,7 +16,7 @@
 
 #include "ConfigManager.hpp"
 #include "MgmtSrvr.hpp"
-#include "DirIterator.hpp"
+#include <NdbDir.hpp>
 
 #include <NdbConfig.h>
 #include <NdbSleep.h>
@@ -130,7 +130,7 @@ NodeId
 ConfigManager::find_nodeid_from_configdir(void)
 {
   BaseString config_name;
-  DirIterator iter;
+  NdbDir::Iterator iter;
 
   if (iter.open(m_configdir) != 0)
     return 0;
@@ -1815,7 +1815,7 @@ delete_file(const char* file_name)
 bool
 ConfigManager::delete_saved_configs(void) const
 {
-  DirIterator iter;
+  NdbDir::Iterator iter;
 
   if (iter.open(m_configdir) != 0)
     return false;
@@ -1857,7 +1857,7 @@ ConfigManager::delete_saved_configs(void
 bool
 ConfigManager::saved_config_exists(BaseString& config_name) const
 {
-  DirIterator iter;
+  NdbDir::Iterator iter;
 
   if (iter.open(m_configdir) != 0)
     return false;
@@ -1895,7 +1895,7 @@ ConfigManager::saved_config_exists(BaseS
 bool
 ConfigManager::failed_config_change_exists() const
 {
-  DirIterator iter;
+  NdbDir::Iterator iter;
 
   if (iter.open(m_configdir) != 0)
     return false;

=== modified file 'storage/ndb/src/mgmsrv/Makefile.am'
--- a/storage/ndb/src/mgmsrv/Makefile.am	2009-05-27 15:21:45 +0000
+++ b/storage/ndb/src/mgmsrv/Makefile.am	2009-09-08 20:08:24 +0000
@@ -31,8 +31,7 @@ ndb_mgmd_SOURCES = \
 	InitConfigFileParser.cpp \
 	Config.cpp \
 	mgm_ndbinfo.cpp \
-	ConfigManager.cpp \
-	DirIterator.cpp
+	ConfigManager.cpp
 
 noinst_PROGRAMS = testConfig
 


Attachment: [text/bzr-bundle] bzr/magnus.blaudd@sun.com-20090909103532-b2a4s0lbrm4pno6g.bundle
Thread
bzr commit into mysql-5.1-telco-7.0 branch (magnus.blaudd:2995)Magnus Blaudd9 Sep