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

« back to all changes in this revision

Viewing changes to sbuild/sbuild-keyfile.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 © 2005-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-keyfile.h"
 
23
 
 
24
#include <fstream>
 
25
 
 
26
#include <boost/format.hpp>
 
27
 
 
28
using boost::format;
 
29
using namespace sbuild;
 
30
 
 
31
keyfile::keyfile ():
 
32
  groups(),
 
33
  separator(',')
 
34
{
 
35
}
 
36
 
 
37
keyfile::keyfile (std::string const& file):
 
38
  groups(),
 
39
  separator(',')
 
40
{
 
41
  std::ifstream fs(file.c_str());
 
42
  if (fs)
 
43
    {
 
44
      fs.imbue(std::locale("C"));
 
45
      fs >> *this;
 
46
    }
 
47
  else
 
48
    {
 
49
      throw error(parse_error::BAD_FILE, file);
 
50
    }
 
51
}
 
52
 
 
53
keyfile::keyfile (std::istream& stream):
 
54
  groups(),
 
55
  separator(',')
 
56
{
 
57
  stream >> *this;
 
58
}
 
59
 
 
60
keyfile::~keyfile()
 
61
{
 
62
}
 
63
 
 
64
string_list
 
65
keyfile::get_groups () const
 
66
{
 
67
  string_list ret;
 
68
 
 
69
  for (group_map_type::const_iterator pos = this->groups.begin();
 
70
       pos != this->groups.end();
 
71
       ++pos)
 
72
    ret.push_back(pos->first);
 
73
 
 
74
  return ret;
 
75
}
 
76
 
 
77
string_list
 
78
keyfile::get_keys (std::string const& group) const
 
79
{
 
80
  string_list ret;
 
81
 
 
82
  const group_type *found_group = find_group(group);
 
83
  if (found_group)
 
84
    {
 
85
      item_map_type const& items(std::tr1::get<1>(*found_group));
 
86
      for (item_map_type::const_iterator pos = items.begin();
 
87
           pos != items.end();
 
88
           ++pos)
 
89
        ret.push_back(pos->first);
 
90
    }
 
91
 
 
92
  return ret;
 
93
}
 
94
 
 
95
bool
 
96
keyfile::has_group (std::string const& group) const
 
97
{
 
98
  return (find_group(group) != 0);
 
99
}
 
100
 
 
101
bool
 
102
keyfile::has_key (std::string const& group,
 
103
                  std::string const& key) const
 
104
{
 
105
  return (find_item(group, key) != 0);
 
106
}
 
107
 
 
108
void
 
109
keyfile::set_group (std::string const& group,
 
110
                    std::string const& comment)
 
111
{
 
112
  set_group(group, comment, 0);
 
113
}
 
114
 
 
115
void
 
116
keyfile::set_group (std::string const& group,
 
117
                    std::string const& comment,
 
118
                    unsigned int       line)
 
119
{
 
120
  if (!has_group(group))
 
121
    this->groups.insert
 
122
      (group_map_type::value_type(group,
 
123
                                  group_type(group,
 
124
                                             item_map_type(),
 
125
                                             comment,
 
126
                                             line)));
 
127
}
 
128
 
 
129
std::string
 
130
keyfile::get_comment (std::string const& group) const
 
131
{
 
132
  const keyfile::group_type *found_group = find_group(group);
 
133
  if (found_group)
 
134
    return std::tr1::get<2>(*found_group);
 
135
  else
 
136
    return std::string();
 
137
}
 
138
 
 
139
std::string
 
140
keyfile::get_comment (std::string const& group,
 
141
                      std::string const& key) const
 
142
{
 
143
  const item_type *found_item = find_item(group, key);
 
144
  if (found_item)
 
145
      return std::tr1::get<2>(*found_item);
 
146
  else
 
147
    return std::string();
 
148
}
 
149
 
 
150
unsigned int
 
151
keyfile::get_line (std::string const& group) const
 
152
{
 
153
  const keyfile::group_type *found_group = find_group(group);
 
154
  if (found_group)
 
155
    return std::tr1::get<3>(*found_group);
 
156
  else
 
157
    return 0;
 
158
}
 
159
 
 
160
unsigned int
 
161
keyfile::get_line (std::string const& group,
 
162
                   std::string const& key) const
 
163
{
 
164
  const item_type *found_item = find_item(group, key);
 
165
  if (found_item)
 
166
      return std::tr1::get<3>(*found_item);
 
167
  else
 
168
    return 0;
 
169
}
 
170
 
 
171
bool
 
172
keyfile::get_locale_string (std::string const& group,
 
173
                            std::string const& key,
 
174
                            std::string&       value) const
 
175
{
 
176
  std::string localename = std::locale("").name();
 
177
  std::string::size_type pos;
 
178
  bool status = false;
 
179
 
 
180
  // Strip off any charset.
 
181
  if ((pos = localename.find_first_of('.')) != std::string::npos)
 
182
    localename = localename.substr(0, pos);
 
183
  status = get_locale_string(group, key, localename, value);
 
184
 
 
185
  // Strip off territory.
 
186
  if (status == false &&
 
187
      (pos = localename.find_first_of('_')) != std::string::npos)
 
188
    {
 
189
      localename = localename.substr(0, pos);
 
190
      status = get_locale_string(group, key, localename, value);
 
191
    }
 
192
 
 
193
  // Fall back to non-localised version.
 
194
  if (status == false)
 
195
    status = get_value(group, key, value);
 
196
 
 
197
  return status;
 
198
}
 
199
 
 
200
bool
 
201
keyfile::get_locale_string (std::string const& group,
 
202
                            std::string const& key,
 
203
                            priority           priority,
 
204
                            std::string&       value) const
 
205
{
 
206
  bool status = get_locale_string(group, key, value);
 
207
  check_priority(group, key, priority, status);
 
208
  return status;
 
209
}
 
210
 
 
211
bool
 
212
keyfile::get_locale_string (std::string const& group,
 
213
                            std::string const& key,
 
214
                            std::string const& locale,
 
215
                            std::string&       value) const
 
216
{
 
217
  std::string lkey = key + '[' + locale + ']';
 
218
  return get_value(group, lkey, value);
 
219
}
 
220
 
 
221
bool
 
222
keyfile::get_locale_string (std::string const& group,
 
223
                            std::string const& key,
 
224
                            std::string const& locale,
 
225
                            priority           priority,
 
226
                            std::string&       value) const
 
227
{
 
228
  bool status = get_locale_string(group, key, locale, value);
 
229
  check_priority(group, key, priority, status);
 
230
  return status;
 
231
}
 
232
 
 
233
void
 
234
keyfile::remove_group (std::string const& group)
 
235
{
 
236
  group_map_type::iterator pos = this->groups.find(group);
 
237
  if (pos != this->groups.end())
 
238
    this->groups.erase(pos);
 
239
}
 
240
 
 
241
void
 
242
keyfile::remove_key (std::string const& group,
 
243
                     std::string const& key)
 
244
{
 
245
  group_type *found_group = find_group(group);
 
246
  if (found_group)
 
247
    {
 
248
      item_map_type& items = std::tr1::get<1>(*found_group);
 
249
      item_map_type::iterator pos = items.find(key);
 
250
      if (pos != items.end())
 
251
        items.erase(pos);
 
252
    }
 
253
}
 
254
 
 
255
keyfile&
 
256
keyfile::operator += (keyfile const& rhs)
 
257
{
 
258
  for (group_map_type::const_iterator gp = rhs.groups.begin();
 
259
       gp != rhs.groups.end();
 
260
       ++gp)
 
261
    {
 
262
      group_type const& group = gp->second;
 
263
      std::string const& groupname = std::tr1::get<0>(group);
 
264
      std::string const& comment = std::tr1::get<2>(group);
 
265
      unsigned int const& line = std::tr1::get<3>(group);
 
266
      set_group(groupname, comment, line);
 
267
 
 
268
      item_map_type const& items(std::tr1::get<1>(group));
 
269
      for (item_map_type::const_iterator it = items.begin();
 
270
           it != items.end();
 
271
           ++it)
 
272
        {
 
273
          item_type const& item = it->second;
 
274
          std::string const& key(std::tr1::get<0>(item));
 
275
          std::string const& value(std::tr1::get<1>(item));
 
276
          std::string const& comment(std::tr1::get<2>(item));
 
277
          unsigned int const& line(std::tr1::get<3>(item));
 
278
          set_value(groupname, key, value, comment, line);
 
279
        }
 
280
    }
 
281
  return *this;
 
282
}
 
283
 
 
284
keyfile
 
285
operator + (keyfile const& lhs,
 
286
            keyfile const& rhs)
 
287
{
 
288
  keyfile ret(lhs);
 
289
  ret += rhs;
 
290
  return ret;
 
291
}
 
292
 
 
293
const keyfile::group_type *
 
294
keyfile::find_group (std::string const& group) const
 
295
{
 
296
  group_map_type::const_iterator pos = this->groups.find(group);
 
297
  if (pos != this->groups.end())
 
298
    return &pos->second;
 
299
 
 
300
  return 0;
 
301
}
 
302
 
 
303
keyfile::group_type *
 
304
keyfile::find_group (std::string const& group)
 
305
{
 
306
  group_map_type::iterator pos = this->groups.find(group);
 
307
  if (pos != this->groups.end())
 
308
    return &pos->second;
 
309
 
 
310
  return 0;
 
311
}
 
312
 
 
313
const keyfile::item_type *
 
314
keyfile::find_item (std::string const& group,
 
315
                    std::string const& key) const
 
316
{
 
317
  const group_type *found_group = find_group(group);
 
318
  if (found_group)
 
319
    {
 
320
      item_map_type const& items = std::tr1::get<1>(*found_group);
 
321
      item_map_type::const_iterator pos = items.find(key);
 
322
      if (pos != items.end())
 
323
        return &pos->second;
 
324
    }
 
325
 
 
326
  return 0;
 
327
}
 
328
 
 
329
keyfile::item_type *
 
330
keyfile::find_item (std::string const& group,
 
331
                    std::string const& key)
 
332
{
 
333
  group_type *found_group = find_group(group);
 
334
  if (found_group)
 
335
    {
 
336
      item_map_type& items = std::tr1::get<1>(*found_group);
 
337
      item_map_type::iterator pos = items.find(key);
 
338
      if (pos != items.end())
 
339
        return &pos->second;
 
340
    }
 
341
 
 
342
  return 0;
 
343
}
 
344
 
 
345
void
 
346
keyfile::print_comment (std::string const& comment,
 
347
                        std::ostream&      stream)
 
348
{
 
349
  std::string::size_type last_pos = 0;
 
350
  std::string::size_type pos = comment.find_first_of('\n', last_pos);
 
351
 
 
352
  while (1)
 
353
    {
 
354
      if (last_pos == pos)
 
355
        stream << "#\n";
 
356
      else
 
357
        stream << '#' << comment.substr(last_pos, pos - last_pos) << '\n';
 
358
 
 
359
      // Find next
 
360
      if (pos < comment.length() - 1)
 
361
        {
 
362
          last_pos = pos + 1;
 
363
          pos = comment.find_first_of('\n', last_pos);
 
364
        }
 
365
      else
 
366
        break;
 
367
    }
 
368
}
 
369
 
 
370
void
 
371
keyfile::check_priority (std::string const& group,
 
372
                         std::string const& key,
 
373
                         priority priority,
 
374
                         bool     valid) const
 
375
{
 
376
  if (valid == false)
 
377
    {
 
378
      switch (priority)
 
379
        {
 
380
        case PRIORITY_REQUIRED:
 
381
          {
 
382
            throw error(group, parse_error::MISSING_KEY, key);
 
383
          }
 
384
          break;
 
385
        default:
 
386
          break;
 
387
        }
 
388
    }
 
389
  else
 
390
    {
 
391
      switch (priority)
 
392
        {
 
393
        case PRIORITY_DEPRECATED:
 
394
          log_warning()
 
395
            << format(_("%1% chroot: A deprecated parameter \"%2%\" has been specified."))
 
396
            % group % key
 
397
            << std::endl;
 
398
          log_info()
 
399
            << _("This option will be removed in the future.") << std::endl;
 
400
          break;
 
401
        case PRIORITY_OBSOLETE:
 
402
          log_warning()
 
403
            << format(_("%1% chroot: An obsolete parameter \"%2%\" has been specified."))
 
404
            % group % key
 
405
            << std::endl;
 
406
          log_info()
 
407
            << _("This option has been removed, and no longer has any effect.") << std::endl;
 
408
          break;
 
409
        case PRIORITY_DISALLOWED:
 
410
          {
 
411
            throw error(group, parse_error::DISALLOWED_KEY, key);
 
412
          }
 
413
          break;
 
414
            default:
 
415
              break;
 
416
        }
 
417
    }
 
418
}