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+ * <p>
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ * <p>
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 .hdfs .protocol ;
19+
20+ import java .io .PrintStream ;
21+ import java .text .SimpleDateFormat ;
22+ import java .util .Date ;
23+ import java .util .EnumSet ;
24+
25+ import org .apache .hadoop .fs .Path ;
26+ import org .apache .hadoop .fs .permission .FsPermission ;
27+ import org .apache .hadoop .hdfs .DFSUtilClient ;
28+
29+ /**
30+ * Metadata about a snapshottable directory
31+ */
32+ public class SnapshotStatus {
33+ /**
34+ * Basic information of the snapshot directory
35+ */
36+ private final HdfsFileStatus dirStatus ;
37+
38+ /**
39+ * Snapshot ID for the snapshot
40+ */
41+ private final int snapshotID ;
42+
43+ /**
44+ * Full path of the parent.
45+ */
46+ private final byte [] parentFullPath ;
47+
48+ public SnapshotStatus (long modification_time , long access_time ,
49+ FsPermission permission ,
50+ EnumSet <HdfsFileStatus .Flags > flags ,
51+ String owner , String group , byte [] localName ,
52+ long inodeId , int childrenNum , int snapshotID ,
53+ byte [] parentFullPath ) {
54+ this .dirStatus = new HdfsFileStatus .Builder ()
55+ .isdir (true )
56+ .mtime (modification_time )
57+ .atime (access_time )
58+ .perm (permission )
59+ .flags (flags )
60+ .owner (owner )
61+ .group (group )
62+ .path (localName )
63+ .fileId (inodeId )
64+ .children (childrenNum )
65+ .build ();
66+ this .snapshotID = snapshotID ;
67+ this .parentFullPath = parentFullPath ;
68+ }
69+
70+ public SnapshotStatus (HdfsFileStatus dirStatus ,
71+ int snapshotNumber , byte [] parentFullPath ) {
72+ this .dirStatus = dirStatus ;
73+ this .snapshotID = snapshotNumber ;
74+ this .parentFullPath = parentFullPath ;
75+ }
76+
77+ /**
78+ * @return snapshot id for the snapshot
79+ */
80+ public int getSnapshotID () {
81+ return snapshotID ;
82+ }
83+
84+ /**
85+ * @return The basic information of the directory
86+ */
87+ public HdfsFileStatus getDirStatus () {
88+ return dirStatus ;
89+ }
90+
91+ /**
92+ * @return Full path of the file
93+ */
94+ public byte [] getParentFullPath () {
95+ return parentFullPath ;
96+ }
97+
98+ /**
99+ * @return Full path of the snapshot
100+ */
101+ public Path getFullPath () {
102+ String parentFullPathStr =
103+ (parentFullPath == null || parentFullPath .length == 0 ) ?
104+ "/" : DFSUtilClient .bytes2String (parentFullPath );
105+ return new Path (getSnapshotPath (parentFullPathStr ,
106+ dirStatus .getLocalName ()));
107+ }
108+
109+ /**
110+ * Print a list of {@link SnapshotStatus} out to a given stream.
111+ *
112+ * @param stats The list of {@link SnapshotStatus}
113+ * @param out The given stream for printing.
114+ */
115+ public static void print (SnapshotStatus [] stats ,
116+ PrintStream out ) {
117+ if (stats == null || stats .length == 0 ) {
118+ out .println ();
119+ return ;
120+ }
121+ int maxRepl = 0 , maxLen = 0 , maxOwner = 0 , maxGroup = 0 ;
122+ int maxSnapshotID = 0 ;
123+ for (SnapshotStatus status : stats ) {
124+ maxRepl = maxLength (maxRepl , status .dirStatus .getReplication ());
125+ maxLen = maxLength (maxLen , status .dirStatus .getLen ());
126+ maxOwner = maxLength (maxOwner , status .dirStatus .getOwner ());
127+ maxGroup = maxLength (maxGroup , status .dirStatus .getGroup ());
128+ maxSnapshotID = maxLength (maxSnapshotID , status .snapshotID );
129+ }
130+
131+ String lineFormat = "%s%s " // permission string
132+ + "%" + maxRepl + "s "
133+ + (maxOwner > 0 ? "%-" + maxOwner + "s " : "%s" )
134+ + (maxGroup > 0 ? "%-" + maxGroup + "s " : "%s" )
135+ + "%" + maxLen + "s "
136+ + "%s " // mod time
137+ + "%" + maxSnapshotID + "s "
138+ + "%s" ; // path
139+ SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm" );
140+
141+ for (SnapshotStatus status : stats ) {
142+ String line = String .format (lineFormat , "d" ,
143+ status .dirStatus .getPermission (),
144+ status .dirStatus .getReplication (),
145+ status .dirStatus .getOwner (),
146+ status .dirStatus .getGroup (),
147+ String .valueOf (status .dirStatus .getLen ()),
148+ dateFormat .format (new Date (status .dirStatus .getModificationTime ())),
149+ status .snapshotID ,
150+ getSnapshotPath (DFSUtilClient .bytes2String (status .parentFullPath ),
151+ status .dirStatus .getLocalName ())
152+ );
153+ out .println (line );
154+ }
155+ }
156+
157+ private static int maxLength (int n , Object value ) {
158+ return Math .max (n , String .valueOf (value ).length ());
159+ }
160+
161+ public static class Bean {
162+ private final String path ;
163+ private final int snapshotID ;
164+ private final long modificationTime ;
165+ private final short permission ;
166+ private final String owner ;
167+ private final String group ;
168+
169+ public Bean (String path , int snapshotID , long
170+ modificationTime , short permission , String owner , String group ) {
171+ this .path = path ;
172+ this .snapshotID = snapshotID ;
173+ this .modificationTime = modificationTime ;
174+ this .permission = permission ;
175+ this .owner = owner ;
176+ this .group = group ;
177+ }
178+
179+ public String getPath () {
180+ return path ;
181+ }
182+
183+ public int getSnapshotID () {
184+ return snapshotID ;
185+ }
186+
187+ public long getModificationTime () {
188+ return modificationTime ;
189+ }
190+
191+ public short getPermission () {
192+ return permission ;
193+ }
194+
195+ public String getOwner () {
196+ return owner ;
197+ }
198+
199+ public String getGroup () {
200+ return group ;
201+ }
202+ }
203+
204+ static String getSnapshotPath (String snapshottableDir ,
205+ String snapshotRelativePath ) {
206+ String parentFullPathStr =
207+ snapshottableDir == null || snapshottableDir .isEmpty () ?
208+ "/" : snapshottableDir ;
209+ final StringBuilder b = new StringBuilder (parentFullPathStr );
210+ if (b .charAt (b .length () - 1 ) != Path .SEPARATOR_CHAR ) {
211+ b .append (Path .SEPARATOR );
212+ }
213+ return b .append (HdfsConstants .DOT_SNAPSHOT_DIR )
214+ .append (Path .SEPARATOR )
215+ .append (snapshotRelativePath )
216+ .toString ();
217+ }
218+ }
0 commit comments