author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 8559 | 826c03991926 |
child 45178 | 9ff621041842 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8559
diff
changeset
|
2 |
* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
25 |
@bug 4091757 6652379 |
2 | 26 |
@summary Basic test for setLastModified method |
27 |
*/ |
|
28 |
||
29 |
import java.io.*; |
|
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
30 |
import java.nio.ByteBuffer; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
31 |
import java.nio.channels.FileChannel; |
2 | 32 |
|
33 |
||
34 |
public class SetLastModified { |
|
35 |
||
36 |
private static void ck(File f, long nt, long rt) throws Exception { |
|
37 |
if (rt == nt) return; |
|
38 |
if ((rt / 10 == nt / 10) |
|
39 |
|| (rt / 100 == nt / 100) |
|
40 |
|| (rt / 1000 == nt / 1000) |
|
41 |
|| (rt / 10000 == (nt / 10000))) { |
|
42 |
System.err.println(f + ": Time set to " + nt |
|
43 |
+ ", rounded down by filesystem to " + rt); |
|
44 |
return; |
|
45 |
} |
|
46 |
if ((rt / 10 == (nt + 5) / 10) |
|
47 |
|| (rt / 100 == (nt + 50) / 100) |
|
48 |
|| (rt / 1000 == (nt + 500) / 1000) |
|
49 |
|| (rt / 10000 == ((nt + 5000) / 10000))) { |
|
50 |
System.err.println(f + ": Time set to " + nt |
|
51 |
+ ", rounded up by filesystem to " + rt); |
|
52 |
return; |
|
53 |
} |
|
54 |
throw new Exception(f + ": Time set to " + nt |
|
55 |
+ ", then read as " + rt); |
|
56 |
} |
|
57 |
||
58 |
public static void main(String[] args) throws Exception { |
|
59 |
File d = new File(System.getProperty("test.dir", ".")); |
|
60 |
File d2 = new File(d, "x.SetLastModified.dir"); |
|
61 |
File f = new File(d2, "x.SetLastModified"); |
|
62 |
long ot, t; |
|
63 |
||
64 |
/* New time: One week ago */ |
|
65 |
long nt = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 7; |
|
66 |
||
67 |
if (f.exists()) f.delete(); |
|
68 |
if (d2.exists()) d2.delete(); |
|
69 |
if (!d2.mkdir()) { |
|
70 |
throw new Exception("Can't create test directory " + d2); |
|
71 |
} |
|
72 |
||
73 |
boolean threw = false; |
|
74 |
try { |
|
75 |
d2.setLastModified(-nt); |
|
76 |
} catch (IllegalArgumentException x) { |
|
77 |
threw = true; |
|
78 |
} |
|
79 |
if (!threw) |
|
80 |
throw new Exception("setLastModified succeeded with a negative time"); |
|
81 |
||
82 |
ot = d2.lastModified(); |
|
83 |
if (ot != 0) { |
|
84 |
if (d2.setLastModified(nt)) { |
|
85 |
ck(d2, nt, d2.lastModified()); |
|
86 |
d2.setLastModified(ot); |
|
87 |
} else { |
|
88 |
System.err.println("Warning: setLastModified on directories " |
|
89 |
+ "not supported"); |
|
90 |
} |
|
91 |
} |
|
92 |
||
93 |
if (f.exists()) { |
|
94 |
if (!f.delete()) |
|
95 |
throw new Exception("Can't delete test file " + f); |
|
96 |
} |
|
97 |
if (f.setLastModified(nt)) |
|
98 |
throw new Exception("Succeeded on non-existent file: " + f); |
|
99 |
||
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
100 |
// set/check last modified on files of size 1, 1GB+1, 2GB+1, .. |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
101 |
// On Windows we only test with a tiny file as that platform doesn't |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
102 |
// support sparse files by default and so the test takes too long. |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
103 |
final long G = 1024L * 1024L * 1024L; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
104 |
final long MAX_POSITION = |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
105 |
System.getProperty("os.name").startsWith("Windows") ? 0L : 3L*G; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
106 |
long pos = 0L; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
107 |
while (pos <= MAX_POSITION) { |
8559 | 108 |
try (FileChannel fc = new FileOutputStream(f).getChannel()) { |
109 |
fc.position(pos).write(ByteBuffer.wrap("x".getBytes())); |
|
110 |
} |
|
692
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
111 |
ot = f.lastModified(); |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
112 |
System.out.format("check with file size: %d\n", f.length()); |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
113 |
if (!f.setLastModified(nt)) |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
114 |
throw new Exception("setLastModified failed on file: " + f); |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
115 |
ck(f, nt, f.lastModified()); |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
116 |
pos += G; |
50a393caed65
6652379: File.setLastModified fails on large files (lnx only)
alanb
parents:
2
diff
changeset
|
117 |
} |
2 | 118 |
|
119 |
if (!f.delete()) throw new Exception("Can't delete test file " + f); |
|
120 |
if (!d2.delete()) throw new Exception("Can't delete test directory " + d2); |
|
121 |
} |
|
122 |
||
123 |
} |