Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/file/Files/DeleteOnClose.java
38828 views
/*1* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.nio.file.*;24import static java.nio.file.StandardOpenOption.*;25import java.io.*;26import java.util.*;2728public class DeleteOnClose {2930public static void main(String[] args) throws IOException {31// open file but do not close it. Its existance will be checked by32// the calling script.33Files.newByteChannel(Paths.get(args[0]), READ, WRITE, DELETE_ON_CLOSE);3435// check temporary file has been deleted after closing it36Path file = Files.createTempFile("blah", "tmp");37Files.newByteChannel(file, READ, WRITE, DELETE_ON_CLOSE).close();38if (Files.exists(file))39throw new RuntimeException("Temporary file was not deleted");4041Path dir = Files.createTempDirectory("blah");42try {43// check that DELETE_ON_CLOSE fails when file is a sym link44if (TestUtil.supportsLinks(dir)) {45file = dir.resolve("foo");46Files.createFile(file);47Path link = dir.resolve("link");48Files.createSymbolicLink(link, file);49try {50Files.newByteChannel(link, READ, WRITE, DELETE_ON_CLOSE);51throw new RuntimeException("IOException expected");52} catch (IOException ignore) { }53}5455// check that DELETE_ON_CLOSE works with files created via open56// directories57try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {58if (stream instanceof SecureDirectoryStream) {59SecureDirectoryStream<Path> secure = (SecureDirectoryStream<Path>)stream;60file = Paths.get("foo");6162Set<OpenOption> opts = new HashSet<>();63opts.add(WRITE);64opts.add(DELETE_ON_CLOSE);65secure.newByteChannel(file, opts).close();6667if (Files.exists(dir.resolve(file)))68throw new RuntimeException("File not deleted");69}70}71} finally {72TestUtil.removeAll(dir);73}74}75}767778