cached_decompress.m 3.88 KB
Newer Older
1 2
% Decompress a file using a cache.
%
3
% [fname,success] = cached_decompress(filename)
4 5 6 7 8 9 10 11 12 13 14 15 16
%
% Input:
%  filename: name of the file which is possibly compressed
%
% Output:
%  fname: the filename of the uncompressed file
%
% Global variables:
% CACHED_DECOMPRESS_DIR (default is the result of tempname)
%    cache directory of decompressed files
% CACHED_DECOMPRESS_LOG_FID (default 1): file id for log message
% CACHED_DECOMPRESS_MAX_SIZE (default 1e10): maximum size of cache in bytes.

17
function fname = cached_decompress(url)
18 19 20 21 22

global CACHED_DECOMPRESS_DIR
global CACHED_DECOMPRESS_LOG_FID
global CACHED_DECOMPRESS_MAX_SIZE

23 24 25 26 27 28 29 30 31

if startswith(url,'http:') || ...
        ~(endswith(url,'.gz') || endswith(url,'.bz2') || endswith(url,'.xz'))
  % opendap url or not compressed file
  fname = url;
  return
end


32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
cache_dir = CACHED_DECOMPRESS_DIR;

if isempty(cache_dir)
    cache_dir = tempname;
    CACHED_DECOMPRESS_DIR = cache_dir; 
    mkdir(cache_dir);
    fprintf('creating directory %s for temporary files.\n',cache_dir);
end

%if exist(cache_dir,'dir') ~= 7
%  error(['cache directory for compressed files does not exist. '...
%         'Please create the directory %s or change le value of the '...
%         'global variable CACHED_DECOMPRESS_DIR'],cache_dir);
%end
    
% where to print logs? default to screen

49
fid = CACHED_DECOMPRESS_LOG_FID;
50 51

if (isempty(fid))
52
    fid = 1;
53 54 55 56 57 58 59
end

% form filename for cache

fname = url;
fname = strrep(fname,'/','_SLASH_');
fname = strrep(fname,'*','_STAR_');
60 61 62
fname = strrep(fname,'\','_BSLASH_');
fname = strrep(fname,':','_COLON_');

63 64 65 66 67 68
fname = fullfile(cache_dir,fname);

% test if in cache

if exist(fname,'file') ~= 2
    if endswith(url,'.gz')
69
      cmd = 'gunzip --stdout -';
70
    elseif endswith(url,'.bz2')
71 72 73
      cmd = 'bunzip2 --stdout -';
    elseif endswith(url,'.xz')
      cmd = 'unxz --stdout -';
74
    else
75
      cmd = 'cat';
76
    end    
77 78 79 80 81 82

    if startswith(url,'ftp://')
      syscmd('curl --silent "%s" | %s > "%s"',url,cmd,fname);
    else
      syscmd('%s < "%s" > "%s"',cmd,url,fname);
    end
83 84 85 86
else
%    fprintf(fid,'retrieve from cache %s\n',url);
end

87 88


89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
% check cache size

d=dir(cache_dir);
cashe_size = sum([d.bytes]);
max_cache_size = CACHED_DECOMPRESS_MAX_SIZE;

if isempty(max_cache_size)
  max_cache_size = 1e10;
end

if (cashe_size > max_cache_size)
    
    % look for oldest files
    fdate = zeros(1,length(d));
    for i=1:length(d);
        fdate(i) = datenum(d(i).date);
    end
    
    [fdate,index] = sort(fdate,'descend');
    d=d(index);
    
    cum_size = cumsum([d(:).bytes]);
    todelete = find(cum_size > max_cache_size);
    
    for i=todelete
        if (d(i).isdir == 0)
            fprintf(fid,'clean cashe: delete %s\n', d(i).name);
            delete(fullfile(cache_dir,d(i).name));
        end
    end
end
end


123 124 125 126 127
function t = startswith(s,ext)

  if length(ext) <= length(s)
    t = strcmp(s(1:length(ext)),ext);
  else
128
    t = 0;
129
  end
130 131 132 133
end

function t = endswith(s,ext)

134
  if length(ext) <= length(s)
135
    t = strcmp(s(end-length(ext)+1:end),ext);
136
  else
137
    t = 0;
138
  end
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
end


function syscmd(varargin)

cmd = sprintf(varargin{:});
%disp(cmd)
status = 0;
[status, output] = system(cmd);

disp(output);
if status ~= 0
  error(['command "' cmd '" failed: ' output]);
end
end


156
% Copyright (C) 2012-2013, 2015 Alexander Barth <barth.alexander@gmail.com>
157 158 159 160 161 162 163 164 165 166 167 168 169 170
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; If not, see <http://www.gnu.org/licenses/>.