void createDirectory(String path, Directory currentDirectory, User currentUser)
      throws InvalidFileNameException, FileAlreadyExistsException, InvalidMaskException,
          FileUnknownException {

    Directory beforeLast = absolutePath(path, currentUser, currentDirectory);
    beforeLast.addFile(
        new Directory(
            this.generateUniqueId(),
            getLastPathToken(path),
            currentUser.getUmask(),
            currentUser,
            beforeLast));
  }
  void createPlainFile(String path, Directory currentDirectory, User currentUser)
      throws InvalidFileNameException, InvalidMaskException, FileAlreadyExistsException {

    Directory directory = absolutePath(path, currentUser, currentDirectory);
    String filename = getLastPathToken(path);
    directory.checkAccessWrite(currentUser);

    for (File f : directory.getFilesSet()) {
      if (f.getFilename().equals(filename)) throw new FileAlreadyExistsException(filename);
    }
    directory.addFile(
        new PlainFile(
            this.generateUniqueId(), filename, currentUser.getUmask(), currentUser, directory));
  }
  public FileSystem(MyDriveManager mdm) {
    super();
    setMyDriveManager(mdm);

    super.setIdSeed(0);

    Root root = new Root();
    this.addUsers(root);

    try {
      super.setFsRoot(
          new Directory(this.generateUniqueId(), PATH_DELIM, root.getUmask(), root, this));

    } catch (InvalidFileNameException | InvalidMaskException e) {
      /* This exception should not occur it only exists to protect the method against
       * bad programming
       */
      log.trace(e.getMessage());
      e.printStackTrace();
    }

    Directory home = new Directory(generateUniqueId(), HOME_DIR, root.getUmask(), root, getSlash());

    addToSlash(home);

    Directory rootHomeDirectory =
        new Directory(generateUniqueId(), root.getUsername(), root.getUmask(), root, home);
    home.addFile(rootHomeDirectory);
    root.setHomeDirectory((Directory) home.getFileByName(ROOT_USER));

    Guest guest = new Guest();
    Directory guestHomeDirectory =
        new Directory(generateUniqueId(), guest.getUsername(), guest.getUmask(), guest, home);
    home.addFile(guestHomeDirectory);
    guest.setHomeDirectory((Directory) home.getFileByName(GUEST_USER));
    addUsers(guest);
  }
  private Directory createDir(Directory current, String name, User user)
      throws ImportDocumentException {
    Directory next = null;

    try {
      File file = current.getFileByName(name);
      file.isCdAble();
      next = (Directory) current.getFileByName(name);
    } catch (FileUnknownException e) {
      /* This exception should not occur it only exists to protect the method against
       * bad programming
       */
      log.trace(e.getMessage());
      e.printStackTrace();
    } catch (IsNotDirectoryException e) {
      throw new ImportDocumentException();
    } finally {
      if (next == null) {
        next = new Directory(this.generateUniqueId(), name, user.getUmask(), user, current);
        current.addFile(next);
      }
    }
    return next;
  }
Esempio n. 5
0
  public static void main(String... args) {
    try {
      ListPrintSort<File> list = new ListPrintSort<>();
      Directory directory =
          new Directory(
              new Audio("record001", "mp3", 1000),
              new Text("doc001", "txt", 10),
              new Image("image001", "gif", 253));

      System.out.println("---------------Directory---------------");
      list.printList(directory.getFiles());

      System.out.println("\n---------------Directory---------------");
      directory.addFile(new Audio("zzzzz", "mp3", 1500), new Text("hello", "txt", 10));
      list.printList(directory.getFiles());
      list.sortList(directory.getFiles());

      System.out.println("\n---------------Directory---------------");
      list.printList(directory.getFiles());

    } catch (NegativeSizeException e) {
      System.err.println("The size should not be less than zero, and has be: " + e.getSize());
    }
  }