Skip to content

Check for open streams and os force file deletion #930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 51 additions & 24 deletions java/src/main/java/com/genexus/util/GXFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,31 +209,58 @@ public boolean create(InputStream input, boolean overwrite) {
return ErrCode == 0;
}

public void delete() {
if (sourceSeted()) {
resetErrors();
try {
if (!(FileSource.isFile() && FileSource.exists())) {
ErrCode = 2;
ErrDescription = "The file couldn't be deleted; file does not exist";
} else {
try {
ret = FileSource.delete();
if (!ret) {
setUnknownError();
}
} catch (SecurityException e) {
ErrCode = 100;
ErrDescription = e.getMessage();
}
}
} catch (Exception e) {
setUnknownError(e);
}
}
}
public void delete() {
if (sourceSeted()) {
resetErrors();
try {
if (!(FileSource.isFile() && FileSource.exists())) {
ErrCode = 2;
ErrDescription = "The file couldn't be deleted because it does not exist";
return;
}

try {
if (fileWriter != null) {
fileWriter.close();
fileWriter = null;
}
if (lineIterator != null) {
lineIterator.close();
lineIterator = null;
}
if (this.getStream() != null) {
this.getStream().close();
}
} catch (Exception ignored) {}

System.gc();

boolean ret = FileSource.delete();
if (!ret) {
try {
String filePath = FileSource.getFileInstance().getAbsolutePath();
ProcessBuilder pb;
if (System.getProperty("os.name").toLowerCase().contains("win")) {
pb = new ProcessBuilder("cmd.exe", "/c", "del /F /Q \"" + filePath + "\"");
} else {
pb = new ProcessBuilder("rm", "-f", filePath);
}
pb.start().waitFor();
if (FileSource.exists()) {
setUnknownError();
}
} catch (Exception e) {
setUnknownError(e);
}
}
} catch (Exception e) {
setUnknownError(e);
}
}
}


public boolean exists() {
public boolean exists() {
if (sourceSeted()) {
try {
resetErrors();
Expand Down
Loading