예제 #1
0
파일: FatFile.java 프로젝트: NanoGame/jnode
  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++;
    }
  }
예제 #2
0
파일: FatFile.java 프로젝트: NanoGame/jnode
 /**
  * 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();
   }
 }
예제 #3
0
파일: FatFile.java 프로젝트: NanoGame/jnode
  /**
   * 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);
  }
예제 #4
0
파일: FatTest.java 프로젝트: NanoGame/jnode
  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);
    }
  }
예제 #5
0
파일: FatFile.java 프로젝트: NanoGame/jnode
  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++;
    }
  }