893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Aimport java.nio.file.*;
893N/Aimport static java.nio.file.StandardOpenOption.*;
893N/Aimport java.io.*;
893N/Aimport java.util.*;
893N/A
893N/Apublic class DeleteOnClose {
893N/A
893N/A public static void main(String[] args) throws IOException {
893N/A // open file but do not close it. Its existance will be checked by
893N/A // the calling script.
3471N/A Files.newByteChannel(Paths.get(args[0]), READ, WRITE, DELETE_ON_CLOSE);
893N/A
893N/A // check temporary file has been deleted after closing it
3471N/A Path file = Files.createTempFile("blah", "tmp");
3471N/A Files.newByteChannel(file, READ, WRITE, DELETE_ON_CLOSE).close();
3471N/A if (Files.exists(file))
893N/A throw new RuntimeException("Temporary file was not deleted");
893N/A
3471N/A Path dir = Files.createTempDirectory("blah");
893N/A try {
893N/A // check that DELETE_ON_CLOSE fails when file is a sym link
893N/A if (TestUtil.supportsLinks(dir)) {
3471N/A file = dir.resolve("foo");
3471N/A Files.createFile(file);
3471N/A Path link = dir.resolve("link");
3471N/A Files.createSymbolicLink(link, file);
893N/A try {
3471N/A Files.newByteChannel(link, READ, WRITE, DELETE_ON_CLOSE);
893N/A throw new RuntimeException("IOException expected");
893N/A } catch (IOException ignore) { }
893N/A }
893N/A
893N/A // check that DELETE_ON_CLOSE works with files created via open
893N/A // directories
3471N/A try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
893N/A if (stream instanceof SecureDirectoryStream) {
3471N/A SecureDirectoryStream<Path> secure = (SecureDirectoryStream<Path>)stream;
893N/A file = Paths.get("foo");
893N/A
3471N/A Set<OpenOption> opts = new HashSet<>();
893N/A opts.add(WRITE);
893N/A opts.add(DELETE_ON_CLOSE);
893N/A secure.newByteChannel(file, opts).close();
893N/A
3471N/A if (Files.exists(dir.resolve(file)))
893N/A throw new RuntimeException("File not deleted");
893N/A }
893N/A }
893N/A } finally {
893N/A TestUtil.removeAll(dir);
893N/A }
893N/A }
893N/A}