Skip to content

Commit 554d49f

Browse files
Check for open streams and os force file deletion (#930)
Issue:203334 (cherry picked from commit ccea293)
1 parent f96e3b4 commit 554d49f

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

java/src/main/java/com/genexus/util/GXFile.java

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -209,31 +209,58 @@ public boolean create(InputStream input, boolean overwrite) {
209209
return ErrCode == 0;
210210
}
211211

212-
public void delete() {
213-
if (sourceSeted()) {
214-
resetErrors();
215-
try {
216-
if (!(FileSource.isFile() && FileSource.exists())) {
217-
ErrCode = 2;
218-
ErrDescription = "The file couldn't be deleted; file does not exist";
219-
} else {
220-
try {
221-
ret = FileSource.delete();
222-
if (!ret) {
223-
setUnknownError();
224-
}
225-
} catch (SecurityException e) {
226-
ErrCode = 100;
227-
ErrDescription = e.getMessage();
228-
}
229-
}
230-
} catch (Exception e) {
231-
setUnknownError(e);
232-
}
233-
}
234-
}
212+
public void delete() {
213+
if (sourceSeted()) {
214+
resetErrors();
215+
try {
216+
if (!(FileSource.isFile() && FileSource.exists())) {
217+
ErrCode = 2;
218+
ErrDescription = "The file couldn't be deleted because it does not exist";
219+
return;
220+
}
221+
222+
try {
223+
if (fileWriter != null) {
224+
fileWriter.close();
225+
fileWriter = null;
226+
}
227+
if (lineIterator != null) {
228+
lineIterator.close();
229+
lineIterator = null;
230+
}
231+
if (this.getStream() != null) {
232+
this.getStream().close();
233+
}
234+
} catch (Exception ignored) {}
235+
236+
System.gc();
237+
238+
boolean ret = FileSource.delete();
239+
if (!ret) {
240+
try {
241+
String filePath = FileSource.getFileInstance().getAbsolutePath();
242+
ProcessBuilder pb;
243+
if (System.getProperty("os.name").toLowerCase().contains("win")) {
244+
pb = new ProcessBuilder("cmd.exe", "/c", "del /F /Q \"" + filePath + "\"");
245+
} else {
246+
pb = new ProcessBuilder("rm", "-f", filePath);
247+
}
248+
pb.start().waitFor();
249+
if (FileSource.exists()) {
250+
setUnknownError();
251+
}
252+
} catch (Exception e) {
253+
setUnknownError(e);
254+
}
255+
}
256+
} catch (Exception e) {
257+
setUnknownError(e);
258+
}
259+
}
260+
}
261+
235262

236-
public boolean exists() {
263+
public boolean exists() {
237264
if (sourceSeted()) {
238265
try {
239266
resetErrors();

0 commit comments

Comments
 (0)