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     * <li> SYNC_BLOCK - to force closed blocks to the disk device.
048     * In addition {@link Syncable#hsync()} should be called after each write,
049     * if true synchronous behavior is required.</li>
050     * </ol>
051     * 
052     * Following combination is not valid and will result in 
053     * {@link HadoopIllegalArgumentException}:
054     * <ol>
055     * <li> APPEND|OVERWRITE</li>
056     * <li> CREATE|APPEND|OVERWRITE</li>
057     * </ol>
058     *****************************************************************/
059    @InterfaceAudience.Public
060    @InterfaceStability.Evolving
061    public enum CreateFlag {
062    
063      /**
064       * Create a file. See javadoc for more description
065       * already exists
066       */
067      CREATE((short) 0x01),
068    
069      /**
070       * Truncate/overwrite a file. Same as POSIX O_TRUNC. See javadoc for description.
071       */
072      OVERWRITE((short) 0x02),
073    
074      /**
075       * Append to a file. See javadoc for more description.
076       */
077      APPEND((short) 0x04),
078    
079      /**
080       * Force closed blocks to disk. Similar to POSIX O_SYNC. See javadoc for description.
081       */
082      SYNC_BLOCK((short) 0x08);
083    
084      private final short mode;
085    
086      private CreateFlag(short mode) {
087        this.mode = mode;
088      }
089    
090      short getMode() {
091        return mode;
092      }
093      
094      /**
095       * Validate the CreateFlag and throw exception if it is invalid
096       * @param flag set of CreateFlag
097       * @throws HadoopIllegalArgumentException if the CreateFlag is invalid
098       */
099      public static void validate(EnumSet<CreateFlag> flag) {
100        if (flag == null || flag.isEmpty()) {
101          throw new HadoopIllegalArgumentException(flag
102              + " does not specify any options");
103        }
104        final boolean append = flag.contains(APPEND);
105        final boolean overwrite = flag.contains(OVERWRITE);
106        
107        // Both append and overwrite is an error
108        if (append && overwrite) {
109          throw new HadoopIllegalArgumentException(
110              flag + "Both append and overwrite options cannot be enabled.");
111        }
112      }
113      
114      /**
115       * Validate the CreateFlag for create operation
116       * @param path Object representing the path; usually String or {@link Path}
117       * @param pathExists pass true if the path exists in the file system
118       * @param flag set of CreateFlag
119       * @throws IOException on error
120       * @throws HadoopIllegalArgumentException if the CreateFlag is invalid
121       */
122      public static void validate(Object path, boolean pathExists,
123          EnumSet<CreateFlag> flag) throws IOException {
124        validate(flag);
125        final boolean append = flag.contains(APPEND);
126        final boolean overwrite = flag.contains(OVERWRITE);
127        if (pathExists) {
128          if (!(append || overwrite)) {
129            throw new FileAlreadyExistsException("File already exists: "
130                + path.toString()
131                + ". Append or overwrite option must be specified in " + flag);
132          }
133        } else if (!flag.contains(CREATE)) {
134          throw new FileNotFoundException("Non existing file: " + path.toString()
135              + ". Create option is not specified in " + flag);
136        }
137      }
138    }