001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.hadoop.fs;
019    
020    import java.io.FileNotFoundException;
021    import java.io.IOException;
022    import java.util.EnumSet;
023    
024    import org.apache.hadoop.HadoopIllegalArgumentException;
025    import org.apache.hadoop.classification.InterfaceAudience;
026    import org.apache.hadoop.classification.InterfaceStability;
027    
028    /****************************************************************
029     * CreateFlag specifies the file create semantic. Users can combine flags like: <br>
030     * <code>
031     * EnumSet.of(CreateFlag.CREATE, CreateFlag.APPEND)
032     * <code>
033     * <p>
034     * 
035     * Use the CreateFlag as follows:
036     * <ol>
037     * <li> CREATE - to create a file if it does not exist, 
038     * else throw FileAlreadyExists.</li>
039     * <li> APPEND - to append to a file if it exists, 
040     * else throw FileNotFoundException.</li>
041     * <li> OVERWRITE - to truncate a file if it exists, 
042     * else throw FileNotFoundException.</li>
043     * <li> CREATE|APPEND - to create a file if it does not exist, 
044     * else append to an existing file.</li>
045     * <li> CREATE|OVERWRITE - to create a file if it does not exist, 
046     * else overwrite an existing file.</li>
047     * </ol>
048     * 
049     * Following combination is not valid and will result in 
050     * {@link HadoopIllegalArgumentException}:
051     * <ol>
052     * <li> APPEND|OVERWRITE</li>
053     * <li> CREATE|APPEND|OVERWRITE</li>
054     * </ol>
055     *****************************************************************/
056    @InterfaceAudience.Public
057    @InterfaceStability.Evolving
058    public enum CreateFlag {
059    
060      /**
061       * Create a file. See javadoc for more description
062       * already exists
063       */
064      CREATE((short) 0x01),
065    
066      /**
067       * Truncate/overwrite a file. Same as POSIX O_TRUNC. See javadoc for description.
068       */
069      OVERWRITE((short) 0x02),
070    
071      /**
072       * Append to a file. See javadoc for more description.
073       */
074      APPEND((short) 0x04);
075    
076      private final short mode;
077    
078      private CreateFlag(short mode) {
079        this.mode = mode;
080      }
081    
082      short getMode() {
083        return mode;
084      }
085      
086      /**
087       * Validate the CreateFlag and throw exception if it is invalid
088       * @param flag set of CreateFlag
089       * @throws HadoopIllegalArgumentException if the CreateFlag is invalid
090       */
091      public static void validate(EnumSet<CreateFlag> flag) {
092        if (flag == null || flag.isEmpty()) {
093          throw new HadoopIllegalArgumentException(flag
094              + " does not specify any options");
095        }
096        final boolean append = flag.contains(APPEND);
097        final boolean overwrite = flag.contains(OVERWRITE);
098        
099        // Both append and overwrite is an error
100        if (append && overwrite) {
101          throw new HadoopIllegalArgumentException(
102              flag + "Both append and overwrite options cannot be enabled.");
103        }
104      }
105      
106      /**
107       * Validate the CreateFlag for create operation
108       * @param path Object representing the path; usually String or {@link Path}
109       * @param pathExists pass true if the path exists in the file system
110       * @param flag set of CreateFlag
111       * @throws IOException on error
112       * @throws HadoopIllegalArgumentException if the CreateFlag is invalid
113       */
114      public static void validate(Object path, boolean pathExists,
115          EnumSet<CreateFlag> flag) throws IOException {
116        validate(flag);
117        final boolean append = flag.contains(APPEND);
118        final boolean overwrite = flag.contains(OVERWRITE);
119        if (pathExists) {
120          if (!(append || overwrite)) {
121            throw new FileAlreadyExistsException("File already exists: "
122                + path.toString()
123                + ". Append or overwrite option must be specified in " + flag);
124          }
125        } else if (!flag.contains(CREATE)) {
126          throw new FileNotFoundException("Non existing file: " + path.toString()
127              + ". Create option is not specified in " + flag);
128        }
129      }
130    }