Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
*/ |
40 |
package org.netbeans.modules.remote.impl.fs; |
41 |
|
42 |
import org.netbeans.modules.nativeexecution.api.ExecutionEnvironment; |
43 |
import java.io.BufferedReader; |
44 |
import java.io.File; |
45 |
import java.io.FileNotFoundException; |
46 |
import java.io.FileReader; |
47 |
import java.io.IOException; |
48 |
import java.io.PrintWriter; |
49 |
import java.io.UnsupportedEncodingException; |
50 |
import java.util.ArrayList; |
51 |
import java.util.Collection; |
52 |
import java.util.LinkedHashSet; |
53 |
import java.util.List; |
54 |
import org.netbeans.modules.nativeexecution.api.NativeProcessBuilder; |
55 |
import org.netbeans.modules.nativeexecution.api.util.ConnectionManager; |
56 |
import org.netbeans.modules.nativeexecution.api.util.ProcessUtils; |
57 |
import org.openide.util.Exceptions; |
58 |
|
59 |
/** |
60 |
* Manages paths that are marked as "delete on exit". |
61 |
* @author vkvashin |
62 |
*/ |
63 |
public class DeleteOnExitSupport { |
64 |
|
65 |
private final ExecutionEnvironment execEnv; |
66 |
private final File cache; |
67 |
|
68 |
/** If the ALLOW_ALTERNATIVE_DELETE_ON_EXIT is ON and transport does not support delete-on-exit, |
69 |
* then alternative delete-on-exit will work */ |
70 |
private static final boolean ALLOW_ALTERNATIVE_DELETE_ON_EXIT = |
71 |
RemoteFileSystemUtils.getBoolean("remote.alternative.delete.on.exit", true); |
72 |
|
73 |
private static final String DELETE_ON_EXIT_FILE_NAME = ".rfs_delete_on_exit"; // NOI18N |
74 |
|
75 |
private static final Object lock = new Object(); |
76 |
|
77 |
// The idea about filesToDelete and filesToRemember is as follows: |
78 |
// When a file is marked as "delete on exit", it is added to filesToRemember |
79 |
// When a disconnect occurs, we move all files from filesToRemember into filesToDelete |
80 |
// (and also store them on disk) |
81 |
// When connnect occurs, filesToDelete are deleted. |
82 |
// This prevents sync issues |
83 |
|
84 |
/** guarded by lock */ |
85 |
private final LinkedHashSet<String> filesToDelete = new LinkedHashSet<>(); |
86 |
|
87 |
/** guarded by lock */ |
88 |
private final LinkedHashSet<String> filesToRemember = new LinkedHashSet<>(); |
89 |
|
90 |
|
91 |
public DeleteOnExitSupport(ExecutionEnvironment execEnv, File cacheRoot) { |
92 |
this.execEnv = execEnv; |
93 |
this.cache = new File(cacheRoot, DELETE_ON_EXIT_FILE_NAME); |
94 |
if (ALLOW_ALTERNATIVE_DELETE_ON_EXIT) { |
95 |
synchronized (lock) { |
96 |
loadDeleteOnExit(cache, filesToDelete); |
97 |
} |
98 |
} |
99 |
} |
100 |
|
101 |
/** Called directly from ConnectionListener.connected */ |
102 |
public void notifyConnected() { |
103 |
} |
104 |
|
105 |
/** Called directly from ConnectionListener.disconnected */ |
106 |
public void notifyDisconnected() { |
107 |
if (ALLOW_ALTERNATIVE_DELETE_ON_EXIT) { |
108 |
List<String> paths; |
109 |
synchronized (lock) { |
110 |
filesToDelete.addAll(filesToRemember); |
111 |
filesToRemember.clear(); |
112 |
paths = new ArrayList<>(filesToDelete); |
113 |
} |
114 |
storeDeleteOnExit(cache, paths); |
115 |
} |
116 |
} |
117 |
/** |
118 |
* Is called from the request processor |
119 |
* in reaction on connect OR disconnect |
120 |
*/ |
121 |
public void processConnectionChange() { |
122 |
if (ALLOW_ALTERNATIVE_DELETE_ON_EXIT) { |
123 |
if (ConnectionManager.getInstance().isConnectedTo(execEnv)) { |
124 |
List<String> paths; |
125 |
synchronized (lock) { |
126 |
paths = new ArrayList<>(filesToDelete); |
127 |
filesToDelete.clear(); |
128 |
} |
129 |
if (!paths.isEmpty()) { |
130 |
deleteImpl(execEnv, paths); |
131 |
} |
132 |
} |
133 |
} |
134 |
} |
135 |
|
136 |
public void deleteOnExit(String... paths) { |
137 |
if (ALLOW_ALTERNATIVE_DELETE_ON_EXIT) { |
138 |
synchronized (lock) { |
139 |
for (String p : paths) { |
140 |
filesToRemember.add(p); |
141 |
} |
142 |
} |
143 |
} |
144 |
} |
145 |
|
146 |
private static void deleteImpl(ExecutionEnvironment execEnv, Collection<String> paths) { |
147 |
assert ALLOW_ALTERNATIVE_DELETE_ON_EXIT; |
148 |
if (paths.isEmpty()) { |
149 |
return; |
150 |
} |
151 |
StringBuilder sb = new StringBuilder(); |
152 |
for (String p : paths) { |
153 |
if (sb.length() > 0) { |
154 |
sb.append(' '); |
155 |
} |
156 |
sb.append(p); |
157 |
} |
158 |
if (!ConnectionManager.getInstance().isConnectedTo(execEnv)) { |
159 |
return; |
160 |
} |
161 |
ProcessUtils.execute(NativeProcessBuilder.newProcessBuilder(execEnv).setExecutable("xargs").setArguments("rm"), sb.toString().getBytes()); |
162 |
|
163 |
} |
164 |
|
165 |
private static void storeDeleteOnExit(File file, Collection<String> paths) { |
166 |
assert ALLOW_ALTERNATIVE_DELETE_ON_EXIT; |
167 |
// the existence of cache root ensured in ctor |
168 |
try (PrintWriter pw = new PrintWriter(file, "UTF8")) { // NOI18N |
169 |
if (!paths.isEmpty()) { |
170 |
for (String path : paths) { |
171 |
pw.append(path).append('\n'); |
172 |
} |
173 |
pw.close(); |
174 |
} |
175 |
} catch (FileNotFoundException | UnsupportedEncodingException ex) { |
176 |
Exceptions.printStackTrace(ex); // should never occur |
177 |
} |
178 |
} |
179 |
|
180 |
private static void loadDeleteOnExit(File file, Collection<String> pathsToAdd) { |
181 |
assert ALLOW_ALTERNATIVE_DELETE_ON_EXIT; |
182 |
// the existence of cache root ensured in ctor |
183 |
// this is called from ctor only, so it's OK to do file ops in sync block |
184 |
try (BufferedReader br = new BufferedReader(new FileReader(file))) { |
185 |
for (String path; (path = br.readLine()) != null;) { |
186 |
if (!path.isEmpty()) { |
187 |
pathsToAdd.add(path); |
188 |
} |
189 |
} |
190 |
// line is not visible here. |
191 |
} catch (FileNotFoundException ex) { |
192 |
// nothing to do: no file is quite normal |
193 |
} catch (IOException ex) { |
194 |
ex.printStackTrace(System.err); |
195 |
} |
196 |
} |
197 |
} |