Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/attribute/BasicFileAttributeView/UnixSocketFile.java
41155 views
1
/*
2
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/* @test
25
* @bug 8139133
26
* @summary Verify ability to set time attributes of socket files with no device
27
* @requires os.family == "linux"
28
*/
29
30
import java.io.File;
31
import java.io.InputStream;
32
import java.io.IOException;
33
import java.nio.file.FileSystem;
34
import java.nio.file.FileSystems;
35
import java.nio.file.Files;
36
import java.nio.file.Path;
37
import java.nio.file.Paths;
38
import java.nio.file.StandardWatchEventKinds;
39
import java.nio.file.WatchKey;
40
import java.nio.file.WatchService;
41
import java.nio.file.attribute.BasicFileAttributeView;
42
import java.nio.file.attribute.BasicFileAttributes;
43
import java.nio.file.attribute.FileTime;
44
45
public class UnixSocketFile {
46
private static final String TEST_SUB_DIR = "UnixSocketFile";
47
private static final String SOCKET_FILE_NAME = "mysocket";
48
private static final String CMD_BASE = "nc -lU";
49
50
public static void main(String[] args)
51
throws InterruptedException, IOException {
52
53
// Use 'which' to verify that 'nc' is available and skip the test
54
// if it is not.
55
Process proc = Runtime.getRuntime().exec("which nc");
56
InputStream stdout = proc.getInputStream();
57
int b = stdout.read();
58
proc.destroy();
59
if (b == -1) {
60
System.err.println("Netcat command unavailable; skipping test.");
61
return;
62
}
63
64
// Verify that 'nc' accepts '-U' for Unix domain sockets.
65
// Skip the test if it is not.
66
Process procHelp = Runtime.getRuntime().exec(CMD_BASE + " -h");
67
if (procHelp.waitFor() != 0) {
68
System.err.println("Netcat does not accept required options; skipping test.");
69
return;
70
}
71
72
// Create a new sub-directory of the nominal test directory in which
73
// 'nc' will create the socket file.
74
String testSubDir = System.getProperty("test.dir", ".")
75
+ File.separator + TEST_SUB_DIR;
76
Path socketTestDir = Paths.get(testSubDir);
77
Files.createDirectory(socketTestDir);
78
79
// Set the path of the socket file.
80
String socketFilePath = testSubDir + File.separator
81
+ SOCKET_FILE_NAME;
82
83
// Create a process which executes the nc (netcat) utility to create
84
// a socket file at the indicated location.
85
FileSystem fs = FileSystems.getDefault();
86
try (WatchService ws = fs.newWatchService()) {
87
// Watch the test sub-directory to receive notification when an
88
// entry, i.e., the socket file, is added to the sub-directory.
89
WatchKey wk = socketTestDir.register(ws,
90
StandardWatchEventKinds.ENTRY_CREATE);
91
92
// Execute the 'nc' command.
93
proc = Runtime.getRuntime().exec(CMD_BASE + " " + socketFilePath);
94
95
// Wait until the socket file is created.
96
WatchKey key = ws.take();
97
if (key != wk) {
98
throw new RuntimeException("Unknown entry created - expected: "
99
+ wk.watchable() + ", actual: " + key.watchable());
100
}
101
wk.cancel();
102
}
103
104
// Verify that the socket file in fact exists.
105
Path socketPath = fs.getPath(socketFilePath);
106
if (!Files.exists(socketPath)) {
107
throw new RuntimeException("Socket file " + socketFilePath
108
+ " was not created by \"nc\" command.");
109
}
110
111
// Retrieve the most recent access and modification times of the
112
// socket file; print the values.
113
BasicFileAttributeView attributeView = Files.getFileAttributeView(
114
socketPath, BasicFileAttributeView.class);
115
BasicFileAttributes oldAttributes = attributeView.readAttributes();
116
FileTime oldAccessTime = oldAttributes.lastAccessTime();
117
FileTime oldModifiedTime = oldAttributes.lastModifiedTime();
118
System.out.println("Old times: " + oldAccessTime
119
+ " " + oldModifiedTime);
120
121
// Calculate the time to which the access and modification times of the
122
// socket file will be changed.
123
FileTime newFileTime =
124
FileTime.fromMillis(oldAccessTime.toMillis() + 1066);
125
126
try {
127
// Set the access and modification times of the socket file.
128
attributeView.setTimes(newFileTime, newFileTime, null);
129
130
// Retrieve the updated access and modification times of the
131
// socket file; print the values.
132
FileTime newAccessTime = null;
133
FileTime newModifiedTime = null;
134
BasicFileAttributes newAttributes = attributeView.readAttributes();
135
newAccessTime = newAttributes.lastAccessTime();
136
newModifiedTime = newAttributes.lastModifiedTime();
137
System.out.println("New times: " + newAccessTime + " "
138
+ newModifiedTime);
139
140
// Verify that the updated times have the expected values.
141
if ((newAccessTime != null && !newAccessTime.equals(newFileTime))
142
|| (newModifiedTime != null
143
&& !newModifiedTime.equals(newFileTime))) {
144
throw new RuntimeException("Failed to set correct times.");
145
}
146
} finally {
147
// Destry the process running netcat and delete the socket file.
148
proc.destroy();
149
Files.delete(socketPath);
150
}
151
}
152
}
153
154