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

ref: cb2e9cb8adf0656a8e68f252bdb1c7e25848b6f9 CTU-FEE-B0B35APO-Semestral-project/file-browser/src/file_open.c -rw-r--r-- 1.8 KiB
cb2e9cb8 — František Boháček fix: list container scroll 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
#include "file_open.h"
#include "file_access.h"
#include <linux/limits.h>
#include <sys/stat.h>
#include <unistd.h>
#include "nonblocking_io.h"
#include "serialize_lock.h"

static void file_prepare_before_open() {
  serialize_unlock();
}

static void file_prepare_after_open() {
  struct termios old;
  file_set_nonblocking(STDIN_FILENO, &old);

  if (serialize_lock(1) <= 0) {
      serialize_lock(0);
  }
}

file_operation_error_t file_open(file_t *file, exec_options_t *options, fileaccess_state_t state) {
  executing_file_t executing;
  if (file->permissions & S_IXUSR) {
    // executable
    file_prepare_before_open();
    executing_file_or_error_t executing_or_error = fileaccess_file_execute(state, file, "");
    if (executing_or_error.error) {
      file_prepare_after_open();
      return executing_or_error.payload.error;
    }
    
    executing = executing_or_error.payload.file;
  } else if (options != NULL) {
    char mime[256];
    fileaccess_file_get_mimetype(state, file, mime);

    char *program = exec_options_get_program(options, mime);

    if (program == NULL) {
      program = exec_options_get_program(options, "text");
    }

    if (program == NULL) {
      return FILOPER_SUCCESS;
    }

    char local_path[PATH_MAX];
    file_operation_error_t error = fileaccess_file_get_local_path(state, file, local_path);

    if (error != FILOPER_SUCCESS) {
      return error;
    }

    file_prepare_before_open();
    executing_file_error_t executing_or_error = executing_file_execute(program, local_path);

    if (executing_or_error.error != FILOPER_SUCCESS) {
      file_prepare_after_open();
      return executing_or_error.error;
    }

    executing = executing_or_error.file;
  } else {
    return FILOPER_UNKNOWN;
  }

  executing_file_wait(&executing);
  file_prepare_after_open();
  // TODO: figure out return data?
  return FILOPER_SUCCESS;
}
Do not follow this link