summaryrefslogtreecommitdiffstats
path: root/sched/src/com/android/sched/vfs/MessageDigestFS.java
blob: 123d607b56877c0cb05768b15806dd839bd84fae (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
 * Copyright (C) 2015 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.google.common.io.NullOutputStream;

import com.android.sched.util.config.MessageDigestFactory;
import com.android.sched.util.file.CannotCreateFileException;
import com.android.sched.util.file.CannotDeleteFileException;
import com.android.sched.util.file.CannotReadException;
import com.android.sched.util.file.NoSuchFileException;
import com.android.sched.util.file.NotDirectoryException;
import com.android.sched.util.file.NotFileException;
import com.android.sched.util.file.WrongPermissionException;
import com.android.sched.util.findbugs.SuppressFBWarnings;
import com.android.sched.util.location.LineLocation;
import com.android.sched.util.location.Location;
import com.android.sched.util.log.LoggerFactory;
import com.android.sched.vfs.MessageDigestFS.MessageDigestVDir;
import com.android.sched.vfs.MessageDigestFS.MessageDigestVFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.security.DigestOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

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

/**
 * A {@link VFS} filter implementation that creates a file containing a message digest for each
 * file.
 */
public class MessageDigestFS extends BaseVFS<MessageDigestVDir, MessageDigestVFile> implements VFS {
  @Nonnull
  private static final Logger logger = LoggerFactory.getLogger();
  @Nonnull
  private static final String DIGEST_FILE_NAME = "digest";

  @Nonnull
  private final BaseVFS<BaseVDir, BaseVFile> vfs;
  @Nonnull
  private final MessageDigestFactory mdFactory;
  @Nonnull
  private final Map<VPath, String> digests = new HashMap<VPath, String>();
  @CheckForNull
  private String digest = null;
  @Nonnull
  private final Set<Capabilities> capabilities;

  class MessageDigestVFile extends BaseVFile {

    @Nonnull
    private final BaseVFile wrappedFile;

    public MessageDigestVFile(@Nonnull BaseVFS<MessageDigestVDir, MessageDigestVFile> vfs,
        @Nonnull BaseVFile wrappedFile) {
      super(vfs, wrappedFile.getName());
      this.wrappedFile = wrappedFile;
    }

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

    @Override
    @Nonnull
    public VPath getPath() {
      return wrappedFile.getPath();
    }

    @Override
    @CheckForNull
    public String getDigest() {
      synchronized (MessageDigestFS.this) {
        return digests.get(this.getPath());
      }
    }

    @Nonnull
    public BaseVFile getWrappedFile() {
      return wrappedFile;
    }

    @Override
    @Nonnull
    public InputStream getInputStream() throws WrongPermissionException {
      return wrappedFile.getInputStream();
    }

    @Override
    @Nonnull
    public OutputStream getOutputStream() throws WrongPermissionException {
      synchronized (MessageDigestFS.this) {
        digests.remove(getPath());
        digest = null;
      }

      return new DigestOutputStream(wrappedFile.getOutputStream(), mdFactory.create()) {
        @Override
        public void close() throws IOException {
          super.close();
          // XXX open order instead of close order !
          synchronized (MessageDigestFS.this) {
            digests.put(getPath(), getDigestString(getMessageDigest().digest()));
            digest = null;
          }
        }
      };
    }
  }

  static class MessageDigestVDir extends BaseVDir {

    @Nonnull
    private final BaseVDir wrappedFile;

    public MessageDigestVDir(@Nonnull BaseVFS<MessageDigestVDir, MessageDigestVFile> vfs,
        @Nonnull BaseVDir wrappedFile) {
      super(vfs, wrappedFile.getName());
      this.wrappedFile = wrappedFile;
    }

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

    @Override
    @Nonnull
    public VPath getPath() {
      return wrappedFile.getPath();
    }

    @Nonnull
    public BaseVDir getWrappedDir() {
      return wrappedFile;
    }
  }

  @SuppressWarnings("unchecked")
  public MessageDigestFS(@Nonnull VFS vfs, @Nonnull MessageDigestFactory factory)
      throws WrongVFSFormatException {
    this.vfs = (BaseVFS<BaseVDir, BaseVFile>) vfs;
    this.mdFactory = factory;

    Set<Capabilities> capabilities = EnumSet.copyOf(vfs.getCapabilities());
    capabilities.add(Capabilities.DIGEST);
    this.capabilities = Collections.unmodifiableSet(capabilities);

    init();
  }

  private void init() throws WrongVFSFormatException {
    BaseVFile digestFile;

    try {
      digestFile = vfs.getRootDir().getVFile(DIGEST_FILE_NAME);
    } catch (NotFileException e) {
      throw new WrongVFSFormatException(this, vfs.getLocation(), e);
    } catch (NoSuchFileException e) {
      if (!vfs.getRootDir().isEmpty()) {
        throw new WrongVFSFormatException(this, vfs.getLocation(), e);
      }

      return;
    }

    LineNumberReader in = null;
    try {
      try {
        in = new LineNumberReader(new InputStreamReader(digestFile.getInputStream()));
      } catch (WrongPermissionException e) {
        throw new WrongVFSFormatException(this, vfs.getLocation(), e);
      }

      try {
        String line;
        while ((line = in.readLine()) != null) {
          int index = line.indexOf(':');
          if (index < 1) {
            throw new WrongVFSFormatException(this, vfs.getLocation(),
                new WrongFileFormatException(new LineLocation(digestFile.getLocation(),
                    in.getLineNumber())));
          }

          String path = line.substring(index + 1);
          String digest = line.substring(0, index);
          if (!path.equals(DIGEST_FILE_NAME)) { // do not put digest file in the map
            digests.put(new VPath(path, '/'), digest);
          } else {
            this.digest = digest;
          }
        }
      } catch (IOException e) {
        throw new WrongVFSFormatException(this, vfs.getLocation(), new CannotReadException(
            digestFile));
      }
    } finally {
      if (in != null) {
        assert digestFile != null;

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

  @Override
  @Nonnull
  public Set<Capabilities> getCapabilities() {
    return capabilities;
  }

  @Nonnull
  private String getDigestString(@Nonnull byte[] digestBytes) {
    return mdFactory.getService().getAlgorithm() + '-' + String.valueOf(encode(digestBytes));
  }

  @Nonnull
  private static final byte[] code = "0123456789ABCDEF".getBytes();

  @Nonnull
  private static char[] encode(@Nonnull byte[] bytes) {
    char[] array = new char[bytes.length * 2];

    for (int idx = 0; idx < bytes.length; idx++) {
      array[(idx << 1)] = (char) code[(bytes[idx] & 0xF0) >> 4];
      array[(idx << 1) + 1] = (char) code[(bytes[idx] & 0x0F)];
    }

    return array;
  }

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

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

  @Override
  @Nonnull
  public synchronized String getDigest() {
    if (digest == null) {
      printDigest(new NullOutputStream());
      assert digest != null;
    }

    return digest;
  }

  @Override
  public synchronized void close() throws CannotCreateFileException, WrongPermissionException,
      IOException {
    if (!closed) {
      printDigest(vfs.getRootDir().createVFile(DIGEST_FILE_NAME).getOutputStream());
      vfs.close();
      closed = true;
    }
  }

  @SuppressFBWarnings("DMI_ENTRY_SETS_MAY_REUSE_ENTRY_OBJECTS")
  private void printDigest(@Nonnull OutputStream out) {
    DigestOutputStream os = new DigestOutputStream(out, mdFactory.create());
    PrintStream printer = new PrintStream(os);

    Set<Entry<VPath, String>> entrySet = digests.entrySet();
    List<Entry<VPath, String>> entryList = new ArrayList<Entry<VPath, String>>(entrySet.size());
    entryList.addAll(entrySet);

    Collections.sort(entryList, new Comparator<Entry<VPath, String>>() {
      @Override
      public int compare(Entry<VPath, String> o1, Entry<VPath, String> o2) {
        return o1.getKey().getPathAsString('/').compareTo(o2.getKey().getPathAsString('/'));
      }
    });

    for (Map.Entry<VPath, String> entry : entryList) {
      String digest = entry.getValue();
      if (digest != null) {
        printer.print(digest);
        printer.print(':');
        printer.print(entry.getKey().getPathAsString('/'));
        printer.println();
      }
    }

    printer.flush();

    digest = getDigestString(os.getMessageDigest().digest());
    printer.print(digest);
    printer.print(":" + DIGEST_FILE_NAME);
    printer.println();

    printer.close();
  }

  @Override
  @Nonnull
  public MessageDigestVDir getRootDir() {
    return new MessageDigestVDir(this, vfs.getRootDir());
  }

  @Override
  @Nonnull
  InputStream openRead(@Nonnull MessageDigestVFile file) {
    // should be implemented in MessageDigestVFile
    throw new UnsupportedOperationException();
  }

  @Override
  @Nonnull
  OutputStream openWrite(@Nonnull final MessageDigestVFile file) {
    // should be implemented in MessageDigestVFile
    throw new UnsupportedOperationException();
  }

  @Override
  @Nonnull
  synchronized void delete(@Nonnull MessageDigestVFile file) throws CannotDeleteFileException {
    file.getWrappedFile().delete();
    digests.remove(file.getPath());
    digest = null;
  }

  @Override
  @Nonnull
  Collection<? extends BaseVElement> list(@Nonnull MessageDigestVDir dir) {
    Collection<? extends BaseVElement> elements = dir.getWrappedDir().list();
    List<BaseVElement> newElements = new ArrayList<BaseVElement>(elements.size());
    for (BaseVElement element : elements) {
      BaseVElement newElement;
      if (element.isVDir()) {
        newElement = new MessageDigestVDir(this, (BaseVDir) element);
      } else {
        newElement = new MessageDigestVFile(this, (BaseVFile) element);
      }
      newElements.add(newElement);
    }

    return newElements;
  }


  @Override
  boolean isEmpty(@Nonnull MessageDigestVDir dir) {
    return vfs.isEmpty(dir);
  }

  @Override
  @Nonnull
  MessageDigestVFile createVFile(@Nonnull MessageDigestVDir parent, @Nonnull String name)
      throws CannotCreateFileException {
    return new MessageDigestVFile(this, parent.getWrappedDir().createVFile(name));
  }

  @Override
  @Nonnull
  MessageDigestVDir createVDir(@Nonnull MessageDigestVDir parent, @Nonnull String name)
      throws CannotCreateFileException {
    return new MessageDigestVDir(this, parent.getWrappedDir().createVDir(name));
  }

  @Override
  @Nonnull
  MessageDigestVDir getVDir(@Nonnull MessageDigestVDir parent, @Nonnull String name)
      throws NotDirectoryException, NoSuchFileException {
    return new MessageDigestVDir(this, parent.getWrappedDir().getVDir(name));
  }

  @Override
  @Nonnull
  MessageDigestVFile getVFile(@Nonnull MessageDigestVDir parent, @Nonnull String name)
      throws NotFileException, NoSuchFileException {
    return new MessageDigestVFile(this, parent.getWrappedDir().getVFile(name));
  }

  @Override
  public boolean needsSequentialWriting() {
    return vfs.needsSequentialWriting();
  }

  @Override
  @Nonnull
  public String getDescription() {
    return "message digest wrapper";
  }

  @Override
  @Nonnull
  Location getVFileLocation(@Nonnull MessageDigestVFile file) {
    // should be implemented in MessageDigestVFile
    throw new UnsupportedOperationException();
  }

  @Override
  @Nonnull
  Location getVFileLocation(@Nonnull MessageDigestVDir parent, @Nonnull String name) {
    return vfs.getVFileLocation(parent, name);
  }

  @Override
  @Nonnull
  Location getVFileLocation(@Nonnull MessageDigestVDir parent, @Nonnull VPath path) {
    return vfs.getVFileLocation(parent, path);
  }

  @Override
  @Nonnull
  Location getVDirLocation(@Nonnull MessageDigestVDir dir) {
    return vfs.getVDirLocation(dir);
  }

  @Override
  @Nonnull
  Location getVDirLocation(@Nonnull MessageDigestVDir parent, @Nonnull String name) {
    return vfs.getVDirLocation(parent, name);
  }

  @Override
  @Nonnull
  Location getVDirLocation(@Nonnull MessageDigestVDir parent, @Nonnull VPath path) {
    return vfs.getVDirLocation(parent, path);
  }
}