~ruther/CTU-FEE-B0B35APO-Semestral-project

a9aa5ba543570543a2fb60ecd0bf7faf622341ca — František Boháček 4 years ago 009988e
refactor: make fileaccess state from union
M file-browser/include/file_access.h => file-browser/include/file_access.h +18 -1
@@ 71,8 71,25 @@ typedef struct {
typedef struct fileaccess_t fileaccess_t;

typedef struct {
  char *path;
} local_fileaccess_state_t;

typedef struct {
  char *path; // ORDER MATTERS!
  char *mount_device;
  bool mounted;
  file_operation_error_t error;
} extern_fileaccess_state_t;

typedef local_fileaccess_state_t temp_fileaccess_state_t;

typedef struct {
  const fileaccess_t *fileaccess;
  void *state;
  union {
    local_fileaccess_state_t local;
    local_fileaccess_state_t temp;
    extern_fileaccess_state_t exter;
  } payload;
} fileaccess_state_t;

typedef fileaccess_state_t (*init_state_fn)(void *data);

M file-browser/src/local_file_access.c => file-browser/src/local_file_access.c +10 -10
@@ 14,7 14,7 @@
#define ROOT "/"

fileaccess_state_t local_fileaccess_init_state(void *data) {
  fileaccess_state_t state = {.state = ROOT, .fileaccess = &local_file_access};
  fileaccess_state_t state = {.payload.local.path = ROOT, .fileaccess = &local_file_access};
  return state;
}



@@ 67,8 67,8 @@ static file_operation_error_t file_get_information(void **malloced,
directory_or_error_t local_fileaccess_directory_list(fileaccess_state_t state,
                                                     char *path) {
  directory_or_error_t ret;
  char full_path[path_join_memory_size(state.state, path)];
  path_join((char *)state.state, path, full_path);
  char full_path[path_join_memory_size(state.payload.local.path, path)];
  path_join((char *)state.payload.local.path, path, full_path);

  uint64_t malloc_offset = sizeof(directory_t) + strlen(path) + 1;
  uint64_t bytes_malloced = sizeof(directory_t) + strlen(path) + 1;


@@ 138,14 138,14 @@ directory_or_error_t local_fileaccess_directory_list(fileaccess_state_t state,
}

directory_or_error_t local_fileaccess_root_list(fileaccess_state_t state) {
  return local_fileaccess_directory_list(state, (char *)state.state);
  return local_fileaccess_directory_list(state, (char *)state.payload.local.path);
}

directory_or_error_t local_fileaccess_directory_create(fileaccess_state_t state,
                                                       char *path) {
  directory_or_error_t ret;
  char full_path[path_join_memory_size(state.state, path)];
  path_join((char *)state.state, path, full_path);
  char full_path[path_join_memory_size(state.payload.local.path, path)];
  path_join((char *)state.payload.local.path, path, full_path);

  int status = mkdir(full_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);



@@ 162,8 162,8 @@ directory_or_error_t local_fileaccess_directory_create(fileaccess_state_t state,
file_operation_error_t
local_fileaccess_directory_delete(fileaccess_state_t state, char *path) {
  file_operation_error_t error = FILOPER_SUCCESS;
  char full_path[path_join_memory_size(state.state, path)];
  path_join((char *)state.state, path, full_path);
  char full_path[path_join_memory_size(state.payload.local.path, path)];
  path_join((char *)state.payload.local.path, path, full_path);

  int status = directory_delete(full_path);
  if (status != 0) {


@@ 228,8 228,8 @@ executing_file_or_error_t local_fileaccess_file_execute(fileaccess_state_t state
file_operation_error_t local_fileaccess_file_delete(fileaccess_state_t state,
                                                    char *path) {
  file_operation_error_t error = FILOPER_SUCCESS;
  char full_path[path_join_memory_size(state.state, path)];
  path_join((char *)state.state, path, full_path);
  char full_path[path_join_memory_size(state.payload.local.path, path)];
  path_join((char *)state.payload.local.path, path, full_path);

  int status = remove(path);


M file-browser/src/local_file_connectors.c => file-browser/src/local_file_connectors.c +6 -6
@@ 14,8 14,8 @@ file_operation_error_t local_connector_file_copy(
                                               file)];
  file_get_full_path(first_state, file->directory, file, src_path);

  char dst_path[path_join_memory_size(second_state.state, destination)];
  path_join(second_state.state, destination, dst_path);
  char dst_path[path_join_memory_size(second_state.payload.local.path, destination)];
  path_join(second_state.payload.local.path, destination, dst_path);

  // TODO: copy folders



@@ 38,7 38,7 @@ file_operation_error_t local_connector_file_copy(
    fwrite(buffer, sizeof(char), in, dst_file);
  }

  file_operation_error_t error;
  file_operation_error_t error = FILOPER_SUCCESS;
  if (!feof(src_file) && ferror(src_file)) {
    error = file_operation_error_from_errno(ferror(src_file));
  } else if (ferror(dst_file)) {


@@ 47,7 47,7 @@ file_operation_error_t local_connector_file_copy(

  fclose(src_file);
  fclose(dst_file);
  return FILOPER_SUCCESS;
  return error;
}

file_operation_error_t local_connector_file_move(


@@ 56,8 56,8 @@ file_operation_error_t local_connector_file_move(
  file_operation_error_t error = FILOPER_SUCCESS;
  char src_path[file_get_full_path_memory_size(first_state, file->directory,
                                               file)];
  char dst_path[path_join_memory_size(second_state.state, destination)];
  path_join(second_state.state, destination, dst_path);
  char dst_path[path_join_memory_size(second_state.payload.local.path, destination)];
  path_join(second_state.payload.local.path, destination, dst_path);

  int status = rename(src_path, dst_path);
  if (status == 0) {

M file-browser/src/local_file_utils.c => file-browser/src/local_file_utils.c +3 -3
@@ 10,13 10,13 @@
static int nfw_callback(const char *fpath, const struct stat *sb, int typeflag);

size_t file_get_full_path_memory_size(fileaccess_state_t state, directory_t *directory, file_t *file) {
  return strlen(file->name) + strlen(state.state) + strlen(file->name) + 3;
  return strlen(file->name) + strlen(state.payload.local.path) + strlen(file->name) + 3;
}

bool file_get_full_path(fileaccess_state_t state,
                        directory_t *directory, file_t *file, char *out) {
  char base_path[path_join_memory_size(state.state, directory->path)];
  if (!path_join((char*)state.state, directory->path, base_path)) {
  char base_path[path_join_memory_size(state.payload.local.path, directory->path)];
  if (!path_join((char*)state.payload.local.path, directory->path, base_path)) {
    return false;
  }