Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 88c8d314243f836aea5b86d6a6b0c31a3481fbc2 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2011, 2015 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 * William Chen (Wind River)- [345387] Open the remote files with a proper editor
 * William Chen (Wind River)- [345552] Edit the remote files with a proper editor
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.filesystem.core.internal.utils;

import java.io.File;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.te.tcf.filesystem.core.activator.CorePlugin;
import org.eclipse.tcf.te.tcf.filesystem.core.interfaces.runtime.IFSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.core.internal.FSTreeNode;
import org.eclipse.tcf.te.tcf.filesystem.core.nls.Messages;

/**
 * The local file system cache used to manage the temporary files downloaded from a remote file
 * system.
 */
public class CacheManager {
	public static final char PATH_ESCAPE_CHAR = '$';

	/**
	 * Get the local path of a node's cached file.
	 * <p>
	 * The preferred location is within the plugin's state location, in example
	 * <code>&lt;state location&gt;agent_<hashcode_of_peerId>/remote/path/to/the/file...</code>.
	 * <p>
	 * If the plug-in is loaded in a RCP workspace-less environment, the fall back strategy is to
	 * use the users home directory.
	 *
	 * @param node The file/folder node.
	 * @return The local path of the node's cached file.
	 */
	public static IPath getCachePath(IFSTreeNode node) {
		File location = getCacheRoot();
		String agentId = node.getRuntimeModel().getPeerNode().getPeerId();
		// Use Math.abs to avoid negative hash value.
		String agent = agentId.replace(':', PATH_ESCAPE_CHAR);
		IPath agentDir = new Path(location.getAbsolutePath()).append(agent);
		File agentDirFile = agentDir.toFile();
		mkdirChecked(agentDirFile);
		return appendNodePath(agentDir, node);
	}

	/**
	 * Check and make a directory if it does not exist. Record the failure message if making fails.
	 *
	 * @param file The file to be deleted.
	 */
	static void mkdirChecked(final File dir) {
		if (!dir.exists()) {
			SafeRunner.run(new ISafeRunnable() {
				@Override
				public void run() throws Exception {
					if (!dir.mkdir()) {
						throw new Exception(NLS.bind(Messages.CacheManager_MkdirFailed, dir
						                .getAbsolutePath()));
					}
				}

				@Override
				public void handleException(Throwable exception) {
					// Ignore on purpose
				}
			});
		}
	}

	/**
	 * Check if the file exists and set its read-only attribute if it does. Record the failure
	 * message if it fails.
	 *
	 * @param file The file to be set.
	 */
	static void setReadOnlyChecked(final File file) {
		if (file.exists()) {
			SafeRunner.run(new ISafeRunnable() {
				@Override
				public void run() throws Exception {
					if (!file.setReadOnly()) {
						throw new Exception(NLS.bind(Messages.CacheManager_SetReadOnlyFailed, file
						                .getAbsolutePath()));
					}
				}

				@Override
				public void handleException(Throwable exception) {
					// Ignore on purpose
				}
			});
		}
	}

	/**
	 * Get the local file of the specified node.
	 *
	 * <p>
	 * The preferred location is within the plugin's state location, in example
	 * <code>&lt;state location&gt;agent_<hashcode_of_peerId>/remote/path/to/the/file...</code>.
	 * <p>
	 * If the plug-in is loaded in a RCP workspace-less environment, the fall back strategy is to
	 * use the users home directory.
	 *
	 * @param node The file/folder node.
	 * @return The file object of the node's local cache.
	 */
	public static File getCacheFile(IFSTreeNode node) {
		return getCachePath(node).toFile();
	}

	/**
	 * Get the cache file system's root directory on the local host's file system.
	 *
	 * @return The root folder's location of the cache file system.
	 */
	public static File getCacheRoot() {
		File location;
		try {
			location = CorePlugin.getDefault().getStateLocation().toFile();
		}
		catch (IllegalStateException e) {
			// An RCP workspace-less environment (-data @none)
			location = new File(System.getProperty("user.home"), ".tcf"); //$NON-NLS-1$ //$NON-NLS-2$
			location = new File(location, "fs"); //$NON-NLS-1$
		}

		// Create the location if it not exist
		mkdirChecked(location);
		return location;
	}

	/**
	 * Append the path with the specified node's context path.
	 *
	 * @param path The path to be appended.
	 * @param node The file/folder node.
	 * @return The path to the node.
	 */
	private static IPath appendNodePath(IPath path, IFSTreeNode node) {
		if (!node.isRootDirectory() && node.getParent() != null) {
			path = appendNodePath(path, node.getParent());
			return appendPathSegment(node, path, node.getName());
		}
		String name = node.getName();
		if (node.isWindowsNode()) {
			name = name.replace('\\', '/');
		}
		name = name.replace(':', PATH_ESCAPE_CHAR);
		if (name.endsWith("/")) //$NON-NLS-1$
			name = name.substring(0, name.length()-1);

		return appendPathSegment(node, path, name);
	}

	/**
	 * Append the path with the segment "name". Create a directory if the node is a directory which
	 * does not yet exist.
	 *
	 * @param node The file/folder node.
	 * @param path The path to appended.
	 * @param name The segment's name.
	 * @return The path with the segment "name" appended.
	 */
	private static IPath appendPathSegment(IFSTreeNode node, IPath path, String name) {
		IPath newPath = path.append(name);
		File newFile = newPath.toFile();
		if (node.isDirectory()) {
			mkdirChecked(newFile);
		}
		return newPath;
	}

	public static void clearCache(FSTreeNode source) {
		if (source != null) {
			File cache = getCacheFile(source);
			if (cache.exists())
				deleteFileOrDir(cache);
		}
	}

	private static boolean deleteFileOrDir(File file) {
		File[] children = file.listFiles();
		boolean ok = true;
		if (children != null) {
			for (File child : children) {
				if (!deleteFileOrDir(child)) {
					ok = false;
				}
			}
		}
		return ok && file.delete();
	}
}

Back to the top