1 /*******************************************************************************
2  * Copyright (c) 2005, 2016 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  * Martin Oberhuber (Wind River) - [170317] add symbolic link support to API
14  * Martin Oberhuber (Wind River) - [183137] liblocalfile for solaris-sparc
15  * Martin Oberhuber (Wind River) - [184433] liblocalfile for Linux x86_64
16  * Martin Oberhuber (Wind River) - [184534] get attributes from native lib
17  *******************************************************************************/
18 package org.eclipse.core.internal.filesystem.local;
19 
20 import java.io.File;
21 import java.net.URI;
22 import org.eclipse.core.filesystem.*;
23 import org.eclipse.core.filesystem.provider.FileSystem;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.osgi.service.environment.Constants;
26 
27 /**
28  * File system provider for the "file" scheme.  This file system provides access to
29  * the local file system that is available via java.io.File.
30  */
31 public class LocalFileSystem extends FileSystem {
32 	/**
33 	 * Cached constant indicating if the current OS is Mac OSX
34 	 */
35 	static final boolean MACOSX = LocalFileSystem.getOS().equals(Constants.OS_MACOSX);
36 
37 	/**
38 	 * Whether the current file system is case sensitive
39 	 */
40 	private static final boolean caseSensitive = MACOSX ? false : new java.io.File("a").compareTo(new java.io.File("A")) != 0; //$NON-NLS-1$ //$NON-NLS-2$
41 
42 	/**
43 	 * The attributes of this file system. The initial value of -1 is used
44 	 * to indicate that the attributes have not yet been computed.
45 	 */
46 	private int attributes = -1;
47 	/**
48 	 * The singleton instance of this file system.
49 	 */
50 	private static IFileSystem instance;
51 
52 	/**
53 	 * Returns the instance of this file system
54 	 *
55 	 * @return The instance of this file system.
56 	 */
getInstance()57 	public static IFileSystem getInstance() {
58 		return instance;
59 	}
60 
61 	/**
62 	 * Returns the current OS.  This is equivalent to Platform.getOS(), but
63 	 * is tolerant of the platform runtime not being present.
64 	 */
getOS()65 	static String getOS() {
66 		return System.getProperty("osgi.os", ""); //$NON-NLS-1$ //$NON-NLS-2$
67 	}
68 
69 	/**
70 	 * Creates a new local file system.
71 	 */
LocalFileSystem()72 	public LocalFileSystem() {
73 		super();
74 		instance = this;
75 	}
76 
77 	@Override
attributes()78 	public int attributes() {
79 		if (attributes != -1)
80 			return attributes;
81 		attributes = 0;
82 
83 		//try to query supported attributes from native lib impl
84 		int nativeAttributes = LocalFileNativesManager.getSupportedAttributes();
85 		if (nativeAttributes >= 0) {
86 			attributes = nativeAttributes;
87 			return attributes;
88 		}
89 
90 		//fallback for older lib: compute attributes as known before
91 		//all known platforms with native implementation support the read only flag
92 		attributes |= EFS.ATTRIBUTE_READ_ONLY;
93 
94 		// this must be kept in sync with functionality of previous libs not implementing nativeAttributes method
95 		String os = getOS();
96 		String arch = System.getProperty("osgi.arch", ""); //$NON-NLS-1$ //$NON-NLS-2$
97 		if (os.equals(Constants.OS_WIN32))
98 			attributes |= EFS.ATTRIBUTE_ARCHIVE | EFS.ATTRIBUTE_HIDDEN;
99 		else if (os.equals(Constants.OS_LINUX) || (os.equals(Constants.OS_SOLARIS) && arch.equals(Constants.ARCH_SPARC)))
100 			attributes |= EFS.ATTRIBUTE_EXECUTABLE | EFS.ATTRIBUTE_SYMLINK | EFS.ATTRIBUTE_LINK_TARGET;
101 		else if (os.equals(Constants.OS_MACOSX) || os.equals(Constants.OS_HPUX) || os.equals(Constants.OS_QNX))
102 			attributes |= EFS.ATTRIBUTE_EXECUTABLE;
103 		return attributes;
104 	}
105 
106 	@Override
canDelete()107 	public boolean canDelete() {
108 		return true;
109 	}
110 
111 	@Override
canWrite()112 	public boolean canWrite() {
113 		return true;
114 	}
115 
116 	@Override
fromLocalFile(File file)117 	public IFileStore fromLocalFile(File file) {
118 		return new LocalFile(file);
119 	}
120 
121 	@Override
getStore(IPath path)122 	public IFileStore getStore(IPath path) {
123 		return new LocalFile(path.toFile());
124 	}
125 
126 	@Override
getStore(URI uri)127 	public IFileStore getStore(URI uri) {
128 		return new LocalFile(new File(uri.getSchemeSpecificPart()));
129 	}
130 
131 	@Override
isCaseSensitive()132 	public boolean isCaseSensitive() {
133 		return caseSensitive;
134 	}
135 }
136