Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions Main.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import filesys.IFileSystem;
import filesys.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.io.FileNotFoundException;

import exception.PermissaoException;
import exception.CaminhoJaExistenteException;
import exception.CaminhoNaoEncontradoException;

import filesys.FileSystem;

// MENU INTERATIVO PARA O SISTEMA DE ARQUIVOS
// SINTA-SE LIVRE PARA ALTERAR A CLASSE MAIN

public class Main {

// Constantes úteis para a versão interativa.
// Para esse tipo de execução, o tamanho max do buffer de
// leitura pode ser menor.

private static final String ROOT_USER = "root";
private static final String ROOT_DIR = "/";
private static final int READ_BUFFER_SIZE = 256;
Expand All @@ -31,15 +33,16 @@ public class Main {

// O sistema de arquivos é inteiramente virtual, ou seja, será reiniciado a cada execução do programa.
// Logo, não é necessário salvar os arquivos em disco. O sistema será uma simulação em memória.

public static void main(String[] args) {
// Usuário que está executando o programa.
// Para quaisquer operações que serão feitas por esse usuário em um caminho /path/**,
// deve-se checar se o usuário tem permissão de escrita (r) neste caminho.
if (args.length < 2) {
if (args.length < 1) {
System.out.println("Usuário não fornecido");
return;
}
user = args[1];
user = args[0];

// Carrega a lista de usuários do sistema a partir de arquivo
// Formato do arquivo users:
Expand All @@ -51,6 +54,9 @@ public static void main(String[] args) {
// A partir do momento que um usuário cria outro diretório ou arquivo,
// a permissão desse usuário é de leitura, escrita e execução nesse novo diretório/arquivo,
// e sempre será rwx para o usuário root.

Map<String, Map<String, String>> permissoes = new HashMap<>();

try {
Scanner userScanner = new Scanner(new java.io.File("users/users"));
while (userScanner.hasNextLine()) {
Expand All @@ -61,11 +67,20 @@ public static void main(String[] args) {
String userListed = parts[0];
String dir = parts[1];
String dirPermission = parts[2];

// Ajuste aqui:
if (dir.equals("/**")) {
dir = "/";
}

/* A FAZER:
* Processar a permissão de todos os usuários existentes por diretório.
* Por enquanto esse código somente imprime as permissões contidas no arquivo users.
*/

permissoes.putIfAbsent(dir, new HashMap<>());
permissoes.get(dir).put(userListed, dirPermission);

System.out.println(userListed + " " + dir + " " + dirPermission); // Somente imprime o usuário, diretório e permissão


Expand All @@ -80,19 +95,30 @@ public static void main(String[] args) {

return;
}

// Verifica se o usuário existe em alguma permissão, exceto root
boolean usuarioExiste = user.equals(ROOT_USER) || permissoes.values().stream()
.anyMatch(mapUserPerm -> mapUserPerm.containsKey(user));

if (!usuarioExiste) {
System.out.println("Usuário não fornecido ou inexistente.");
return;
}

// Usuário Importante
// Finalmente cria o Sistema de Arquivos
// Lista de usuários é imutável durante a execução do programa
// Obs: Como passar a lista de usuários para o FileSystem?
fileSystem = new FileSystem(/*usuários?*/);
fileSystem = new FileSystem(permissoes); // passar o mapa de permissoes ao criar o FileSystem ; fileSystem = new FileSystem(/*usuários?*/);

// // DESCOMENTE O BLOCO ABAIXO PARA CRIAR O DIRETÓRIO RAIZ ANTES DE RODAR O MENU
// // Cria o diretório raiz do sistema. Root sempre tem permissão total "rwx"
// try {
// fileSystem.mkdir(ROOT_DIR, ROOT_USER);
// } catch (CaminhoJaExistenteException | PermissaoException e) {
// System.out.println(e.getMessage());
// }
try {
// Cria diretório raiz com permissão total para root
fileSystem.mkdir(ROOT_DIR, ROOT_USER);
} catch (CaminhoJaExistenteException | PermissaoException e) {
System.out.println(e.getMessage());
}

// Menu interativo.
menu();
Expand All @@ -101,6 +127,7 @@ public static void main(String[] args) {
// Menu interativo para fins de teste.
// Os testes junit não são feitos com esse menu,
// mas diretamente na interface IFileSystem

public static void menu() {
while (true) {
System.out.println("\nComandos disponíveis:");
Expand Down Expand Up @@ -246,4 +273,5 @@ public static void cp() throws CaminhoNaoEncontradoException, PermissaoException

fileSystem.cp(caminhoOrigem, caminhoDestino, user, recursivo);
}

}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ clean:

# Run the application with a username
run: compile
$(JAVA) -cp $(BIN_DIR) $(MAIN_CLASS) -u $(USERNAME)
$(JAVA) -cp $(BIN_DIR) $(MAIN_CLASS) $(USERNAME)

# Help target
help:
Expand Down
Binary file added bin/Main.class
Binary file not shown.
Binary file added bin/exception/CaminhoJaExistenteException.class
Binary file not shown.
Binary file added bin/exception/CaminhoNaoEncontradoException.class
Binary file not shown.
Binary file added bin/exception/PermissaoException.class
Binary file not shown.
Binary file added bin/filesys/FileSystem.class
Binary file not shown.
Binary file added bin/filesys/FileSystemImpl.class
Binary file not shown.
Binary file added bin/filesys/IFileSystem.class
Binary file not shown.
2 changes: 2 additions & 0 deletions exception/CaminhoJaExistenteException.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.io.IOException;

public class CaminhoJaExistenteException extends IOException {

public CaminhoJaExistenteException(String message) {
super(message);
}

}
2 changes: 2 additions & 0 deletions exception/CaminhoNaoEncontradoException.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package exception;

public class CaminhoNaoEncontradoException extends Exception {

public CaminhoNaoEncontradoException(String message) {
super(message);
}

}
2 changes: 2 additions & 0 deletions exception/PermissaoException.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package exception;

public class PermissaoException extends Exception {

public PermissaoException(String message) {
super(message);
}

}
19 changes: 18 additions & 1 deletion filesys/FileSystem.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
package filesys;

import java.util.Map;

import exception.CaminhoJaExistenteException;
import exception.CaminhoNaoEncontradoException;
import exception.PermissaoException;

// Essa classe deve servir apenas como proxy para o FileSystemImpl

final public class FileSystem implements IFileSystem {

private final IFileSystem fileSystemImpl;

// Implementação

public FileSystem(Map<String, Map<String, String>> permissoes) {
this.fileSystemImpl = new FileSystemImpl(permissoes);
}

public FileSystem() {
fileSystemImpl = new FileSystemImpl();
this.fileSystemImpl = new FileSystemImpl();
}

// Função de add Usuário
public void addUser(String usuario) {
if (fileSystemImpl instanceof FileSystemImpl fsImpl) {
fsImpl.addUser(usuario);
}
}

@Override
Expand Down Expand Up @@ -64,4 +80,5 @@ public void cp(String caminhoOrigem, String caminhoDestino, String usuario, bool
throws CaminhoNaoEncontradoException, PermissaoException {
fileSystemImpl.cp(caminhoOrigem, caminhoDestino, usuario, recursivo);
}

}
Loading