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

ref: b5b9794a1780a2aaf0d87374d0cb631dc0b82452 CTU-FEE-B0B35APO-Semestral-project/file-browser/src/local_file_utils.c -rw-r--r-- 1.5 KiB
b5b9794a — František Boháček fix: dialog fill unused space 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
#include "local_file_utils.h"
#include <stdlib.h>
#include <sys/stat.h>
#include "path.h"
#include <errno.h>
#include <ftw.h>
#include <string.h>
#include <stdio.h>

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) {
  size_t root = strlen(state.payload.local.path);
  size_t dir = strlen(directory->path);
  size_t file_name = strlen(file->name);

  return root + 1 + dir + 1 + file_name + 1;
}

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.payload.local.path, directory->path)];
  if (!path_join((char*)state.payload.local.path, directory->path, base_path)) {
    return false;
  }

  if (!path_join(base_path, file->name, out)) {
    return false;
  }

  return true;
}

int directory_delete(const char *path) {
  int err = ftw(path, nfw_callback, 5);
  if (err != 1) {
    err = FILOPER_UNKNOWN;
  }

  if (err != 0) {
    return err;
  }

  return file_delete(path);
}

int file_delete(const char *path) {
  int err = 0;
  if (remove(path) == -1) {
    err = file_operation_error_from_errno(errno);
  }

  return err;
}

static int nfw_callback(const char *fpath, const struct stat *sb,
                        int typeflag) {
  if (typeflag == FTW_D) {
    return directory_delete(fpath);
  } else {
    return file_delete(fpath);
  }
}
Do not follow this link