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