1 /*******************************************************************************
2  * Copyright (c) 2005, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.core.internal.filesystem;
15 
16 import java.io.*;
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import org.eclipse.core.filesystem.*;
20 import org.eclipse.core.filesystem.provider.FileInfo;
21 import org.eclipse.core.filesystem.provider.FileStore;
22 import org.eclipse.core.runtime.*;
23 
24 /**
25  * A null file store represents a file whose location is unknown,
26  * such as a location based on an undefined variable.  This store
27  * acts much like /dev/null on *nix: writes are ignored, reads return
28  * empty streams, and modifications such as delete, mkdir, will fail.
29  */
30 public class NullFileStore extends FileStore {
31 	private IPath path;
32 
33 	/**
34 	 * Creates a null file store
35 	 * @param path The path of the file in this store
36 	 */
NullFileStore(IPath path)37 	public NullFileStore(IPath path) {
38 		Assert.isNotNull(path);
39 		this.path = path;
40 	}
41 
42 	@Override
childInfos(int options, IProgressMonitor monitor)43 	public IFileInfo[] childInfos(int options, IProgressMonitor monitor) {
44 		return EMPTY_FILE_INFO_ARRAY;
45 	}
46 
47 	@Override
childNames(int options, IProgressMonitor monitor)48 	public String[] childNames(int options, IProgressMonitor monitor) {
49 		return EMPTY_STRING_ARRAY;
50 	}
51 
52 	@Override
delete(int options, IProgressMonitor monitor)53 	public void delete(int options, IProgressMonitor monitor) throws CoreException {
54 		//super implementation will always fail
55 		super.delete(options, monitor);
56 	}
57 
58 	@Override
fetchInfo(int options, IProgressMonitor monitor)59 	public IFileInfo fetchInfo(int options, IProgressMonitor monitor) {
60 		FileInfo result = new FileInfo(getName());
61 		result.setExists(false);
62 		return result;
63 	}
64 
65 	@Override
getChild(String name)66 	public IFileStore getChild(String name) {
67 		return new NullFileStore(path.append(name));
68 	}
69 
70 	@Override
getFileSystem()71 	public IFileSystem getFileSystem() {
72 		return NullFileSystem.getInstance();
73 	}
74 
75 	@Override
getName()76 	public String getName() {
77 		return String.valueOf(path.lastSegment());
78 	}
79 
80 	@Override
getParent()81 	public IFileStore getParent() {
82 		return path.segmentCount() == 0 ? null : new NullFileStore(path.removeLastSegments(1));
83 	}
84 
85 	@Override
mkdir(int options, IProgressMonitor monitor)86 	public IFileStore mkdir(int options, IProgressMonitor monitor) throws CoreException {
87 		//super implementation will always fail
88 		return super.mkdir(options, monitor);
89 	}
90 
91 	@Override
openInputStream(int options, IProgressMonitor monitor)92 	public InputStream openInputStream(int options, IProgressMonitor monitor) {
93 		return new ByteArrayInputStream(new byte[0]);
94 	}
95 
96 	@Override
openOutputStream(int options, IProgressMonitor monitor)97 	public OutputStream openOutputStream(int options, IProgressMonitor monitor) {
98 		return new OutputStream() {
99 			@Override
100 			public void write(int b) {
101 				//do nothing
102 			}
103 		};
104 	}
105 
106 	@Override
putInfo(IFileInfo info, int options, IProgressMonitor monitor)107 	public void putInfo(IFileInfo info, int options, IProgressMonitor monitor) throws CoreException {
108 		//super implementation will always fail
109 		super.putInfo(info, options, monitor);
110 	}
111 
112 	@Override
toString()113 	public String toString() {
114 		return path.toString();
115 	}
116 
117 	@Override
toURI()118 	public URI toURI() {
119 		try {
120 			return new URI(EFS.SCHEME_NULL, null, path.isEmpty() ? "/" : path.toString(), null); //$NON-NLS-1$
121 		} catch (URISyntaxException e) {
122 			//should never happen
123 			Policy.log(IStatus.ERROR, "Invalid URI", e); //$NON-NLS-1$
124 			return null;
125 		}
126 	}
127 }
128