Update e-mail address
[pysize.git] / pysize / core / pysize_global_fs_cache.py
blob51a48fe07cbd9dde4efa6a920bbeefda0e26052c
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>
19 import os
20 from pysize.core.exception_propagation import raises
22 # The cache for directories sizes
23 # {device: {directory_inode: size}}
24 DEV_DIR_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
35 DEV_HLINK_DNAME = {}
37 @raises(OSError)
38 def get_dev_ino(path):
39 """Return (device_number, inode) for path."""
40 st = os.lstat(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, {})
46 inodes[ino] = size
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)
60 if inodes:
61 return inodes.get(ino, None)
63 def drop_caches():
64 DEV_DIR_SIZE.clear()
65 DEV_HLINK_DNAME.clear()