Skip to content

Commit f3c6ce8

Browse files
meteorcloudydslomov
authored andcommitted
Revert "WindowsFileSystem: open files with delete-sharing"
This reverts commit 1a95502. Related: #6731 Closes #6732. PiperOrigin-RevId: 222958770
1 parent db46207 commit f3c6ce8

File tree

4 files changed

+16
-214
lines changed

4 files changed

+16
-214
lines changed

src/main/java/com/google/devtools/build/lib/vfs/AbstractFileSystem.java

Lines changed: 16 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import java.io.IOException;
2424
import java.io.InputStream;
2525
import java.io.OutputStream;
26-
import java.nio.channels.FileChannel;
27-
import javax.annotation.Nullable;
2826

2927
/** This class implements the FileSystem interface using direct calls to the UNIX filesystem. */
3028
@ThreadSafe
@@ -58,49 +56,42 @@ protected InputStream getInputStream(Path path) throws IOException {
5856
}
5957

6058
/** Returns either normal or profiled FileInputStream. */
61-
private InputStream createFileInputStream(Path path) throws IOException {
59+
private InputStream createFileInputStream(Path path) throws FileNotFoundException {
6260
final String name = path.toString();
6361
if (profiler.isActive()
6462
&& (profiler.isProfiling(ProfilerTask.VFS_READ)
6563
|| profiler.isProfiling(ProfilerTask.VFS_OPEN))) {
6664
long startTime = Profiler.nanoTimeMaybe();
6765
try {
6866
// Replace default FileInputStream instance with the custom one that does profiling.
69-
return new ProfiledInputStream(name, newFileInputStream(name));
67+
return new ProfiledFileInputStream(name);
7068
} finally {
7169
profiler.logSimpleTask(startTime, ProfilerTask.VFS_OPEN, name);
7270
}
7371
} else {
7472
// Use normal FileInputStream instance if profiler is not enabled.
75-
return newFileInputStream(name);
73+
return new FileInputStream(path.toString());
7674
}
7775
}
7876

79-
protected InputStream newFileInputStream(String path) throws IOException {
80-
return new FileInputStream(path);
81-
}
82-
83-
protected OutputStream newFileOutputStream(String path, boolean append) throws IOException {
84-
return new FileOutputStream(path, append);
85-
}
86-
8777
/**
8878
* Returns either normal or profiled FileOutputStream. Should be used by subclasses to create
8979
* default OutputStream instance.
9080
*/
91-
protected OutputStream createFileOutputStream(Path path, boolean append) throws IOException {
81+
protected OutputStream createFileOutputStream(Path path, boolean append)
82+
throws FileNotFoundException {
9283
final String name = path.toString();
9384
if (profiler.isActive()
9485
&& (profiler.isProfiling(ProfilerTask.VFS_WRITE)
9586
|| profiler.isProfiling(ProfilerTask.VFS_OPEN))) {
9687
long startTime = Profiler.nanoTimeMaybe();
9788
try {
98-
return new ProfiledFileOutputStream(name, newFileOutputStream(name, append));
89+
return new ProfiledFileOutputStream(name, append);
9990
} finally {
10091
profiler.logSimpleTask(startTime, ProfilerTask.VFS_OPEN, name);
10192
}
10293
} else {
103-
return newFileOutputStream(name, append);
94+
return new FileOutputStream(name, append);
10495
}
10596
}
10697

@@ -119,34 +110,12 @@ protected OutputStream getOutputStream(Path path, boolean append) throws IOExcep
119110
}
120111
}
121112

122-
private static final class ProfiledInputStream extends InputStream implements
123-
FileChannelSupplier {
113+
private static final class ProfiledFileInputStream extends FileInputStream {
124114
private final String name;
125-
private final InputStream stm;
126115

127-
public ProfiledInputStream(String name, InputStream stm) {
116+
public ProfiledFileInputStream(String name) throws FileNotFoundException {
117+
super(name);
128118
this.name = name;
129-
this.stm = stm;
130-
}
131-
132-
@Override
133-
public int available() throws IOException {
134-
return stm.available();
135-
}
136-
137-
@Override
138-
public void close() throws IOException {
139-
stm.close();
140-
}
141-
142-
@Override
143-
public void mark(int readlimit) {
144-
stm.mark(readlimit);
145-
}
146-
147-
@Override
148-
public boolean markSupported() {
149-
return stm.markSupported();
150119
}
151120

152121
@Override
@@ -155,7 +124,7 @@ public int read() throws IOException {
155124
try {
156125
// Note that FileInputStream#read() does *not* call any of our overridden methods,
157126
// so there's no concern with double counting here.
158-
return stm.read();
127+
return super.read();
159128
} finally {
160129
profiler.logSimpleTask(startTime, ProfilerTask.VFS_READ, name);
161130
}
@@ -170,55 +139,19 @@ public int read(byte[] b) throws IOException {
170139
public int read(byte[] b, int off, int len) throws IOException {
171140
long startTime = Profiler.nanoTimeMaybe();
172141
try {
173-
return stm.read(b, off, len);
142+
return super.read(b, off, len);
174143
} finally {
175144
profiler.logSimpleTask(startTime, ProfilerTask.VFS_READ, name);
176145
}
177146
}
178-
179-
@Override
180-
public void reset() throws IOException {
181-
stm.reset();
182-
}
183-
184-
@Override
185-
public long skip(long n) throws IOException {
186-
return stm.skip(n);
187-
}
188-
189-
@Override
190-
public FileChannel getChannel() {
191-
return stm instanceof FileInputStream
192-
? ((FileInputStream) stm).getChannel()
193-
: null;
194-
}
195147
}
196148

197-
/**
198-
* Interface to return a {@link FileChannel}.
199-
*/
200-
public interface FileChannelSupplier {
201-
@Nullable
202-
FileChannel getChannel();
203-
}
204-
205-
private static final class ProfiledFileOutputStream extends OutputStream {
149+
private static final class ProfiledFileOutputStream extends FileOutputStream {
206150
private final String name;
207-
private final OutputStream stm;
208151

209-
public ProfiledFileOutputStream(String name, OutputStream stm) {
152+
public ProfiledFileOutputStream(String name, boolean append) throws FileNotFoundException {
153+
super(name, append);
210154
this.name = name;
211-
this.stm = stm;
212-
}
213-
214-
@Override
215-
public void close() throws IOException {
216-
stm.close();
217-
}
218-
219-
@Override
220-
public void flush() throws IOException {
221-
stm.flush();
222155
}
223156

224157
@Override
@@ -230,17 +163,7 @@ public void write(byte[] b) throws IOException {
230163
public void write(byte[] b, int off, int len) throws IOException {
231164
long startTime = Profiler.nanoTimeMaybe();
232165
try {
233-
stm.write(b, off, len);
234-
} finally {
235-
profiler.logSimpleTask(startTime, ProfilerTask.VFS_WRITE, name);
236-
}
237-
}
238-
239-
@Override
240-
public void write(int b) throws IOException {
241-
long startTime = Profiler.nanoTimeMaybe();
242-
try {
243-
stm.write(b);
166+
super.write(b, off, len);
244167
} finally {
245168
profiler.logSimpleTask(startTime, ProfilerTask.VFS_WRITE, name);
246169
}

src/main/java/com/google/devtools/build/lib/windows/WindowsFileSystem.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,8 @@
2727
import java.io.File;
2828
import java.io.FileNotFoundException;
2929
import java.io.IOException;
30-
import java.io.InputStream;
31-
import java.io.OutputStream;
3230
import java.nio.file.Files;
3331
import java.nio.file.LinkOption;
34-
import java.nio.file.Paths;
35-
import java.nio.file.StandardOpenOption;
3632
import java.nio.file.attribute.DosFileAttributes;
3733

3834
/** File system implementation for Windows. */
@@ -55,22 +51,6 @@ public String getFileSystemType(Path path) {
5551
return "ntfs";
5652
}
5753

58-
private static java.nio.file.Path getNioPath(String path) {
59-
return Paths.get(path);
60-
}
61-
62-
@Override
63-
protected InputStream newFileInputStream(String path) throws IOException {
64-
return Files.newInputStream(getNioPath(path));
65-
}
66-
67-
@Override
68-
protected OutputStream newFileOutputStream(String path, boolean append) throws IOException {
69-
return append
70-
? Files.newOutputStream(getNioPath(path), StandardOpenOption.APPEND)
71-
: Files.newOutputStream(getNioPath(path));
72-
}
73-
7454
@Override
7555
public boolean delete(Path path) throws IOException {
7656
long startTime = Profiler.nanoTimeMaybe();

src/test/java/com/google/devtools/build/lib/vfs/JavaIoFileSystemTest.java

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import com.google.devtools.build.lib.testutil.ManualClock;
2424
import com.google.devtools.build.lib.testutil.TestUtils;
2525
import java.io.IOException;
26-
import java.io.InputStream;
27-
import java.io.OutputStream;
2826
import java.nio.file.Files;
2927
import java.nio.file.LinkOption;
3028
import java.nio.file.Paths;
@@ -138,46 +136,4 @@ private static List<Path> getSubDirectories(Path base, int loopi, int subDirecto
138136
}
139137
return subDirs;
140138
}
141-
142-
@Test
143-
public void testDeleteFileOpenForReading() throws Exception {
144-
Path path = absolutize("foo.txt");
145-
FileSystemUtils.writeIsoLatin1(path, "foo");
146-
// Sanity check: attempt reading from the file.
147-
try (InputStream is = path.getInputStream()) {
148-
byte[] contents = new byte[3];
149-
assertThat(is.read(contents)).isEqualTo(3);
150-
assertThat(contents).isEqualTo(new byte[] {'f', 'o', 'o'});
151-
}
152-
// Actual test: attempt deleting the file while open, then reading from it.
153-
try (InputStream is = path.getInputStream()) {
154-
assertThat(path.delete()).isTrue();
155-
assertThat(path.exists()).isFalse();
156-
157-
byte[] contents = new byte[3];
158-
assertThat(is.read(contents)).isEqualTo(3);
159-
assertThat(contents).isEqualTo(new byte[] {'f', 'o', 'o'});
160-
}
161-
}
162-
163-
@Test
164-
public void testDeleteFileOpenForWriting() throws Exception {
165-
Path path = absolutize("foo.txt");
166-
// Sanity check: attempt writing to the file.
167-
assertThat(path.exists()).isFalse();
168-
try (OutputStream os = path.getOutputStream(/* append */ false)) {
169-
os.write(new byte[] {'b', 'a', 'r'});
170-
}
171-
try (InputStream is = path.getInputStream()) {
172-
byte[] contents = new byte[3];
173-
assertThat(is.read(contents)).isEqualTo(3);
174-
assertThat(contents).isEqualTo(new byte[] {'b', 'a', 'r'});
175-
}
176-
// Actual test: attempt deleting the file while open, then writing to it.
177-
try (OutputStream os = path.getOutputStream(/* append */ false)) {
178-
assertThat(path.delete()).isTrue();
179-
assertThat(path.exists()).isFalse();
180-
os.write(new byte[] {'b', 'a', 'r'});
181-
}
182-
}
183139
}

src/test/java/com/google/devtools/build/lib/windows/WindowsFileSystemTest.java

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
import com.google.devtools.build.lib.windows.util.WindowsTestUtil;
3232
import java.io.File;
3333
import java.io.IOException;
34-
import java.io.InputStream;
35-
import java.io.OutputStream;
36-
import java.nio.file.AccessDeniedException;
3734
import java.nio.file.Files;
3835
import java.util.Arrays;
3936
import java.util.HashMap;
@@ -335,58 +332,4 @@ public String apply(File input) {
335332
assertThat(p.isFile()).isTrue();
336333
}
337334
}
338-
339-
@Test
340-
public void testDeleteFileOpenForReading() throws Exception {
341-
testUtil.scratchFile("foo.txt", "foo");
342-
Path path = testUtil.createVfsPath(fs, "foo.txt");
343-
// Sanity check: attempt reading from the file.
344-
try (InputStream is = path.getInputStream()) {
345-
byte[] contents = new byte[3];
346-
assertThat(is.read(contents)).isEqualTo(3);
347-
assertThat(contents).isEqualTo(new byte[] {'f', 'o', 'o'});
348-
}
349-
// Actual test: attempt deleting the file while open, then reading from it.
350-
try (InputStream is = path.getInputStream()) {
351-
assertThat(path.delete()).isTrue();
352-
assertThat(path.exists()).isFalse();
353-
354-
byte[] contents = new byte[3];
355-
assertThat(is.read(contents)).isEqualTo(3);
356-
assertThat(contents).isEqualTo(new byte[] {'f', 'o', 'o'});
357-
}
358-
}
359-
360-
@Test
361-
public void testDeleteFileOpenForWriting() throws Exception {
362-
testUtil.scratchFile("bar.txt", "bar");
363-
Path path = testUtil.createVfsPath(fs, "bar.txt");
364-
// Sanity check: attempt writing to the file.
365-
try (InputStream is = path.getInputStream()) {
366-
byte[] contents = new byte[3];
367-
assertThat(is.read(contents)).isEqualTo(3);
368-
assertThat(contents).isEqualTo(new byte[] {'b', 'a', 'r'});
369-
}
370-
// Actual test: attempt deleting the file while open, then writing to it.
371-
try (OutputStream os = path.getOutputStream(/* append */ false)) {
372-
assertThat(path.delete()).isTrue();
373-
assertThat(path.exists()).isFalse();
374-
os.write(new byte[] {'b', 'a', 'r'});
375-
}
376-
}
377-
378-
@Test
379-
public void testCannotOpenDirectoryAsFile() throws Exception {
380-
testUtil.scratchFile("foo/bar.txt", "bar");
381-
Path file = testUtil.createVfsPath(fs, "foo/bar.txt");
382-
Path dir = file.getParentDirectory();
383-
// Sanity check: we can open the file.
384-
try (InputStream is = file.getInputStream()) {}
385-
assertThat(dir.isDirectory()).isTrue();
386-
try (InputStream is = dir.getInputStream()) {
387-
fail("Expected failure");
388-
} catch (IOException e) {
389-
assertThat(e).isInstanceOf(AccessDeniedException.class);
390-
}
391-
}
392335
}

0 commit comments

Comments
 (0)