~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/cached_directory.cc

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
/**
21
 
 * @file
22
 
 *   cached_directory.cc
23
 
 *
24
 
 * @brief
25
 
 *   Implementation of CachedDirectory class.
26
 
 */
27
 
 
28
 
#include "config.h"
29
 
 
30
 
#include <strings.h>
31
 
#include <limits.h>
32
 
 
33
 
#include "drizzled/cached_directory.h"
34
 
 
35
 
using namespace std;
36
 
 
37
 
namespace drizzled
38
 
{
39
 
 
40
 
CachedDirectory::CachedDirectory() : 
41
 
  error(0)
42
 
{
43
 
}
44
 
 
45
 
 
46
 
CachedDirectory::CachedDirectory(const string &in_path) :
47
 
  error(0)
48
 
{
49
 
  // TODO: Toss future exception
50
 
  (void) open(in_path);
51
 
}
52
 
 
53
 
 
54
 
CachedDirectory::CachedDirectory(const string& in_path, set<string>& allowed_exts) :
55
 
  error(0)
56
 
{
57
 
  // TODO: Toss future exception
58
 
  (void) open(in_path, allowed_exts);
59
 
}
60
 
 
61
 
 
62
 
CachedDirectory::~CachedDirectory()
63
 
{
64
 
  for (Entries::iterator p= entries.begin(); p != entries.end(); ++p)
65
 
  {
66
 
    if (*p)
67
 
      delete *p;
68
 
  }
69
 
  entries.clear();
70
 
}
71
 
 
72
 
bool CachedDirectory::open(const string &in_path)
73
 
{
74
 
  set<string> empty;
75
 
 
76
 
  return open(in_path, empty);
77
 
}
78
 
 
79
 
bool CachedDirectory::open(const string &in_path, set<string> &allowed_exts)
80
 
{
81
 
  DIR *dirp= opendir(in_path.c_str());
82
 
 
83
 
  if (dirp == NULL)
84
 
  {
85
 
    error= errno;
86
 
    return false;
87
 
  }
88
 
 
89
 
  path= in_path;
90
 
 
91
 
  union {
92
 
    dirent entry;
93
 
#ifdef __sun
94
 
    /*
95
 
     * The readdir_r() call on Solaris operates a bit differently from other
96
 
     * systems in that the dirent structure must be allocated along with enough
97
 
     * space to contain the filename (see man page for readdir_r on Solaris).
98
 
     * Instead of dynamically try to allocate this buffer, just set the max
99
 
     * name for a path instead.
100
 
     */
101
 
    char space[sizeof(dirent) + PATH_MAX + 1];
102
 
#endif
103
 
  } buffer;
104
 
 
105
 
  int retcode;
106
 
  dirent *result;
107
 
 
108
 
  while ((retcode= readdir_r(dirp, &buffer.entry, &result)) == 0 &&
109
 
         result != NULL)
110
 
  {
111
 
    if (! allowed_exts.empty())
112
 
    {
113
 
      char *ptr= rindex(result->d_name, '.');
114
 
 
115
 
      if (ptr)
116
 
      {
117
 
        set<string>::iterator it;
118
 
        it= allowed_exts.find(ptr);
119
 
 
120
 
        if (it != allowed_exts.end())
121
 
        {
122
 
          entries.push_back(new Entry(result->d_name));
123
 
        }
124
 
      }
125
 
    }
126
 
    else
127
 
    {
128
 
      entries.push_back(new Entry(result->d_name));
129
 
    }
130
 
  }
131
 
    
132
 
  closedir(dirp);
133
 
  error= retcode;
134
 
 
135
 
  return error == 0;
136
 
}
137
 
 
138
 
} /* namespace drizzled */