summaryrefslogtreecommitdiffstats
path: root/sched/src/com/android/sched/vfs/CachedDirectFS.java
blob: 83512be10fdf3b5b3d7a3593b80f91546d729077 (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
/*
 * 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.android.sched.util.ConcurrentIOException;
import com.android.sched.util.file.AbstractStreamFile;
import com.android.sched.util.file.CannotCreateFileException;
import com.android.sched.util.file.CannotDeleteFileException;
import com.android.sched.util.file.Directory;
import com.android.sched.util.file.FileAlreadyExistsException;
import com.android.sched.util.file.FileOrDirectory;
import com.android.sched.util.file.FileOrDirectory.Permission;
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.location.DirectoryLocation;
import com.android.sched.util.location.FileLocation;
import com.android.sched.util.location.Location;
import com.android.sched.vfs.CachedDirectFS.CachedParentVDir;
import com.android.sched.vfs.CachedDirectFS.CachedParentVFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;

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

/**
 * A {@link VFS} implementation backed by a real file system, but where directories are cached in
 * memory.
 */
public class CachedDirectFS extends BaseVFS<CachedParentVDir, CachedParentVFile> implements VFS {

  static class CachedParentVDir extends InMemoryVDir {

    @CheckForNull
    private CachedParentVDir parent;

    CachedParentVDir(@Nonnull BaseVFS<? extends CachedParentVDir, ? extends ParentVFile> vfs,
        @Nonnull String name) {
      super(vfs, name);
    }

    CachedParentVDir(@Nonnull BaseVFS<? extends CachedParentVDir, ? extends ParentVFile> vfs,
        @Nonnull CachedParentVDir parent, @Nonnull String name) {
      super(vfs, name);
      this.parent = parent;
    }

    @Override
    @Nonnull
    public VPath getPath() {
      if (parent != null) {
        return parent.getPath().clone().appendPath(new VPath(name, '/'));
      } else {
        return VPath.ROOT;
      }
    }

    @Override
    @Nonnull
    public BaseVFile getVFile(@Nonnull String name) throws NoSuchFileException,
        NotFileException {
      return vfs.getVFile(this, name);
    }

    @Override
    @Nonnull
    public BaseVDir getVDir(@Nonnull String name) throws NotDirectoryException,
        NoSuchFileException {
      return vfs.getVDir(this, name);
    }

    @Override
    @Nonnull
    public BaseVFile createVFile(@Nonnull String name) throws CannotCreateFileException {
      return vfs.createVFile(this, name);
    }

    @Override
    @Nonnull
    public BaseVDir createVDir(@Nonnull String name) throws CannotCreateFileException {
      return vfs.createVDir(this, name);
    }

    @Override
    @Nonnull
    public Collection<? extends BaseVElement> list() {
      return vfs.list(this);
    }
  }

  static class CachedParentVFile extends ParentVFile {

    CachedParentVFile(@Nonnull BaseVFS<? extends BaseVDir, ? extends BaseVFile> vfs,
        @Nonnull VDir parent, @Nonnull String name) {
      super(vfs, parent, name);
    }

    @Override
    public void delete() throws CannotDeleteFileException {
      vfs.delete(this);
    }

    public void deleteFromCache() {
      ((InMemoryVDir) parent).internalDelete(name);
    }
  }

  @Nonnull
  private final Directory  dir;
  @Nonnull
  private final CachedParentVDir root;
  @Nonnull
  private final Set<Capabilities> capabilities;

  public CachedDirectFS(@Nonnull Directory dir, int permissions) {
    this.dir = dir;
    this.root = new CachedParentVDir(this, "");

    Set<Capabilities> capabilities = EnumSet.noneOf(Capabilities.class);
    if ((permissions & Permission.READ) != 0) {
      capabilities.add(Capabilities.READ);
      capabilities.add(Capabilities.PARALLEL_READ);
    }
    if ((permissions & Permission.WRITE) != 0) {
      capabilities.add(Capabilities.WRITE);
      capabilities.add(Capabilities.PARALLEL_WRITE);
    }
    capabilities.add(Capabilities.UNIQUE_ELEMENT);
    this.capabilities = Collections.unmodifiableSet(capabilities);

    fillVDirFromRealDirectory(dir.getFile(), root);
  }

  private void fillVDirFromRealDirectory(@Nonnull File dir, @Nonnull VDir vDir) {
    for (File element : dir.listFiles()) {
      try {
        if (element.isDirectory()) {
          VDir newVDir = vDir.createVDir(element.getName());
          fillVDirFromRealDirectory(element, newVDir);
        } else {
          vDir.createVFile(element.getName());
        }
      } catch (CannotCreateFileException e) {
        throw new AssertionError(e);
      }
    }
  }

  @Override
  @Nonnull
  public String getDescription() {
    return "directory on disk with cache";
  }

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

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

  @Override
  public synchronized void close() {
    closed = true;
  }

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

  @Override
  public CachedParentVDir getRootDir() {
    return root;
  }

  @Override
  @Nonnull
  InputStream openRead(@Nonnull CachedParentVFile file) throws WrongPermissionException {
    assert !isClosed();
    assert capabilities.contains(Capabilities.READ);

    File path = getNativeFile(file.getPath());
    try {
      return new FileInputStream(path);
    } catch (FileNotFoundException e) {
      FileOrDirectory.checkPermissions(path, file.getLocation(), Permission.READ);
      throw new ConcurrentIOException(e);
    }
  }

  @Nonnull
  @Override
  OutputStream openWrite(@Nonnull CachedParentVFile file) throws WrongPermissionException {
    assert !isClosed();
    assert capabilities.contains(Capabilities.WRITE);

    File path = getNativeFile(file.getPath());
    try {
      return new FileOutputStream(path);
    } catch (FileNotFoundException e) {
      FileOrDirectory.checkPermissions(path, file.getLocation(), Permission.WRITE);
      throw new ConcurrentIOException(e);
    }
  }

  @Nonnull
  @Override
  Collection<? extends BaseVElement> list(@Nonnull CachedParentVDir dir) {
    return dir.getAllFromCache();
  }

  @Override
  boolean isEmpty(@Nonnull CachedParentVDir dir) {
    assert !isClosed();
    assert capabilities.contains(Capabilities.READ);

    return getNativeFile(dir.getPath()).listFiles().length == 0;
  }

  @Override
  @Nonnull
  CachedParentVDir getVDir(@Nonnull CachedParentVDir parent, @Nonnull String name)
      throws NotDirectoryException, NoSuchFileException {
    BaseVElement element = parent.getFromCache(name);
    if (element != null) {
      if (element.isVDir()) {
        return (CachedParentVDir) element;
      } else {
        throw new NotDirectoryException(getVDirLocation(parent, name));
      }
    } else {
      throw new NoSuchFileException(getVDirLocation(parent, name));
    }
  }

  @Override
  @Nonnull
  CachedParentVFile getVFile(@Nonnull CachedParentVDir parent, @Nonnull String name)
      throws NotFileException, NoSuchFileException {
    BaseVElement element = parent.getFromCache(name);
    if (element != null) {
      if (!element.isVDir()) {
        return (CachedParentVFile) element;
      } else {
        throw new NotFileException(getVFileLocation(parent, name));
      }
    } else {
      throw new NoSuchFileException(getVFileLocation(parent, name));
    }
  }

  @Override
  @Nonnull
  void delete(@Nonnull CachedParentVFile file) throws CannotDeleteFileException {
    assert !isClosed();

    File path = getNativeFile(file.getPath());
    if (!path.delete() || path.exists()) {
      throw new CannotDeleteFileException(file);
    }

    file.deleteFromCache();
  }


  @Override
  @Nonnull
  synchronized CachedParentVFile createVFile(@Nonnull CachedParentVDir parent,
      @Nonnull String name) throws CannotCreateFileException {
    assert !isClosed();

    try {
      return getVFile(parent, name);

    } catch (NoSuchFileException e) {

    File path = getNativeFile(parent.getPath(), name);
    try {
      AbstractStreamFile.create(path, new FileLocation(path));
    } catch (FileAlreadyExistsException e2) {
      // Nothing to do
    }
    CachedParentVFile vFile = new CachedParentVFile(this, parent, name);

    parent.putInCache(name, vFile);
    return vFile;

    } catch (NotFileException e) {
      throw new CannotCreateFileException(getVFileLocation(parent, name));
    }
  }

  @Override
  @Nonnull
  synchronized CachedParentVDir createVDir(@Nonnull CachedParentVDir parent,
      @Nonnull String name)
      throws CannotCreateFileException {
    assert !isClosed();

    try {
      return getVDir(parent, name);

    } catch (NoSuchFileException e) {

      File path = getNativeFile(parent.getPath(), name);
      try {
        Directory.create(path, new DirectoryLocation(path));
      } catch (FileAlreadyExistsException e2) {
        // Nothing to do
      }
      CachedParentVDir vDir = new CachedParentVDir(this, parent, name);

      parent.putInCache(name, vDir);
      return vDir;

    } catch (NotDirectoryException e) {
      throw new CannotCreateFileException(getVDirLocation(parent, name));
    }
  }

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

  @Override
  synchronized boolean isClosed() {
    return closed;
  }

  @Override
  @Nonnull
  FileLocation getVFileLocation(@Nonnull CachedParentVFile file) {
    return new FileLocation(getNativeFile(file.getPath()));
  }

  @Override
  @Nonnull
  FileLocation getVFileLocation(@Nonnull CachedParentVDir parent, @Nonnull String name) {
    return new FileLocation(getNativeFile(parent.getPath(), name));
  }

  @Override
  @Nonnull
  DirectoryLocation getVDirLocation(@Nonnull CachedParentVDir dir) {
    return new DirectoryLocation(getNativeFile(dir.getPath()));
  }

  @Override
  @Nonnull
  DirectoryLocation getVDirLocation(@Nonnull CachedParentVDir parent, @Nonnull String name) {
    return new DirectoryLocation(getNativeFile(parent.getPath(), name));
  }

  @Override
  @Nonnull
  FileLocation getVFileLocation(CachedParentVDir parent, VPath path) {
    return new FileLocation(getNativeFile(parent.getPath().clone().appendPath(path)));
  }

  @Override
  @Nonnull
  DirectoryLocation getVDirLocation(CachedParentVDir parent, VPath path) {
    return new DirectoryLocation(getNativeFile(parent.getPath().clone().appendPath(path)));
  }

  @Nonnull
  private File getNativeFile(@Nonnull VPath path) {
    return new File(dir.getFile(), path.getPathAsString(File.separatorChar));
  }

  @Nonnull
  private File getNativeFile(@Nonnull VPath path, @Nonnull String name) {
    return new File(new File(dir.getFile(), path.getPathAsString(File.separatorChar)), name);
  }
}