mirror of
https://github.com/openjdk/jdk.git
synced 2026-01-28 12:09:14 +00:00
8367284: (fs) Support current working directory target in SecureDirectoryStream.move
Reviewed-by: alanb
This commit is contained in:
parent
26aab3cccd
commit
07f6617e0b
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2007, 2026, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -185,8 +185,8 @@ public interface SecureDirectoryStream<T>
|
||||
/**
|
||||
* Move a file from this directory to another directory.
|
||||
*
|
||||
* <p> This method works in a similar manner to {@link Files#move move}
|
||||
* method when the {@link StandardCopyOption#ATOMIC_MOVE ATOMIC_MOVE} option
|
||||
* <p> This method works in a similar manner to {@link Files#move Files.move}
|
||||
* when the {@link StandardCopyOption#ATOMIC_MOVE ATOMIC_MOVE} option
|
||||
* is specified. That is, this method moves a file as an atomic file system
|
||||
* operation. If the {@code srcpath} parameter is an {@link Path#isAbsolute
|
||||
* absolute} path then it locates the source file. If the parameter is a
|
||||
@ -194,14 +194,15 @@ public interface SecureDirectoryStream<T>
|
||||
* the {@code targetpath} parameter is absolute then it locates the target
|
||||
* file (the {@code targetdir} parameter is ignored). If the parameter is
|
||||
* a relative path it is located relative to the open directory identified
|
||||
* by the {@code targetdir} parameter. In all cases, if the target file
|
||||
* exists then it is implementation specific if it is replaced or this
|
||||
* method fails.
|
||||
* by the {@code targetdir} parameter, unless {@code targetdir} is
|
||||
* {@code null}, in which case it is located relative to the current
|
||||
* working directory. In all cases, if the target file exists then it is
|
||||
* implementation specific if it is replaced or this method fails.
|
||||
*
|
||||
* @param srcpath
|
||||
* the name of the file to move
|
||||
* @param targetdir
|
||||
* the destination directory
|
||||
* the destination directory; can be {@code null}
|
||||
* @param targetpath
|
||||
* the name to give the file in the destination directory
|
||||
*
|
||||
|
||||
@ -202,21 +202,21 @@ class UnixSecureDirectoryStream
|
||||
{
|
||||
UnixPath from = getName(fromObj);
|
||||
UnixPath to = getName(toObj);
|
||||
if (dir == null)
|
||||
throw new NullPointerException();
|
||||
if (!(dir instanceof UnixSecureDirectoryStream))
|
||||
if (dir != null && !(dir instanceof UnixSecureDirectoryStream))
|
||||
throw new ProviderMismatchException();
|
||||
UnixSecureDirectoryStream that = (UnixSecureDirectoryStream)dir;
|
||||
int todfd = that != null ? that.dfd : AT_FDCWD;
|
||||
|
||||
// lock ordering doesn't matter
|
||||
this.ds.readLock().lock();
|
||||
try {
|
||||
that.ds.readLock().lock();
|
||||
if (that != null)
|
||||
that.ds.readLock().lock();
|
||||
try {
|
||||
if (!this.ds.isOpen() || !that.ds.isOpen())
|
||||
if (!this.ds.isOpen() || (that != null && !that.ds.isOpen()))
|
||||
throw new ClosedDirectoryStreamException();
|
||||
try {
|
||||
renameat(this.dfd, from.asByteArray(), that.dfd, to.asByteArray());
|
||||
renameat(this.dfd, from.asByteArray(), todfd, to.asByteArray());
|
||||
} catch (UnixException x) {
|
||||
if (x.errno() == EXDEV) {
|
||||
throw new AtomicMoveNotSupportedException(
|
||||
@ -225,7 +225,8 @@ class UnixSecureDirectoryStream
|
||||
x.rethrowAsIOException(from, to);
|
||||
}
|
||||
} finally {
|
||||
that.ds.readLock().unlock();
|
||||
if (that != null)
|
||||
that.ds.readLock().unlock();
|
||||
}
|
||||
} finally {
|
||||
this.ds.readLock().unlock();
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2008, 2026, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -292,6 +292,27 @@ public class SecureDS {
|
||||
}
|
||||
}
|
||||
|
||||
// Test: move to cwd
|
||||
final String TEXT = "Sous le pont Mirabeau coule la Seine";
|
||||
Path file = Path.of("file");
|
||||
Path filepath = dir.resolve(file);
|
||||
Path cwd = Path.of(System.getProperty("user.dir"));
|
||||
Path result = cwd.resolve(file);
|
||||
Files.writeString(filepath, TEXT);
|
||||
try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir);) {
|
||||
if (ds instanceof SecureDirectoryStream<Path> sds) {
|
||||
sds.move(file, null, file);
|
||||
if (!TEXT.equals(Files.readString(result)))
|
||||
throw new RuntimeException(result + " content incorrect");
|
||||
} else {
|
||||
throw new RuntimeException("Not a SecureDirectoryStream");
|
||||
}
|
||||
} finally {
|
||||
boolean fileDeleted = Files.deleteIfExists(filepath);
|
||||
if (!fileDeleted)
|
||||
Files.deleteIfExists(result);
|
||||
}
|
||||
|
||||
// clean-up
|
||||
delete(dir1);
|
||||
delete(dir2);
|
||||
@ -334,10 +355,6 @@ public class SecureDS {
|
||||
stream.move(null, stream, file);
|
||||
shouldNotGetHere();
|
||||
} catch (NullPointerException x) { }
|
||||
try {
|
||||
stream.move(file, null, file);
|
||||
shouldNotGetHere();
|
||||
} catch (NullPointerException x) { }
|
||||
try {
|
||||
stream.move(file, stream, null);
|
||||
shouldNotGetHere();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user