| 1 | |
package org.jscsi.target.storage; |
| 2 | |
|
| 3 | |
|
| 4 | |
import java.io.File; |
| 5 | |
import java.io.FileNotFoundException; |
| 6 | |
import java.io.FileOutputStream; |
| 7 | |
import java.io.IOException; |
| 8 | |
import java.io.RandomAccessFile; |
| 9 | |
import java.lang.reflect.Constructor; |
| 10 | |
import java.lang.reflect.InvocationTargetException; |
| 11 | |
import java.nio.channels.FileChannel; |
| 12 | |
|
| 13 | |
import org.slf4j.Logger; |
| 14 | |
import org.slf4j.LoggerFactory; |
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
public class RandomAccessStorageModule implements IStorageModule { |
| 27 | |
|
| 28 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(RandomAccessStorageModule.class); |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
private static final String MODE = "rwd"; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
protected final long sizeInBlocks; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
private final RandomAccessFile randomAccessFile; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | 0 | public RandomAccessStorageModule (final long sizeInBlocks, final File file) throws FileNotFoundException { |
| 62 | 0 | this.sizeInBlocks = sizeInBlocks; |
| 63 | 0 | this.randomAccessFile = new RandomAccessFile(file, MODE); |
| 64 | 0 | } |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
@Override |
| 70 | |
public void read (byte[] bytes, long storageIndex) throws IOException { |
| 71 | 0 | randomAccessFile.seek(storageIndex); |
| 72 | 0 | randomAccessFile.read(bytes, 0, bytes.length); |
| 73 | 0 | } |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
@Override |
| 79 | |
public void write (byte[] bytes, long storageIndex) throws IOException { |
| 80 | 0 | randomAccessFile.seek(storageIndex); |
| 81 | 0 | randomAccessFile.write(bytes, 0, bytes.length); |
| 82 | 0 | } |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
@Override |
| 88 | |
public final long getSizeInBlocks () { |
| 89 | 0 | return sizeInBlocks; |
| 90 | |
} |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
@Override |
| 96 | |
public final int checkBounds (final long logicalBlockAddress, final int transferLengthInBlocks) { |
| 97 | 0 | if (logicalBlockAddress < 0 || logicalBlockAddress >= sizeInBlocks) return 1; |
| 98 | 0 | if (transferLengthInBlocks < 0 || logicalBlockAddress + transferLengthInBlocks > sizeInBlocks) return 2; |
| 99 | 0 | return 0; |
| 100 | |
} |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
public final void close () throws IOException { |
| 108 | 0 | randomAccessFile.close(); |
| 109 | 0 | } |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
public static synchronized final IStorageModule open (final File file, final long storageLength, final boolean create, Class<? extends IStorageModule> kind) throws IOException { |
| 122 | |
long sizeInBlocks; |
| 123 | 0 | sizeInBlocks = storageLength / VIRTUAL_BLOCK_SIZE; |
| 124 | 0 | if (create && !(kind.equals(JCloudsStorageModule.class))) { |
| 125 | 0 | createStorageVolume(file, storageLength); |
| 126 | |
} |
| 127 | |
|
| 128 | |
@SuppressWarnings ("unchecked") |
| 129 | 0 | Constructor<? extends IStorageModule> cons = (Constructor<? extends IStorageModule>) kind.getConstructors()[0]; |
| 130 | |
try { |
| 131 | 0 | IStorageModule mod = cons.newInstance(sizeInBlocks, file); |
| 132 | 0 | return mod; |
| 133 | 0 | } catch (InvocationTargetException | IllegalAccessException | InstantiationException exc) { |
| 134 | 0 | throw new IOException(exc); |
| 135 | |
} |
| 136 | |
} |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
private static synchronized boolean createStorageVolume (final File pToCreate, final long pLength) throws IOException { |
| 147 | 0 | FileOutputStream outStream = null; |
| 148 | |
try { |
| 149 | |
|
| 150 | 0 | if (pToCreate.exists()) { |
| 151 | 0 | if (!pToCreate.delete()) { |
| 152 | 0 | LOGGER.debug("Removal of old storage " + pToCreate.toString() + " unsucessful."); |
| 153 | 0 | return false; |
| 154 | |
} |
| 155 | 0 | LOGGER.debug("Removal of old storage " + pToCreate.toString() + " sucessful."); |
| 156 | |
} |
| 157 | |
|
| 158 | |
|
| 159 | 0 | final File parent = pToCreate.getCanonicalFile().getParentFile(); |
| 160 | 0 | if (!parent.exists() && !parent.mkdirs()) { throw new FileNotFoundException("Unable to create directory: " + parent.getAbsolutePath()); } |
| 161 | |
|
| 162 | 0 | pToCreate.createNewFile(); |
| 163 | 0 | outStream = new FileOutputStream(pToCreate); |
| 164 | 0 | final FileChannel fcout = outStream.getChannel(); |
| 165 | 0 | fcout.position(pLength); |
| 166 | 0 | outStream.write(26); |
| 167 | 0 | fcout.force(true); |
| 168 | 0 | LOGGER.debug("Creation of storage " + pToCreate.toString() + " sucessful."); |
| 169 | 0 | return true; |
| 170 | 0 | } catch (IOException e) { |
| 171 | 0 | LOGGER.error("Exception creating storage volume " + pToCreate.getAbsolutePath() + ": " + e.getMessage(), e); |
| 172 | 0 | throw e; |
| 173 | |
} finally { |
| 174 | 0 | if (outStream != null) { |
| 175 | |
try { |
| 176 | 0 | outStream.close(); |
| 177 | 0 | } catch (IOException e) { |
| 178 | 0 | LOGGER.error("Exception closing storage volume: " + e.getMessage(), e); |
| 179 | 0 | } |
| 180 | |
} |
| 181 | |
} |
| 182 | |
|
| 183 | |
} |
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
public static boolean recursiveDelete (final File pFile) { |
| 192 | 0 | if (pFile.isDirectory()) { |
| 193 | 0 | for (final File child : pFile.listFiles()) { |
| 194 | 0 | if (!recursiveDelete(child)) { return false; } |
| 195 | |
} |
| 196 | |
} |
| 197 | 0 | return pFile.delete(); |
| 198 | |
} |
| 199 | |
|
| 200 | |
} |