1 /*
2 * Copyright (c) 2009, 2018, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
411 // returns the list of child paths of "path"
412 Iterator<Path> iteratorOf(ZipPath dir,
413 DirectoryStream.Filter<? super Path> filter)
414 throws IOException
415 {
416 beginWrite(); // iteration of inodes needs exclusive lock
417 try {
418 ensureOpen();
419 byte[] path = dir.getResolvedPath();
420 IndexNode inode = getInode(path);
421 if (inode == null)
422 throw new NotDirectoryException(getString(path));
423 List<Path> list = new ArrayList<>();
424 IndexNode child = inode.child;
425 while (child != null) {
426 // (1) assume all path from zip file itself is "normalized"
427 // (2) IndexNode.name is absolute. see IndexNode(byte[],int,int)
428 // (3) if parent "dir" is relative when ZipDirectoryStream
429 // is created, the returned child path needs to be relative
430 // as well.
431 byte[] cname = child.name;
432 if (!dir.isAbsolute()) {
433 cname = Arrays.copyOfRange(cname, 1, cname.length);
434 }
435 ZipPath zpath = new ZipPath(this, cname, true);
436 if (filter == null || filter.accept(zpath))
437 list.add(zpath);
438 child = child.sibling;
439 }
440 return list.iterator();
441 } finally {
442 endWrite();
443 }
444 }
445
446 void createDirectory(byte[] dir, FileAttribute<?>... attrs)
447 throws IOException
448 {
449 checkWritable();
450 // dir = toDirectoryPath(dir);
451 beginWrite();
452 try {
453 ensureOpen();
454 if (dir.length == 0 || exists(dir)) // root dir, or exiting dir
455 throw new FileAlreadyExistsException(getString(dir));
|
1 /*
2 * Copyright (c) 2009, 2019, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
411 // returns the list of child paths of "path"
412 Iterator<Path> iteratorOf(ZipPath dir,
413 DirectoryStream.Filter<? super Path> filter)
414 throws IOException
415 {
416 beginWrite(); // iteration of inodes needs exclusive lock
417 try {
418 ensureOpen();
419 byte[] path = dir.getResolvedPath();
420 IndexNode inode = getInode(path);
421 if (inode == null)
422 throw new NotDirectoryException(getString(path));
423 List<Path> list = new ArrayList<>();
424 IndexNode child = inode.child;
425 while (child != null) {
426 // (1) assume all path from zip file itself is "normalized"
427 // (2) IndexNode.name is absolute. see IndexNode(byte[],int,int)
428 // (3) if parent "dir" is relative when ZipDirectoryStream
429 // is created, the returned child path needs to be relative
430 // as well.
431 // (4) if parent "dir" starts with './' or is '.' when ZipDirectoryStream
432 // is created, the returned child path needs to also start
433 // with './' as well.
434 byte[] cname = child.name;
435 ZipPath zpath;
436
437 if (dir.isAbsolute()) {
438 zpath = new ZipPath(this, cname, true);
439 } else {
440 ZipPath childPath = new ZipPath(this, cname, true);
441 ZipPath childFileName = childPath.getFileName();
442 zpath = dir.resolve(childFileName);
443 }
444 if (filter == null || filter.accept(zpath))
445 list.add(zpath);
446 child = child.sibling;
447 }
448 return list.iterator();
449 } finally {
450 endWrite();
451 }
452 }
453
454 void createDirectory(byte[] dir, FileAttribute<?>... attrs)
455 throws IOException
456 {
457 checkWritable();
458 // dir = toDirectoryPath(dir);
459 beginWrite();
460 try {
461 ensureOpen();
462 if (dir.length == 0 || exists(dir)) // root dir, or exiting dir
463 throw new FileAlreadyExistsException(getString(dir));
|