aboutsummaryrefslogtreecommitdiff
path: root/src/os/fs.c
blob: fdf9d0795e9e191992a5dd7d5343a7a6a8521431 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// fs.c -- filesystem access

#include <uv.h>

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

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)) != 0) {
    vim_strncpy(buf, (char_u *)uv_strerror(errno), len - 1);
    return FAIL;
  }
  return OK;
}

/// Get the absolute name of the given relative directory.
///
/// @param directory Directory name, relative to current directory.
/// @return `FAIL` for failure, `OK` for success.
int os_full_dir_name(char *directory, char *buffer, int len)
{
  int retval = OK;

  if (STRLEN(directory) == 0) {
    return os_dirname((char_u *) buffer, len);
  }

  char old_dir[MAXPATHL];

  // Get current directory name.
  if (os_dirname((char_u *) old_dir, MAXPATHL) == FAIL) {
    return FAIL;
  }

  // We have to get back to the current dir at the end, check if that works.
  if (os_chdir(old_dir) != 0) {
    return FAIL;
  }

  if (os_chdir(directory) != 0) {
    // Do not return immediatly since we may be in the wrong directory.
    retval = FAIL;
  }

  if (retval == FAIL || os_dirname((char_u *) buffer, len) == FAIL) {
    // Do not return immediatly since we are in the wrong directory.
    retval = FAIL;
  }

  if (os_chdir(old_dir) != 0) {
    // That shouldn't happen, since we've tested if it works.
    retval = FAIL;
    EMSG(_(e_prev_dir));
  }

  return retval;
}

// Append to_append to path with a slash in between.
int append_path(char *path, const char *to_append, int max_len)
{
  int current_length = STRLEN(path);
  int to_append_length = STRLEN(to_append);

  // Do not append empty strings.
  if (to_append_length == 0) {
    return OK;
  }

  // Do not append a dot.
  if (STRCMP(to_append, ".") == 0) {
    return OK;
  }

  // Glue both paths with a slash.
  if (current_length > 0 && path[current_length-1] != '/') {
    current_length += 1;  // Count the trailing slash.

    // +1 for the NUL at the end.
    if (current_length + 1 > max_len) {
      return FAIL;
    }

    STRCAT(path, "/");
  }

  // +1 for the NUL at the end.
  if (current_length + to_append_length + 1 > max_len) {
    return FAIL;
  }

  STRCAT(path, to_append);
  return OK;
}

int os_get_absolute_path(char_u *fname, char_u *buf, int len, int force)
{
  char_u *p;
  *buf = NUL;

  char relative_directory[len];
  char *end_of_path = (char *) fname;

  // expand it if forced or not an absolute path
  if (force || !os_is_absolute_path(fname)) {
    if ((p = vim_strrchr(fname, '/')) != NULL) {
      STRNCPY(relative_directory, fname, p-fname);
      relative_directory[p-fname] = NUL;
      end_of_path = (char *) (p + 1);
    } else {
      relative_directory[0] = NUL;
      end_of_path = (char *) fname;
    }

    if (FAIL == os_full_dir_name(relative_directory, (char *) buf, len)) {
      return FAIL;
    }
  }
  return append_path((char *) buf, (char *) end_of_path, len);
}

int os_is_absolute_path(const char_u *fname)
{
  return *fname == '/' || *fname == '~';
}

int 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;
}

static int is_executable(const char_u *name);
static int is_executable_in_path(const char_u *name);

int os_can_exe(const char_u *name)
{
  // If it's an absolute or relative path don't need to use $PATH.
  if (os_is_absolute_path(name) ||
     (name[0] == '.' && (name[1] == '/' ||
                        (name[1] == '.' && name[2] == '/')))) {
    return is_executable(name);
  }

  return is_executable_in_path(name);
}

// Return TRUE if "name" is an executable file, FALSE if not or it doesn't
// exist.
static int is_executable(const char_u *name)
{
  int32_t mode = os_getperm(name);

  if (mode < 0) {
    return FALSE;
  }

  if (S_ISREG(mode) && (S_IEXEC & mode)) {
    return TRUE;
  }

  return FALSE;
}

/// Check if a file is inside the $PATH and is executable.
///
/// @return `TRUE` if `name` is an executable inside $PATH.
static int is_executable_in_path(const char_u *name)
{
  const char *path = getenv("PATH");
  // PATH environment variable does not exist or is empty.
  if (path == NULL || *path == NUL) {
    return FALSE;
  }

  int buf_len = STRLEN(name) + STRLEN(path) + 2;
  char_u *buf = alloc((unsigned)(buf_len));
  if (buf == NULL) {
    return FALSE;
  }

  // Walk through all entries in $PATH to check if "name" exists there and
  // is an executable file.
  for (;; ) {
    const char *e = strchr(path, ':');
    if (e == NULL) {
      e = path + STRLEN(path);
    }

    // Glue together the given directory from $PATH with name and save into
    // buf.
    vim_strncpy(buf, (char_u *) path, e - path);
    append_path((char *) buf, (const char *) name, buf_len);

    if (is_executable(buf)) {
      // Found our executable. Free buf and return.
      vim_free(buf);
      return OK;
    }

    if (*e != ':') {
      // End of $PATH without finding any executable called name.
      vim_free(buf);
      return FALSE;
    }

    path = e + 1;
  }

  // We should never get to this point.
  assert(false);
  return FALSE;
}

int32_t os_getperm(const char_u *name)
{
  uv_fs_t request;
  int result = uv_fs_stat(uv_default_loop(), &request,
                          (const char *)name, NULL);
  uint64_t mode = request.statbuf.st_mode;
  uv_fs_req_cleanup(&request);

  if (result != 0) {
    return -1;
  } else {
    return (int32_t) 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 != 0) {
    return FAIL;
  } else {
    return OK;
  }
}

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

  if (result != 0) {
    return FALSE;
  } else {
    return TRUE;
  }
}

// return TRUE if a file appears to be read-only from the file permissions.
int os_file_is_readonly(const char *name)
{
  if (access(name, W_OK) == 0) {
    return FALSE;
  } else {
    return TRUE;
  }
}

// return 0 for not writable, 1 for writable file, 2 for a dir which we have
// rights to write into.
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;
}