1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.vfs2.filter;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.vfs2.Capability;
22 import org.apache.commons.vfs2.FileFilter;
23 import org.apache.commons.vfs2.FileObject;
24 import org.apache.commons.vfs2.FileSelectInfo;
25 import org.apache.commons.vfs2.FileSystem;
26 import org.apache.commons.vfs2.FileSystemException;
27
28 /**
29 * This filter accepts {@code File}s that can be written to.
30 * <p>
31 * Example, showing how to print out a list of the current directory's
32 * <em>writable</em> files:
33 * </p>
34 *
35 * <pre>
36 * FileSystemManager fsManager = VFS.getManager();
37 * FileObject dir = fsManager.toFileObject(new File("."));
38 * FileObject[] files = dir.findFiles(new FileFilterSelector(CanWriteFileFilter.CAN_WRITE));
39 * for (int i = 0; i < files.length; i++) {
40 * System.out.println(files[i]);
41 * }
42 * </pre>
43 *
44 * <p>
45 * Example, showing how to print out a list of the current directory's
46 * <em>un-writable</em> files:
47 * </p>
48 *
49 * <pre>
50 * FileSystemManager fsManager = VFS.getManager();
51 * FileObject dir = fsManager.toFileObject(new File("."));
52 * FileObject[] files = dir.findFiles(new FileFilterSelector(CanWriteFileFilter.CANNOT_WRITE));
53 * for (int i = 0; i < files.length; i++) {
54 * System.out.println(files[i]);
55 * }
56 * </pre>
57 *
58 * <p>
59 * <strong>N.B.</strong> For read-only files, use
60 * {@code CanReadFileFilter.READ_ONLY}.
61 * </p>
62 *
63 * @author This code was originally ported from Apache Commons IO File Filter
64 * @see "https://commons.apache.org/proper/commons-io/"
65 * @since 2.4
66 */
67 public class CanWriteFileFilter implements FileFilter, Serializable {
68
69 /** Singleton instance of <em>writable</em> filter. */
70 public static final FileFilter CAN_WRITE = new CanWriteFileFilter();
71
72 /** Singleton instance of not <em>writable</em> filter. */
73 public static final FileFilter CANNOT_WRITE = new NotFileFilter(CAN_WRITE);
74
75 private static final long serialVersionUID = 1L;
76
77 /**
78 * Restrictive constructor.
79 */
80 protected CanWriteFileFilter() {
81 }
82
83 /**
84 * Checks to see if the file can be written to.
85 *
86 * @param fileSelectInfo the File to check
87 * @return {@code true} if the file can be written to, otherwise {@code false}.
88 * @throws FileSystemException Thrown for file system errors.
89 */
90 @Override
91 public boolean accept(final FileSelectInfo fileSelectInfo) throws FileSystemException {
92 try (FileObject file = fileSelectInfo.getFile()) {
93 final FileSystem fileSystem = file.getFileSystem();
94 if (file.exists()) {
95 if (!fileSystem.hasCapability(Capability.WRITE_CONTENT)) {
96 return false;
97 }
98 return file.isWriteable();
99 }
100 if (!fileSystem.hasCapability(Capability.CREATE)) {
101 return false;
102 }
103 return file.getParent().isWriteable();
104 }
105 }
106
107 }