aboutsummaryrefslogtreecommitdiff
path: root/src/os/fs.c
blob: 2834b1e83618b99bcce3f1eba6d0fd0b068c5f00 (plain) (blame)
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
// fs.c -- filesystem access

#include "os/os.h"
#include "memory.h"
#include "message.h"
#include "misc1.h"
#include "misc2.h"
#include "path.h"

// Many fs functions from libuv return that value on success.
static const int kLibuvSuccess = 0;

int os_chdir(const char *path) {
  if (p_verbose >= 5) {
    verbose_enter();
    smsg((char_u *)"chdir(%s)", path);
    verbose_leave();
  }
  return uv_chdir(path);
}

int os_dirname(char_u *buf, size_t len)
{
  assert(buf && len);

  int errno;
  if ((errno = uv_cwd((char *)buf, &len)) != kLibuvSuccess) {
    vim_strncpy(buf, (char_u *)uv_strerror(errno), len - 1);
    return FAIL;
  }
  return OK;
}

bool os_isdir(const char_u *name)
{
  int32_t mode = os_getperm(name);
  if (mode < 0) {
    return false;
  }

  if (!S_ISDIR(mode)) {
    return false;
  }

  return true;
}

int os_stat(const char_u *name, uv_stat_t *statbuf)
{
  uv_fs_t request;
  int result = uv_fs_stat(uv_default_loop(), &request,
                          (const char *)name, NULL);
  *statbuf = request.statbuf;
  uv_fs_req_cleanup(&request);

  if (result == kLibuvSuccess) {
    return OK;
  }

  return FAIL;
}

int32_t os_getperm(const char_u *name)
{
  uv_stat_t statbuf;
  if (os_stat(name, &statbuf) == FAIL) {
    return -1;
  } else {
    return (int32_t)statbuf.st_mode;
  }
}

int os_setperm(const char_u *name, int perm)
{
  uv_fs_t request;
  int result = uv_fs_chmod(uv_default_loop(), &request,
                           (const char*)name, perm, NULL);
  uv_fs_req_cleanup(&request);

  if (result == kLibuvSuccess) {
    return OK;
  }

  return FAIL;
}

bool os_file_exists(const char_u *name)
{
  uv_stat_t statbuf;
  if (os_stat(name, &statbuf) == OK) {
    return true;
  }

  return false;
}

bool os_file_is_readonly(const char *name)
{
  return access(name, W_OK) != 0;
}

int os_file_is_writable(const char *name)
{
  if (access(name, W_OK) == 0) {
    if (os_isdir((char_u *)name)) {
      return 2;
    }
    return 1;
  }
  return 0;
}

int os_rename(const char_u *path, const char_u *new_path)
{
  uv_fs_t request;
  int result = uv_fs_rename(uv_default_loop(), &request,
                            (const char *)path, (const char *)new_path, NULL);
  uv_fs_req_cleanup(&request);

  if (result == kLibuvSuccess) {
    return OK;
  }

  return FAIL;
}