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 */
018package org.apache.hadoop.fs;
019
020import java.io.FileNotFoundException;
021import java.io.IOException;
022import java.util.EnumSet;
023
024import org.apache.hadoop.HadoopIllegalArgumentException;
025import org.apache.hadoop.classification.InterfaceAudience;
026import 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 * <li> LAZY_PERSIST - Create the block on transient storage (RAM) if
051 * available.</li>
052 * <li> APPEND_NEWBLOCK - Append data to a new block instead of end of the last
053 * partial block.</li>
054 * </ol>
055 * 
056 * Following combination is not valid and will result in 
057 * {@link HadoopIllegalArgumentException}:
058 * <ol>
059 * <li> APPEND|OVERWRITE</li>
060 * <li> CREATE|APPEND|OVERWRITE</li>
061 * </ol>
062 *****************************************************************/
063@InterfaceAudience.Public
064@InterfaceStability.Evolving
065public enum CreateFlag {
066
067  /**
068   * Create a file. See javadoc for more description
069   * already exists
070   */
071  CREATE((short) 0x01),
072
073  /**
074   * Truncate/overwrite a file. Same as POSIX O_TRUNC. See javadoc for description.
075   */
076  OVERWRITE((short) 0x02),
077
078  /**
079   * Append to a file. See javadoc for more description.
080   */
081  APPEND((short) 0x04),
082
083  /**
084   * Force closed blocks to disk. Similar to POSIX O_SYNC. See javadoc for description.
085   */
086  SYNC_BLOCK((short) 0x08),
087
088  /**
089   * Create the block on transient storage (RAM) if available. If
090   * transient storage is unavailable then the block will be created
091   * on disk.
092   *
093   * HDFS will make a best effort to lazily write these files to persistent
094   * storage, however file contents may be lost at any time due to process/
095   * node restarts, hence there is no guarantee of data durability.
096   *
097   * This flag must only be used for intermediate data whose loss can be
098   * tolerated by the application.
099   */
100  LAZY_PERSIST((short) 0x10),
101
102  /**
103   * Append data to a new block instead of the end of the last partial block.
104   * This is only useful for APPEND.
105   */
106  NEW_BLOCK((short) 0x20);
107
108  private final short mode;
109
110  private CreateFlag(short mode) {
111    this.mode = mode;
112  }
113
114  short getMode() {
115    return mode;
116  }
117  
118  /**
119   * Validate the CreateFlag and throw exception if it is invalid
120   * @param flag set of CreateFlag
121   * @throws HadoopIllegalArgumentException if the CreateFlag is invalid
122   */
123  public static void validate(EnumSet<CreateFlag> flag) {
124    if (flag == null || flag.isEmpty()) {
125      throw new HadoopIllegalArgumentException(flag
126          + " does not specify any options");
127    }
128    final boolean append = flag.contains(APPEND);
129    final boolean overwrite = flag.contains(OVERWRITE);
130    
131    // Both append and overwrite is an error
132    if (append && overwrite) {
133      throw new HadoopIllegalArgumentException(
134          flag + "Both append and overwrite options cannot be enabled.");
135    }
136  }
137  
138  /**
139   * Validate the CreateFlag for create operation
140   * @param path Object representing the path; usually String or {@link Path}
141   * @param pathExists pass true if the path exists in the file system
142   * @param flag set of CreateFlag
143   * @throws IOException on error
144   * @throws HadoopIllegalArgumentException if the CreateFlag is invalid
145   */
146  public static void validate(Object path, boolean pathExists,
147      EnumSet<CreateFlag> flag) throws IOException {
148    validate(flag);
149    final boolean append = flag.contains(APPEND);
150    final boolean overwrite = flag.contains(OVERWRITE);
151    if (pathExists) {
152      if (!(append || overwrite)) {
153        throw new FileAlreadyExistsException("File already exists: "
154            + path.toString()
155            + ". Append or overwrite option must be specified in " + flag);
156      }
157    } else if (!flag.contains(CREATE)) {
158      throw new FileNotFoundException("Non existing file: " + path.toString()
159          + ". Create option is not specified in " + flag);
160    }
161  }
162
163  /**
164   * Validate the CreateFlag for the append operation. The flag must contain
165   * APPEND, and cannot contain OVERWRITE.
166   */
167  public static void validateForAppend(EnumSet<CreateFlag> flag) {
168    validate(flag);
169    if (!flag.contains(APPEND)) {
170      throw new HadoopIllegalArgumentException(flag
171          + " does not contain APPEND");
172    }
173  }
174}