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
019package org.apache.hadoop.fs;
020
021import java.io.*;
022import java.net.URI;
023import java.net.URISyntaxException;
024import java.util.Collection;
025import java.util.EnumSet;
026import java.util.List;
027import java.util.Map;
028
029import org.apache.hadoop.classification.InterfaceAudience;
030import org.apache.hadoop.classification.InterfaceStability;
031import org.apache.hadoop.conf.Configuration;
032import org.apache.hadoop.fs.permission.AclEntry;
033import org.apache.hadoop.fs.permission.AclStatus;
034import org.apache.hadoop.fs.permission.FsAction;
035import org.apache.hadoop.fs.permission.FsPermission;
036import org.apache.hadoop.fs.Options.ChecksumOpt;
037import org.apache.hadoop.security.AccessControlException;
038import org.apache.hadoop.util.Progressable;
039
040/****************************************************************
041 * A <code>FilterFileSystem</code> contains
042 * some other file system, which it uses as
043 * its  basic file system, possibly transforming
044 * the data along the way or providing  additional
045 * functionality. The class <code>FilterFileSystem</code>
046 * itself simply overrides all  methods of
047 * <code>FileSystem</code> with versions that
048 * pass all requests to the contained  file
049 * system. Subclasses of <code>FilterFileSystem</code>
050 * may further override some of  these methods
051 * and may also provide additional methods
052 * and fields.
053 *
054 *****************************************************************/
055@InterfaceAudience.Public
056@InterfaceStability.Stable
057public class FilterFileSystem extends FileSystem {
058  
059  protected FileSystem fs;
060  protected String swapScheme;
061  
062  /*
063   * so that extending classes can define it
064   */
065  public FilterFileSystem() {
066  }
067  
068  public FilterFileSystem(FileSystem fs) {
069    this.fs = fs;
070    this.statistics = fs.statistics;
071  }
072
073  /**
074   * Get the raw file system 
075   * @return FileSystem being filtered
076   */
077  public FileSystem getRawFileSystem() {
078    return fs;
079  }
080
081  /** Called after a new FileSystem instance is constructed.
082   * @param name a uri whose authority section names the host, port, etc.
083   *   for this FileSystem
084   * @param conf the configuration
085   */
086  @Override
087  public void initialize(URI name, Configuration conf) throws IOException {
088    super.initialize(name, conf);
089    // this is less than ideal, but existing filesystems sometimes neglect
090    // to initialize the embedded filesystem
091    if (fs.getConf() == null) {
092      fs.initialize(name, conf);
093    }
094    String scheme = name.getScheme();
095    if (!scheme.equals(fs.getUri().getScheme())) {
096      swapScheme = scheme;
097    }
098  }
099
100  /** Returns a URI whose scheme and authority identify this FileSystem.*/
101  @Override
102  public URI getUri() {
103    return fs.getUri();
104  }
105  
106  
107  @Override
108  protected URI getCanonicalUri() {
109    return fs.getCanonicalUri();
110  }
111
112  @Override
113  protected URI canonicalizeUri(URI uri) {
114    return fs.canonicalizeUri(uri);
115  }
116
117  /** Make sure that a path specifies a FileSystem. */
118  @Override
119  public Path makeQualified(Path path) {
120    Path fqPath = fs.makeQualified(path);
121    // swap in our scheme if the filtered fs is using a different scheme
122    if (swapScheme != null) {
123      try {
124        // NOTE: should deal with authority, but too much other stuff is broken 
125        fqPath = new Path(
126            new URI(swapScheme, fqPath.toUri().getSchemeSpecificPart(), null)
127        );
128      } catch (URISyntaxException e) {
129        throw new IllegalArgumentException(e);
130      }
131    }
132    return fqPath;
133  }
134  
135  ///////////////////////////////////////////////////////////////
136  // FileSystem
137  ///////////////////////////////////////////////////////////////
138
139  /** Check that a Path belongs to this FileSystem. */
140  @Override
141  protected void checkPath(Path path) {
142    fs.checkPath(path);
143  }
144
145  @Override
146  public BlockLocation[] getFileBlockLocations(FileStatus file, long start,
147    long len) throws IOException {
148      return fs.getFileBlockLocations(file, start, len);
149  }
150
151  @Override
152  public Path resolvePath(final Path p) throws IOException {
153    return fs.resolvePath(p);
154  }
155  /**
156   * Opens an FSDataInputStream at the indicated Path.
157   * @param f the file name to open
158   * @param bufferSize the size of the buffer to be used.
159   */
160  @Override
161  public FSDataInputStream open(Path f, int bufferSize) throws IOException {
162    return fs.open(f, bufferSize);
163  }
164
165  @Override
166  public FSDataOutputStream append(Path f, int bufferSize,
167      Progressable progress) throws IOException {
168    return fs.append(f, bufferSize, progress);
169  }
170
171  @Override
172  public void concat(Path f, Path[] psrcs) throws IOException {
173    fs.concat(f, psrcs);
174  }
175
176  @Override
177  public FSDataOutputStream create(Path f, FsPermission permission,
178      boolean overwrite, int bufferSize, short replication, long blockSize,
179      Progressable progress) throws IOException {
180    return fs.create(f, permission,
181        overwrite, bufferSize, replication, blockSize, progress);
182  }
183
184  @Override
185  public FSDataOutputStream create(Path f,
186        FsPermission permission,
187        EnumSet<CreateFlag> flags,
188        int bufferSize,
189        short replication,
190        long blockSize,
191        Progressable progress,
192        ChecksumOpt checksumOpt) throws IOException {
193    return fs.create(f, permission,
194      flags, bufferSize, replication, blockSize, progress, checksumOpt);
195  }
196
197  @Override
198  protected RemoteIterator<LocatedFileStatus> listLocatedStatus(final Path f,
199      final PathFilter filter)
200  throws FileNotFoundException, IOException {
201    return fs.listLocatedStatus(f, filter);
202  }
203
204
205  @Override
206  public FSDataOutputStream createNonRecursive(Path f, FsPermission permission,
207      EnumSet<CreateFlag> flags, int bufferSize, short replication, long blockSize,
208      Progressable progress) throws IOException {
209    
210    return fs.createNonRecursive(f, permission, flags, bufferSize, replication, blockSize,
211        progress);
212  }
213
214  /**
215   * Set replication for an existing file.
216   * 
217   * @param src file name
218   * @param replication new replication
219   * @throws IOException
220   * @return true if successful;
221   *         false if file does not exist or is a directory
222   */
223  @Override
224  public boolean setReplication(Path src, short replication) throws IOException {
225    return fs.setReplication(src, replication);
226  }
227  
228  /**
229   * Renames Path src to Path dst.  Can take place on local fs
230   * or remote DFS.
231   */
232  @Override
233  public boolean rename(Path src, Path dst) throws IOException {
234    return fs.rename(src, dst);
235  }
236
237  @Override
238  public boolean truncate(Path f, final long newLength) throws IOException {
239    return fs.truncate(f, newLength);
240  }
241  
242  /** Delete a file */
243  @Override
244  public boolean delete(Path f, boolean recursive) throws IOException {
245    return fs.delete(f, recursive);
246  }
247  
248  /** List files in a directory. */
249  @Override
250  public FileStatus[] listStatus(Path f) throws IOException {
251    return fs.listStatus(f);
252  }
253
254  @Override
255  public RemoteIterator<Path> listCorruptFileBlocks(Path path)
256    throws IOException {
257    return fs.listCorruptFileBlocks(path);
258  }
259
260  /** List files and its block locations in a directory. */
261  @Override
262  public RemoteIterator<LocatedFileStatus> listLocatedStatus(Path f)
263  throws IOException {
264    return fs.listLocatedStatus(f);
265  }
266  
267  /** Return a remote iterator for listing in a directory */
268  @Override
269  public RemoteIterator<FileStatus> listStatusIterator(Path f)
270  throws IOException {
271    return fs.listStatusIterator(f);
272   }
273
274  @Override
275  public Path getHomeDirectory() {
276    return fs.getHomeDirectory();
277  }
278
279
280  /**
281   * Set the current working directory for the given file system. All relative
282   * paths will be resolved relative to it.
283   * 
284   * @param newDir
285   */
286  @Override
287  public void setWorkingDirectory(Path newDir) {
288    fs.setWorkingDirectory(newDir);
289  }
290  
291  /**
292   * Get the current working directory for the given file system
293   * 
294   * @return the directory pathname
295   */
296  @Override
297  public Path getWorkingDirectory() {
298    return fs.getWorkingDirectory();
299  }
300  
301  @Override
302  protected Path getInitialWorkingDirectory() {
303    return fs.getInitialWorkingDirectory();
304  }
305  
306  @Override
307  public FsStatus getStatus(Path p) throws IOException {
308    return fs.getStatus(p);
309  }
310  
311  @Override
312  public boolean mkdirs(Path f, FsPermission permission) throws IOException {
313    return fs.mkdirs(f, permission);
314  }
315
316
317  /**
318   * The src file is on the local disk.  Add it to FS at
319   * the given dst name.
320   * delSrc indicates if the source should be removed
321   */
322  @Override
323  public void copyFromLocalFile(boolean delSrc, Path src, Path dst)
324    throws IOException {
325    fs.copyFromLocalFile(delSrc, src, dst);
326  }
327  
328  /**
329   * The src files are on the local disk.  Add it to FS at
330   * the given dst name.
331   * delSrc indicates if the source should be removed
332   */
333  @Override
334  public void copyFromLocalFile(boolean delSrc, boolean overwrite, 
335                                Path[] srcs, Path dst)
336    throws IOException {
337    fs.copyFromLocalFile(delSrc, overwrite, srcs, dst);
338  }
339  
340  /**
341   * The src file is on the local disk.  Add it to FS at
342   * the given dst name.
343   * delSrc indicates if the source should be removed
344   */
345  @Override
346  public void copyFromLocalFile(boolean delSrc, boolean overwrite, 
347                                Path src, Path dst)
348    throws IOException {
349    fs.copyFromLocalFile(delSrc, overwrite, src, dst);
350  }
351
352  /**
353   * The src file is under FS, and the dst is on the local disk.
354   * Copy it from FS control to the local dst name.
355   * delSrc indicates if the src will be removed or not.
356   */   
357  @Override
358  public void copyToLocalFile(boolean delSrc, Path src, Path dst)
359    throws IOException {
360    fs.copyToLocalFile(delSrc, src, dst);
361  }
362  
363  /**
364   * Returns a local File that the user can write output to.  The caller
365   * provides both the eventual FS target name and the local working
366   * file.  If the FS is local, we write directly into the target.  If
367   * the FS is remote, we write into the tmp local area.
368   */
369  @Override
370  public Path startLocalOutput(Path fsOutputFile, Path tmpLocalFile)
371    throws IOException {
372    return fs.startLocalOutput(fsOutputFile, tmpLocalFile);
373  }
374
375  /**
376   * Called when we're all done writing to the target.  A local FS will
377   * do nothing, because we've written to exactly the right place.  A remote
378   * FS will copy the contents of tmpLocalFile to the correct target at
379   * fsOutputFile.
380   */
381  @Override
382  public void completeLocalOutput(Path fsOutputFile, Path tmpLocalFile)
383    throws IOException {
384    fs.completeLocalOutput(fsOutputFile, tmpLocalFile);
385  }
386
387  /** Return the total size of all files in the filesystem.*/
388  @Override
389  public long getUsed() throws IOException{
390    return fs.getUsed();
391  }
392
393  /** Return the total size of all files from a specified path.*/
394  @Override
395  public long getUsed(Path path) throws IOException {
396    return fs.getUsed(path);
397  }
398
399  @Override
400  public long getDefaultBlockSize() {
401    return fs.getDefaultBlockSize();
402  }
403  
404  @Override
405  public short getDefaultReplication() {
406    return fs.getDefaultReplication();
407  }
408
409  @Override
410  public FsServerDefaults getServerDefaults() throws IOException {
411    return fs.getServerDefaults();
412  }
413
414  // path variants delegate to underlying filesystem 
415  @Override
416  public long getDefaultBlockSize(Path f) {
417    return fs.getDefaultBlockSize(f);
418  }
419
420  @Override
421  public short getDefaultReplication(Path f) {
422    return fs.getDefaultReplication(f);
423  }
424
425  @Override
426  public FsServerDefaults getServerDefaults(Path f) throws IOException {
427    return fs.getServerDefaults(f);
428  }
429
430  /**
431   * Get file status.
432   */
433  @Override
434  public FileStatus getFileStatus(Path f) throws IOException {
435    return fs.getFileStatus(f);
436  }
437
438  @Override
439  public void access(Path path, FsAction mode) throws AccessControlException,
440      FileNotFoundException, IOException {
441    fs.access(path, mode);
442  }
443
444  public void createSymlink(final Path target, final Path link,
445      final boolean createParent) throws AccessControlException,
446      FileAlreadyExistsException, FileNotFoundException,
447      ParentNotDirectoryException, UnsupportedFileSystemException, 
448      IOException {
449    fs.createSymlink(target, link, createParent);
450  }
451
452  public FileStatus getFileLinkStatus(final Path f)
453      throws AccessControlException, FileNotFoundException,
454      UnsupportedFileSystemException, IOException {
455    return fs.getFileLinkStatus(f);
456  }
457
458  public boolean supportsSymlinks() {
459    return fs.supportsSymlinks();
460  }
461
462  public Path getLinkTarget(Path f) throws IOException {
463    return fs.getLinkTarget(f);
464  }
465
466  protected Path resolveLink(Path f) throws IOException {
467    return fs.resolveLink(f);
468  }
469
470  @Override
471  public FileChecksum getFileChecksum(Path f) throws IOException {
472    return fs.getFileChecksum(f);
473  }
474
475  @Override
476  public FileChecksum getFileChecksum(Path f, long length) throws IOException {
477    return fs.getFileChecksum(f, length);
478  }
479
480  @Override
481  public void setVerifyChecksum(boolean verifyChecksum) {
482    fs.setVerifyChecksum(verifyChecksum);
483  }
484  
485  @Override
486  public void setWriteChecksum(boolean writeChecksum) {
487    fs.setWriteChecksum(writeChecksum);
488  }
489
490  @Override
491  public Configuration getConf() {
492    return fs.getConf();
493  }
494  
495  @Override
496  public void close() throws IOException {
497    super.close();
498    fs.close();
499  }
500
501  @Override
502  public void setOwner(Path p, String username, String groupname
503      ) throws IOException {
504    fs.setOwner(p, username, groupname);
505  }
506
507  @Override
508  public void setTimes(Path p, long mtime, long atime
509      ) throws IOException {
510    fs.setTimes(p, mtime, atime);
511  }
512
513  @Override
514  public void setPermission(Path p, FsPermission permission
515      ) throws IOException {
516    fs.setPermission(p, permission);
517  }
518
519  @Override
520  protected FSDataOutputStream primitiveCreate(Path f,
521      FsPermission absolutePermission, EnumSet<CreateFlag> flag,
522      int bufferSize, short replication, long blockSize,
523      Progressable progress, ChecksumOpt checksumOpt)
524      throws IOException {
525    return fs.primitiveCreate(f, absolutePermission, flag,
526        bufferSize, replication, blockSize, progress, checksumOpt);
527  }
528
529  @Override
530  @SuppressWarnings("deprecation")
531  protected boolean primitiveMkdir(Path f, FsPermission abdolutePermission)
532      throws IOException {
533    return fs.primitiveMkdir(f, abdolutePermission);
534  }
535  
536  @Override // FileSystem
537  public FileSystem[] getChildFileSystems() {
538    return new FileSystem[]{fs};
539  }
540
541  @Override // FileSystem
542  public Path createSnapshot(Path path, String snapshotName)
543      throws IOException {
544    return fs.createSnapshot(path, snapshotName);
545  }
546  
547  @Override // FileSystem
548  public void renameSnapshot(Path path, String snapshotOldName,
549      String snapshotNewName) throws IOException {
550    fs.renameSnapshot(path, snapshotOldName, snapshotNewName);
551  }
552  
553  @Override // FileSystem
554  public void deleteSnapshot(Path path, String snapshotName)
555      throws IOException {
556    fs.deleteSnapshot(path, snapshotName);
557  }
558
559  @Override
560  public void modifyAclEntries(Path path, List<AclEntry> aclSpec)
561      throws IOException {
562    fs.modifyAclEntries(path, aclSpec);
563  }
564
565  @Override
566  public void removeAclEntries(Path path, List<AclEntry> aclSpec)
567      throws IOException {
568    fs.removeAclEntries(path, aclSpec);
569  }
570
571  @Override
572  public void removeDefaultAcl(Path path) throws IOException {
573    fs.removeDefaultAcl(path);
574  }
575
576  @Override
577  public void removeAcl(Path path) throws IOException {
578    fs.removeAcl(path);
579  }
580
581  @Override
582  public void setAcl(Path path, List<AclEntry> aclSpec) throws IOException {
583    fs.setAcl(path, aclSpec);
584  }
585
586  @Override
587  public AclStatus getAclStatus(Path path) throws IOException {
588    return fs.getAclStatus(path);
589  }
590
591  @Override
592  public void setXAttr(Path path, String name, byte[] value)
593      throws IOException {
594    fs.setXAttr(path, name, value);
595  }
596
597  @Override
598  public void setXAttr(Path path, String name, byte[] value,
599      EnumSet<XAttrSetFlag> flag) throws IOException {
600    fs.setXAttr(path, name, value, flag);
601  }
602
603  @Override
604  public byte[] getXAttr(Path path, String name) throws IOException {
605    return fs.getXAttr(path, name);
606  }
607
608  @Override
609  public Map<String, byte[]> getXAttrs(Path path) throws IOException {
610    return fs.getXAttrs(path);
611  }
612
613  @Override
614  public Map<String, byte[]> getXAttrs(Path path, List<String> names)
615      throws IOException {
616    return fs.getXAttrs(path, names);
617  }
618
619  @Override
620  public List<String> listXAttrs(Path path) throws IOException {
621    return fs.listXAttrs(path);
622  }
623
624  @Override
625  public void removeXAttr(Path path, String name) throws IOException {
626    fs.removeXAttr(path, name);
627  }
628
629  @Override
630  public void setStoragePolicy(Path src, String policyName)
631      throws IOException {
632    fs.setStoragePolicy(src, policyName);
633  }
634
635  @Override
636  public void unsetStoragePolicy(Path src) throws IOException {
637    fs.unsetStoragePolicy(src);
638  }
639
640  @Override
641  public BlockStoragePolicySpi getStoragePolicy(final Path src)
642      throws IOException {
643    return fs.getStoragePolicy(src);
644  }
645
646  @Override
647  public Collection<? extends BlockStoragePolicySpi> getAllStoragePolicies()
648      throws IOException {
649    return fs.getAllStoragePolicies();
650  }
651
652  @Override
653  public Path getTrashRoot(Path path) {
654    return fs.getTrashRoot(path);
655  }
656
657  @Override
658  public Collection<FileStatus> getTrashRoots(boolean allUsers) {
659    return fs.getTrashRoots(allUsers);
660  }
661}