001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.filter;
018
019import java.io.Serializable;
020
021import org.apache.commons.vfs2.Capability;
022import org.apache.commons.vfs2.FileFilter;
023import org.apache.commons.vfs2.FileObject;
024import org.apache.commons.vfs2.FileSelectInfo;
025import org.apache.commons.vfs2.FileSystem;
026import org.apache.commons.vfs2.FileSystemException;
027
028/**
029 * This filter accepts {@code File}s that can be written to.
030 * <p>
031 * Example, showing how to print out a list of the current directory's
032 * <em>writable</em> files:
033 * </p>
034 *
035 * <pre>
036 * FileSystemManager fsManager = VFS.getManager();
037 * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
038 * FileObject[] files = dir.findFiles(new FileFilterSelector(CanWriteFileFilter.CAN_WRITE));
039 * for (int i = 0; i &lt; files.length; i++) {
040 *     System.out.println(files[i]);
041 * }
042 * </pre>
043 *
044 * <p>
045 * Example, showing how to print out a list of the current directory's
046 * <em>un-writable</em> files:
047 * </p>
048 *
049 * <pre>
050 * FileSystemManager fsManager = VFS.getManager();
051 * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
052 * FileObject[] files = dir.findFiles(new FileFilterSelector(CanWriteFileFilter.CANNOT_WRITE));
053 * for (int i = 0; i &lt; files.length; i++) {
054 *     System.out.println(files[i]);
055 * }
056 * </pre>
057 *
058 * <p>
059 * <strong>N.B.</strong> For read-only files, use
060 * {@code CanReadFileFilter.READ_ONLY}.
061 * </p>
062 *
063 * @author This code was originally ported from Apache Commons IO File Filter
064 * @see "https://commons.apache.org/proper/commons-io/"
065 * @since 2.4
066 */
067public class CanWriteFileFilter implements FileFilter, Serializable {
068
069    /** Singleton instance of <em>writable</em> filter. */
070    public static final FileFilter CAN_WRITE = new CanWriteFileFilter();
071
072    /** Singleton instance of not <em>writable</em> filter. */
073    public static final FileFilter CANNOT_WRITE = new NotFileFilter(CAN_WRITE);
074
075    private static final long serialVersionUID = 1L;
076
077    /**
078     * Restrictive constructor.
079     */
080    protected CanWriteFileFilter() {
081    }
082
083    /**
084     * Checks to see if the file can be written to.
085     *
086     * @param fileSelectInfo the File to check
087     * @return {@code true} if the file can be written to, otherwise {@code false}.
088     * @throws FileSystemException Thrown for file system errors.
089     */
090    @Override
091    public boolean accept(final FileSelectInfo fileSelectInfo) throws FileSystemException {
092        try (FileObject file = fileSelectInfo.getFile()) {
093            final FileSystem fileSystem = file.getFileSystem();
094            if (file.exists()) {
095                if (!fileSystem.hasCapability(Capability.WRITE_CONTENT)) {
096                    return false;
097                }
098                return file.isWriteable();
099            }
100            if (!fileSystem.hasCapability(Capability.CREATE)) {
101                return false;
102            }
103            return file.getParent().isWriteable();
104        }
105    }
106
107}