~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/cached_directory.cc

  • Committer: Brian Aker
  • Date: 2009-07-11 08:51:36 UTC
  • mfrom: (1089.3.11 merge)
  • Revision ID: brian@gaz-20090711085136-qj01nwm3qynghwtc
Merge Monty

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 "cached_directory.h"
 
29
 
 
30
using namespace std;
 
31
 
 
32
 
 
33
CachedDirectory::CachedDirectory(const string &in_path)
 
34
  : error(0)
 
35
{
 
36
  (void) open(in_path);
 
37
}
 
38
 
 
39
CachedDirectory::~CachedDirectory()
 
40
{
 
41
  Entries::iterator p= entries.begin();
 
42
  while (p != entries.end())
 
43
  {
 
44
    if (*p)
 
45
      delete *p;
 
46
    ++p;
 
47
  }
 
48
}
 
49
 
 
50
bool CachedDirectory::open(const string &in_path)
 
51
{
 
52
  size_t size;
 
53
  DIR *dirp;
 
54
 
 
55
  if ((dirp= opendir(in_path.c_str())) == NULL)
 
56
  {
 
57
    error= errno;
 
58
    return false;
 
59
  }
 
60
 
 
61
  /*
 
62
   * The readdir_r() call on Solaris operates a bit differently from other
 
63
   * systems in that the dirent structure must be allocated along with enough
 
64
   * space to contain the filename (see man page for readdir_r on Solaris).
 
65
   */
 
66
 
 
67
#ifdef SOLARIS
 
68
  size= sizeof(dirent) + pathconf(in_path.c_str(), _PC_NAME_MAX);
 
69
#else
 
70
  size= sizeof(dirent);
 
71
#endif
 
72
 
 
73
  dirent *entry= (dirent *) malloc(size);
 
74
 
 
75
  if (entry == NULL)
 
76
  {
 
77
    error= errno;
 
78
    closedir(dirp);
 
79
    return false;
 
80
  }
 
81
 
 
82
  int retcode;
 
83
  dirent *result;
 
84
 
 
85
  retcode= readdir_r(dirp, entry, &result);
 
86
  while ((retcode == 0) && (result != NULL))
 
87
  {
 
88
    entries.push_back(new Entry(entry->d_name));
 
89
    retcode= readdir_r(dirp, entry, &result);
 
90
  }
 
91
    
 
92
  closedir(dirp);
 
93
  free(entry);
 
94
 
 
95
  error= retcode;
 
96
 
 
97
  return error == 0;
 
98
}