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, 2008 Guillaume Chazarain <guichaz@gmail.com>
20 from pysize
.core
.exception_propagation
import raises
22 # The cache for directories sizes
23 # {device: {directory_inode: size}}
26 # The cache for hardlinks locations
27 # {device: {hardlink_inode: (directory_inode, basename)}}
28 # For memory efficiency reasons, we suppose directory_inode lives
29 # in the same device as hardlink_inode, but this can be defeated with
30 # mount --bind on files. So if we have the same hardlink device/inode
31 # in two directories with the same inode on different devices, we may
32 # count both instances as canonical, and then counting many times the
33 # hardlink size. But this is very unlikely, unless on purpose, but then
34 # you get what you deserve :-p
38 def get_dev_ino(path
):
39 """Return (device_number, inode) for path."""
41 return st
.st_dev
, st
.st_ino
43 def cache_add_dir((dev
, ino
), size
):
44 """Add a directory and its size to the cache."""
45 inodes
= DEV_DIR_SIZE
.setdefault(dev
, {})
48 def cache_add_hardlink(dev
, ino
, dir_ino
, path
):
49 """Add a hardlink and its location to the cache, and returns True iff the
50 location should be considered as the canonical location. Unlike for the
51 canonical location, a hardlink size is 0."""
52 entry
= dir_ino
, os
.path
.basename(path
)
53 inodes
= DEV_HLINK_DNAME
.setdefault(dev
, {})
54 cached_entry
= inodes
.setdefault(ino
, entry
)
55 return cached_entry
== entry
57 def cache_get_dir_size((dev
, ino
)):
58 """Retrieve a directory size from the cache."""
59 inodes
= DEV_DIR_SIZE
.get(dev
, None)
61 return inodes
.get(ino
, None)
65 DEV_HLINK_DNAME
.clear()