"Fossies" - the Fresh Open Source Software Archive

Member "pysize-0.2/pysize/core/pysize_global_fs_cache.py" (11 Mar 2007, 2465 Bytes) of package /linux/privat/old/pysize-0.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    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.
    5 #
    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.
   10 #
   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.
   14 #
   15 # See the COPYING file for license information.
   16 #
   17 # Copyright (c) 2006, 2007 Guillaume Chazarain <guichaz@yahoo.fr>
   18 
   19 import os
   20 
   21 # The cache for directories sizes
   22 # {device: {directory_inode: size}}
   23 DEV_DIR_SIZE = {}
   24 
   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 = {}
   35 
   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
   40 
   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
   45 
   46 def cache_add_hardlink(dev, ino, dir_ino, path):
   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, os.path.basename(path)
   51     inodes = DEV_HLINK_DNAME.setdefault(dev, {})
   52     cached_entry = inodes.setdefault(ino, entry)
   53     return cached_entry == entry
   54 
   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)
   60 
   61 def drop_caches():
   62     DEV_DIR_SIZE.clear()
   63     DEV_HLINK_DNAME.clear()