PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/java/org/apache/hadoop/fs/shell/PathData.java

https://github.com/RS1999ent/hadoop-common
Java | 241 lines | 112 code | 20 blank | 109 comment | 14 complexity | a2fbee64a0b27a6cffd802070ea00614 MD5 | raw file
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.fs.shell;
  19. import java.io.File;
  20. import java.io.FileNotFoundException;
  21. import java.io.IOException;
  22. import org.apache.hadoop.classification.InterfaceAudience;
  23. import org.apache.hadoop.classification.InterfaceStability;
  24. import org.apache.hadoop.conf.Configuration;
  25. import org.apache.hadoop.fs.FileStatus;
  26. import org.apache.hadoop.fs.FileSystem;
  27. import org.apache.hadoop.fs.Path;
  28. import org.apache.hadoop.fs.shell.PathExceptions.PathIsNotDirectoryException;
  29. /**
  30. * Encapsulates a Path (path), its FileStatus (stat), and its FileSystem (fs).
  31. * The stat field will be null if the path does not exist.
  32. */
  33. @InterfaceAudience.Private
  34. @InterfaceStability.Evolving
  35. public class PathData {
  36. protected String string = null;
  37. public final Path path;
  38. public FileStatus stat;
  39. public final FileSystem fs;
  40. public boolean exists;
  41. /**
  42. * Creates an object to wrap the given parameters as fields. The string
  43. * used to create the path will be recorded since the Path object does not
  44. * return exactly the same string used to initialize it
  45. * @param pathString a string for a path
  46. * @param conf the configuration file
  47. * @throws IOException if anything goes wrong...
  48. */
  49. public PathData(String pathString, Configuration conf) throws IOException {
  50. this.string = pathString;
  51. this.path = new Path(pathString);
  52. this.fs = path.getFileSystem(conf);
  53. setStat(getStat(fs, path));
  54. }
  55. /**
  56. * Creates an object to wrap the given parameters as fields. The string
  57. * used to create the path will be recorded since the Path object does not
  58. * return exactly the same string used to initialize it
  59. * @param localPath a local File
  60. * @param conf the configuration file
  61. * @throws IOException if anything goes wrong...
  62. */
  63. public PathData(File localPath, Configuration conf) throws IOException {
  64. this.string = localPath.toString();
  65. this.path = new Path(this.string);
  66. this.fs = FileSystem.getLocal(conf);
  67. setStat(getStat(fs, path));
  68. }
  69. /**
  70. * Creates an object to wrap the given parameters as fields.
  71. * @param fs the FileSystem
  72. * @param path a Path
  73. * @param stat the FileStatus (may be null if the path doesn't exist)
  74. */
  75. public PathData(FileSystem fs, Path path, FileStatus stat) {
  76. this.string = path.toString();
  77. this.path = path;
  78. this.fs = fs;
  79. setStat(stat);
  80. }
  81. /**
  82. * Convenience ctor that looks up the file status for a path. If the path
  83. * doesn't exist, then the status will be null
  84. * @param fs the FileSystem for the path
  85. * @param path the pathname to lookup
  86. * @throws IOException if anything goes wrong
  87. */
  88. public PathData(FileSystem fs, Path path) throws IOException {
  89. this(fs, path, getStat(fs, path));
  90. }
  91. /**
  92. * Creates an object to wrap the given parameters as fields. The string
  93. * used to create the path will be recorded since the Path object does not
  94. * return exactly the same string used to initialize it. If the FileStatus
  95. * is not null, then its Path will be used to initialized the path, else
  96. * the string of the path will be used.
  97. * @param fs the FileSystem
  98. * @param pathString a String of the path
  99. * @param stat the FileStatus (may be null if the path doesn't exist)
  100. */
  101. public PathData(FileSystem fs, String pathString, FileStatus stat) {
  102. this.string = pathString;
  103. this.path = (stat != null) ? stat.getPath() : new Path(pathString);
  104. this.fs = fs;
  105. setStat(stat);
  106. }
  107. // need a static method for the ctor above
  108. private static FileStatus getStat(FileSystem fs, Path path)
  109. throws IOException {
  110. FileStatus status = null;
  111. try {
  112. status = fs.getFileStatus(path);
  113. } catch (FileNotFoundException e) {} // ignore FNF
  114. return status;
  115. }
  116. private void setStat(FileStatus theStat) {
  117. stat = theStat;
  118. exists = (stat != null);
  119. }
  120. /**
  121. * Convenience ctor that extracts the path from the given file status
  122. * @param fs the FileSystem for the FileStatus
  123. * @param stat the FileStatus
  124. */
  125. public PathData(FileSystem fs, FileStatus stat) {
  126. this(fs, stat.getPath(), stat);
  127. }
  128. /**
  129. * Updates the paths's file status
  130. * @return the updated FileStatus
  131. * @throws IOException if anything goes wrong...
  132. */
  133. public FileStatus refreshStatus() throws IOException {
  134. setStat(fs.getFileStatus(path));
  135. return stat;
  136. }
  137. /**
  138. * Returns a list of PathData objects of the items contained in the given
  139. * directory.
  140. * @return list of PathData objects for its children
  141. * @throws IOException if anything else goes wrong...
  142. */
  143. public PathData[] getDirectoryContents() throws IOException {
  144. if (!stat.isDirectory()) {
  145. throw new PathIsNotDirectoryException(string);
  146. }
  147. FileStatus[] stats = fs.listStatus(path);
  148. PathData[] items = new PathData[stats.length];
  149. for (int i=0; i < stats.length; i++) {
  150. // preserve relative paths
  151. String basename = stats[i].getPath().getName();
  152. String parent = string;
  153. if (!parent.endsWith(Path.SEPARATOR)) parent += Path.SEPARATOR;
  154. items[i] = new PathData(fs, parent + basename, stats[i]);
  155. }
  156. return items;
  157. }
  158. /**
  159. * Creates a new object for a child entry in this directory
  160. * @param child the basename will be appended to this object's path
  161. * @return PathData for the child
  162. * @throws IOException if this object does not exist or is not a directory
  163. */
  164. public PathData getPathDataForChild(PathData child) throws IOException {
  165. if (!stat.isDirectory()) {
  166. throw new PathIsNotDirectoryException(string);
  167. }
  168. return new PathData(fs, new Path(path, child.path.getName()));
  169. }
  170. /**
  171. * Expand the given path as a glob pattern. Non-existent paths do not
  172. * throw an exception because creation commands like touch and mkdir need
  173. * to create them. The "stat" field will be null if the path does not
  174. * exist.
  175. * @param pattern the pattern to expand as a glob
  176. * @param conf the hadoop configuration
  177. * @return list of {@link PathData} objects. if the pattern is not a glob,
  178. * and does not exist, the list will contain a single PathData with a null
  179. * stat
  180. * @throws IOException anything else goes wrong...
  181. */
  182. public static PathData[] expandAsGlob(String pattern, Configuration conf)
  183. throws IOException {
  184. Path globPath = new Path(pattern);
  185. FileSystem fs = globPath.getFileSystem(conf);
  186. FileStatus[] stats = fs.globStatus(globPath);
  187. PathData[] items = null;
  188. if (stats == null) {
  189. // not a glob & file not found, so add the path with a null stat
  190. items = new PathData[]{ new PathData(fs, pattern, null) };
  191. } else if (
  192. // this is very ugly, but needed to avoid breaking hdfs tests...
  193. // if a path has no authority, then the FileStatus from globStatus
  194. // will add the "-fs" authority into the path, so we need to sub
  195. // it back out to satisfy the tests
  196. stats.length == 1
  197. &&
  198. stats[0].getPath().equals(fs.makeQualified(globPath)))
  199. {
  200. // if the fq path is identical to the pattern passed, use the pattern
  201. // to initialize the string value
  202. items = new PathData[]{ new PathData(fs, pattern, stats[0]) };
  203. } else {
  204. // convert stats to PathData
  205. items = new PathData[stats.length];
  206. int i=0;
  207. for (FileStatus stat : stats) {
  208. items[i++] = new PathData(fs, stat);
  209. }
  210. }
  211. return items;
  212. }
  213. /**
  214. * Returns the printable version of the path that is either the path
  215. * as given on the commandline, or the full path
  216. * @return String of the path
  217. */
  218. public String toString() {
  219. return (string != null) ? string : path.toString();
  220. }
  221. }