1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 # See the COPYING file for license information.
17 # Copyright (c) 2006, 2007 Guillaume Chazarain <guichaz@yahoo.fr>
22 from pysize
.core
.deletion
import filter_deleted
24 # We cannot use a generator because we rely on the finally block to
25 # be sure finalize() will be called. Python 2.5 allows finally block
26 # in generator, but it relies on the GC so it's not sure it will be
27 # called at the right time.
29 # The expected use of these browsing functions is:
31 # cookie = chdir_browsing.init(path)
33 # for child in chdir_browsing.browsedir(cookie, (True|False)):
34 # .. Do something with child ..
36 # chdir_browsing.finalize(cookie)
39 previous_cwd
= os
.open('.', os
.O_RDONLY
)
44 os
.close(previous_cwd
)
47 def browsedir(previous_cwd
, cross_device
, account_deletion
=True):
50 def same_dev(dev
, path
):
52 return os
.lstat(path
)[stat
.ST_DEV
] == dev
55 dev
= os
.lstat('.')[stat
.ST_DEV
]
56 res
= [p
for p
in res
if same_dev(dev
, p
)]
58 res
= filter_deleted(res
)
59 # We want the order to be always so same so that hard links
60 # detection will be consistent across filesystems
64 def finalize(previous_cwd
):
67 os
.fchdir(previous_cwd
)
69 os
.close(previous_cwd
)
71 def listdir(path
, cross_device
):
74 return browsedir(cookie
, cross_device
)