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

ref: 584549e4cfab9c1764e22e7b11ca94c8d36459f7 CTU-FEE-B0B35APO-Semestral-project/file-browser/src/extern_file_access.c -rw-r--r-- 4.4 KiB
584549e4 — František Boháček feat: add filoperation error get text 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "extern_file_access.h"
#include "device_mount.h"
#include "local_file_access.h"
#include <stdlib.h>
#include <string.h>

static bool extern_fileaccess_check_mount(fileaccess_state_t state) {
  return true;
}

fileaccess_state_t extern_fileaccess_init_state(void *data) {
  size_t device_length = strlen(data);

  extern_fileaccess_state_t externstate = {.path = "/mnt/file_browser_device",
                                           .mount_device = NULL,
                                           .mounted = false,
                                           .error = FILOPER_SUCCESS};

  char *malloced = malloc(sizeof(char) * (device_length + 1));
  if (malloced == NULL) {
    externstate.error = FILOPER_UNKNOWN;
  } else {
    strcpy(malloced, data);
    externstate.mount_device = malloced;
  }

  externstate.error = device_mount(externstate.mount_device, externstate.path);

  fileaccess_state_t state = {
      .fileaccess = &extern_file_access,
      .payload.exter = externstate,
  };

  return state;
}

bool extern_fileaccess_deinit_state(fileaccess_state_t state) {
  // unmount
  state.payload.exter.error =
      device_umount(state.payload.exter.mount_device, state.payload.exter.path);
  free(state.payload.exter.mount_device);

  return true;
}

directory_or_error_t extern_fileaccess_directory_list(fileaccess_state_t state,
                                                      char *path) {
  if (!extern_fileaccess_check_mount(state)) {
    directory_or_error_t error = {.error = true,
                                  .payload.error = FILOPER_UNKNOWN};

    return error;
  }

  return local_fileaccess_directory_list(state, path);
}

directory_or_error_t extern_fileaccess_root_list(fileaccess_state_t state) {
  if (!extern_fileaccess_check_mount(state)) {
    directory_or_error_t error = {.error = true,
                                  .payload.error = FILOPER_UNKNOWN};

    return error;
  }

  return local_fileaccess_root_list(state);
}

directory_or_error_t
extern_fileaccess_directory_create(fileaccess_state_t state, char *path) {
  if (!extern_fileaccess_check_mount(state)) {
    directory_or_error_t error = {.error = true,
                                  .payload.error = FILOPER_UNKNOWN};

    return error;
  }

  return local_fileaccess_directory_create(state, path);
}

file_operation_error_t
extern_fileaccess_directory_delete(fileaccess_state_t state, char *path) {
  if (!extern_fileaccess_check_mount(state)) {
    return FILOPER_UNKNOWN;
  }

  return local_fileaccess_directory_delete(state, path);
}

file_operation_error_t
extern_fileaccess_directory_close(fileaccess_state_t state,
                                  directory_t *directory) {
  if (!extern_fileaccess_check_mount(state)) {
    return FILOPER_UNKNOWN;
  }

  return local_fileaccess_directory_close(state, directory);
}

file_operation_error_t
extern_fileaccess_file_get_mime_type(fileaccess_state_t state, file_t *file,
                                     char *mime) {
  if (!extern_fileaccess_check_mount(state)) {
    return FILOPER_UNKNOWN;
  }

  return local_fileaccess_file_get_mime_type(state, file, mime);
}

executing_file_or_error_t
extern_fileaccess_file_execute(fileaccess_state_t state, file_t *file,
                               char *args) {
  if (!extern_fileaccess_check_mount(state)) {
    executing_file_or_error_t error = {.error = true,
                                       .payload.error = FILOPER_UNKNOWN};

    return error;
  }

  return local_fileaccess_file_execute(state, file, args);
}

file_operation_error_t extern_fileaccess_file_delete(fileaccess_state_t state,
                                                     char *path) {
  if (!extern_fileaccess_check_mount(state)) {
    return FILOPER_UNKNOWN;
  }

  return local_fileaccess_file_delete(state, path);
}

const fileaccess_t extern_file_access = {
    .type = FA_EXTERN,
    .init = extern_fileaccess_init_state,
    .deinit = extern_fileaccess_deinit_state,

    .delete_file = extern_fileaccess_file_delete,
    .execute_file = extern_fileaccess_file_execute,
    .get_mime_type = extern_fileaccess_file_get_mime_type,
    .get_file_local_path = local_fileaccess_file_get_local_path,

    .list_directory = extern_fileaccess_directory_list,
    .close_directory = extern_fileaccess_directory_close,
    .create_directory = extern_fileaccess_directory_create,
    .delete_directory = extern_fileaccess_directory_delete,

    .list_root = extern_fileaccess_root_list,

};
Do not follow this link