1 /*
2  * Sleuth Kit Data Model
3  *
4  * Copyright 2011-2017 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.datamodel;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.sleuthkit.datamodel.TskData.TSK_VS_TYPE_ENUM;
24 
25 /**
26  * Represents a volume system. Populated based on data in database.
27  */
28 public class VolumeSystem extends AbstractContent {
29 
30 	private volatile long volumeSystemHandle = 0;
31 	private long type, imgOffset, blockSize;
32 
33 	/**
34 	 * Constructor most inputs are from the database
35 	 *
36 	 * @param db        case database handle
37 	 * @param obj_id    the unique content object id for the volume system
38 	 * @param name      name of the volume system
39 	 * @param type      type of the volume system
40 	 * @param imgOffset offset of the volume system with respect to image
41 	 * @param blockSize block size of this volume system
42 	 */
VolumeSystem(SleuthkitCase db, long obj_id, String name, long type, long imgOffset, long blockSize)43 	protected VolumeSystem(SleuthkitCase db, long obj_id, String name, long type, long imgOffset, long blockSize) {
44 		super(db, obj_id, name);
45 		this.type = type;
46 		this.imgOffset = imgOffset;
47 		this.blockSize = blockSize;
48 	}
49 
50 	@Override
read(byte[] readBuffer, long offset, long len)51 	public int read(byte[] readBuffer, long offset, long len) throws TskCoreException {
52 		synchronized (this) {
53 			if (volumeSystemHandle == 0) {
54 				getVolumeSystemHandle();
55 			}
56 		}
57 		return SleuthkitJNI.readVs(volumeSystemHandle, readBuffer, offset, len);
58 	}
59 
60 	@Override
getSize()61 	public long getSize() {
62 		return 0;
63 	}
64 
65 	/**
66 	 * get the type
67 	 *
68 	 * @return type
69 	 */
getType()70 	public TSK_VS_TYPE_ENUM getType() {
71 		return TskData.TSK_VS_TYPE_ENUM.valueOf(type);
72 	}
73 
74 	/**
75 	 * get the byte offset
76 	 *
77 	 * @return byte offset
78 	 */
getOffset()79 	public long getOffset() {
80 		return imgOffset;
81 	}
82 
83 	/**
84 	 * get the block size
85 	 *
86 	 * @return block size
87 	 */
getBlockSize()88 	public long getBlockSize() {
89 		return blockSize;
90 	}
91 
92 	/**
93 	 * get the volume system Handle pointer Open a new handle if needed,
94 	 * otherwise resuse the existing handle.
95 	 *
96 	 * @return volume system Handle pointer
97 	 *
98 	 * @throws TskException
99 	 */
getVolumeSystemHandle()100 	protected synchronized long getVolumeSystemHandle() throws TskCoreException {
101 		if (volumeSystemHandle == 0) {
102 			Content dataSource = getDataSource();
103 			if ((dataSource != null) && (dataSource instanceof Image)) {
104 				Image image = (Image) dataSource;
105 				volumeSystemHandle = SleuthkitJNI.openVs(image.getImageHandle(), imgOffset);
106 			} else {
107 				throw new TskCoreException("Volume System data source is not an image");
108 			}
109 		}
110 
111 		return volumeSystemHandle;
112 	}
113 
114 	@Override
close()115 	public void close() {
116 		if (volumeSystemHandle != 0) {
117 			synchronized (this) {
118 				if (volumeSystemHandle != 0) {
119 					// SleuthkitJNI.closeVs(volumeSystemHandle); // closeVs is currently a no-op
120 					volumeSystemHandle = 0;
121 				}
122 			}
123 		}
124 	}
125 
126 	@Override
finalize()127 	public void finalize() throws Throwable {
128 		try {
129 			close();
130 		} finally {
131 			super.finalize();
132 		}
133 	}
134 
135 	@Override
accept(SleuthkitItemVisitor<T> v)136 	public <T> T accept(SleuthkitItemVisitor<T> v) {
137 		return v.visit(this);
138 	}
139 
140 	@Override
accept(ContentVisitor<T> v)141 	public <T> T accept(ContentVisitor<T> v) {
142 		return v.visit(this);
143 	}
144 
145 	@Override
getChildren()146 	public List<Content> getChildren() throws TskCoreException {
147 		return getSleuthkitCase().getVolumeSystemChildren(this);
148 	}
149 
150 	@Override
getChildrenIds()151 	public List<Long> getChildrenIds() throws TskCoreException {
152 		return getSleuthkitCase().getVolumeSystemChildrenIds(this);
153 	}
154 
155 	/**
156 	 * @return a list of Volumes that are direct children of this VolumeSystem
157 	 *
158 	 * @throws TskCoreException
159 	 */
getVolumes()160 	public List<Volume> getVolumes() throws TskCoreException {
161 		List<Volume> volumes = new ArrayList<Volume>();
162 		for (Content child : getChildren()) {
163 			if (child instanceof Volume) {
164 				volumes.add((Volume) child);
165 			}
166 		}
167 		return volumes;
168 	}
169 
170 	@Override
toString(boolean preserveState)171 	public String toString(boolean preserveState) {
172 		return super.toString(preserveState) + "VolumeSystem [\t" + "blockSize " + blockSize + "\t" + "imgOffset " + imgOffset + "\t" + "type " + type + "]\t"; //NON-NLS
173 	}
174 }
175