~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/cached_directory.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

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 <sys/types.h>
 
31
#include <sys/stat.h>
 
32
#include <unistd.h>
 
33
 
 
34
#include <strings.h>
 
35
#include <limits.h>
 
36
 
 
37
#include "drizzled/cached_directory.h"
 
38
 
 
39
using namespace std;
 
40
 
 
41
namespace drizzled
 
42
{
 
43
 
 
44
CachedDirectory::CachedDirectory() : 
 
45
  error(0)
 
46
{
 
47
}
 
48
 
 
49
 
 
50
CachedDirectory::CachedDirectory(const string &in_path) :
 
51
  error(0)
 
52
{
 
53
  // TODO: Toss future exception
 
54
  (void) open(in_path);
 
55
}
 
56
 
 
57
 
 
58
CachedDirectory::CachedDirectory(const string& in_path, set<string>& allowed_exts) :
 
59
  error(0)
 
60
{
 
61
  // TODO: Toss future exception
 
62
  (void) open(in_path, allowed_exts);
 
63
}
 
64
 
 
65
CachedDirectory::CachedDirectory(const string& in_path, enum CachedDirectory::FILTER filter) :
 
66
  error(0)
 
67
{
 
68
  set<string> empty;
 
69
  // TODO: Toss future exception
 
70
  (void) open(in_path, empty, filter);
 
71
}
 
72
 
 
73
 
 
74
CachedDirectory::~CachedDirectory()
 
75
{
 
76
  for (Entries::iterator p= entries.begin(); p != entries.end(); ++p)
 
77
  {
 
78
    if (*p)
 
79
      delete *p;
 
80
  }
 
81
  entries.clear();
 
82
}
 
83
 
 
84
bool CachedDirectory::open(const string &in_path)
 
85
{
 
86
  set<string> empty;
 
87
 
 
88
  return open(in_path, empty);
 
89
}
 
90
 
 
91
bool CachedDirectory::open(const string &in_path, set<string> &allowed_exts)
 
92
{
 
93
  return open(in_path, allowed_exts, CachedDirectory::NONE);
 
94
}
 
95
 
 
96
bool CachedDirectory::open(const string &in_path, set<string> &allowed_exts, enum CachedDirectory::FILTER filter)
 
97
{
 
98
  DIR *dirp= opendir(in_path.c_str());
 
99
 
 
100
  if (dirp == NULL)
 
101
  {
 
102
    error= errno;
 
103
    return false;
 
104
  }
 
105
 
 
106
  path= in_path;
 
107
 
 
108
  union {
 
109
    dirent entry;
 
110
#ifdef __sun
 
111
    /*
 
112
     * The readdir_r() call on Solaris operates a bit differently from other
 
113
     * systems in that the dirent structure must be allocated along with enough
 
114
     * space to contain the filename (see man page for readdir_r on Solaris).
 
115
     * Instead of dynamically try to allocate this buffer, just set the max
 
116
     * name for a path instead.
 
117
     */
 
118
    char space[sizeof(dirent) + PATH_MAX + 1];
 
119
#endif
 
120
  } buffer;
 
121
 
 
122
  int retcode;
 
123
  dirent *result;
 
124
 
 
125
  while ((retcode= readdir_r(dirp, &buffer.entry, &result)) == 0 &&
 
126
         result != NULL)
 
127
  {
 
128
    if (! allowed_exts.empty())
 
129
    {
 
130
      char *ptr= rindex(result->d_name, '.');
 
131
 
 
132
      if (ptr)
 
133
      {
 
134
        set<string>::iterator it;
 
135
        it= allowed_exts.find(ptr);
 
136
 
 
137
        if (it != allowed_exts.end())
 
138
        {
 
139
          entries.push_back(new Entry(result->d_name));
 
140
        }
 
141
      }
 
142
    }
 
143
    else
 
144
    {
 
145
      switch (filter)
 
146
      {
 
147
      case DIRECTORY:
 
148
        {
 
149
          struct stat entrystat;
 
150
 
 
151
          if (result->d_name[0] == '.')
 
152
            continue;
 
153
 
 
154
          stat(result->d_name, &entrystat);
 
155
 
 
156
          if (S_ISDIR(entrystat.st_mode))
 
157
          {
 
158
            entries.push_back(new Entry(result->d_name));
 
159
          }
 
160
        }
 
161
        break;
 
162
      case FILE:
 
163
        {
 
164
          struct stat entrystat;
 
165
 
 
166
          stat(result->d_name, &entrystat);
 
167
 
 
168
          if (S_ISREG(entrystat.st_mode))
 
169
          {
 
170
            entries.push_back(new Entry(result->d_name));
 
171
          }
 
172
        }
 
173
        break;
 
174
      case NONE:
 
175
      case MAX:
 
176
        entries.push_back(new Entry(result->d_name));
 
177
        break;
 
178
      }
 
179
    }
 
180
  }
 
181
    
 
182
  closedir(dirp);
 
183
  error= retcode;
 
184
 
 
185
  return error == 0;
 
186
}
 
187
 
 
188
} /* namespace drizzled */