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 */ 018package org.apache.hadoop.hdfs.server.namenode; 019 020import java.io.PrintStream; 021import java.io.PrintWriter; 022import java.io.StringWriter; 023import java.util.List; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.apache.hadoop.classification.InterfaceAudience; 028import org.apache.hadoop.fs.ContentSummary; 029import org.apache.hadoop.fs.Path; 030import org.apache.hadoop.fs.permission.FsPermission; 031import org.apache.hadoop.fs.permission.PermissionStatus; 032import org.apache.hadoop.hdfs.server.blockmanagement.BlockStoragePolicySuite; 033import org.apache.hadoop.hdfs.DFSUtil; 034import org.apache.hadoop.hdfs.protocol.Block; 035import org.apache.hadoop.hdfs.protocol.QuotaExceededException; 036import org.apache.hadoop.hdfs.server.namenode.INodeReference.DstReference; 037import org.apache.hadoop.hdfs.server.namenode.INodeReference.WithName; 038import org.apache.hadoop.hdfs.server.namenode.snapshot.Snapshot; 039import org.apache.hadoop.hdfs.util.Diff; 040import org.apache.hadoop.util.ChunkedArrayList; 041import org.apache.hadoop.util.StringUtils; 042 043import com.google.common.annotations.VisibleForTesting; 044import com.google.common.base.Preconditions; 045 046/** 047 * We keep an in-memory representation of the file/block hierarchy. 048 * This is a base INode class containing common fields for file and 049 * directory inodes. 050 */ 051@InterfaceAudience.Private 052public abstract class INode implements INodeAttributes, Diff.Element<byte[]>, 053 AuthorizationProvider.INodeAuthorizationInfo { 054 public static final Log LOG = LogFactory.getLog(INode.class); 055 056 /** parent is either an {@link INodeDirectory} or an {@link INodeReference}.*/ 057 private INode parent = null; 058 059 INode(INode parent) { 060 this.parent = parent; 061 } 062 063 /** Get inode id */ 064 public abstract long getId(); 065 066 /** 067 * Check whether this is the root inode. 068 */ 069 final boolean isRoot() { 070 return getLocalNameBytes().length == 0; 071 } 072 073 /** Get the {@link PermissionStatus} */ 074 abstract PermissionStatus getPermissionStatus(int snapshotId); 075 076 /** The same as getPermissionStatus(null). */ 077 final PermissionStatus getPermissionStatus() { 078 return getPermissionStatus(Snapshot.CURRENT_STATE_ID); 079 } 080 081 /** 082 * @param snapshotId 083 * if it is not {@link Snapshot#CURRENT_STATE_ID}, get the result 084 * from the given snapshot; otherwise, get the result from the 085 * current inode. 086 * @return user name 087 */ 088 @Override 089 public abstract String getUserName(int snapshotId); 090 091 /** The same as getUserName(Snapshot.CURRENT_STATE_ID). */ 092 @Override 093 public final String getUserName() { 094 return getUserName(Snapshot.CURRENT_STATE_ID); 095 } 096 097 /** Set user */ 098 abstract void setUser(String user); 099 100 /** Set user */ 101 final INode setUser(String user, int latestSnapshotId) { 102 recordModification(latestSnapshotId); 103 setUser(user); 104 return this; 105 } 106 /** 107 * @param snapshotId 108 * if it is not {@link Snapshot#CURRENT_STATE_ID}, get the result 109 * from the given snapshot; otherwise, get the result from the 110 * current inode. 111 * @return group name 112 */ 113 @Override 114 public abstract String getGroupName(int snapshotId); 115 116 /** The same as getGroupName(Snapshot.CURRENT_STATE_ID). */ 117 @Override 118 public final String getGroupName() { 119 return getGroupName(Snapshot.CURRENT_STATE_ID); 120 } 121 122 /** Set group */ 123 abstract void setGroup(String group); 124 125 /** Set group */ 126 final INode setGroup(String group, int latestSnapshotId) { 127 recordModification(latestSnapshotId); 128 setGroup(group); 129 return this; 130 } 131 132 /** 133 * @param snapshotId 134 * if it is not {@link Snapshot#CURRENT_STATE_ID}, get the result 135 * from the given snapshot; otherwise, get the result from the 136 * current inode. 137 * @return permission. 138 */ 139 @Override 140 public abstract FsPermission getFsPermission(int snapshotId); 141 142 /** The same as getFsPermission(Snapshot.CURRENT_STATE_ID). */ 143 @Override 144 public final FsPermission getFsPermission() { 145 return getFsPermission(Snapshot.CURRENT_STATE_ID); 146 } 147 148 /** Set the {@link FsPermission} of this {@link INode} */ 149 abstract void setPermission(FsPermission permission); 150 151 /** Set the {@link FsPermission} of this {@link INode} */ 152 INode setPermission(FsPermission permission, int latestSnapshotId) { 153 recordModification(latestSnapshotId); 154 setPermission(permission); 155 return this; 156 } 157 158 @Override 159 public abstract AclFeature getAclFeature(int snapshotId); 160 161 @Override 162 public final AclFeature getAclFeature() { 163 return getAclFeature(Snapshot.CURRENT_STATE_ID); 164 } 165 166 abstract void addAclFeature(AclFeature aclFeature); 167 168 final INode addAclFeature(AclFeature aclFeature, int latestSnapshotId) { 169 recordModification(latestSnapshotId); 170 addAclFeature(aclFeature); 171 return this; 172 } 173 174 abstract void removeAclFeature(); 175 176 final INode removeAclFeature(int latestSnapshotId) { 177 recordModification(latestSnapshotId); 178 removeAclFeature(); 179 return this; 180 } 181 182 /** 183 * @param snapshotId 184 * if it is not {@link Snapshot#CURRENT_STATE_ID}, get the result 185 * from the given snapshot; otherwise, get the result from the 186 * current inode. 187 * @return XAttrFeature 188 */ 189 abstract XAttrFeature getXAttrFeature(int snapshotId); 190 191 @Override 192 public final XAttrFeature getXAttrFeature() { 193 return getXAttrFeature(Snapshot.CURRENT_STATE_ID); 194 } 195 196 /** 197 * Set <code>XAttrFeature</code> 198 */ 199 abstract void addXAttrFeature(XAttrFeature xAttrFeature); 200 201 final INode addXAttrFeature(XAttrFeature xAttrFeature, int latestSnapshotId) { 202 recordModification(latestSnapshotId); 203 addXAttrFeature(xAttrFeature); 204 return this; 205 } 206 207 /** 208 * Remove <code>XAttrFeature</code> 209 */ 210 abstract void removeXAttrFeature(); 211 212 final INode removeXAttrFeature(int lastestSnapshotId) { 213 recordModification(lastestSnapshotId); 214 removeXAttrFeature(); 215 return this; 216 } 217 218 /** 219 * @return if the given snapshot id is {@link Snapshot#CURRENT_STATE_ID}, 220 * return this; otherwise return the corresponding snapshot inode. 221 */ 222 public INodeAttributes getSnapshotINode(final int snapshotId) { 223 return this; 224 } 225 226 /** Is this inode in the latest snapshot? */ 227 public final boolean isInLatestSnapshot(final int latestSnapshotId) { 228 if (latestSnapshotId == Snapshot.CURRENT_STATE_ID) { 229 return false; 230 } 231 // if parent is a reference node, parent must be a renamed node. We can 232 // stop the check at the reference node. 233 if (parent != null && parent.isReference()) { 234 return true; 235 } 236 final INodeDirectory parentDir = getParent(); 237 if (parentDir == null) { // root 238 return true; 239 } 240 if (!parentDir.isInLatestSnapshot(latestSnapshotId)) { 241 return false; 242 } 243 final INode child = parentDir.getChild(getLocalNameBytes(), latestSnapshotId); 244 if (this == child) { 245 return true; 246 } 247 return child != null && child.isReference() && 248 this == child.asReference().getReferredINode(); 249 } 250 251 /** @return true if the given inode is an ancestor directory of this inode. */ 252 public final boolean isAncestorDirectory(final INodeDirectory dir) { 253 for(INodeDirectory p = getParent(); p != null; p = p.getParent()) { 254 if (p == dir) { 255 return true; 256 } 257 } 258 return false; 259 } 260 261 /** 262 * When {@link #recordModification} is called on a referred node, 263 * this method tells which snapshot the modification should be 264 * associated with: the snapshot that belongs to the SRC tree of the rename 265 * operation, or the snapshot belonging to the DST tree. 266 * 267 * @param latestInDst 268 * id of the latest snapshot in the DST tree above the reference node 269 * @return True: the modification should be recorded in the snapshot that 270 * belongs to the SRC tree. False: the modification should be 271 * recorded in the snapshot that belongs to the DST tree. 272 */ 273 public final boolean shouldRecordInSrcSnapshot(final int latestInDst) { 274 Preconditions.checkState(!isReference()); 275 276 if (latestInDst == Snapshot.CURRENT_STATE_ID) { 277 return true; 278 } 279 INodeReference withCount = getParentReference(); 280 if (withCount != null) { 281 int dstSnapshotId = withCount.getParentReference().getDstSnapshotId(); 282 if (dstSnapshotId != Snapshot.CURRENT_STATE_ID 283 && dstSnapshotId >= latestInDst) { 284 return true; 285 } 286 } 287 return false; 288 } 289 290 /** 291 * This inode is being modified. The previous version of the inode needs to 292 * be recorded in the latest snapshot. 293 * 294 * @param latestSnapshotId The id of the latest snapshot that has been taken. 295 * Note that it is {@link Snapshot#CURRENT_STATE_ID} 296 * if no snapshots have been taken. 297 */ 298 abstract void recordModification(final int latestSnapshotId); 299 300 /** Check whether it's a reference. */ 301 public boolean isReference() { 302 return false; 303 } 304 305 /** Cast this inode to an {@link INodeReference}. */ 306 public INodeReference asReference() { 307 throw new IllegalStateException("Current inode is not a reference: " 308 + this.toDetailString()); 309 } 310 311 /** 312 * Check whether it's a file. 313 */ 314 public boolean isFile() { 315 return false; 316 } 317 318 /** Cast this inode to an {@link INodeFile}. */ 319 public INodeFile asFile() { 320 throw new IllegalStateException("Current inode is not a file: " 321 + this.toDetailString()); 322 } 323 324 /** 325 * Check whether it's a directory 326 */ 327 @Override 328 public boolean isDirectory() { 329 return false; 330 } 331 332 /** Cast this inode to an {@link INodeDirectory}. */ 333 public INodeDirectory asDirectory() { 334 throw new IllegalStateException("Current inode is not a directory: " 335 + this.toDetailString()); 336 } 337 338 /** 339 * Check whether it's a symlink 340 */ 341 public boolean isSymlink() { 342 return false; 343 } 344 345 /** Cast this inode to an {@link INodeSymlink}. */ 346 public INodeSymlink asSymlink() { 347 throw new IllegalStateException("Current inode is not a symlink: " 348 + this.toDetailString()); 349 } 350 351 /** 352 * Clean the subtree under this inode and collect the blocks from the descents 353 * for further block deletion/update. The current inode can either resides in 354 * the current tree or be stored as a snapshot copy. 355 * 356 * <pre> 357 * In general, we have the following rules. 358 * 1. When deleting a file/directory in the current tree, we have different 359 * actions according to the type of the node to delete. 360 * 361 * 1.1 The current inode (this) is an {@link INodeFile}. 362 * 1.1.1 If {@code prior} is null, there is no snapshot taken on ancestors 363 * before. Thus we simply destroy (i.e., to delete completely, no need to save 364 * snapshot copy) the current INode and collect its blocks for further 365 * cleansing. 366 * 1.1.2 Else do nothing since the current INode will be stored as a snapshot 367 * copy. 368 * 369 * 1.2 The current inode is an {@link INodeDirectory}. 370 * 1.2.1 If {@code prior} is null, there is no snapshot taken on ancestors 371 * before. Similarly, we destroy the whole subtree and collect blocks. 372 * 1.2.2 Else do nothing with the current INode. Recursively clean its 373 * children. 374 * 375 * 1.3 The current inode is a file with snapshot. 376 * Call recordModification(..) to capture the current states. 377 * Mark the INode as deleted. 378 * 379 * 1.4 The current inode is an {@link INodeDirectory} with snapshot feature. 380 * Call recordModification(..) to capture the current states. 381 * Destroy files/directories created after the latest snapshot 382 * (i.e., the inodes stored in the created list of the latest snapshot). 383 * Recursively clean remaining children. 384 * 385 * 2. When deleting a snapshot. 386 * 2.1 To clean {@link INodeFile}: do nothing. 387 * 2.2 To clean {@link INodeDirectory}: recursively clean its children. 388 * 2.3 To clean INodeFile with snapshot: delete the corresponding snapshot in 389 * its diff list. 390 * 2.4 To clean {@link INodeDirectory} with snapshot: delete the corresponding 391 * snapshot in its diff list. Recursively clean its children. 392 * </pre> 393 * 394 * @param snapshotId 395 * The id of the snapshot to delete. 396 * {@link Snapshot#CURRENT_STATE_ID} means to delete the current 397 * file/directory. 398 * @param priorSnapshotId 399 * The id of the latest snapshot before the to-be-deleted snapshot. 400 * When deleting a current inode, this parameter captures the latest 401 * snapshot. 402 * @param collectedBlocks 403 * blocks collected from the descents for further block 404 * deletion/update will be added to the given map. 405 * @param removedINodes 406 * INodes collected from the descents for further cleaning up of 407 * inodeMap 408 * @return quota usage delta when deleting a snapshot 409 */ 410 public abstract Quota.Counts cleanSubtree(final int snapshotId, 411 int priorSnapshotId, BlocksMapUpdateInfo collectedBlocks, 412 List<INode> removedINodes); 413 414 /** 415 * Destroy self and clear everything! If the INode is a file, this method 416 * collects its blocks for further block deletion. If the INode is a 417 * directory, the method goes down the subtree and collects blocks from the 418 * descents, and clears its parent/children references as well. The method 419 * also clears the diff list if the INode contains snapshot diff list. 420 * 421 * @param collectedBlocks 422 * blocks collected from the descents for further block 423 * deletion/update will be added to this map. 424 * @param removedINodes 425 * INodes collected from the descents for further cleaning up of 426 * inodeMap 427 */ 428 public abstract void destroyAndCollectBlocks( 429 BlocksMapUpdateInfo collectedBlocks, List<INode> removedINodes); 430 431 /** Compute {@link ContentSummary}. Blocking call */ 432 public final ContentSummary computeContentSummary() { 433 return computeAndConvertContentSummary( 434 new ContentSummaryComputationContext()); 435 } 436 437 /** 438 * Compute {@link ContentSummary}. 439 */ 440 public final ContentSummary computeAndConvertContentSummary( 441 ContentSummaryComputationContext summary) { 442 Content.Counts counts = computeContentSummary(summary).getCounts(); 443 final Quota.Counts q = getQuotaCounts(); 444 return new ContentSummary(counts.get(Content.LENGTH), 445 counts.get(Content.FILE) + counts.get(Content.SYMLINK), 446 counts.get(Content.DIRECTORY), q.get(Quota.NAMESPACE), 447 counts.get(Content.DISKSPACE), q.get(Quota.DISKSPACE)); 448 } 449 450 /** 451 * Count subtree content summary with a {@link Content.Counts}. 452 * 453 * @param summary the context object holding counts for the subtree. 454 * @return The same objects as summary. 455 */ 456 public abstract ContentSummaryComputationContext computeContentSummary( 457 ContentSummaryComputationContext summary); 458 459 460 /** 461 * Check and add namespace/diskspace consumed to itself and the ancestors. 462 * @throws QuotaExceededException if quote is violated. 463 */ 464 public void addSpaceConsumed(long nsDelta, long dsDelta, boolean verify) 465 throws QuotaExceededException { 466 addSpaceConsumed2Parent(nsDelta, dsDelta, verify); 467 } 468 469 /** 470 * Check and add namespace/diskspace consumed to itself and the ancestors. 471 * @throws QuotaExceededException if quote is violated. 472 */ 473 void addSpaceConsumed2Parent(long nsDelta, long dsDelta, boolean verify) 474 throws QuotaExceededException { 475 if (parent != null) { 476 parent.addSpaceConsumed(nsDelta, dsDelta, verify); 477 } 478 } 479 480 /** 481 * Get the quota set for this inode 482 * @return the quota counts. The count is -1 if it is not set. 483 */ 484 public Quota.Counts getQuotaCounts() { 485 return Quota.Counts.newInstance(-1, -1); 486 } 487 488 public final boolean isQuotaSet() { 489 final Quota.Counts q = getQuotaCounts(); 490 return q.get(Quota.NAMESPACE) >= 0 || q.get(Quota.DISKSPACE) >= 0; 491 } 492 493 /** 494 * Count subtree {@link Quota#NAMESPACE} and {@link Quota#DISKSPACE} usages. 495 */ 496 public final Quota.Counts computeQuotaUsage() { 497 return computeQuotaUsage(new Quota.Counts(), true); 498 } 499 500 /** 501 * Count subtree {@link Quota#NAMESPACE} and {@link Quota#DISKSPACE} usages. 502 * 503 * With the existence of {@link INodeReference}, the same inode and its 504 * subtree may be referred by multiple {@link WithName} nodes and a 505 * {@link DstReference} node. To avoid circles while quota usage computation, 506 * we have the following rules: 507 * 508 * <pre> 509 * 1. For a {@link DstReference} node, since the node must be in the current 510 * tree (or has been deleted as the end point of a series of rename 511 * operations), we compute the quota usage of the referred node (and its 512 * subtree) in the regular manner, i.e., including every inode in the current 513 * tree and in snapshot copies, as well as the size of diff list. 514 * 515 * 2. For a {@link WithName} node, since the node must be in a snapshot, we 516 * only count the quota usage for those nodes that still existed at the 517 * creation time of the snapshot associated with the {@link WithName} node. 518 * We do not count in the size of the diff list. 519 * <pre> 520 * 521 * @param counts The subtree counts for returning. 522 * @param useCache Whether to use cached quota usage. Note that 523 * {@link WithName} node never uses cache for its subtree. 524 * @param lastSnapshotId {@link Snapshot#CURRENT_STATE_ID} indicates the 525 * computation is in the current tree. Otherwise the id 526 * indicates the computation range for a 527 * {@link WithName} node. 528 * @return The same objects as the counts parameter. 529 */ 530 public abstract Quota.Counts computeQuotaUsage(Quota.Counts counts, 531 boolean useCache, int lastSnapshotId); 532 533 public final Quota.Counts computeQuotaUsage(Quota.Counts counts, 534 boolean useCache) { 535 return computeQuotaUsage(counts, useCache, Snapshot.CURRENT_STATE_ID); 536 } 537 538 /** 539 * @return null if the local name is null; otherwise, return the local name. 540 */ 541 public final String getLocalName() { 542 final byte[] name = getLocalNameBytes(); 543 return name == null? null: DFSUtil.bytes2String(name); 544 } 545 546 @Override 547 public final byte[] getKey() { 548 return getLocalNameBytes(); 549 } 550 551 /** 552 * Set local file name 553 */ 554 public abstract void setLocalName(byte[] name); 555 556 @Override 557 public String getFullPathName() { 558 // Get the full path name of this inode. 559 return FSDirectory.getFullPathName(this); 560 } 561 562 @Override 563 public String toString() { 564 return getLocalName(); 565 } 566 567 @VisibleForTesting 568 public final String getObjectString() { 569 return getClass().getSimpleName() + "@" 570 + Integer.toHexString(super.hashCode()); 571 } 572 573 /** @return a string description of the parent. */ 574 @VisibleForTesting 575 public final String getParentString() { 576 final INodeReference parentRef = getParentReference(); 577 if (parentRef != null) { 578 return "parentRef=" + parentRef.getLocalName() + "->"; 579 } else { 580 final INodeDirectory parentDir = getParent(); 581 if (parentDir != null) { 582 return "parentDir=" + parentDir.getLocalName() + "/"; 583 } else { 584 return "parent=null"; 585 } 586 } 587 } 588 589 @VisibleForTesting 590 public String toDetailString() { 591 return toString() + "(" + getObjectString() + "), " + getParentString(); 592 } 593 594 /** @return the parent directory */ 595 public final INodeDirectory getParent() { 596 return parent == null? null 597 : parent.isReference()? getParentReference().getParent(): parent.asDirectory(); 598 } 599 600 /** 601 * @return the parent as a reference if this is a referred inode; 602 * otherwise, return null. 603 */ 604 public INodeReference getParentReference() { 605 return parent == null || !parent.isReference()? null: (INodeReference)parent; 606 } 607 608 /** Set parent directory */ 609 public final void setParent(INodeDirectory parent) { 610 this.parent = parent; 611 } 612 613 /** Set container. */ 614 public final void setParentReference(INodeReference parent) { 615 this.parent = parent; 616 } 617 618 /** Clear references to other objects. */ 619 public void clear() { 620 setParent(null); 621 } 622 623 /** 624 * @param snapshotId 625 * if it is not {@link Snapshot#CURRENT_STATE_ID}, get the result 626 * from the given snapshot; otherwise, get the result from the 627 * current inode. 628 * @return modification time. 629 */ 630 abstract long getModificationTime(int snapshotId); 631 632 /** The same as getModificationTime(Snapshot.CURRENT_STATE_ID). */ 633 @Override 634 public final long getModificationTime() { 635 return getModificationTime(Snapshot.CURRENT_STATE_ID); 636 } 637 638 /** Update modification time if it is larger than the current value. */ 639 public abstract INode updateModificationTime(long mtime, int latestSnapshotId); 640 641 /** Set the last modification time of inode. */ 642 public abstract void setModificationTime(long modificationTime); 643 644 /** Set the last modification time of inode. */ 645 public final INode setModificationTime(long modificationTime, 646 int latestSnapshotId) { 647 recordModification(latestSnapshotId); 648 setModificationTime(modificationTime); 649 return this; 650 } 651 652 /** 653 * @param snapshotId 654 * if it is not {@link Snapshot#CURRENT_STATE_ID}, get the result 655 * from the given snapshot; otherwise, get the result from the 656 * current inode. 657 * @return access time 658 */ 659 abstract long getAccessTime(int snapshotId); 660 661 /** The same as getAccessTime(Snapshot.CURRENT_STATE_ID). */ 662 @Override 663 public final long getAccessTime() { 664 return getAccessTime(Snapshot.CURRENT_STATE_ID); 665 } 666 667 /** 668 * Set last access time of inode. 669 */ 670 public abstract void setAccessTime(long accessTime); 671 672 /** 673 * Set last access time of inode. 674 */ 675 public final INode setAccessTime(long accessTime, int latestSnapshotId) { 676 recordModification(latestSnapshotId); 677 setAccessTime(accessTime); 678 return this; 679 } 680 681 /** 682 * @return the latest block storage policy id of the INode. Specifically, 683 * if a storage policy is directly specified on the INode then return the ID 684 * of that policy. Otherwise follow the latest parental path and return the 685 * ID of the first specified storage policy. 686 */ 687 public abstract byte getStoragePolicyID(); 688 689 /** 690 * @return the storage policy directly specified on the INode. Return 691 * {@link BlockStoragePolicySuite#ID_UNSPECIFIED} if no policy has 692 * been specified. 693 */ 694 public abstract byte getLocalStoragePolicyID(); 695 696 /** 697 * Breaks {@code path} into components. 698 * @return array of byte arrays each of which represents 699 * a single path component. 700 */ 701 @VisibleForTesting 702 public static byte[][] getPathComponents(String path) { 703 return getPathComponents(getPathNames(path)); 704 } 705 706 /** Convert strings to byte arrays for path components. */ 707 static byte[][] getPathComponents(String[] strings) { 708 if (strings.length == 0) { 709 return new byte[][]{null}; 710 } 711 byte[][] bytes = new byte[strings.length][]; 712 for (int i = 0; i < strings.length; i++) 713 bytes[i] = DFSUtil.string2Bytes(strings[i]); 714 return bytes; 715 } 716 717 /** 718 * Splits an absolute {@code path} into an array of path components. 719 * @throws AssertionError if the given path is invalid. 720 * @return array of path components. 721 */ 722 public static String[] getPathNames(String path) { 723 if (path == null || !path.startsWith(Path.SEPARATOR)) { 724 throw new AssertionError("Absolute path required"); 725 } 726 return StringUtils.split(path, Path.SEPARATOR_CHAR); 727 } 728 729 @Override 730 public final int compareTo(byte[] bytes) { 731 return DFSUtil.compareBytes(getLocalNameBytes(), bytes); 732 } 733 734 @Override 735 public final boolean equals(Object that) { 736 if (this == that) { 737 return true; 738 } 739 if (that == null || !(that instanceof INode)) { 740 return false; 741 } 742 return getId() == ((INode) that).getId(); 743 } 744 745 @Override 746 public final int hashCode() { 747 long id = getId(); 748 return (int)(id^(id>>>32)); 749 } 750 751 /** 752 * Dump the subtree starting from this inode. 753 * @return a text representation of the tree. 754 */ 755 @VisibleForTesting 756 public final StringBuffer dumpTreeRecursively() { 757 final StringWriter out = new StringWriter(); 758 dumpTreeRecursively(new PrintWriter(out, true), new StringBuilder(), 759 Snapshot.CURRENT_STATE_ID); 760 return out.getBuffer(); 761 } 762 763 @VisibleForTesting 764 public final void dumpTreeRecursively(PrintStream out) { 765 dumpTreeRecursively(new PrintWriter(out, true), new StringBuilder(), 766 Snapshot.CURRENT_STATE_ID); 767 } 768 769 /** 770 * Dump tree recursively. 771 * @param prefix The prefix string that each line should print. 772 */ 773 @VisibleForTesting 774 public void dumpTreeRecursively(PrintWriter out, StringBuilder prefix, 775 int snapshotId) { 776 out.print(prefix); 777 out.print(" "); 778 final String name = getLocalName(); 779 out.print(name.isEmpty()? "/": name); 780 out.print(" ("); 781 out.print(getObjectString()); 782 out.print("), "); 783 out.print(getParentString()); 784 out.print(", " + getPermissionStatus(snapshotId)); 785 } 786 787 /** 788 * Information used for updating the blocksMap when deleting files. 789 */ 790 public static class BlocksMapUpdateInfo { 791 /** 792 * The list of blocks that need to be removed from blocksMap 793 */ 794 private final List<Block> toDeleteList; 795 796 public BlocksMapUpdateInfo() { 797 toDeleteList = new ChunkedArrayList<Block>(); 798 } 799 800 /** 801 * @return The list of blocks that need to be removed from blocksMap 802 */ 803 public List<Block> getToDeleteList() { 804 return toDeleteList; 805 } 806 807 /** 808 * Add a to-be-deleted block into the 809 * {@link BlocksMapUpdateInfo#toDeleteList} 810 * @param toDelete the to-be-deleted block 811 */ 812 public void addDeleteBlock(Block toDelete) { 813 if (toDelete != null) { 814 toDeleteList.add(toDelete); 815 } 816 } 817 818 /** 819 * Clear {@link BlocksMapUpdateInfo#toDeleteList} 820 */ 821 public void clear() { 822 toDeleteList.clear(); 823 } 824 } 825 826 /** 827 * INode feature such as {@link FileUnderConstructionFeature} 828 * and {@link DirectoryWithQuotaFeature}. 829 */ 830 public interface Feature { 831 } 832}