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 org.apache.hadoop.classification.InterfaceAudience; 026 import org.apache.hadoop.classification.InterfaceStability; 027 import org.apache.hadoop.conf.Configuration; 028 import org.apache.hadoop.fs.permission.FsPermission; 029 import org.apache.hadoop.fs.ContentSummary; 030 import org.apache.hadoop.fs.Options.ChecksumOpt; 031 import org.apache.hadoop.util.Progressable; 032 033 /**************************************************************** 034 * A <code>FilterFileSystem</code> contains 035 * some other file system, which it uses as 036 * its basic file system, possibly transforming 037 * the data along the way or providing additional 038 * functionality. The class <code>FilterFileSystem</code> 039 * itself simply overrides all methods of 040 * <code>FileSystem</code> with versions that 041 * pass all requests to the contained file 042 * system. Subclasses of <code>FilterFileSystem</code> 043 * may further override some of these methods 044 * and may also provide additional methods 045 * and fields. 046 * 047 *****************************************************************/ 048 @InterfaceAudience.Public 049 @InterfaceStability.Stable 050 public class FilterFileSystem extends FileSystem { 051 052 protected FileSystem fs; 053 protected String swapScheme; 054 055 /* 056 * so that extending classes can define it 057 */ 058 public FilterFileSystem() { 059 } 060 061 public FilterFileSystem(FileSystem fs) { 062 this.fs = fs; 063 this.statistics = fs.statistics; 064 } 065 066 /** 067 * Get the raw file system 068 * @return FileSystem being filtered 069 */ 070 public FileSystem getRawFileSystem() { 071 return fs; 072 } 073 074 /** Called after a new FileSystem instance is constructed. 075 * @param name a uri whose authority section names the host, port, etc. 076 * for this FileSystem 077 * @param conf the configuration 078 */ 079 public void initialize(URI name, Configuration conf) throws IOException { 080 super.initialize(name, conf); 081 // this is less than ideal, but existing filesystems sometimes neglect 082 // to initialize the embedded filesystem 083 if (fs.getConf() == null) { 084 fs.initialize(name, conf); 085 } 086 String scheme = name.getScheme(); 087 if (!scheme.equals(fs.getUri().getScheme())) { 088 swapScheme = scheme; 089 } 090 } 091 092 /** Returns a URI whose scheme and authority identify this FileSystem.*/ 093 public URI getUri() { 094 return fs.getUri(); 095 } 096 097 098 @Override 099 protected URI getCanonicalUri() { 100 return fs.getCanonicalUri(); 101 } 102 103 @Override 104 protected URI canonicalizeUri(URI uri) { 105 return fs.canonicalizeUri(uri); 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 void concat(Path f, Path[] psrcs) throws IOException { 161 fs.concat(f, psrcs); 162 } 163 164 @Override 165 public FSDataOutputStream create(Path f, FsPermission permission, 166 boolean overwrite, int bufferSize, short replication, long blockSize, 167 Progressable progress) throws IOException { 168 return fs.create(f, permission, 169 overwrite, bufferSize, replication, blockSize, progress); 170 } 171 172 173 174 @Override 175 @Deprecated 176 public FSDataOutputStream createNonRecursive(Path f, FsPermission permission, 177 EnumSet<CreateFlag> flags, int bufferSize, short replication, long blockSize, 178 Progressable progress) throws IOException { 179 180 return fs.createNonRecursive(f, permission, flags, bufferSize, replication, blockSize, 181 progress); 182 } 183 184 /** 185 * Set replication for an existing file. 186 * 187 * @param src file name 188 * @param replication new replication 189 * @throws IOException 190 * @return true if successful; 191 * false if file does not exist or is a directory 192 */ 193 public boolean setReplication(Path src, short replication) throws IOException { 194 return fs.setReplication(src, replication); 195 } 196 197 /** 198 * Renames Path src to Path dst. Can take place on local fs 199 * or remote DFS. 200 */ 201 public boolean rename(Path src, Path dst) throws IOException { 202 return fs.rename(src, dst); 203 } 204 205 /** Delete a file */ 206 public boolean delete(Path f, boolean recursive) throws IOException { 207 return fs.delete(f, recursive); 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 /** 272 * The src file is on the local disk. Add it to FS at 273 * the given dst name. 274 * delSrc indicates if the source should be removed 275 */ 276 public void copyFromLocalFile(boolean delSrc, Path src, Path dst) 277 throws IOException { 278 fs.copyFromLocalFile(delSrc, src, dst); 279 } 280 281 /** 282 * The src files are on the local disk. Add it to FS at 283 * the given dst name. 284 * delSrc indicates if the source should be removed 285 */ 286 public void copyFromLocalFile(boolean delSrc, boolean overwrite, 287 Path[] srcs, Path dst) 288 throws IOException { 289 fs.copyFromLocalFile(delSrc, overwrite, srcs, dst); 290 } 291 292 /** 293 * The src file is on the local disk. Add it to FS at 294 * the given dst name. 295 * delSrc indicates if the source should be removed 296 */ 297 public void copyFromLocalFile(boolean delSrc, boolean overwrite, 298 Path src, Path dst) 299 throws IOException { 300 fs.copyFromLocalFile(delSrc, overwrite, src, dst); 301 } 302 303 /** 304 * The src file is under FS, and the dst is on the local disk. 305 * Copy it from FS control to the local dst name. 306 * delSrc indicates if the src will be removed or not. 307 */ 308 public void copyToLocalFile(boolean delSrc, Path src, Path dst) 309 throws IOException { 310 fs.copyToLocalFile(delSrc, src, dst); 311 } 312 313 /** 314 * Returns a local File that the user can write output to. The caller 315 * provides both the eventual FS target name and the local working 316 * file. If the FS is local, we write directly into the target. If 317 * the FS is remote, we write into the tmp local area. 318 */ 319 public Path startLocalOutput(Path fsOutputFile, Path tmpLocalFile) 320 throws IOException { 321 return fs.startLocalOutput(fsOutputFile, tmpLocalFile); 322 } 323 324 /** 325 * Called when we're all done writing to the target. A local FS will 326 * do nothing, because we've written to exactly the right place. A remote 327 * FS will copy the contents of tmpLocalFile to the correct target at 328 * fsOutputFile. 329 */ 330 public void completeLocalOutput(Path fsOutputFile, Path tmpLocalFile) 331 throws IOException { 332 fs.completeLocalOutput(fsOutputFile, tmpLocalFile); 333 } 334 335 /** Return the total size of all files in the filesystem.*/ 336 public long getUsed() throws IOException{ 337 return fs.getUsed(); 338 } 339 340 @Override 341 public long getDefaultBlockSize() { 342 return fs.getDefaultBlockSize(); 343 } 344 345 @Override 346 public short getDefaultReplication() { 347 return fs.getDefaultReplication(); 348 } 349 350 @Override 351 public FsServerDefaults getServerDefaults() throws IOException { 352 return fs.getServerDefaults(); 353 } 354 355 // path variants delegate to underlying filesystem 356 @Override 357 public ContentSummary getContentSummary(Path f) throws IOException { 358 return fs.getContentSummary(f); 359 } 360 361 @Override 362 public long getDefaultBlockSize(Path f) { 363 return fs.getDefaultBlockSize(f); 364 } 365 366 @Override 367 public short getDefaultReplication(Path f) { 368 return fs.getDefaultReplication(f); 369 } 370 371 @Override 372 public FsServerDefaults getServerDefaults(Path f) throws IOException { 373 return fs.getServerDefaults(f); 374 } 375 376 /** 377 * Get file status. 378 */ 379 public FileStatus getFileStatus(Path f) throws IOException { 380 return fs.getFileStatus(f); 381 } 382 383 /** {@inheritDoc} */ 384 public FileChecksum getFileChecksum(Path f) throws IOException { 385 return fs.getFileChecksum(f); 386 } 387 388 /** {@inheritDoc} */ 389 public void setVerifyChecksum(boolean verifyChecksum) { 390 fs.setVerifyChecksum(verifyChecksum); 391 } 392 393 @Override 394 public void setWriteChecksum(boolean writeChecksum) { 395 fs.setWriteChecksum(writeChecksum); 396 } 397 398 @Override 399 public Configuration getConf() { 400 return fs.getConf(); 401 } 402 403 @Override 404 public void close() throws IOException { 405 super.close(); 406 fs.close(); 407 } 408 409 /** {@inheritDoc} */ 410 @Override 411 public void setOwner(Path p, String username, String groupname 412 ) throws IOException { 413 fs.setOwner(p, username, groupname); 414 } 415 416 /** {@inheritDoc} */ 417 @Override 418 public void setTimes(Path p, long mtime, long atime 419 ) throws IOException { 420 fs.setTimes(p, mtime, atime); 421 } 422 423 /** {@inheritDoc} */ 424 @Override 425 public void setPermission(Path p, FsPermission permission 426 ) throws IOException { 427 fs.setPermission(p, permission); 428 } 429 430 @Override 431 protected FSDataOutputStream primitiveCreate(Path f, 432 FsPermission absolutePermission, EnumSet<CreateFlag> flag, 433 int bufferSize, short replication, long blockSize, 434 Progressable progress, ChecksumOpt checksumOpt) 435 throws IOException { 436 return fs.primitiveCreate(f, absolutePermission, flag, 437 bufferSize, replication, blockSize, progress, checksumOpt); 438 } 439 440 @Override 441 @SuppressWarnings("deprecation") 442 protected boolean primitiveMkdir(Path f, FsPermission abdolutePermission) 443 throws IOException { 444 return fs.primitiveMkdir(f, abdolutePermission); 445 } 446 447 @Override // FileSystem 448 public FileSystem[] getChildFileSystems() { 449 return new FileSystem[]{fs}; 450 } 451 }