~ubuntu-branches/ubuntu/intrepid/schroot/intrepid

« back to all changes in this revision

Viewing changes to sbuild/sbuild-dirstream.cc

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2006-07-08 18:33:28 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060708183328-rlo4mpldmyoda55q
Tags: 0.99.2-2ubuntu1
* remerge ubuntu changes:
  + debian/control: libpam-dev (>> 0.79-3ubuntu6)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright © 2003,2006  Roger Leigh <rleigh@debian.org>
 
2
 *
 
3
 * schroot is free software; you can redistribute it and/or modify it
 
4
 * under the terms of the GNU General Public License as published by
 
5
 * the Free Software Foundation; either version 2 of the License, or
 
6
 * (at your option) any later version.
 
7
 *
 
8
 * schroot is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program; if not, write to the Free Software
 
15
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 
16
 * MA  02111-1307  USA
 
17
 *
 
18
 *********************************************************************/
 
19
 
 
20
#include <config.h>
 
21
 
 
22
#include "sbuild-dirstream.h"
 
23
 
 
24
#include <cerrno>
 
25
#include <cstring>
 
26
 
 
27
using namespace sbuild;
 
28
 
 
29
namespace
 
30
{
 
31
 
 
32
  typedef std::pair<dirstream::error_code,const char *> emap;
 
33
 
 
34
  /**
 
35
   * This is a list of the supported error codes.  It's used to
 
36
   * construct the real error codes map.
 
37
   */
 
38
  emap init_errors[] =
 
39
    {
 
40
      emap(dirstream::DIR_OPEN,    N_("Failed to open directory")),
 
41
      emap(dirstream::DIR_READ,    N_("Failed to read directory"))
 
42
    };
 
43
 
 
44
}
 
45
 
 
46
template<>
 
47
custom_error<dirstream::error_code>::map_type
 
48
custom_error<dirstream::error_code>::error_strings
 
49
(init_errors,
 
50
 init_errors + (sizeof(init_errors) / sizeof(init_errors[0])));
 
51
 
 
52
 
 
53
dirstream::dirstream(std::string const& dirname):
 
54
  dirname(),
 
55
  dir(0),
 
56
  data(),
 
57
  error_status(true),
 
58
  eof_status(true)
 
59
{
 
60
  open(dirname);
 
61
}
 
62
 
 
63
 
 
64
dirstream::~dirstream()
 
65
{
 
66
  close();
 
67
}
 
68
 
 
69
void
 
70
dirstream::open(std::string const& dirname)
 
71
{
 
72
  this->dir = opendir(dirname.c_str());
 
73
  if (this->dir == 0)
 
74
    {
 
75
      this->dirname.clear();
 
76
      this->error_status = true;
 
77
      this->eof_status = true;
 
78
      throw error(dirname, DIR_OPEN, errno);
 
79
    }
 
80
  this->dirname = dirname;
 
81
  this->error_status = false;
 
82
  this->eof_status = false;
 
83
  read();
 
84
}
 
85
 
 
86
void
 
87
dirstream::read(int quantity)
 
88
{
 
89
  int i;
 
90
 
 
91
  if (this->dir == 0)
 
92
    return;
 
93
 
 
94
  for (i = 0; i < quantity; ++i)
 
95
    {
 
96
      struct dirent* entry;
 
97
      errno = 0;
 
98
      entry = readdir(dir);
 
99
 
 
100
      if (entry == 0) // EOF or error
 
101
        {
 
102
          //std::cerr << "Directory read error: ";
 
103
          if (errno) // error
 
104
            {
 
105
              this->error_status = true;
 
106
              throw error(this->dirname, DIR_READ, errno);
 
107
            }
 
108
          return;
 
109
        }
 
110
 
 
111
      direntry newentry(entry); // make a direntry
 
112
      this->data.push_back(newentry); // push onto the end of the list
 
113
    }
 
114
}
 
115
 
 
116
// close the directory
 
117
// this also clears all the direntry data
 
118
void
 
119
dirstream::close()
 
120
{
 
121
  if (this->dir)
 
122
    closedir(this->dir); // don't throw an exception on failure -- it could
 
123
                   // be called in the destructor
 
124
  this->dir = 0;
 
125
  this->data.clear();    // clear all data
 
126
  this->dirname.clear();
 
127
  this->error_status = true;
 
128
  this->eof_status = true;
 
129
}
 
130
 
 
131
 
 
132
bool
 
133
dirstream::eof() const
 
134
{
 
135
  return this->eof_status;
 
136
}
 
137
 
 
138
bool
 
139
dirstream::bad() const
 
140
{
 
141
  return this->error_status;
 
142
}
 
143
 
 
144
sbuild::dirstream::operator bool ()
 
145
{
 
146
  return !(bad() || eof());
 
147
}
 
148
 
 
149
bool
 
150
sbuild::dirstream::operator ! ()
 
151
{
 
152
  return bad() || eof();
 
153
}
 
154
 
 
155
 
 
156
dirstream&
 
157
sbuild::operator >> (dirstream& stream,
 
158
                     direntry&  entry)
 
159
{
 
160
  stream.read(); // read a new entry
 
161
  if (stream && !stream.data.empty()) // not at end of file or bad.
 
162
    {
 
163
      entry = stream.data.front(); // assign next direntry to entry
 
164
      stream.data.pop_front(); // remove the entry
 
165
    }
 
166
  else // blank the direntry and set EOF status
 
167
    {
 
168
      std::memset(&entry, 0, sizeof(direntry));
 
169
      stream.eof_status = true;
 
170
    }
 
171
 
 
172
  return stream;
 
173
}