Skip to content
Permalink
Browse files

Improve accessibility check per #75

  • Loading branch information
mordechaim committed Jan 13, 2020
1 parent a1acf63 commit 0ef7373c2d3550f777d642068a29ef70fb6a9eeb
@@ -239,7 +239,7 @@ private static void completeDownloads(Map<FileMetadata, Path> files, Path tempDi
}

for (FileMetadata fm : files.keySet()) {
FileUtils.verifyNotLocked(fm.getNormalizedPath());
FileUtils.verifyAccessible(fm.getNormalizedPath());
}

// mimic a single transaction.
@@ -266,7 +266,7 @@ private static void completeDownloads(Map<FileMetadata, Path> files, Path tempDi
out.writeObject(updateTempData);
}

FileUtils.windowsHide(updateDataFile);
FileUtils.windowsHidden(updateDataFile, true);
}
}
}
@@ -189,7 +189,7 @@ public static void execute(List<String> args, Consumer<? super List<String>> onN
FileLock lock = randomAccess.getChannel().tryLock();

if (lock != null) {
FileUtils.windowsHide(lockFile);
FileUtils.windowsHidden(lockFile, true);

ServerSocket server = new ServerSocket(0, 0, InetAddress.getByName(null));

@@ -212,7 +212,7 @@ public void run() {
out.write("" + server.getLocalPort());
}

FileUtils.windowsHide(portFile);
FileUtils.windowsHidden(portFile, false);

Thread listen = new Thread(() -> {
while (!server.isClosed()) {
@@ -109,7 +109,7 @@ public static boolean finalizeUpdate(Path tempDir) throws IOException {

try {
for (Map.Entry<Path, Path> e : files.entrySet()) {
FileUtils.verifyNotLocked(e.getValue());
FileUtils.verifyAccessible(e.getValue());
}
} catch (FileSystemException fse) {
String msg = fse.getMessage();
@@ -19,13 +19,16 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.file.AccessDeniedException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
@@ -186,46 +189,31 @@ public static boolean isEmptyDirectory(Path path) throws IOException {
return false;
}

public static void windowsHide(Path file) {
public static void windowsHidden(Path file, boolean hidden) {
if (OS.CURRENT != OS.WINDOWS)
return;

try {
Files.setAttribute(file, "dos:hidden", true);
Files.setAttribute(file, "dos:hidden", hidden);
} catch (Exception e) {
}
}

public static void windowsUnhide(Path file) {
if (OS.CURRENT != OS.WINDOWS)
return;
public static void verifyAccessible(Path path) throws IOException {
boolean exists = Files.exists(path);

try {
Files.setAttribute(file, "dos:hidden", false);
} catch (Exception e) {
}
}

public static void verifyNotLocked(Path path) throws IOException {
if (OS.CURRENT != OS.WINDOWS)
return;

if (!Files.isRegularFile(path)) {
return;
}

Path temp = Files.createTempFile(path.getParent(), null, null);

try {
Files.move(path, temp, StandardCopyOption.REPLACE_EXISTING);
Files.move(temp, path);
if (exists && !Files.isWritable(path))
throw new AccessDeniedException(path.toString());

try (Writer out = Files.newBufferedWriter(path, exists ? StandardOpenOption.APPEND : StandardOpenOption.CREATE)) {
} finally {
Files.deleteIfExists(temp);
if(!exists)
Files.deleteIfExists(path);
}

}

public static void secureMoveFile(Path source, Path target) throws IOException {
// for windows we can't go wrong because the OS manages locking
if (OS.CURRENT == OS.WINDOWS || Files.notExists(target)) {
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
return;

0 comments on commit 0ef7373

Please sign in to comment.
You can’t perform that action at this time.