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