示例#1
0
  public synchronized SVDBFileSystemDataInput readFile(String path, int id) throws IOException {
    SVDBFileSystemDataInput ret = new SVDBFileSystemDataInput();
    byte tmp[] = new byte[BLK_SIZE];
    FileInfo info = null;

    // Read root block
    readBlock(path, id, tmp);
    ret.addPage(tmp);

    int length = ret.readInt(); // Total length of the file
    int nblocks = ret.readInt(); // Number of non-root blocks

    if (fTrackFiles) {
      info = findFileInfo(id);

      if (info == null) {
        System.out.println("readFile: failed to find id=" + id + " " + path);
        try {
          throw new Exception();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }

      if (info != null && nblocks != info.fBlockIdList.size()) {
        System.out.println(
            "readFile: block count wrong for "
                + id
                + " "
                + path
                + " nblocks="
                + nblocks
                + " actual="
                + info.fBlockIdList.size());
      }
    }

    // Read in the remaining root and storage blocks
    for (int i = 0; i < nblocks; i++) {
      int block_id = ret.readInt();

      if (fTrackFiles && info != null) {
        if (block_id != info.fBlockIdList.get(i)) {
          System.out.println(
              "readFile: block id "
                  + i
                  + " wrong for "
                  + id
                  + " "
                  + path
                  + " block_id="
                  + block_id
                  + " actual="
                  + info.fBlockIdList.get(i));
        }
      }
      tmp = new byte[BLK_SIZE];
      readBlock(path, block_id, tmp);
      ret.addPage(tmp);
    }
    int start_idx = ret.getOffset();
    ret.setStartIdx(start_idx);

    // Compute the initial page and page offset
    ret.finalize(length);

    return ret;
  }
示例#2
0
  public synchronized void deleteFile(String path, int id) throws IOException {
    FileInfo info = null;
    SVDBFileSystemDataInput ret = new SVDBFileSystemDataInput();
    byte tmp[] = new byte[BLK_SIZE];

    if (id < 1) {
      throw new IOException("Cannot delete root block");
    }

    if (fTrackFiles) {
      info = findFileInfo(id);

      if (info == null) {
        System.out.println("deleteFile: failed to find " + id + " " + path);
      }

      removeFileInfo(info);
    }

    // Read root block
    readBlock(path, id, tmp);
    ret.addPage(tmp);

    /* int length  = */ ret.readInt(); // Total length of the file
    int nblocks = ret.readInt(); // Number of non-root blocks
    if (nblocks < 0) {}

    int blocks_to_read = computeRootBlockCount(nblocks);

    if (fTrackFiles && info != null) {
      if (nblocks != info.fBlockIdList.size()) {
        System.out.println(
            "deleteFile: mismatch in blocks for "
                + id
                + " "
                + path
                + " nblocks="
                + nblocks
                + " actual="
                + info.fBlockIdList.size());
      }
    }

    // Read in the remaining block ids and free them
    for (int i = 0; i < nblocks; i++) {
      int block_id = ret.readInt();

      if (fTrackFiles && info != null) {
        if (block_id != info.fBlockIdList.get(i)) {
          System.out.println(
              "deleteFile: block "
                  + i
                  + " mismatch "
                  + "block_id="
                  + block_id
                  + " actual="
                  + info.fBlockIdList.get(i));
        }
      }

      if (i < blocks_to_read) {
        tmp = new byte[BLK_SIZE];
        readBlock(path, block_id, tmp);
        ret.addPage(tmp);
      }
      freeBlock(block_id);
    }

    freeBlock(id);
  }
示例#3
0
  private boolean open_filesystem(File f) throws IOException {
    boolean ret = true;
    RandomAccessFile rw = null;

    rw = new RandomAccessFile(f, "rw");

    fFileRWList.add(rw);

    // Read the root block
    byte tmp[] = new byte[BLK_SIZE];
    readBlock("rootFile", 0, tmp);

    SVDBFileSystemDataInput in = new SVDBFileSystemDataInput();
    in.addPage(tmp);

    /**
     * Root block is: - Magic number - Filesystem version-string length - Filesystem version
     * (string) - Number of storage files in this filesystem - Block length of the last file -
     * Handle to the alloc list - Alloc list length - Handle to the file-info list - File-info list
     * length - user data length - user data
     */
    int magic_number = in.readInt();

    if (magic_number != VALID_MAGIC_NUMBER) {
      ret = false;
    } else {
      String version = in.readString();

      if (!fVersion.equals(version)) {
        // Version doesn't match, so re-initialize the filesystem
        ret = false;
        cleanup();
        initialize();
      } else {
        int n_files = in.readInt();
        fLastRwBlkLen = in.readInt();
        fAllocListFile = in.readInt();
        int alloc_list_len = in.readInt();

        // Read back the file list;
        fFileInfoHndl = in.readInt();
        int file_list_size = in.readInt();

        if (fTrackFiles) {
          fTrackFiles = false;
          SVDBFileSystemDataInput file_info_in = readFile("fileInfo", fFileInfoHndl);
          fTrackFiles = true;

          fFileList.clear();
          for (int i = 0; i < file_list_size; i++) {
            FileInfo info = new FileInfo();
            info.fFileId = file_info_in.readInt();
            int n_blocks = file_info_in.readInt();
            for (int j = 0; j < n_blocks; j++) {
              info.fBlockIdList.add(file_info_in.readInt());
            }
            fFileList.add(info);
          }
        }

        int ud_len = in.readInt();

        if (ud_len == -1) {
          fUserData = null;
        } else {
          fUserData = new byte[ud_len];
          in.readFully(fUserData);
        }

        for (int i = 2; i <= n_files; i++) {
          f = new File(fDBDir, i + ".db");
          rw = new RandomAccessFile(f, "rw");
          fFileRWList.add(rw);
        }

        // Now we can read in the alloc list and initialize the AllocList
        boolean track_files = fTrackFiles;
        fTrackFiles = false;
        SVDBFileSystemDataInput alloc_in = readFile("allocList", fAllocListFile);
        fTrackFiles = track_files;
        fAllocList = new byte[alloc_list_len];
        alloc_in.readFully(fAllocList);

        if (fTrackFiles) {
          for (int i = 0; i < fAllocList.length; i++) {
            if ((i % 16) == 0) {
              if (i != 0) {
                System.out.println();
              }
              System.out.print("" + i + ": ");
            }
            System.out.print(Integer.toHexString(fAllocList[i]) + " ");
          }
          System.out.println();

          System.out.println("--> Validate Initial Load alloc_list_len=" + alloc_list_len);
          removeFileInfo(null);
          System.out.println("<-- Validate Initial Load");
        }
      }
    }

    if (ret) {
      // Mark the filesystem as open and invalid. This enables us to
      // detect filesystem invalidity due to improper close
      write32(0, tmp, ~VALID_MAGIC_NUMBER);
      writeBlock("rootBlock", 0, tmp);
    }

    return ret;
  }