forked from j62/ctbrec
Delete parent directories if they are empty
Stop deleting parent dirs at the recordings dir level
This commit is contained in:
parent
69cfc8a6ec
commit
331e2e2e80
|
@ -626,16 +626,19 @@ public class LocalRecorder implements Recorder {
|
||||||
|
|
||||||
if(path.isFile()) {
|
if(path.isFile()) {
|
||||||
Files.delete(path.toPath());
|
Files.delete(path.toPath());
|
||||||
deleteEmptyParents(path);
|
deleteEmptyParents(path.getParentFile());
|
||||||
} else {
|
} else {
|
||||||
deleteDirectory(path);
|
deleteDirectory(path);
|
||||||
deleteEmptyParents(path);
|
deleteEmptyParents(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteEmptyParents(File path) throws IOException {
|
private void deleteEmptyParents(File parent) throws IOException {
|
||||||
File parent = path.getParentFile();
|
File recDir = new File(Config.getInstance().getSettings().recordingsDir);
|
||||||
while(parent != null && parent.list() != null && parent.list().length == 0) {
|
while(parent != null && parent.list() != null && parent.list().length == 0) {
|
||||||
|
if(parent.equals(recDir)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
LOG.debug("Deleting empty directory {}", parent.getAbsolutePath());
|
LOG.debug("Deleting empty directory {}", parent.getAbsolutePath());
|
||||||
Files.delete(parent.toPath());
|
Files.delete(parent.toPath());
|
||||||
parent = parent.getParentFile();
|
parent = parent.getParentFile();
|
||||||
|
@ -661,21 +664,7 @@ public class LocalRecorder implements Recorder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (deletedAllFiles) {
|
if (!deletedAllFiles) {
|
||||||
LOG.debug("All files deleted");
|
|
||||||
if (directory.list().length == 0) {
|
|
||||||
LOG.debug("Deleting directory {}", directory);
|
|
||||||
boolean deleted = directory.delete();
|
|
||||||
if (deleted) {
|
|
||||||
if (directory.getParentFile().list().length == 0) {
|
|
||||||
LOG.debug("Deleting parent directory {}", directory.getParentFile());
|
|
||||||
directory.getParentFile().delete();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new IOException("Couldn't delete " + directory);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new IOException("Couldn't delete all files in " + directory);
|
throw new IOException("Couldn't delete all files in " + directory);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
|
Loading…
Reference in New Issue