Esempio n. 1
0
  public static void createFloppy(File f) throws Exception {

    GrubFatFormatter ff = new GrubFatFormatter(0, null, null);
    FileDevice newFd = new FileDevice(f, "rw");
    newFd.setLength(1440 * 1024);
    ff.format(newFd);

    // newFd.start();
    final FileSystemService fSS = InitialNaming.lookup(FileSystemService.NAME);
    FatFileSystemType type = fSS.getFileSystemType(FatFileSystemType.ID);
    FatFileSystem fs = new FatFileSystem(newFd, false, type);

    FSDirectory dir = fs.getRootEntry().getDirectory();
    FSDirectory bDir = dir.addDirectory("boot").getDirectory();
    FSDirectory bgDir = bDir.addDirectory("grub").getDirectory();

    URLConnection urlConn = FatTest.class.getClassLoader().getResource("menu.lst").openConnection();
    // byte[] buf = new byte[urlConn.getContentLength()];
    ByteBuffer buf = ByteBuffer.allocate(urlConn.getContentLength());
    FileUtils.copy(urlConn.getInputStream(), buf.array());

    final FSFile fh1 = dir.addFile("test.lst").getFile();
    fh1.setLength(urlConn.getContentLength());
    fh1.write(0, buf);

    final FSFile fh2 = bgDir.addFile("menu.lst").getFile();
    fh2.setLength(urlConn.getContentLength());
    fh2.write(0, buf);

    fs.flush();

    // newFd.stop();
    newFd.close();
  }
Esempio n. 2
0
  public synchronized void read(long fileOffset, ByteBuffer destBuf) throws IOException {
    int len = destBuf.remaining();

    final long max = (isDir) ? getLengthOnDisk() : getLength();
    if (fileOffset + len > max) {
      throw new IOException("Cannot read beyond the EOF");
    }

    final FatFileSystem fs = getFatFileSystem();
    final long[] chain = fs.getFat().getChain(startCluster);
    final BlockDeviceAPI api = fs.getApi();

    int chainIdx = (int) (fileOffset / clusterSize);
    if (fileOffset % clusterSize != 0) {
      int clusOfs = (int) (fileOffset % clusterSize);
      int size = Math.min(len, (int) (clusterSize - (fileOffset % clusterSize) - 1));
      destBuf.limit(destBuf.position() + size);
      api.read(getDevOffset(chain[chainIdx], clusOfs), destBuf);
      fileOffset += size;
      len -= size;
      chainIdx++;
    }
    while (len > 0) {
      int size = Math.min(clusterSize, len);
      destBuf.limit(destBuf.position() + size);
      api.read(getDevOffset(chain[chainIdx], 0), destBuf);
      len -= size;
      chainIdx++;
    }
  }
Esempio n. 3
0
 /**
  * Gets the size this file occupies on disk
  *
  * @return long
  */
 public long getLengthOnDisk() {
   if (this.length == 0) {
     return 0;
   } else {
     final FatFileSystem fs = getFatFileSystem();
     final long[] chain = fs.getFat().getChain(getStartCluster());
     return ((long) chain.length) * fs.getClusterSize();
   }
 }
Esempio n. 4
0
 public FatFile(
     FatFileSystem fs, FatDirEntry myEntry, long startCluster, long length, boolean isDir) {
   super(fs);
   this.myEntry = myEntry;
   this.startCluster = startCluster;
   this.length = length;
   this.clusterSize = fs.getClusterSize();
   this.isDir = isDir;
 }
Esempio n. 5
0
  /**
   * Sets the length.
   *
   * @param length The length to set
   */
  public synchronized void setLength(long length) throws IOException {

    if (getFileSystem().isReadOnly()) {
      throw new ReadOnlyFileSystemException("setLength in readonly filesystem");
    }

    if (this.length == length) {
      // Do nothing
      return;
    }

    final FatFileSystem fs = getFatFileSystem();
    final Fat fat = fs.getFat();
    final int nrClusters = (int) ((length + clusterSize - 1) / clusterSize);

    if (this.length == 0) {
      final long[] chain = fat.allocNew(nrClusters);
      this.startCluster = chain[0];
      this.myEntry.setStartCluster((int) startCluster);
    } else {
      final long[] chain = fs.getFat().getChain(startCluster);

      if (nrClusters != chain.length) {
        if (nrClusters > chain.length) {
          // Grow
          int count = nrClusters - chain.length;
          while (count > 0) {
            fat.allocAppend(getStartCluster());
            count--;
          }
        } else {
          // Shrink
          fat.setEof(chain[nrClusters - 1]);
          for (int i = nrClusters; i < chain.length; i++) {
            fat.setFree(chain[i]);
          }
        }
      }
    }

    this.length = length;
    this.myEntry.updateLength(length);
  }
Esempio n. 6
0
  public synchronized void write(long fileOffset, ByteBuffer srcBuf) throws IOException {
    int len = srcBuf.remaining();

    if (getFileSystem().isReadOnly()) {
      throw new ReadOnlyFileSystemException("write in readonly filesystem");
    }

    final long max = (isDir) ? getLengthOnDisk() : getLength();
    if (fileOffset > max) {
      throw new IOException("Cannot write beyond the EOF");
    }

    if (fileOffset + len > max) { // this is too short increase the size
      // of the file
      setLength(fileOffset + len);
    }

    final FatFileSystem fs = getFatFileSystem();
    final long[] chain = fs.getFat().getChain(getStartCluster());
    final BlockDeviceAPI api = fs.getApi();

    int chainIdx = (int) (fileOffset / clusterSize);
    if (fileOffset % clusterSize != 0) {
      int clusOfs = (int) (fileOffset % clusterSize);
      int size = Math.min(len, (int) (clusterSize - (fileOffset % clusterSize) - 1));
      srcBuf.limit(srcBuf.position() + size);
      api.write(getDevOffset(chain[chainIdx], clusOfs), srcBuf);
      fileOffset += size;
      len -= size;
      chainIdx++;
    }
    while (len > 0) {
      int size = Math.min(clusterSize, len);
      srcBuf.limit(srcBuf.position() + size);
      api.write(getDevOffset(chain[chainIdx], 0), srcBuf);
      len -= size;
      chainIdx++;
    }
  }
Esempio n. 7
0
  public static void printInfo(File file, PrintWriter out) throws IOException, FileSystemException {
    FileDevice fd = new FileDevice(file, "r");
    try {
      final FileSystemService fSS = InitialNaming.lookup(FileSystemService.NAME);
      FatFileSystemType type = fSS.getFileSystemType(FatFileSystemType.ID);
      FatFileSystem fs = new FatFileSystem(fd, false, type);
      try {
        BootSector bs = fs.getBootSector();
        bs.read(fd);

        out.println("OEM name          " + bs.getOemName());
        out.println("bytes/sector      " + bs.getBytesPerSector());
        out.println("sectors/cluster   " + bs.getSectorsPerCluster());
        out.println("#reserved sectors " + bs.getNrReservedSectors());
        out.println("#fats             " + bs.getNrFats());
        out.println("#rootdir entries  " + bs.getNrRootDirEntries());
        out.println("#logical sectors  " + bs.getNrLogicalSectors());
        out.println("Medium descriptor 0x" + Integer.toHexString(bs.getMediumDescriptor()));
        out.println("sectors/fat       " + bs.getSectorsPerFat());
        out.println("sectors/track     " + bs.getSectorsPerTrack());
        out.println("#heads            " + bs.getNrHeads());
        out.println("#hidden sectors   " + bs.getNrHiddenSectors());

        fs.getFat().printTo(out);
        fs.getRootDir().printTo(out);

        try {
          FatDirectory dir =
              (FatDirectory) fs.getRootEntry().getDirectory().getEntry("AAP").getDirectory();
          dir.printTo(out);
        } catch (FileNotFoundException ex) {
          out.println("No AAP directory");
        }

        try {
          FatDirectory dir =
              (FatDirectory) fs.getRootEntry().getDirectory().getEntry("boot").getDirectory();
          dir.printTo(out);
        } catch (FileNotFoundException ex) {
          out.println("No boot directory");
        }

      } finally {
        // fd.stop();
        fd.close();
      }
    } catch (NameNotFoundException e) {
      throw new FileSystemException(e);
    }
  }
Esempio n. 8
0
 /**
  * Calculates the device offset (0-based) for the given cluster and offset within the cluster.
  *
  * @param cluster
  * @param clusterOffset
  * @return long
  */
 protected long getDevOffset(long cluster, int clusterOffset) {
   final FatFileSystem fs = getFatFileSystem();
   final long filesOffset = FatUtils.getFilesOffset(fs.getBootSector());
   return filesOffset + clusterOffset + ((cluster - FatUtils.FIRST_CLUSTER) * clusterSize);
 }