Home | History | Annotate | Download | only in vogar
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package vogar;
     18 
     19 import java.io.File;
     20 import java.util.List;
     21 import vogar.commands.Command;
     22 import vogar.commands.Mkdir;
     23 
     24 public class HostFileCache implements FileCache {
     25     private final File CACHE_ROOT = new File("/tmp/vogar-md5-cache");
     26 
     27     private final Log log;
     28     private final Mkdir mkdir;
     29 
     30     public HostFileCache(Log log, Mkdir mkdir) {
     31         this.log = log;
     32         this.mkdir = mkdir;
     33     }
     34 
     35     private void cp(File source, File destination) {
     36         List<String> rawResult = new Command.Builder(log).args("cp", source, destination).execute();
     37         // A successful copy returns no results.
     38         if (!rawResult.isEmpty()) {
     39             throw new RuntimeException("Couldn't copy " + source + " to " + destination
     40                     + ": " + rawResult.get(0));
     41         }
     42     }
     43 
     44     private void mv(File source, File destination) {
     45         List<String> rawResult = new Command.Builder(log).args("mv", source, destination).execute();
     46         // A successful move returns no results.
     47         if (!rawResult.isEmpty()) {
     48             throw new RuntimeException("Couldn't move " + source + " to " + destination
     49                     + ": " + rawResult.get(0));
     50         }
     51     }
     52 
     53     public void copyFromCache(String key, File destination) {
     54         File cachedFile = new File(CACHE_ROOT, key);
     55         cp(cachedFile, destination);
     56     }
     57 
     58     public void copyToCache(File source, String key) {
     59         File cachedFile = new File(CACHE_ROOT, key);
     60         mkdir.mkdirs(CACHE_ROOT);
     61         // Copy it onto the same file system first, then atomically move it into place.
     62         // That way, if we fail, we don't leave anything dangerous lying around.
     63         File temporary = new File(cachedFile + ".tmp");
     64         cp(source, temporary);
     65         mv(temporary, cachedFile);
     66     }
     67 
     68     public boolean existsInCache(String key) {
     69         return new File(CACHE_ROOT, key).exists();
     70     }
     71 }
     72