aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs/InMemoryContentInfo.java
blob: 6a721f1f94a077c1e3d729689b87bfedf5312b36 (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
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// 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.google.devtools.build.lib.vfs.inmemoryfs;

import com.google.common.base.Preconditions;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
import com.google.devtools.build.lib.util.Clock;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;

import java.io.IOException;

/**
 * This interface defines the function directly supported by the "files" stored
 * in a InMemoryFileSystem. This corresponds to a file or inode in UNIX: it
 * doesn't have a path (it could have many paths due to hard links, or none if
 * it's unlinked, i.e. garbage).
 *
 * <p>This class is thread-safe: instances may be accessed and modified from
 * concurrent threads. Subclasses must preserve this property.
 */
@ThreadSafe
public abstract class InMemoryContentInfo implements ScopeEscapableStatus {

  private final Clock clock;

  /**
   * Stores the time when the file was last modified. This is atomically updated
   * whenever the file changes, so all accesses must be synchronized.
   */
  private long lastModifiedTime;

  /**
   * Stores the time when the file information was changed. This is atomically updated
   * whenever the file changes, so all accesses must be synchronized.
   */
  private long lastChangeTime;

  /**
   * Modifications to the isWritable field do not update the lastModifiedTime,
   * so we don't need to synchronize; using volatile is enough.
   */
  private volatile boolean isWritable = true;
  private volatile boolean isExecutable = false;
  private volatile boolean isReadable = true;

  protected InMemoryContentInfo(Clock clock) {
    this(clock, true);
  }

  protected InMemoryContentInfo(Clock clock, boolean isMutable) {
    this.clock = clock;
    // When we create the file, it is modified.
    if (isMutable) {
      markModificationTime();
    }
  }

  /**
   * Returns true if the current object is a directory.
   */
  @Override
  public abstract boolean isDirectory();

  /**
   * Returns true if the current object is a symbolic link.
   */
  @Override
  public abstract boolean isSymbolicLink();

  /**
   * Returns true if the current object is a regular file.
   */
  @Override
  public abstract boolean isFile();

  /**
   * Returns the size of the entity denoted by the current object. For files,
   * this is the length in bytes, for directories the number of children. The
   * size of links is unspecified.
   */
  @Override
  public abstract long getSize() throws IOException;

  /**
   * Returns the time when the entity denoted by the current object was last
   * modified.
   */
  @Override
  public synchronized long getLastModifiedTime() {
    return lastModifiedTime;
  }

  /**
   * Returns the time when the entity denoted by the current object was last
   * changed.
   */
  @Override
  public synchronized long getLastChangeTime() {
    return lastChangeTime;
  }

  /**
   * Returns the file node id for the given instance, emulated by the
   * identity hash code.
   */
  @Override
  public long getNodeId() {
    return System.identityHashCode(this);
  }

  /**
   * Sets the time that denotes when the entity denoted by this object was last
   * modified.
   */
  synchronized void setLastModifiedTime(long newTime) {
    lastModifiedTime = newTime;
    markChangeTime();
  }

  /**
   * Sets the last modification and change times to the current time.
   */
  protected synchronized void markModificationTime() {
    Preconditions.checkState(clock != null);
    lastModifiedTime = clock.currentTimeMillis();
    lastChangeTime = lastModifiedTime;
  }

  /**
   * Sets the last change time to the current time.
   */
  protected synchronized void markChangeTime() {
    Preconditions.checkState(clock != null);
    lastChangeTime = clock.currentTimeMillis();
  }

  /**
   * Sets whether the current file is readable.
   */
  boolean isReadable() {
    return isReadable;
  }

  /**
   * Returns whether the current file is readable.
   */
  void setReadable(boolean readable) {
    isReadable = readable;
  }


  /**
   * Sets whether the current file is writable.
   */
  void setWritable(boolean writable) {
    isWritable = writable;
    markChangeTime();
  }

  /**
   * Returns whether the current file is writable.
   */
  boolean isWritable() {
    return isWritable;
  }

  /**
   * Sets whether the current file is executable.
   */
  void setExecutable(boolean executable) {
    isExecutable = executable;
    markChangeTime();
  }

  /**
   * Returns whether the current file is executable.
   */
  boolean isExecutable() {
    return isExecutable;
  }

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

  @Override
  public PathFragment getEscapingPath() {
    return null;
  }

  /**
   * Called just before this inode is moved.
   *
   * @param targetPath where the inode is relocated.
   * @throws IOException
   */
  protected void movedTo(Path targetPath) throws IOException {
  }
}