1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it 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.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
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, MA 02111-1307 USA
17
"""A collection of file names which is persisted on a transport."""
19
from bzrlib.lazy_import import lazy_import
20
lazy_import(globals(), """
27
class FileNames(object):
28
"""A collection of file names.
30
The file names are persisted to a file on a transport, and cand be
31
added, removed and initialised.
33
The set of names are stored in a flat file, one name per line.
34
New names are allocated sequentially.
35
Initialisation creates an empty file.
37
This is intended to support management of modest numbers of files in
38
write-locked environments which may be read from unlistable transports.
40
The save method must be called to cause the state to be saved to the
43
Finally, load is used to obtain a previously saved set.
46
def __init__(self, transport, index_name):
47
"""Create a collection on transport called index_name."""
48
self._transport = transport
49
self._index_name = index_name
54
for number in xrange(self._cap):
55
if str(number) not in self._names:
56
self._names.add(str(number))
58
raise errors.BzrError('too many files')
61
"""Initialise the collection."""
65
"""Load the names from the transport."""
66
self._names = set(self._transport.get_bytes(
67
self._index_name).split('\n'))
68
self._names.discard('')
71
"""What are the names in this collection?"""
72
return frozenset(self._names)
74
def remove(self, name):
75
"""Remove name from the collection."""
76
self._names.remove(name)
79
"""Save the set of names."""
80
self._transport.put_bytes(self._index_name, '\n'.join(self._names))