📄 treeblockdevice.java
字号:
empty[i] = (byte) 0x00; start = new byte[512*95]; for(int i=1; i<95; i++) System.arraycopy(empty, 0, start, i*512, 512); System.arraycopy(MBR, 0, start, 0, 512); System.arraycopy(header, 0, start, 63*512, 512); System.arraycopy(header2, 0, start, 64*512, 512); System.arraycopy(header, 0, start, 69*512, 512); System.arraycopy(header2, 0, start, 70*512, 512); } public void dumpChanges(DataOutput output) throws IOException { //write out writeMap } public void loadChanges(DataInput input) throws IOException { //load up writeMap } public String getImageFileName() { return source.getName(); } //print the byte values of an array in hex public void printArray(byte[] input, int start, int length) { String output = ""; for (int j=start, count=1; j<start+length; j++, count++) { output = output + Long.toHexString(input[j]+0L) + " "; if (count%16==0) output = output + " | "; if (count%64==0) output=output+ "\n"; } System.out.println(output); } //****************************************************************************// public int getFAT(int cluster) { if (cluster > totalClusters) return 0; //retrieve a mapping for a cluster from the FAT int answer = (FAT[cluster*4] & 0xFF) + ((FAT[cluster*4 +1] & 0xFF) << 8) + ((FAT[cluster*4 +2] & 0xFF) << 16)+((FAT[cluster*4 +3] & 0xFF) << 24); return answer; } public void close() { /* try { BufferedWriter file = new BufferedWriter(...("driveimage.img","rw"); writeImage(file); } catch (FileNotFoundException e){} */ //fit writes into directory structure and write out to either an image file or original files try { } catch (Exception e) {} } //READ up to 16 sectors at a time public int read(long sectorNumber, byte[] buffer, int size) { //check map of writes to see if sector has been written to if (bufferWrites) { if (writeMap.containsKey(new Long(sectorNumber))) { System.arraycopy((byte[]) writeMap.get(new Long(sectorNumber)),0,buffer,0,512); return 0; } } if (sectorNumber < 95) // Initial sectors System.arraycopy(start, (int) sectorNumber*512, buffer, 0, size*512); else if (sectorNumber < 95+2*sectorsPerFAT & sectorNumber>94) // FAT sectors System.arraycopy(FAT, (int) ((sectorNumber-95) % sectorsPerFAT)*512, buffer, 0, size*512); //check datamap to get data sector else if (dataMap.containsKey(new Long(sectorNumber))) { try { ((MyFATEntry) dataMap.get(new Long(sectorNumber))).getSector(sectorNumber, buffer); } catch (IOException e) { System.out.println("IO Exception in reading FAT filesystem"); e.printStackTrace(); return -1; } } else if (sectorNumber > totalSectors) return -1; else System.arraycopy(empty, 0, buffer, 0, 512); return 0; } //***************************************************************************// //write sectors public int write(long sectorNumber, byte[] buffer, int size) { if (sectorNumber > totalSectors) return -1; if (bufferWrites) { byte[] write = new byte[512]; System.arraycopy(buffer, 0, write, 0, 512); writeMap.put(new Long(sectorNumber),write); return 0; /* if (sectorNumber <95) { System.arraycopy(buffer, 0, start, (int) sectorNumber*512, size*512); return 0; }else if ((sectorNumber < 95+2*sectorsPerFAT) & (sectorNumber>94)) { System.arraycopy(buffer, 0,FAT, (int) ((sectorNumber-95) % sectorsPerFAT), size*512); return 0; } */ } else //continually commit writes { //check to see if the sector is in the data section if (sectorNumber > 95+2*sectorsPerFAT - 1) { long offset = (long) (sectorNumber - 95 - 2*sectorsPerFAT) % sectorsPerCluster; long cluster = (long) (sectorNumber - 95 - 2*sectorsPerFAT)/sectorsPerCluster+2; //see if cluster is allocated in FAT if (!dataMap.containsKey(new Long(sectorNumber-offset))) { //cluster is not allocated byte[] temp = new byte[512]; System.arraycopy(buffer, 0, temp, 0, 512); writeMap.put(new Long(sectorNumber), temp); if (!bufferedClusters.contains(new Long(cluster))) bufferedClusters.add(new Long(cluster)); return 0; } else { //cluster is allocated //check if it is allocated to a file or a directory MyFATEntry myFile = (MyFATEntry) dataMap.get(new Long(sectorNumber-offset)); File file = myFile.getFile(); int nextCluster = (int) myFile.getStartCluster(); int clusterCounter = 0; int sum = 0; while(nextCluster != cluster) { //get next cluster from FAT nextCluster = getFAT(nextCluster); clusterCounter++; } if (file.isDirectory()) //start of tricky part... { MyDir myDir = (MyDir) myFile; byte[] oldDirSector = new byte[512]; //it is a directory, compare with old directory entry and act accordingly read(sectorNumber, oldDirSector, 1); //add buffer to directory's set of direntries myDir.changeDirEntry(buffer, clusterCounter*sectorsPerCluster + offset, sectorNumber); //update dataMap dataMap.put(new Long(sectorNumber), myDir); for (int i = 0; i<16; i++) { int newStartCluster = (buffer[32*i+26] & 0xFF) + ((buffer[32*i+27] & 0xFF) << 8) + ((buffer[32*i+20] & 0xFF) << 16) + ((buffer[32*i+21] & 0xFF) << 24); if ( ((buffer[32*i] & 0xFF) == 0xE5) && (getFAT(newStartCluster)==0)) { //file has been deleted so we need to as well nextCluster = newStartCluster; if ( dataMap.get(new Long((newStartCluster-2)*sectorsPerCluster + 95 + 2*sectorsPerFAT)) != null) { ((MyFATEntry) dataMap.get(new Long((newStartCluster-2)*sectorsPerCluster + 95 + 2*sectorsPerFAT))).getFile().delete(); //remove all entries for file in dataMap for (int n = 0; n < clusterCounter+1; n++) { for (int s =0; s < 8; s++) { dataMap.remove(new Long(95 + 2*sectorsPerFAT + (nextCluster-2)*sectorsPerCluster + s)); } //get next cluster from FAT nextCluster = getFAT(nextCluster); } } continue; } if ((buffer[32*i+11] & 0xFF) == 0xF) continue; //skip LFN's for now if ( (buffer[32*i] & 0xFF) == 0xE5) continue; boolean changed = false; for (int j = 0; j< 32; j++) { if ((oldDirSector[j + 32*i] & 0xFF )!= (buffer[j + 32*i] & 0xFF)) changed = true; } if (!changed) continue; String name = ""; String ext = ""; for(int m =0; m<8;m++) {name += (char) buffer[32*i + m];} for(int m =0; m<3;m++) {ext += (char) buffer[32*i + 8 + m];} name = name.trim(); ext = ext.trim(); if ((name.equals(".")) || (name.equals(".."))) //skip current and parent directory entries continue; long newStartSector = (newStartCluster-2)*sectorsPerCluster + 95 + 2*sectorsPerFAT; boolean isDirectory = (buffer[32*i + 11] & 0x10) == 0x10; //add in other attributes here like readonly, hidden etc. if (dataMap.get(new Long(newStartSector)) == null) { //new dir entry was created and we need to create a new File File next; if (isDirectory) { next = new File(file.getPath(), name + ext); next.mkdir(); //make into a MyDir and add to data map MyDir myNewDir = new MyDir(next.getPath(), next.getName(), newStartCluster); //initialise it's dirEntry to a cluster of 0's byte[] zero = new byte[512*sectorsPerCluster]; for (int c = 0; c< zero.length; c++) {zero[c]=0;} myNewDir.changeDirEntry(zero,0,newStartSector); //need to think about whether this is necessary for (int d = 0; d <8; d++) dataMap.put(new Long(newStartSector +d), myNewDir); } else { long fileSize = (buffer[32*i+ 28] & 0xFF) + ((buffer[32*i+ 29] & 0xFF) << 8) + ((buffer[32*i+ 30] & 0xFF) << 16) + ((buffer[32*i+ 31] & 0xFF) << 24); next = new File(file.getPath(), name + "." + ext); try { next.createNewFile(); //make into a MyFile and add to data map MyFile myNewFile = new MyFile(file.getPath(), name + "." + ext, newStartCluster); myNewFile.setSizeClusters(-1); myNewFile.setfileSize(fileSize); dataMap.put(new Long(newStartSector), myNewFile); } catch (Exception e) { System.out.println("IO Exception - Can't create new file"); e.printStackTrace(); } } //check if the new object created corresponds to data which was written to an unallocated cluster for (int k = 0; k < 8; k++) { MyFATEntry newone = (MyFATEntry) dataMap.get(new Long(newStartSector)); if (writeMap.get(new Long(newStartSector +k)) != null) { if (!next.isDirectory()) { try { RandomAccessFile out = new RandomAccessFile(next, "rw"); out.seek(k*bytesPerSector); int len = 512; MyFile castFile = (MyFile) newone; if ((clusterCounter*sectorsPerCluster + offset + 1)*bytesPerSector > castFile.getfileSize()) { len = (int) (512 +castFile.getfileSize() - (clusterCounter*sectorsPerCluster + offset + 1)*bytesPerSector); } out.write((byte[]) writeMap.get(new Long(newStartSector+k)), 0, len); out.close(); //update cluster list updateClusterList(newone, newStartSector+k); if (!dataMap.containsKey(new Long(newStartSector + k))) dataMap.put(new Long(newStartSector + k), newone); writeMap.remove(new Long(newStartSector+k)); } catch (IOException e) { System.out.println("IO Exception in writing to file"); e.printStackTrace(); } } else { MyDir newDir = (MyDir) newone; newDir.changeDirEntry((byte[]) writeMap.get(new Long(newStartSector+k)), (long) k, newStartSector); writeMap.remove(new Long(newStartSector+k)); } bufferedClusters.remove(new Long(newStartCluster)); } } continue; } else { //it has changed the properties of a file which is already allocated and we need to update, possibly rename it MyFATEntry changedFile = (MyFATEntry) dataMap.get(new Long(newStartSector)); File oldfile = changedFile.getFile(); String path = oldfile.getPath(); path = path.substring(0,path.length() - oldfile.getName().length()); if (isDirectory) { oldfile.renameTo(new File(path, name + ext)); changedFile.setFile(new File(path, name + ext)); //dataMap.put(new Long(newStartSector), changedFile); //need to recurse though directories contents and change their paths changePathOfTree(changedFile, changedFile.getFile().getPath()); } else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -