summaryrefslogtreecommitdiffstats
path: root/sched/src/com/android/sched/vfs/MessageDigestInputVFS.java
blob: c2a6d23bdec11f857918b1daded2c1b8d00040de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.sched.vfs;

import com.android.sched.util.file.NoSuchFileException;
import com.android.sched.util.file.NotDirectoryException;
import com.android.sched.util.file.NotFileOrDirectoryException;
import com.android.sched.util.location.Location;
import com.android.sched.util.log.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
 * An {@link InputVFS} which read a file with message digest of each file in the VFS.
 */
public class MessageDigestInputVFS extends MessageDigestVFS implements InputVFS {
  @Nonnull
  private static final Logger logger = LoggerFactory.getLogger();

  @Nonnull
  protected InputVFS vfs;
  @Nonnull
  private final MessageDigestInputVDir root;
  @Nonnull
  private final Map<VPath, String> digests = new HashMap<VPath, String>();
  @CheckForNull
  private String algorithm = null;
  @CheckForNull
  private String digest = null;

  /**
   * An {@link InputVFile} which have a message digest.
   */
  public static class MessageDigestInputVFile implements InputVFile {
    @Nonnull
    private final InputVFile file;
    @CheckForNull
    private final String digest;

    public MessageDigestInputVFile(@Nonnull InputVFile file, @CheckForNull String digest) {
      this.file = file;
      this.digest = digest;
    }

    @Override
    public boolean isVDir() {
      return false;
    }

    @Override
    @Nonnull
    public String getName() {
      return file.getName();
    }

    @Override
    @Nonnull
    public Location getLocation() {
      return file.getLocation();
    }

    @Override
    @Nonnull
    public InputStream openRead() throws IOException {
      return file.openRead();
    }

    @CheckForNull
    public String getDigest() {
      return digest;
    }

    @Override
    public void delete() {
      // This implementation is obsolete anyway
      throw new UnsupportedOperationException();
    }
  }

  /**
   * An {@link InputVDir} which return {@link MessageDigestInputVFile}.
   */
  public class MessageDigestInputVDir implements InputVDir {
    @Nonnull
    private final InputVDir dir;
    @Nonnull
    private final VPath pathToRoot;

    private MessageDigestInputVDir (@Nonnull InputVDir dir, @Nonnull VPath pathToRoot) {
      this.dir = dir;
      this.pathToRoot = pathToRoot;
    }

    @Override
    public boolean isVDir() {
      return true;
    }

    @Override
    @Nonnull
    public String getName() {
      return dir.getName();
    }

    @Override
    @Nonnull
    public Location getLocation() {
      return dir.getLocation();
    }

    @Override
    @Nonnull
    public Collection<? extends InputVElement> list() {
      return dir.list();
    }

    @Override
    @Nonnull
    public MessageDigestInputVDir getInputVDir(@Nonnull VPath path)
        throws NotDirectoryException, NoSuchFileException {
      VPath newPathToRoot = pathToRoot.clone();
      newPathToRoot.appendPath(path);
      return new MessageDigestInputVDir(dir.getInputVDir(path), newPathToRoot);
    }

    @Override
    @Nonnull
    public MessageDigestInputVFile getInputVFile(@Nonnull VPath path)
        throws NoSuchFileException, NotFileOrDirectoryException {
      VPath filePathToRoot = pathToRoot.clone();
      filePathToRoot.appendPath(path);
      return new MessageDigestInputVFile(dir.getInputVFile(path), digests.get(filePathToRoot));
    }
 }

  public MessageDigestInputVFS(@Nonnull InputVFS vfs) {
    this.root = new MessageDigestInputVDir(vfs.getRootInputVDir(), VPath.ROOT);
    this.vfs  = vfs;

    // Reading digest directory is best effort
    // If there is one error, let's skip that step
    BufferedReader in = null;
    InputVFile     file = null;
    try {
      try {
        file = root.getInputVFile(new VPath(DIGEST_DIRECTORY_NAME, '/'));
      } catch (NotFileOrDirectoryException e) {
        logger.log(Level.WARNING, "Cannot open '" + DIGEST_DIRECTORY_NAME + "' file in {0}", vfs
            .getLocation().getDescription());
        logger.log(Level.WARNING, "Stacktrace", e);
        return;
      } catch (NoSuchFileException e) {
        logger.log(Level.WARNING, "Cannot open '" + DIGEST_DIRECTORY_NAME + "' file in {0}", vfs
            .getLocation().getDescription());
        logger.log(Level.WARNING, "Stacktrace", e);
        return;
      }

      try {
        in = new BufferedReader(new InputStreamReader(file.openRead()));
      } catch (IOException e) {
        logger.log(Level.WARNING, "Cannot open {0}", file.getLocation().getDescription());
        logger.log(Level.WARNING, "Stacktrace", e);
        return;
      }

      try {
        algorithm = in.readLine();

        String line;
        while ((line = in.readLine()) != null) {
          int index = line.indexOf(':');
          if (index < 1) {
            logger.log(Level.WARNING, "Bad format in {0}", file.getLocation().getDescription());
            continue;
          }

          digests.put(new VPath(line.substring(index + 1), '/'), line.substring(0, index));
        }
      } catch (IOException e) {
        logger.log(Level.WARNING, "Error reading {0}", file.getLocation().getDescription());
        return;
      }

      digest = digests.get(new VPath(MessageDigestOutputVFS.DIGEST_DIRECTORY_NAME, '/'));
    } finally {
      if (in != null) {
        assert file != null;

        try {
          in.close();
        } catch (IOException e) {
          logger.log(Level.WARNING, "Cannot close {0}", file.getLocation().getDescription());
        }
      }
    }
  }

  @Override
  @Nonnull
  public Location getLocation() {
    return vfs.getLocation();
  }

  @Override
  public synchronized void close() throws IOException {
    vfs.close();
  }

  @Override
  @Nonnull
  public MessageDigestInputVDir getRootInputVDir() {
    return root;
  }

  @Override
  @Nonnull
  public String getPath() {
    return vfs.getPath();
  }

  @CheckForNull
  public String getDigestAlgorithm() {
    return algorithm;
  }

  @Override
  @CheckForNull
  public String getDigest() {
    return digest;
  }
}