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);
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  @Deprecated
207  public FSDataOutputStream createNonRecursive(Path f, FsPermission permission,
208      EnumSet<CreateFlag> flags, int bufferSize, short replication, long blockSize,
209      Progressable progress) throws IOException {
210    
211    return fs.createNonRecursive(f, permission, flags, bufferSize, replication, blockSize,
212        progress);
213  }
214
215  /**
216   * Set replication for an existing file.
217   * 
218   * @param src file name
219   * @param replication new replication
220   * @throws IOException
221   * @return true if successful;
222   *         false if file does not exist or is a directory
223   */
224  @Override
225  public boolean setReplication(Path src, short replication) throws IOException {
226    return fs.setReplication(src, replication);
227  }
228  
229  /**
230   * Renames Path src to Path dst.  Can take place on local fs
231   * or remote DFS.
232   */
233  @Override
234  public boolean rename(Path src, Path dst) throws IOException {
235    return fs.rename(src, dst);
236  }
237  
238  /** Delete a file */
239  @Override
240  public boolean delete(Path f, boolean recursive) throws IOException {
241    return fs.delete(f, recursive);
242  }
243  
244  /** List files in a directory. */
245  @Override
246  public FileStatus[] listStatus(Path f) throws IOException {
247    return fs.listStatus(f);
248  }
249
250  @Override
251  public RemoteIterator<Path> listCorruptFileBlocks(Path path)
252    throws IOException {
253    return fs.listCorruptFileBlocks(path);
254  }
255
256  /** List files and its block locations in a directory. */
257  @Override
258  public RemoteIterator<LocatedFileStatus> listLocatedStatus(Path f)
259  throws IOException {
260    return fs.listLocatedStatus(f);
261  }
262  
263  /** Return a remote iterator for listing in a directory */
264  @Override
265  public RemoteIterator<FileStatus> listStatusIterator(Path f)
266  throws IOException {
267    return fs.listStatusIterator(f);
268   }
269
270  @Override
271  public Path getHomeDirectory() {
272    return fs.getHomeDirectory();
273  }
274
275
276  /**
277   * Set the current working directory for the given file system. All relative
278   * paths will be resolved relative to it.
279   * 
280   * @param newDir
281   */
282  @Override
283  public void setWorkingDirectory(Path newDir) {
284    fs.setWorkingDirectory(newDir);
285  }
286  
287  /**
288   * Get the current working directory for the given file system
289   * 
290   * @return the directory pathname
291   */
292  @Override
293  public Path getWorkingDirectory() {
294    return fs.getWorkingDirectory();
295  }
296  
297  @Override
298  protected Path getInitialWorkingDirectory() {
299    return fs.getInitialWorkingDirectory();
300  }
301  
302  @Override
303  public FsStatus getStatus(Path p) throws IOException {
304    return fs.getStatus(p);
305  }
306  
307  @Override
308  public boolean mkdirs(Path f, FsPermission permission) throws IOException {
309    return fs.mkdirs(f, permission);
310  }
311
312
313  /**
314   * The src file is on the local disk.  Add it to FS at
315   * the given dst name.
316   * delSrc indicates if the source should be removed
317   */
318  @Override
319  public void copyFromLocalFile(boolean delSrc, Path src, Path dst)
320    throws IOException {
321    fs.copyFromLocalFile(delSrc, src, dst);
322  }
323  
324  /**
325   * The src files are on the local disk.  Add it to FS at
326   * the given dst name.
327   * delSrc indicates if the source should be removed
328   */
329  @Override
330  public void copyFromLocalFile(boolean delSrc, boolean overwrite, 
331                                Path[] srcs, Path dst)
332    throws IOException {
333    fs.copyFromLocalFile(delSrc, overwrite, srcs, dst);
334  }
335  
336  /**
337   * The src file is on the local disk.  Add it to FS at
338   * the given dst name.
339   * delSrc indicates if the source should be removed
340   */
341  @Override
342  public void copyFromLocalFile(boolean delSrc, boolean overwrite, 
343                                Path src, Path dst)
344    throws IOException {
345    fs.copyFromLocalFile(delSrc, overwrite, src, dst);
346  }
347
348  /**
349   * The src file is under FS, and the dst is on the local disk.
350   * Copy it from FS control to the local dst name.
351   * delSrc indicates if the src will be removed or not.
352   */   
353  @Override
354  public void copyToLocalFile(boolean delSrc, Path src, Path dst)
355    throws IOException {
356    fs.copyToLocalFile(delSrc, src, dst);
357  }
358  
359  /**
360   * Returns a local File that the user can write output to.  The caller
361   * provides both the eventual FS target name and the local working
362   * file.  If the FS is local, we write directly into the target.  If
363   * the FS is remote, we write into the tmp local area.
364   */
365  @Override
366  public Path startLocalOutput(Path fsOutputFile, Path tmpLocalFile)
367    throws IOException {
368    return fs.startLocalOutput(fsOutputFile, tmpLocalFile);
369  }
370
371  /**
372   * Called when we're all done writing to the target.  A local FS will
373   * do nothing, because we've written to exactly the right place.  A remote
374   * FS will copy the contents of tmpLocalFile to the correct target at
375   * fsOutputFile.
376   */
377  @Override
378  public void completeLocalOutput(Path fsOutputFile, Path tmpLocalFile)
379    throws IOException {
380    fs.completeLocalOutput(fsOutputFile, tmpLocalFile);
381  }
382
383  /** Return the total size of all files in the filesystem.*/
384  @Override
385  public long getUsed() throws IOException{
386    return fs.getUsed();
387  }
388  
389  @Override
390  public long getDefaultBlockSize() {
391    return fs.getDefaultBlockSize();
392  }
393  
394  @Override
395  public short getDefaultReplication() {
396    return fs.getDefaultReplication();
397  }
398
399  @Override
400  public FsServerDefaults getServerDefaults() throws IOException {
401    return fs.getServerDefaults();
402  }
403
404  // path variants delegate to underlying filesystem 
405  @Override
406  public long getDefaultBlockSize(Path f) {
407    return fs.getDefaultBlockSize(f);
408  }
409
410  @Override
411  public short getDefaultReplication(Path f) {
412    return fs.getDefaultReplication(f);
413  }
414
415  @Override
416  public FsServerDefaults getServerDefaults(Path f) throws IOException {
417    return fs.getServerDefaults(f);
418  }
419
420  /**
421   * Get file status.
422   */
423  @Override
424  public FileStatus getFileStatus(Path f) throws IOException {
425    return fs.getFileStatus(f);
426  }
427
428  @Override
429  public void access(Path path, FsAction mode) throws AccessControlException,
430      FileNotFoundException, IOException {
431    fs.access(path, mode);
432  }
433
434  public void createSymlink(final Path target, final Path link,
435      final boolean createParent) throws AccessControlException,
436      FileAlreadyExistsException, FileNotFoundException,
437      ParentNotDirectoryException, UnsupportedFileSystemException, 
438      IOException {
439    fs.createSymlink(target, link, createParent);
440  }
441
442  public FileStatus getFileLinkStatus(final Path f)
443      throws AccessControlException, FileNotFoundException,
444      UnsupportedFileSystemException, IOException {
445    return fs.getFileLinkStatus(f);
446  }
447
448  public boolean supportsSymlinks() {
449    return fs.supportsSymlinks();
450  }
451
452  public Path getLinkTarget(Path f) throws IOException {
453    return fs.getLinkTarget(f);
454  }
455
456  protected Path resolveLink(Path f) throws IOException {
457    return fs.resolveLink(f);
458  }
459
460  @Override
461  public FileChecksum getFileChecksum(Path f) throws IOException {
462    return fs.getFileChecksum(f);
463  }
464
465  @Override
466  public FileChecksum getFileChecksum(Path f, long length) throws IOException {
467    return fs.getFileChecksum(f, length);
468  }
469
470  @Override
471  public void setVerifyChecksum(boolean verifyChecksum) {
472    fs.setVerifyChecksum(verifyChecksum);
473  }
474  
475  @Override
476  public void setWriteChecksum(boolean writeChecksum) {
477    fs.setWriteChecksum(writeChecksum);
478  }
479
480  @Override
481  public Configuration getConf() {
482    return fs.getConf();
483  }
484  
485  @Override
486  public void close() throws IOException {
487    super.close();
488    fs.close();
489  }
490
491  @Override
492  public void setOwner(Path p, String username, String groupname
493      ) throws IOException {
494    fs.setOwner(p, username, groupname);
495  }
496
497  @Override
498  public void setTimes(Path p, long mtime, long atime
499      ) throws IOException {
500    fs.setTimes(p, mtime, atime);
501  }
502
503  @Override
504  public void setPermission(Path p, FsPermission permission
505      ) throws IOException {
506    fs.setPermission(p, permission);
507  }
508
509  @Override
510  protected FSDataOutputStream primitiveCreate(Path f,
511      FsPermission absolutePermission, EnumSet<CreateFlag> flag,
512      int bufferSize, short replication, long blockSize,
513      Progressable progress, ChecksumOpt checksumOpt)
514      throws IOException {
515    return fs.primitiveCreate(f, absolutePermission, flag,
516        bufferSize, replication, blockSize, progress, checksumOpt);
517  }
518
519  @Override
520  @SuppressWarnings("deprecation")
521  protected boolean primitiveMkdir(Path f, FsPermission abdolutePermission)
522      throws IOException {
523    return fs.primitiveMkdir(f, abdolutePermission);
524  }
525  
526  @Override // FileSystem
527  public FileSystem[] getChildFileSystems() {
528    return new FileSystem[]{fs};
529  }
530
531  @Override // FileSystem
532  public Path createSnapshot(Path path, String snapshotName)
533      throws IOException {
534    return fs.createSnapshot(path, snapshotName);
535  }
536  
537  @Override // FileSystem
538  public void renameSnapshot(Path path, String snapshotOldName,
539      String snapshotNewName) throws IOException {
540    fs.renameSnapshot(path, snapshotOldName, snapshotNewName);
541  }
542  
543  @Override // FileSystem
544  public void deleteSnapshot(Path path, String snapshotName)
545      throws IOException {
546    fs.deleteSnapshot(path, snapshotName);
547  }
548
549  @Override
550  public void modifyAclEntries(Path path, List<AclEntry> aclSpec)
551      throws IOException {
552    fs.modifyAclEntries(path, aclSpec);
553  }
554
555  @Override
556  public void removeAclEntries(Path path, List<AclEntry> aclSpec)
557      throws IOException {
558    fs.removeAclEntries(path, aclSpec);
559  }
560
561  @Override
562  public void removeDefaultAcl(Path path) throws IOException {
563    fs.removeDefaultAcl(path);
564  }
565
566  @Override
567  public void removeAcl(Path path) throws IOException {
568    fs.removeAcl(path);
569  }
570
571  @Override
572  public void setAcl(Path path, List<AclEntry> aclSpec) throws IOException {
573    fs.setAcl(path, aclSpec);
574  }
575
576  @Override
577  public AclStatus getAclStatus(Path path) throws IOException {
578    return fs.getAclStatus(path);
579  }
580
581  @Override
582  public void setXAttr(Path path, String name, byte[] value)
583      throws IOException {
584    fs.setXAttr(path, name, value);
585  }
586
587  @Override
588  public void setXAttr(Path path, String name, byte[] value,
589      EnumSet<XAttrSetFlag> flag) throws IOException {
590    fs.setXAttr(path, name, value, flag);
591  }
592
593  @Override
594  public byte[] getXAttr(Path path, String name) throws IOException {
595    return fs.getXAttr(path, name);
596  }
597
598  @Override
599  public Map<String, byte[]> getXAttrs(Path path) throws IOException {
600    return fs.getXAttrs(path);
601  }
602
603  @Override
604  public Map<String, byte[]> getXAttrs(Path path, List<String> names)
605      throws IOException {
606    return fs.getXAttrs(path, names);
607  }
608
609  @Override
610  public List<String> listXAttrs(Path path) throws IOException {
611    return fs.listXAttrs(path);
612  }
613
614  @Override
615  public void removeXAttr(Path path, String name) throws IOException {
616    fs.removeXAttr(path, name);
617  }
618
619  @Override
620  public Path getTrashRoot(Path path) {
621    return fs.getTrashRoot(path);
622  }
623
624  @Override
625  public Collection<FileStatus> getTrashRoots(boolean allUsers) {
626    return fs.getTrashRoots(allUsers);
627  }
628}