Use nio Files.delete instead of File.delete everywhere

This commit is contained in:
AsamK 2016-11-28 12:28:44 +01:00
parent e364610c93
commit 2351a89b00
2 changed files with 64 additions and 33 deletions

View file

@ -993,7 +993,6 @@ class Manager implements Signal {
continue; continue;
} }
String sender = dir.getName();
for (final File fileEntry : dir.listFiles()) { for (final File fileEntry : dir.listFiles()) {
if (!fileEntry.isFile()) { if (!fileEntry.isFile()) {
continue; continue;
@ -1019,7 +1018,11 @@ class Manager implements Signal {
} }
save(); save();
handler.handleMessage(envelope, content, null); handler.handleMessage(envelope, content, null);
fileEntry.delete(); try {
Files.delete(fileEntry.toPath());
} catch (IOException e) {
System.out.println("Failed to delete cached message file “" + fileEntry + "”: " + e.getMessage());
}
} }
} }
} }
@ -1069,12 +1072,12 @@ class Manager implements Signal {
save(); save();
handler.handleMessage(envelope, content, exception); handler.handleMessage(envelope, content, exception);
if (exception == null || !(exception instanceof org.whispersystems.libsignal.UntrustedIdentityException)) { if (exception == null || !(exception instanceof org.whispersystems.libsignal.UntrustedIdentityException)) {
File cacheFile = null;
try { try {
File cacheFile = getMessageCacheFile(envelope.getSource(), now, envelope.getTimestamp()); cacheFile = getMessageCacheFile(envelope.getSource(), now, envelope.getTimestamp());
cacheFile.delete(); Files.delete(cacheFile.toPath());
} catch (IOException e) { } catch (IOException e) {
// Ignoring System.out.println("Failed to delete cached message file “" + cacheFile + "”: " + e.getMessage());
return;
} }
} }
} }
@ -1114,8 +1117,10 @@ class Manager implements Signal {
} }
} }
if (syncMessage.getGroups().isPresent()) { if (syncMessage.getGroups().isPresent()) {
File tmpFile = null;
try { try {
DeviceGroupsInputStream s = new DeviceGroupsInputStream(retrieveAttachmentAsStream(syncMessage.getGroups().get().asPointer())); tmpFile = Util.createTempFile();
DeviceGroupsInputStream s = new DeviceGroupsInputStream(retrieveAttachmentAsStream(syncMessage.getGroups().get().asPointer(), tmpFile));
DeviceGroup g; DeviceGroup g;
while ((g = s.read()) != null) { while ((g = s.read()) != null) {
GroupInfo syncGroup = groupStore.getGroup(g.getId()); GroupInfo syncGroup = groupStore.getGroup(g.getId());
@ -1135,14 +1140,24 @@ class Manager implements Signal {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (tmpFile != null) {
try {
Files.delete(tmpFile.toPath());
} catch (IOException e) {
System.out.println("Failed to delete temp file “" + tmpFile + "”: " + e.getMessage());
}
}
} }
if (syncMessage.getBlockedList().isPresent()) { if (syncMessage.getBlockedList().isPresent()) {
// TODO store list of blocked numbers // TODO store list of blocked numbers
} }
} }
if (syncMessage.getContacts().isPresent()) { if (syncMessage.getContacts().isPresent()) {
File tmpFile = null;
try { try {
DeviceContactsInputStream s = new DeviceContactsInputStream(retrieveAttachmentAsStream(syncMessage.getContacts().get().asPointer())); tmpFile = Util.createTempFile();
DeviceContactsInputStream s = new DeviceContactsInputStream(retrieveAttachmentAsStream(syncMessage.getContacts().get().asPointer(), tmpFile));
DeviceContact c; DeviceContact c;
while ((c = s.read()) != null) { while ((c = s.read()) != null) {
ContactInfo contact = contactStore.getContact(c.getNumber()); ContactInfo contact = contactStore.getContact(c.getNumber());
@ -1164,6 +1179,14 @@ class Manager implements Signal {
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
if (tmpFile != null) {
try {
Files.delete(tmpFile.toPath());
} catch (IOException e) {
System.out.println("Failed to delete temp file “" + tmpFile + "”: " + e.getMessage());
}
}
} }
} }
} }
@ -1305,7 +1328,7 @@ class Manager implements Signal {
final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, deviceId, signalingKey, USER_AGENT); final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, deviceId, signalingKey, USER_AGENT);
File tmpFile = File.createTempFile("ts_attach_" + pointer.getId(), ".tmp"); File tmpFile = Util.createTempFile();
InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile); InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile);
OutputStream output = null; OutputStream output = null;
@ -1327,20 +1350,16 @@ class Manager implements Signal {
input.close(); input.close();
try { try {
Files.delete(tmpFile.toPath()); Files.delete(tmpFile.toPath());
} catch(Exception e) { } catch (IOException e) {
System.out.println("Failed to delete temp file: " + tmpFile); System.out.println("Failed to delete temp file “" + tmpFile + "”: " + e.getMessage());
e.printStackTrace();
} }
} }
return outputFile; return outputFile;
} }
private InputStream retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer) throws IOException, InvalidMessageException { private InputStream retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer, File tmpFile) throws IOException, InvalidMessageException {
final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, deviceId, signalingKey, USER_AGENT); final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, deviceId, signalingKey, USER_AGENT);
File file = File.createTempFile("ts_tmp", "tmp"); return messageReceiver.retrieveAttachment(pointer, tmpFile);
file.deleteOnExit();
return messageReceiver.retrieveAttachment(pointer, file);
} }
private String canonicalizeNumber(String number) throws InvalidNumberException { private String canonicalizeNumber(String number) throws InvalidNumberException {
@ -1359,7 +1378,7 @@ class Manager implements Signal {
} }
private void sendGroups() throws IOException, UntrustedIdentityException { private void sendGroups() throws IOException, UntrustedIdentityException {
File groupsFile = File.createTempFile("multidevice-group-update", ".tmp"); File groupsFile = Util.createTempFile();
try { try {
DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(new FileOutputStream(groupsFile)); DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(new FileOutputStream(groupsFile));
@ -1374,26 +1393,27 @@ class Manager implements Signal {
} }
if (groupsFile.exists() && groupsFile.length() > 0) { if (groupsFile.exists() && groupsFile.length() > 0) {
FileInputStream contactsFileStream = new FileInputStream(groupsFile); try (FileInputStream groupsFileStream = new FileInputStream(groupsFile)) {
try {
SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder() SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder()
.withStream(contactsFileStream) .withStream(groupsFileStream)
.withContentType("application/octet-stream") .withContentType("application/octet-stream")
.withLength(groupsFile.length()) .withLength(groupsFile.length())
.build(); .build();
sendSyncMessage(SignalServiceSyncMessage.forGroups(attachmentStream)); sendSyncMessage(SignalServiceSyncMessage.forGroups(attachmentStream));
} finally {
contactsFileStream.close();
} }
} }
} finally { } finally {
groupsFile.delete(); try {
Files.delete(groupsFile.toPath());
} catch (IOException e) {
System.out.println("Failed to delete temp file “" + groupsFile + "”: " + e.getMessage());
}
} }
} }
private void sendContacts() throws IOException, UntrustedIdentityException { private void sendContacts() throws IOException, UntrustedIdentityException {
File contactsFile = File.createTempFile("multidevice-contact-update", ".tmp"); File contactsFile = Util.createTempFile();
try { try {
DeviceContactsOutputStream out = new DeviceContactsOutputStream(new FileOutputStream(contactsFile)); DeviceContactsOutputStream out = new DeviceContactsOutputStream(new FileOutputStream(contactsFile));
@ -1407,7 +1427,7 @@ class Manager implements Signal {
} }
if (contactsFile.exists() && contactsFile.length() > 0) { if (contactsFile.exists() && contactsFile.length() > 0) {
FileInputStream contactsFileStream = new FileInputStream(contactsFile); try (FileInputStream contactsFileStream = new FileInputStream(contactsFile)) {
SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder() SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder()
.withStream(contactsFileStream) .withStream(contactsFileStream)
.withContentType("application/octet-stream") .withContentType("application/octet-stream")
@ -1416,8 +1436,13 @@ class Manager implements Signal {
sendSyncMessage(SignalServiceSyncMessage.forContacts(attachmentStream)); sendSyncMessage(SignalServiceSyncMessage.forContacts(attachmentStream));
} }
}
} finally { } finally {
contactsFile.delete(); try {
Files.delete(contactsFile.toPath());
} catch (IOException e) {
System.out.println("Failed to delete temp file “" + contactsFile + "”: " + e.getMessage());
}
} }
} }

View file

@ -1,5 +1,7 @@
package org.asamk.signal; package org.asamk.signal;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import java.security.SecureRandom;
@ -22,4 +24,8 @@ class Util {
throw new AssertionError(e); throw new AssertionError(e);
} }
} }
public static File createTempFile() throws IOException {
return File.createTempFile("signal_tmp_", ".tmp");
}
} }