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 019 package org.apache.hadoop.fs; 020 021 import java.io.*; 022 import java.net.URI; 023 import java.net.URISyntaxException; 024 import java.util.EnumSet; 025 import java.util.List; 026 027 import org.apache.hadoop.classification.InterfaceAudience; 028 import org.apache.hadoop.classification.InterfaceStability; 029 import org.apache.hadoop.conf.Configuration; 030 import org.apache.hadoop.fs.permission.FsPermission; 031 import org.apache.hadoop.security.Credentials; 032 import org.apache.hadoop.security.token.Token; 033 import org.apache.hadoop.util.Progressable; 034 035 /**************************************************************** 036 * A <code>FilterFileSystem</code> contains 037 * some other file system, which it uses as 038 * its basic file system, possibly transforming 039 * the data along the way or providing additional 040 * functionality. The class <code>FilterFileSystem</code> 041 * itself simply overrides all methods of 042 * <code>FileSystem</code> with versions that 043 * pass all requests to the contained file 044 * system. Subclasses of <code>FilterFileSystem</code> 045 * may further override some of these methods 046 * and may also provide additional methods 047 * and fields. 048 * 049 *****************************************************************/ 050 @InterfaceAudience.Public 051 @InterfaceStability.Stable 052 public class FilterFileSystem extends FileSystem { 053 054 protected FileSystem fs; 055 private String swapScheme; 056 057 /* 058 * so that extending classes can define it 059 */ 060 public FilterFileSystem() { 061 } 062 063 public FilterFileSystem(FileSystem fs) { 064 this.fs = fs; 065 this.statistics = fs.statistics; 066 } 067 068 /** 069 * Get the raw file system 070 * @return FileSystem being filtered 071 */ 072 public FileSystem getRawFileSystem() { 073 return fs; 074 } 075 076 /** Called after a new FileSystem instance is constructed. 077 * @param name a uri whose authority section names the host, port, etc. 078 * for this FileSystem 079 * @param conf the configuration 080 */ 081 public void initialize(URI name, Configuration conf) throws IOException { 082 super.initialize(name, conf); 083 // this is less than ideal, but existing filesystems sometimes neglect 084 // to initialize the embedded filesystem 085 if (fs.getConf() == null) { 086 fs.initialize(name, conf); 087 } 088 String scheme = name.getScheme(); 089 if (!scheme.equals(fs.getUri().getScheme())) { 090 swapScheme = scheme; 091 } 092 } 093 094 /** Returns a URI whose scheme and authority identify this FileSystem.*/ 095 public URI getUri() { 096 return fs.getUri(); 097 } 098 099 /** 100 * Returns a qualified URI whose scheme and authority identify this 101 * FileSystem. 102 */ 103 @Override 104 protected URI getCanonicalUri() { 105 return fs.getCanonicalUri(); 106 } 107 108 /** Make sure that a path specifies a FileSystem. */ 109 public Path makeQualified(Path path) { 110 Path fqPath = fs.makeQualified(path); 111 // swap in our scheme if the filtered fs is using a different scheme 112 if (swapScheme != null) { 113 try { 114 // NOTE: should deal with authority, but too much other stuff is broken 115 fqPath = new Path( 116 new URI(swapScheme, fqPath.toUri().getSchemeSpecificPart(), null) 117 ); 118 } catch (URISyntaxException e) { 119 throw new IllegalArgumentException(e); 120 } 121 } 122 return fqPath; 123 } 124 125 /////////////////////////////////////////////////////////////// 126 // FileSystem 127 /////////////////////////////////////////////////////////////// 128 129 /** Check that a Path belongs to this FileSystem. */ 130 protected void checkPath(Path path) { 131 fs.checkPath(path); 132 } 133 134 public BlockLocation[] getFileBlockLocations(FileStatus file, long start, 135 long len) throws IOException { 136 return fs.getFileBlockLocations(file, start, len); 137 } 138 139 @Override 140 public Path resolvePath(final Path p) throws IOException { 141 return fs.resolvePath(p); 142 } 143 /** 144 * Opens an FSDataInputStream at the indicated Path. 145 * @param f the file name to open 146 * @param bufferSize the size of the buffer to be used. 147 */ 148 public FSDataInputStream open(Path f, int bufferSize) throws IOException { 149 return fs.open(f, bufferSize); 150 } 151 152 /** {@inheritDoc} */ 153 public FSDataOutputStream append(Path f, int bufferSize, 154 Progressable progress) throws IOException { 155 return fs.append(f, bufferSize, progress); 156 } 157 158 /** {@inheritDoc} */ 159 @Override 160 public FSDataOutputStream create(Path f, FsPermission permission, 161 boolean overwrite, int bufferSize, short replication, long blockSize, 162 Progressable progress) throws IOException { 163 return fs.create(f, permission, 164 overwrite, bufferSize, replication, blockSize, progress); 165 } 166 167 /** 168 * Set replication for an existing file. 169 * 170 * @param src file name 171 * @param replication new replication 172 * @throws IOException 173 * @return true if successful; 174 * false if file does not exist or is a directory 175 */ 176 public boolean setReplication(Path src, short replication) throws IOException { 177 return fs.setReplication(src, replication); 178 } 179 180 /** 181 * Renames Path src to Path dst. Can take place on local fs 182 * or remote DFS. 183 */ 184 public boolean rename(Path src, Path dst) throws IOException { 185 return fs.rename(src, dst); 186 } 187 188 /** Delete a file */ 189 public boolean delete(Path f, boolean recursive) throws IOException { 190 return fs.delete(f, recursive); 191 } 192 193 /** 194 * Mark a path to be deleted when FileSystem is closed. 195 * When the JVM shuts down, 196 * all FileSystem objects will be closed automatically. 197 * Then, 198 * the marked path will be deleted as a result of closing the FileSystem. 199 * 200 * The path has to exist in the file system. 201 * 202 * @param f the path to delete. 203 * @return true if deleteOnExit is successful, otherwise false. 204 * @throws IOException 205 */ 206 public boolean deleteOnExit(Path f) throws IOException { 207 return fs.deleteOnExit(f); 208 } 209 210 /** List files in a directory. */ 211 public FileStatus[] listStatus(Path f) throws IOException { 212 return fs.listStatus(f); 213 } 214 215 /** 216 * {@inheritDoc} 217 */ 218 @Override 219 public RemoteIterator<Path> listCorruptFileBlocks(Path path) 220 throws IOException { 221 return fs.listCorruptFileBlocks(path); 222 } 223 224 /** List files and its block locations in a directory. */ 225 public RemoteIterator<LocatedFileStatus> listLocatedStatus(Path f) 226 throws IOException { 227 return fs.listLocatedStatus(f); 228 } 229 230 public Path getHomeDirectory() { 231 return fs.getHomeDirectory(); 232 } 233 234 235 /** 236 * Set the current working directory for the given file system. All relative 237 * paths will be resolved relative to it. 238 * 239 * @param newDir 240 */ 241 public void setWorkingDirectory(Path newDir) { 242 fs.setWorkingDirectory(newDir); 243 } 244 245 /** 246 * Get the current working directory for the given file system 247 * 248 * @return the directory pathname 249 */ 250 public Path getWorkingDirectory() { 251 return fs.getWorkingDirectory(); 252 } 253 254 protected Path getInitialWorkingDirectory() { 255 return fs.getInitialWorkingDirectory(); 256 } 257 258 /** {@inheritDoc} */ 259 @Override 260 public FsStatus getStatus(Path p) throws IOException { 261 return fs.getStatus(p); 262 } 263 264 /** {@inheritDoc} */ 265 @Override 266 public boolean mkdirs(Path f, FsPermission permission) throws IOException { 267 return fs.mkdirs(f, permission); 268 } 269 270 /** 271 * The src file is on the local disk. Add it to FS at 272 * the given dst name. 273 * delSrc indicates if the source should be removed 274 */ 275 public void copyFromLocalFile(boolean delSrc, Path src, Path dst) 276 throws IOException { 277 fs.copyFromLocalFile(delSrc, src, dst); 278 } 279 280 /** 281 * The src files are on the local disk. Add it to FS at 282 * the given dst name. 283 * delSrc indicates if the source should be removed 284 */ 285 public void copyFromLocalFile(boolean delSrc, boolean overwrite, 286 Path[] srcs, Path dst) 287 throws IOException { 288 fs.copyFromLocalFile(delSrc, overwrite, srcs, dst); 289 } 290 291 /** 292 * The src file is on the local disk. Add it to FS at 293 * the given dst name. 294 * delSrc indicates if the source should be removed 295 */ 296 public void copyFromLocalFile(boolean delSrc, boolean overwrite, 297 Path src, Path dst) 298 throws IOException { 299 fs.copyFromLocalFile(delSrc, overwrite, src, dst); 300 } 301 302 /** 303 * The src file is under FS, and the dst is on the local disk. 304 * Copy it from FS control to the local dst name. 305 * delSrc indicates if the src will be removed or not. 306 */ 307 public void copyToLocalFile(boolean delSrc, Path src, Path dst) 308 throws IOException { 309 fs.copyToLocalFile(delSrc, src, dst); 310 } 311 312 /** 313 * Returns a local File that the user can write output to. The caller 314 * provides both the eventual FS target name and the local working 315 * file. If the FS is local, we write directly into the target. If 316 * the FS is remote, we write into the tmp local area. 317 */ 318 public Path startLocalOutput(Path fsOutputFile, Path tmpLocalFile) 319 throws IOException { 320 return fs.startLocalOutput(fsOutputFile, tmpLocalFile); 321 } 322 323 /** 324 * Called when we're all done writing to the target. A local FS will 325 * do nothing, because we've written to exactly the right place. A remote 326 * FS will copy the contents of tmpLocalFile to the correct target at 327 * fsOutputFile. 328 */ 329 public void completeLocalOutput(Path fsOutputFile, Path tmpLocalFile) 330 throws IOException { 331 fs.completeLocalOutput(fsOutputFile, tmpLocalFile); 332 } 333 334 /** Return the total size of all files in the filesystem.*/ 335 public long getUsed() throws IOException{ 336 return fs.getUsed(); 337 } 338 339 /** Return the number of bytes that large input files should be optimally 340 * be split into to minimize i/o time. */ 341 public long getDefaultBlockSize() { 342 return fs.getDefaultBlockSize(); 343 } 344 345 /** 346 * Get the default replication. 347 */ 348 public short getDefaultReplication() { 349 return fs.getDefaultReplication(); 350 } 351 352 /** 353 * Get file status. 354 */ 355 public FileStatus getFileStatus(Path f) throws IOException { 356 return fs.getFileStatus(f); 357 } 358 359 /** {@inheritDoc} */ 360 public FileChecksum getFileChecksum(Path f) throws IOException { 361 return fs.getFileChecksum(f); 362 } 363 364 /** {@inheritDoc} */ 365 public void setVerifyChecksum(boolean verifyChecksum) { 366 fs.setVerifyChecksum(verifyChecksum); 367 } 368 369 @Override 370 public Configuration getConf() { 371 return fs.getConf(); 372 } 373 374 @Override 375 public void close() throws IOException { 376 super.close(); 377 fs.close(); 378 } 379 380 /** {@inheritDoc} */ 381 @Override 382 public void setOwner(Path p, String username, String groupname 383 ) throws IOException { 384 fs.setOwner(p, username, groupname); 385 } 386 387 /** {@inheritDoc} */ 388 @Override 389 public void setTimes(Path p, long mtime, long atime 390 ) throws IOException { 391 fs.setTimes(p, mtime, atime); 392 } 393 394 /** {@inheritDoc} */ 395 @Override 396 public void setPermission(Path p, FsPermission permission 397 ) throws IOException { 398 fs.setPermission(p, permission); 399 } 400 401 @Override 402 protected FSDataOutputStream primitiveCreate(Path f, 403 FsPermission absolutePermission, EnumSet<CreateFlag> flag, 404 int bufferSize, short replication, long blockSize, Progressable progress, int bytesPerChecksum) 405 throws IOException { 406 return fs.primitiveCreate(f, absolutePermission, flag, 407 bufferSize, replication, blockSize, progress, bytesPerChecksum); 408 } 409 410 @Override 411 @SuppressWarnings("deprecation") 412 protected boolean primitiveMkdir(Path f, FsPermission abdolutePermission) 413 throws IOException { 414 return fs.primitiveMkdir(f, abdolutePermission); 415 } 416 417 @Override // FileSystem 418 public String getCanonicalServiceName() { 419 return fs.getCanonicalServiceName(); 420 } 421 422 @Override // FileSystem 423 @SuppressWarnings("deprecation") 424 public Token<?> getDelegationToken(String renewer) throws IOException { 425 return fs.getDelegationToken(renewer); 426 } 427 428 @Override // FileSystem 429 public List<Token<?>> getDelegationTokens(String renewer) throws IOException { 430 return fs.getDelegationTokens(renewer); 431 } 432 433 @Override 434 // FileSystem 435 public List<Token<?>> getDelegationTokens(String renewer, 436 Credentials credentials) throws IOException { 437 return fs.getDelegationTokens(renewer, credentials); 438 } 439 }