Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NetPath.java « fs « nio « sun « openjdk - github.com/mono/ikvm-fork.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6861a1e27f8d205d4ebcef46de20d1cab6840803 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
  Copyright (C) 2011 Jeroen Frijters

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  Jeroen Frijters
  jeroen@frijters.net

*/

package sun.nio.fs;

import com.sun.nio.file.ExtendedWatchEventModifier;
import com.sun.nio.file.SensitivityWatchEventModifier;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.Iterator;
import static ikvm.internal.Util.MACOSX;
import static ikvm.internal.Util.WINDOWS;

final class NetPath extends AbstractPath
{
    private static final char[] invalid = cli.System.IO.Path.GetInvalidFileNameChars();
    private final NetFileSystem fs;
    final String path;

    NetPath(NetFileSystem fs, String path)
    {
        if (WINDOWS)
        {
            path = WindowsPathParser.parse(path).path();
        }
        else
        {
            StringBuilder sb = null;
            int separatorCount = 0;
            boolean prevWasSeparator = false;
            for (int i = 0; i < path.length(); i++)
            {
                char c = path.charAt(i);
                if (c == 0)
                {
                    throw new InvalidPathException(path, "Nul character not allowed");
                }
                else if (c == '/')
                {
                    if (prevWasSeparator)
                    {
                        if (sb == null)
                        {
                            sb = new StringBuilder();
                            sb.append(path, 0, i);
                        }
                        continue;
                    }
                    separatorCount++;
                    prevWasSeparator = true;
                }
                else
                {
                    prevWasSeparator = false;
                }
                if (sb != null)
                {
                    sb.append(c);
                }
            }
            if (sb != null)
            {
                path = sb.toString();
            }
            if (path.length() > 1 && path.charAt(path.length() - 1) == '/')
            {
                path = path.substring(0, path.length() - 1);
            }
        }
        this.fs = fs;
        this.path = path;
    }

    public FileSystem getFileSystem()
    {
        return fs;
    }

    public boolean isAbsolute()
    {
        return cli.System.IO.Path.IsPathRooted(path)
            && (!WINDOWS || path.startsWith("\\\\") || (path.length() >= 3 && path.charAt(1) == ':' && path.charAt(2) == '\\'));
    }

    public Path getRoot()
    {
        int len = getRootLength();
        return len == 0 ? null : new NetPath(fs, path.substring(0, len));
    }

    private int getRootLength()
    {
        if (WINDOWS)
        {
            if (path.length() >= 2 && path.charAt(1) == ':')
            {
                if (path.length() >= 3 && path.charAt(2) == '\\')
                {
                    return 3;
                }
                else
                {
                    return 2;
                }
            }
            else if (path.startsWith("\\\\"))
            {
                return path.indexOf('\\', path.indexOf('\\', 2) + 1) + 1;
            }
        }

        if (path.length() >= 1 && path.charAt(0) == cli.System.IO.Path.DirectorySeparatorChar)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

    public Path getFileName()
    {
        if (path.length() == 0)
        {
            return this;
        }
        if (path.length() == getRootLength())
        {
            return null;
        }
        String name = cli.System.IO.Path.GetFileName(path);
        if (name == null || name.length() == 0)
        {
            return null;
        }
        return new NetPath(fs, name);
    }

    public Path getParent()
    {
        if (path.length() == getRootLength())
        {
            return null;
        }
        String parent = cli.System.IO.Path.GetDirectoryName(path);
        if (parent == null || parent.length() == 0)
        {
            return null;
        }
        return new NetPath(fs, parent);
    }

    public int getNameCount()
    {
        int len = getRootLength();
        if (path.length() == len)
        {
            return len == 0 ? 1 : 0;
        }
        int count = 1;
        for (int i = len; i < path.length(); i++)
        {
            if (path.charAt(i) == cli.System.IO.Path.DirectorySeparatorChar)
            {
                count++;
            }
        }
        return count;
    }

    public Path getName(int index)
    {
        return new NetPath(fs, getNameImpl(index));
    }

    private String getNameImpl(int index)
    {
        for (int pos = getRootLength(); pos < path.length(); index--)
        {
            int next = path.indexOf(cli.System.IO.Path.DirectorySeparatorChar, pos);
            if (index == 0)
            {
                return next == -1 ? path.substring(pos) : path.substring(pos, next);
            }
            if (next == -1)
            {
                break;
            }
            pos = next + 1;
        }
        if (path.length() == 0 && index == 0)
        {
            return "";
        }
        throw new IllegalArgumentException();
    }

    public Path subpath(int beginIndex, int endIndex)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = beginIndex; i < endIndex; i++)
        {
            if (i != beginIndex)
            {
                sb.append(cli.System.IO.Path.DirectorySeparatorChar);
            }
            sb.append(getNameImpl(i));
        }
        return new NetPath(fs, sb.toString());
    }

    public boolean startsWith(Path other)
    {
        String npath = NetPath.from(other).path;
        if (npath.length() == 0)
        {
            return path.length() == 0;
        }
        return path.regionMatches(WINDOWS, 0, npath, 0, npath.length())
            && (npath.length() == getRootLength()
                || (npath.length() > getRootLength()
                    && (path.length() == npath.length()
                        || (path.length() > npath.length() && path.charAt(npath.length()) == cli.System.IO.Path.DirectorySeparatorChar))));
    }

    public boolean endsWith(Path other)
    {
        NetPath nother = NetPath.from(other);
        String npath = nother.path;
        if (npath.length() > path.length())
        {
            return false;
        }
        if (npath.length() == 0)
        {
            return path.length() == 0;
        }
        int nameCount = getNameCount();
        int otherNameCount = nother.getNameCount();
        if (otherNameCount > nameCount)
        {
            return false;
        }
        int otherRootLength = nother.getRootLength();
        if (otherRootLength > 0)
        {
            if (otherNameCount != nameCount
                || getRootLength() != otherRootLength
                || !path.regionMatches(WINDOWS, 0, npath, 0, otherRootLength))
            {
                return false;
            }
        }
        int skip = nameCount - otherNameCount;
        for (int i = 0; i < otherNameCount; i++)
        {
            String s1 = getNameImpl(i + skip);
            String s2 = nother.getNameImpl(i);
            if (s1.length() != s2.length() || !s1.regionMatches(WINDOWS, 0, s2, 0, s1.length()))
            {
                return false;
            }
        }
        return true;
    }

    public Path normalize()
    {
        int rootLength = getRootLength();
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0, count = getNameCount(); i < count; i++)
        {
            String s = getNameImpl(i);
            if (s.equals(".."))
            {
                if (list.size() == 0)
                {
                    if (rootLength == 0 || (WINDOWS && rootLength == 2))
                    {
                        list.add("..");
                    }
                }
                else if (list.get(list.size() - 1).equals(".."))
                {
                    list.add("..");
                }
                else
                {
                    list.remove(list.size() - 1);
                }
            }
            else if (!s.equals("."))
            {
                list.add(s);
            }
        }
        StringBuilder sb = new StringBuilder();
        sb.append(path.substring(0, getRootLength()));
        for (int i = 0; i < list.size(); i++)
        {
            if (i != 0)
            {
                sb.append(cli.System.IO.Path.DirectorySeparatorChar);
            }
            sb.append(list.get(i));
        }
        return new NetPath(fs, sb.toString());
    }

    public Path resolve(Path other)
    {
        NetPath nother = NetPath.from(other);
        String npath = nother.path;
        if (nother.isAbsolute())
        {
            return other;
        }
        if (npath.length() == 0)
        {
            return this;
        }
        if (WINDOWS)
        {
            if (nother.getRootLength() == 2 && getRootLength() == 3 && (path.charAt(0) | 0x20) == (npath.charAt(0) | 0x20))
            {
                // we're in the case where we have a root "x:\" and other "x:", so we have to chop off "x:" from other because
                // otherwise Path.Combine will just return other
                npath = npath.substring(2);
            }
            else if (nother.getRootLength() == 1 && getRootLength() > 3)
            {
                // we're in the case where we have a root "\\host\share\" and other "\",
                // we have to manually handle this because Path.Combine doesn't do the right thing
                return new NetPath(fs, path.substring(0, getRootLength()) + npath);
            }
        }
        return new NetPath(fs, cli.System.IO.Path.Combine(path, npath));
    }

    public Path relativize(Path other)
    {
        NetPath nother = NetPath.from(other);
        if (equals(nother))
        {
            return new NetPath(fs, "");
        }
        int rootLength = getRootLength();
        if (nother.getRootLength() != rootLength || !path.regionMatches(true, 0, nother.path, 0, rootLength))
        {
            throw new IllegalArgumentException("'other' has different root");
        }
        int nameCount = getNameCount();
        int otherNameCount = nother.getNameCount();
        int count = Math.min(nameCount, otherNameCount);
        int i = 0;
        // skip the common parts
        for (; i < count && getNameImpl(i).equals(nother.getNameImpl(i)); i++)
        {
        }
        // remove the unused parts of our path
        StringBuilder sb = new StringBuilder();
        for (int j = i; j < nameCount; j++)
        {
            sb.append("..\\");
        }
        // append the new parts of other
        for (int j = i; j < otherNameCount; j++)
        {
            if (j != i)
            {
                sb.append("\\");
            }
            sb.append(nother.getNameImpl(j));
        }
        return new NetPath(fs, sb.toString());
    }

    public URI toUri()
    {
        if (WINDOWS)
        {
            return WindowsUriSupport.toUri(this);
        }
        else
        {
            return UnixUriUtils.toUri(this);
        }
    }

    public NetPath toAbsolutePath()
    {
        if (isAbsolute())
        {
            return this;
        }
        // System.getProperty("user.dir") will trigger the specified security check
        return new NetPath(fs, cli.System.IO.Path.GetFullPath(cli.System.IO.Path.Combine(System.getProperty("user.dir"), path)));
    }

    public Path toRealPath(LinkOption... options) throws IOException
    {
        SecurityManager sm = System.getSecurityManager();
        if (sm != null)
        {
            sm.checkRead(path);
            if (!isAbsolute())
            {
                sm.checkPropertyAccess("user.dir");
            }
        }
        return new NetPath(fs, toRealPathImpl(path));
    }
    
    private static native String toRealPathImpl(String path);

    public WatchKey register(WatchService watcher, WatchEvent.Kind<?>[] events, WatchEvent.Modifier... modifiers) throws IOException
    {
        if (!(watcher instanceof NetFileSystem.NetWatchService))
        {
            // null check
            watcher.getClass();
            throw new ProviderMismatchException();
        }
        boolean create = false;
        boolean delete = false;
        boolean modify = false;
        boolean overflow = false;
        boolean subtree = false;
        for (WatchEvent.Kind<?> kind : events)
        {
            if (kind == StandardWatchEventKinds.ENTRY_CREATE)
            {
                create = true;
            }
            else if (kind == StandardWatchEventKinds.ENTRY_DELETE)
            {
                delete = true;
            }
            else if (kind == StandardWatchEventKinds.ENTRY_MODIFY)
            {
                modify = true;
            }
            else if (kind == StandardWatchEventKinds.OVERFLOW)
            {
                overflow = true;
            }
            else
            {
                // null check
                kind.getClass();
                throw new UnsupportedOperationException();
            }
        }
        if (!create && !delete && !modify)
        {
            throw new IllegalArgumentException();
        }
        for (WatchEvent.Modifier modifier : modifiers)
        {
            if (modifier == ExtendedWatchEventModifier.FILE_TREE)
            {
                subtree = true;
            }
            else if (modifier instanceof SensitivityWatchEventModifier)
            {
                // ignore
            }
            else
            {
                // null check
                modifier.getClass();
                throw new UnsupportedOperationException();
            }
        }
        SecurityManager sm = System.getSecurityManager();
        if (sm != null)
        {
            sm.checkRead(path);
            if (subtree)
            {
                sm.checkRead(path + cli.System.IO.Path.DirectorySeparatorChar + '-');
            }
        }
        return ((NetFileSystem.NetWatchService)watcher).register(this, create, delete, modify, overflow, subtree);
    }

    public int compareTo(Path other)
    {
        String path2 = ((NetPath)other).path;
        int len1 = path.length();
        int len2 = path2.length();
        int min = Math.min(len1, len2);
        for (int i = 0; i < min; i++)
        {
            char c1 = path.charAt(i);
            char c2 = path2.charAt(i);
            if (c1 != c2 && Character.toUpperCase(c1) != Character.toUpperCase(c2))
            {
                return c1 - c2;
            }
        }
        return len1 - len2;
    }

    public boolean equals(Object other)
    {
        if (!(other instanceof NetPath))
        {
            return false;
        }
        return compareTo((NetPath)other) == 0;
    }

    public int hashCode()
    {
        int hash = 0;
        for (int i = 0; i < path.length(); i++)
        {
            hash = 97 * hash + Character.toUpperCase(path.charAt(i));
        }
        return hash;
    }

    public String toString()
    {
        return path;
    }

    boolean isUnc()
    {
        return WINDOWS && getRootLength() > 3;
    }

    static NetPath from(Path path)
    {
        if (!(path instanceof NetPath))
        {
            // null check
            path.getClass();
            throw new ProviderMismatchException();
        }
        return (NetPath)path;
    }
}