package ftp_client;

// Classe permettant de gerer les permissions a partir d'une chaine de permission du type RWXRWXRWX
public class Permission {
	
	/* Permission string */
	String permission;
	
	/* Right position */
	public static final byte OWNER_READ		= 0;
	public static final byte OWNER_WRITE	= 1;
	public static final byte OWNER_EXEC		= 2;
	public static final byte GROUP_READ		= 3;
	public static final byte GROUP_WRITE	= 4;
	public static final byte GROUP_EXEC		= 5;
	public static final byte OTHER_READ		= 6;
	public static final byte OTHER_WRITE	= 7;
	public static final byte OTHER_EXEC		= 8;
	
	/* Right letters */
	private final static String rwxn = "rwx-";
	
	/* Constructor */
	public Permission(String permission){
		// Si permission est null, alors il n'y a aucun droit
		if(permission == null) permission = "---------";
		// Si la taille n'est pas bonne, rajouter des non-permissions
		while(permission.length() != 9) permission += "-";
		this.permission = permission;
	}
	
	/* Get permission */
	// Recuperer un droit
	public boolean getPerm(byte perm){
		// Si le caractere a la permission voulu egal '-', alors le droit n'est pas accorde
		return (permission.charAt(perm) != '-');
	}
	
	/* Set permission */
	// Definir un droit
	public void setPerm(byte perm, boolean value){
		permission = permission.substring(0, perm) + (value ? rwxn.charAt(perm%3) : '-') + permission.substring(perm+1);
	}
}
