1 | // This is an open source non-commercial project. Dear PVS-Studio, please check |
2 | // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
3 | |
4 | // fs.c -- filesystem access |
5 | #include <stdbool.h> |
6 | #include <stddef.h> |
7 | #include <assert.h> |
8 | #include <limits.h> |
9 | #include <fcntl.h> |
10 | #include <errno.h> |
11 | |
12 | #include "auto/config.h" |
13 | |
14 | #ifdef HAVE_SYS_UIO_H |
15 | # include <sys/uio.h> |
16 | #endif |
17 | |
18 | #include <uv.h> |
19 | |
20 | #include "nvim/os/os.h" |
21 | #include "nvim/os/os_defs.h" |
22 | #include "nvim/ascii.h" |
23 | #include "nvim/memory.h" |
24 | #include "nvim/message.h" |
25 | #include "nvim/assert.h" |
26 | #include "nvim/misc1.h" |
27 | #include "nvim/option.h" |
28 | #include "nvim/path.h" |
29 | #include "nvim/strings.h" |
30 | |
31 | #ifdef WIN32 |
32 | #include "nvim/mbyte.h" // for utf8_to_utf16, utf16_to_utf8 |
33 | #endif |
34 | |
35 | #ifdef INCLUDE_GENERATED_DECLARATIONS |
36 | # include "os/fs.c.generated.h" |
37 | #endif |
38 | |
39 | #define RUN_UV_FS_FUNC(ret, func, ...) \ |
40 | do { \ |
41 | bool did_try_to_free = false; \ |
42 | uv_call_start: {} \ |
43 | uv_fs_t req; \ |
44 | ret = func(&fs_loop, &req, __VA_ARGS__); \ |
45 | uv_fs_req_cleanup(&req); \ |
46 | if (ret == UV_ENOMEM && !did_try_to_free) { \ |
47 | try_to_free_memory(); \ |
48 | did_try_to_free = true; \ |
49 | goto uv_call_start; \ |
50 | } \ |
51 | } while (0) |
52 | |
53 | // Many fs functions from libuv return that value on success. |
54 | static const int kLibuvSuccess = 0; |
55 | static uv_loop_t fs_loop; |
56 | |
57 | |
58 | // Initialize the fs module |
59 | void fs_init(void) |
60 | { |
61 | uv_loop_init(&fs_loop); |
62 | } |
63 | |
64 | |
65 | /// Changes the current directory to `path`. |
66 | /// |
67 | /// @return 0 on success, or negative error code. |
68 | int os_chdir(const char *path) |
69 | FUNC_ATTR_NONNULL_ALL |
70 | { |
71 | if (p_verbose >= 5) { |
72 | verbose_enter(); |
73 | smsg("chdir(%s)" , path); |
74 | verbose_leave(); |
75 | } |
76 | return uv_chdir(path); |
77 | } |
78 | |
79 | /// Get the name of current directory. |
80 | /// |
81 | /// @param buf Buffer to store the directory name. |
82 | /// @param len Length of `buf`. |
83 | /// @return `OK` for success, `FAIL` for failure. |
84 | int os_dirname(char_u *buf, size_t len) |
85 | FUNC_ATTR_NONNULL_ALL |
86 | { |
87 | int error_number; |
88 | if ((error_number = uv_cwd((char *)buf, &len)) != kLibuvSuccess) { |
89 | STRLCPY(buf, uv_strerror(error_number), len); |
90 | return FAIL; |
91 | } |
92 | return OK; |
93 | } |
94 | |
95 | /// Check if the given path is a directory and not a symlink to a directory. |
96 | /// @return `true` if `name` is a directory and NOT a symlink to a directory. |
97 | /// `false` if `name` is not a directory or if an error occurred. |
98 | bool os_isrealdir(const char *name) |
99 | FUNC_ATTR_NONNULL_ALL |
100 | { |
101 | uv_fs_t request; |
102 | if (uv_fs_lstat(&fs_loop, &request, name, NULL) != kLibuvSuccess) { |
103 | return false; |
104 | } |
105 | if (S_ISLNK(request.statbuf.st_mode)) { |
106 | return false; |
107 | } else { |
108 | return S_ISDIR(request.statbuf.st_mode); |
109 | } |
110 | } |
111 | |
112 | /// Check if the given path is a directory or not. |
113 | /// |
114 | /// @return `true` if `name` is a directory. |
115 | bool os_isdir(const char_u *name) |
116 | FUNC_ATTR_NONNULL_ALL |
117 | { |
118 | int32_t mode = os_getperm((const char *)name); |
119 | if (mode < 0) { |
120 | return false; |
121 | } |
122 | |
123 | if (!S_ISDIR(mode)) { |
124 | return false; |
125 | } |
126 | |
127 | return true; |
128 | } |
129 | |
130 | /// Check if the given path is a directory and is executable. |
131 | /// Gives the same results as `os_isdir()` on Windows. |
132 | /// |
133 | /// @return `true` if `name` is a directory and executable. |
134 | bool os_isdir_executable(const char *name) |
135 | FUNC_ATTR_NONNULL_ALL |
136 | { |
137 | int32_t mode = os_getperm((const char *)name); |
138 | if (mode < 0) { |
139 | return false; |
140 | } |
141 | |
142 | #ifdef WIN32 |
143 | return (S_ISDIR(mode)); |
144 | #else |
145 | return (S_ISDIR(mode) && (S_IXUSR & mode)); |
146 | #endif |
147 | } |
148 | |
149 | /// Check what `name` is: |
150 | /// @return NODE_NORMAL: file or directory (or doesn't exist) |
151 | /// NODE_WRITABLE: writable device, socket, fifo, etc. |
152 | /// NODE_OTHER: non-writable things |
153 | int os_nodetype(const char *name) |
154 | FUNC_ATTR_NONNULL_ALL |
155 | { |
156 | #ifndef WIN32 // Unix |
157 | uv_stat_t statbuf; |
158 | if (0 != os_stat(name, &statbuf)) { |
159 | return NODE_NORMAL; // File doesn't exist. |
160 | } |
161 | // uv_handle_type does not distinguish BLK and DIR. |
162 | // Related: https://github.com/joyent/libuv/pull/1421 |
163 | if (S_ISREG(statbuf.st_mode) || S_ISDIR(statbuf.st_mode)) { |
164 | return NODE_NORMAL; |
165 | } |
166 | if (S_ISBLK(statbuf.st_mode)) { // block device isn't writable |
167 | return NODE_OTHER; |
168 | } |
169 | // Everything else is writable? |
170 | // buf_write() expects NODE_WRITABLE for char device /dev/stderr. |
171 | return NODE_WRITABLE; |
172 | #else // Windows |
173 | // Edge case from Vim os_win32.c: |
174 | // We can't open a file with a name "\\.\con" or "\\.\prn", trying to read |
175 | // from it later will cause Vim to hang. Thus return NODE_WRITABLE here. |
176 | if (STRNCMP(name, "\\\\.\\" , 4) == 0) { |
177 | return NODE_WRITABLE; |
178 | } |
179 | |
180 | // Vim os_win32.c:mch_nodetype does (since 7.4.015): |
181 | // wn = enc_to_utf16(name, NULL); |
182 | // hFile = CreatFile(wn, ...) |
183 | // to get a HANDLE. Whereas libuv just calls _get_osfhandle() on the fd we |
184 | // give it. But uv_fs_open later calls fs__capture_path which does a similar |
185 | // utf8-to-utf16 dance and saves us the hassle. |
186 | |
187 | // macOS: os_open(/dev/stderr) would return UV_EACCES. |
188 | int fd = os_open(name, O_RDONLY |
189 | # ifdef O_NONBLOCK |
190 | | O_NONBLOCK |
191 | # endif |
192 | , 0); |
193 | if (fd < 0) { // open() failed. |
194 | return NODE_NORMAL; |
195 | } |
196 | int guess = uv_guess_handle(fd); |
197 | if (close(fd) == -1) { |
198 | ELOG("close(%d) failed. name='%s'" , fd, name); |
199 | } |
200 | |
201 | switch (guess) { |
202 | case UV_TTY: // FILE_TYPE_CHAR |
203 | return NODE_WRITABLE; |
204 | case UV_FILE: // FILE_TYPE_DISK |
205 | return NODE_NORMAL; |
206 | case UV_NAMED_PIPE: // not handled explicitly in Vim os_win32.c |
207 | case UV_UDP: // unix only |
208 | case UV_TCP: // unix only |
209 | case UV_UNKNOWN_HANDLE: |
210 | default: |
211 | return NODE_OTHER; // Vim os_win32.c default |
212 | } |
213 | #endif |
214 | } |
215 | |
216 | /// Gets the absolute path of the currently running executable. |
217 | /// May fail if procfs is missing. #6734 |
218 | /// @see path_exepath |
219 | /// |
220 | /// @param[out] buffer Full path to the executable. |
221 | /// @param[in] size Size of `buffer`. |
222 | /// |
223 | /// @return 0 on success, or libuv error code. |
224 | int os_exepath(char *buffer, size_t *size) |
225 | FUNC_ATTR_NONNULL_ALL |
226 | { |
227 | return uv_exepath(buffer, size); |
228 | } |
229 | |
230 | /// Checks if the file `name` is executable. |
231 | /// |
232 | /// @param[in] name Filename to check. |
233 | /// @param[out,allocated] abspath Returns resolved exe path, if not NULL. |
234 | /// @param[in] use_path Also search $PATH. |
235 | /// |
236 | /// @return true if `name` is executable and |
237 | /// - can be found in $PATH, |
238 | /// - is relative to current dir or |
239 | /// - is absolute. |
240 | /// |
241 | /// @return `false` otherwise. |
242 | bool os_can_exe(const char *name, char **abspath, bool use_path) |
243 | FUNC_ATTR_NONNULL_ARG(1) |
244 | { |
245 | if (!use_path || gettail_dir(name) != name) { |
246 | #ifdef WIN32 |
247 | if (is_executable_ext(name, abspath)) { |
248 | #else |
249 | // Must have path separator, cannot execute files in the current directory. |
250 | if ((use_path || gettail_dir(name) != name) |
251 | && is_executable(name, abspath)) { |
252 | #endif |
253 | return true; |
254 | } else { |
255 | return false; |
256 | } |
257 | } |
258 | |
259 | return is_executable_in_path(name, abspath); |
260 | } |
261 | |
262 | /// Returns true if `name` is an executable file. |
263 | /// |
264 | /// @param[in] name Filename to check. |
265 | /// @param[out,allocated] abspath Returns full exe path, if not NULL. |
266 | static bool is_executable(const char *name, char **abspath) |
267 | FUNC_ATTR_NONNULL_ARG(1) |
268 | { |
269 | int32_t mode = os_getperm(name); |
270 | |
271 | if (mode < 0) { |
272 | return false; |
273 | } |
274 | |
275 | #ifdef WIN32 |
276 | // Windows does not have exec bit; just check if the file exists and is not |
277 | // a directory. |
278 | const bool ok = S_ISREG(mode); |
279 | #else |
280 | int r = -1; |
281 | if (S_ISREG(mode)) { |
282 | RUN_UV_FS_FUNC(r, uv_fs_access, name, X_OK, NULL); |
283 | } |
284 | const bool ok = (r == 0); |
285 | #endif |
286 | if (ok && abspath != NULL) { |
287 | *abspath = save_abs_path(name); |
288 | } |
289 | return ok; |
290 | } |
291 | |
292 | #ifdef WIN32 |
293 | /// Checks if file `name` is executable under any of these conditions: |
294 | /// - extension is in $PATHEXT and `name` is executable |
295 | /// - result of any $PATHEXT extension appended to `name` is executable |
296 | static bool is_executable_ext(const char *name, char **abspath) |
297 | FUNC_ATTR_NONNULL_ARG(1) |
298 | { |
299 | const bool is_unix_shell = strstr((char *)path_tail(p_sh), "sh" ) != NULL; |
300 | char *nameext = strrchr(name, '.'); |
301 | size_t nameext_len = nameext ? strlen(nameext) : 0; |
302 | xstrlcpy(os_buf, name, sizeof(os_buf)); |
303 | char *buf_end = xstrchrnul(os_buf, '\0'); |
304 | const char *pathext = os_getenv("PATHEXT" ); |
305 | if (!pathext) { |
306 | pathext = ".com;.exe;.bat;.cmd" ; |
307 | } |
308 | const char *ext = pathext; |
309 | while (*ext) { |
310 | // If $PATHEXT itself contains dot: |
311 | if (ext[0] == '.' && (ext[1] == '\0' || ext[1] == ENV_SEPCHAR)) { |
312 | if (is_executable(name, abspath)) { |
313 | return true; |
314 | } |
315 | // Skip it. |
316 | ext++; |
317 | if (*ext) { |
318 | ext++; |
319 | } |
320 | continue; |
321 | } |
322 | |
323 | const char *ext_end = ext; |
324 | size_t ext_len = |
325 | copy_option_part((char_u **)&ext_end, (char_u *)buf_end, |
326 | sizeof(os_buf) - (size_t)(buf_end - os_buf), ENV_SEPSTR); |
327 | if (ext_len != 0) { |
328 | bool in_pathext = nameext_len == ext_len |
329 | && 0 == mb_strnicmp((char_u *)nameext, (char_u *)ext, ext_len); |
330 | |
331 | if (((in_pathext || is_unix_shell) && is_executable(name, abspath)) |
332 | || is_executable(os_buf, abspath)) { |
333 | return true; |
334 | } |
335 | } |
336 | ext = ext_end; |
337 | } |
338 | return false; |
339 | } |
340 | #endif |
341 | |
342 | /// Checks if a file is in `$PATH` and is executable. |
343 | /// |
344 | /// @param[in] name Filename to check. |
345 | /// @param[out] abspath Returns resolved executable path, if not NULL. |
346 | /// |
347 | /// @return `true` if `name` is an executable inside `$PATH`. |
348 | static bool is_executable_in_path(const char *name, char **abspath) |
349 | FUNC_ATTR_NONNULL_ARG(1) |
350 | { |
351 | const char *path_env = os_getenv("PATH" ); |
352 | if (path_env == NULL) { |
353 | return false; |
354 | } |
355 | |
356 | #ifdef WIN32 |
357 | // Prepend ".;" to $PATH. |
358 | size_t pathlen = strlen(path_env); |
359 | char *path = memcpy(xmallocz(pathlen + 2), "." ENV_SEPSTR, 2); |
360 | memcpy(path + 2, path_env, pathlen); |
361 | #else |
362 | char *path = xstrdup(path_env); |
363 | #endif |
364 | |
365 | size_t buf_len = strlen(name) + strlen(path) + 2; |
366 | char *buf = xmalloc(buf_len); |
367 | |
368 | // Walk through all entries in $PATH to check if "name" exists there and |
369 | // is an executable file. |
370 | char *p = path; |
371 | bool rv = false; |
372 | for (;; ) { |
373 | char *e = xstrchrnul(p, ENV_SEPCHAR); |
374 | |
375 | // Combine the $PATH segment with `name`. |
376 | STRLCPY(buf, p, e - p + 1); |
377 | append_path(buf, name, buf_len); |
378 | |
379 | #ifdef WIN32 |
380 | if (is_executable_ext(buf, abspath)) { |
381 | #else |
382 | if (is_executable(buf, abspath)) { |
383 | #endif |
384 | rv = true; |
385 | goto end; |
386 | } |
387 | |
388 | if (*e != ENV_SEPCHAR) { |
389 | // End of $PATH without finding any executable called name. |
390 | goto end; |
391 | } |
392 | |
393 | p = e + 1; |
394 | } |
395 | |
396 | end: |
397 | xfree(buf); |
398 | xfree(path); |
399 | return rv; |
400 | } |
401 | |
402 | /// Opens or creates a file and returns a non-negative integer representing |
403 | /// the lowest-numbered unused file descriptor, for use in subsequent system |
404 | /// calls (read, write, lseek, fcntl, etc.). If the operation fails, a libuv |
405 | /// error code is returned, and no file is created or modified. |
406 | /// |
407 | /// @param path Filename |
408 | /// @param flags Bitwise OR of flags defined in <fcntl.h> |
409 | /// @param mode Permissions for the newly-created file (IGNORED if 'flags' is |
410 | /// not `O_CREAT` or `O_TMPFILE`), subject to the current umask |
411 | /// @return file descriptor, or negative error code on failure |
412 | int os_open(const char *path, int flags, int mode) |
413 | { |
414 | if (path == NULL) { // uv_fs_open asserts on NULL. #7561 |
415 | return UV_EINVAL; |
416 | } |
417 | int r; |
418 | RUN_UV_FS_FUNC(r, uv_fs_open, path, flags, mode, NULL); |
419 | return r; |
420 | } |
421 | |
422 | /// Compatibility wrapper conforming to fopen(3). |
423 | /// |
424 | /// Windows: works with UTF-16 filepaths by delegating to libuv (os_open). |
425 | /// |
426 | /// Future: remove this, migrate callers to os/fileio.c ? |
427 | /// But file_open_fd does not support O_RDWR yet. |
428 | /// |
429 | /// @param path Filename |
430 | /// @param flags String flags, one of { r w a r+ w+ a+ rb wb ab } |
431 | /// @return FILE pointer, or NULL on error. |
432 | FILE *os_fopen(const char *path, const char *flags) |
433 | { |
434 | assert(flags != NULL && strlen(flags) > 0 && strlen(flags) <= 2); |
435 | int iflags = 0; |
436 | // Per table in fopen(3) manpage. |
437 | if (flags[1] == '\0' || flags[1] == 'b') { |
438 | switch (flags[0]) { |
439 | case 'r': |
440 | iflags = O_RDONLY; |
441 | break; |
442 | case 'w': |
443 | iflags = O_WRONLY | O_CREAT | O_TRUNC; |
444 | break; |
445 | case 'a': |
446 | iflags = O_WRONLY | O_CREAT | O_APPEND; |
447 | break; |
448 | default: |
449 | abort(); |
450 | } |
451 | #ifdef WIN32 |
452 | if (flags[1] == 'b') { |
453 | iflags |= O_BINARY; |
454 | } |
455 | #endif |
456 | } else { |
457 | // char 0 must be one of ('r','w','a'). |
458 | // char 1 is always '+' ('b' is handled above). |
459 | assert(flags[1] == '+'); |
460 | switch (flags[0]) { |
461 | case 'r': |
462 | iflags = O_RDWR; |
463 | break; |
464 | case 'w': |
465 | iflags = O_RDWR | O_CREAT | O_TRUNC; |
466 | break; |
467 | case 'a': |
468 | iflags = O_RDWR | O_CREAT | O_APPEND; |
469 | break; |
470 | default: |
471 | abort(); |
472 | } |
473 | } |
474 | // Per open(2) manpage. |
475 | assert((iflags|O_RDONLY) || (iflags|O_WRONLY) || (iflags|O_RDWR)); |
476 | // Per fopen(3) manpage: default to 0666, it will be umask-adjusted. |
477 | int fd = os_open(path, iflags, 0666); |
478 | if (fd < 0) { |
479 | return NULL; |
480 | } |
481 | return fdopen(fd, flags); |
482 | } |
483 | |
484 | /// Sets file descriptor `fd` to close-on-exec. |
485 | // |
486 | // @return -1 if failed to set, 0 otherwise. |
487 | int os_set_cloexec(const int fd) |
488 | { |
489 | #ifdef HAVE_FD_CLOEXEC |
490 | int e; |
491 | int fdflags = fcntl(fd, F_GETFD); |
492 | if (fdflags < 0) { |
493 | e = errno; |
494 | ELOG("Failed to get flags on descriptor %d: %s" , fd, strerror(e)); |
495 | errno = e; |
496 | return -1; |
497 | } |
498 | if ((fdflags & FD_CLOEXEC) == 0 |
499 | && fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC) == -1) { |
500 | e = errno; |
501 | ELOG("Failed to set CLOEXEC on descriptor %d: %s" , fd, strerror(e)); |
502 | errno = e; |
503 | return -1; |
504 | } |
505 | return 0; |
506 | #endif |
507 | |
508 | // No FD_CLOEXEC flag. On Windows, the file should have been opened with |
509 | // O_NOINHERIT anyway. |
510 | return -1; |
511 | } |
512 | |
513 | /// Close a file |
514 | /// |
515 | /// @return 0 or libuv error code on failure. |
516 | int os_close(const int fd) |
517 | { |
518 | int r; |
519 | RUN_UV_FS_FUNC(r, uv_fs_close, fd, NULL); |
520 | return r; |
521 | } |
522 | |
523 | /// Duplicate file descriptor |
524 | /// |
525 | /// @param[in] fd File descriptor to duplicate. |
526 | /// |
527 | /// @return New file descriptor or libuv error code (< 0). |
528 | int os_dup(const int fd) |
529 | FUNC_ATTR_WARN_UNUSED_RESULT |
530 | { |
531 | int ret; |
532 | os_dup_dup: |
533 | ret = dup(fd); |
534 | if (ret < 0) { |
535 | const int error = os_translate_sys_error(errno); |
536 | errno = 0; |
537 | if (error == UV_EINTR) { |
538 | goto os_dup_dup; |
539 | } else { |
540 | return error; |
541 | } |
542 | } |
543 | return ret; |
544 | } |
545 | |
546 | /// Read from a file |
547 | /// |
548 | /// Handles EINTR and ENOMEM, but not other errors. |
549 | /// |
550 | /// @param[in] fd File descriptor to read from. |
551 | /// @param[out] ret_eof Is set to true if EOF was encountered, otherwise set |
552 | /// to false. Initial value is ignored. |
553 | /// @param[out] ret_buf Buffer to write to. May be NULL if size is zero. |
554 | /// @param[in] size Amount of bytes to read. |
555 | /// @param[in] non_blocking Do not restart syscall if EAGAIN was encountered. |
556 | /// |
557 | /// @return Number of bytes read or libuv error code (< 0). |
558 | ptrdiff_t os_read(const int fd, bool *const ret_eof, char *const ret_buf, |
559 | const size_t size, const bool non_blocking) |
560 | FUNC_ATTR_WARN_UNUSED_RESULT |
561 | { |
562 | *ret_eof = false; |
563 | if (ret_buf == NULL) { |
564 | assert(size == 0); |
565 | return 0; |
566 | } |
567 | size_t read_bytes = 0; |
568 | bool did_try_to_free = false; |
569 | while (read_bytes != size) { |
570 | assert(size >= read_bytes); |
571 | const ptrdiff_t cur_read_bytes = read(fd, ret_buf + read_bytes, |
572 | IO_COUNT(size - read_bytes)); |
573 | if (cur_read_bytes > 0) { |
574 | read_bytes += (size_t)cur_read_bytes; |
575 | } |
576 | if (cur_read_bytes < 0) { |
577 | const int error = os_translate_sys_error(errno); |
578 | errno = 0; |
579 | if (non_blocking && error == UV_EAGAIN) { |
580 | break; |
581 | } else if (error == UV_EINTR || error == UV_EAGAIN) { |
582 | continue; |
583 | } else if (error == UV_ENOMEM && !did_try_to_free) { |
584 | try_to_free_memory(); |
585 | did_try_to_free = true; |
586 | continue; |
587 | } else { |
588 | return (ptrdiff_t)error; |
589 | } |
590 | } |
591 | if (cur_read_bytes == 0) { |
592 | *ret_eof = true; |
593 | break; |
594 | } |
595 | } |
596 | return (ptrdiff_t)read_bytes; |
597 | } |
598 | |
599 | #ifdef HAVE_READV |
600 | /// Read from a file to multiple buffers at once |
601 | /// |
602 | /// Wrapper for readv(). |
603 | /// |
604 | /// @param[in] fd File descriptor to read from. |
605 | /// @param[out] ret_eof Is set to true if EOF was encountered, otherwise set |
606 | /// to false. Initial value is ignored. |
607 | /// @param[out] iov Description of buffers to write to. Note: this description |
608 | /// may change, it is incorrect to use data it points to after |
609 | /// os_readv(). |
610 | /// @param[in] iov_size Number of buffers in iov. |
611 | /// @param[in] non_blocking Do not restart syscall if EAGAIN was encountered. |
612 | /// |
613 | /// @return Number of bytes read or libuv error code (< 0). |
614 | ptrdiff_t os_readv(const int fd, bool *const ret_eof, struct iovec *iov, |
615 | size_t iov_size, const bool non_blocking) |
616 | FUNC_ATTR_NONNULL_ALL |
617 | { |
618 | *ret_eof = false; |
619 | size_t read_bytes = 0; |
620 | bool did_try_to_free = false; |
621 | size_t toread = 0; |
622 | for (size_t i = 0; i < iov_size; i++) { |
623 | // Overflow, trying to read too much data |
624 | assert(toread <= SIZE_MAX - iov[i].iov_len); |
625 | toread += iov[i].iov_len; |
626 | } |
627 | while (read_bytes < toread && iov_size && !*ret_eof) { |
628 | ptrdiff_t cur_read_bytes = readv(fd, iov, (int)iov_size); |
629 | if (cur_read_bytes == 0) { |
630 | *ret_eof = true; |
631 | } |
632 | if (cur_read_bytes > 0) { |
633 | read_bytes += (size_t)cur_read_bytes; |
634 | while (iov_size && cur_read_bytes) { |
635 | if (cur_read_bytes < (ptrdiff_t)iov->iov_len) { |
636 | iov->iov_len -= (size_t)cur_read_bytes; |
637 | iov->iov_base = (char *)iov->iov_base + cur_read_bytes; |
638 | cur_read_bytes = 0; |
639 | } else { |
640 | cur_read_bytes -= (ptrdiff_t)iov->iov_len; |
641 | iov_size--; |
642 | iov++; |
643 | } |
644 | } |
645 | } else if (cur_read_bytes < 0) { |
646 | const int error = os_translate_sys_error(errno); |
647 | errno = 0; |
648 | if (non_blocking && error == UV_EAGAIN) { |
649 | break; |
650 | } else if (error == UV_EINTR || error == UV_EAGAIN) { |
651 | continue; |
652 | } else if (error == UV_ENOMEM && !did_try_to_free) { |
653 | try_to_free_memory(); |
654 | did_try_to_free = true; |
655 | continue; |
656 | } else { |
657 | return (ptrdiff_t)error; |
658 | } |
659 | } |
660 | } |
661 | return (ptrdiff_t)read_bytes; |
662 | } |
663 | #endif // HAVE_READV |
664 | |
665 | /// Write to a file |
666 | /// |
667 | /// @param[in] fd File descriptor to write to. |
668 | /// @param[in] buf Data to write. May be NULL if size is zero. |
669 | /// @param[in] size Amount of bytes to write. |
670 | /// @param[in] non_blocking Do not restart syscall if EAGAIN was encountered. |
671 | /// |
672 | /// @return Number of bytes written or libuv error code (< 0). |
673 | ptrdiff_t os_write(const int fd, const char *const buf, const size_t size, |
674 | const bool non_blocking) |
675 | FUNC_ATTR_WARN_UNUSED_RESULT |
676 | { |
677 | if (buf == NULL) { |
678 | assert(size == 0); |
679 | return 0; |
680 | } |
681 | size_t written_bytes = 0; |
682 | while (written_bytes != size) { |
683 | assert(size >= written_bytes); |
684 | const ptrdiff_t cur_written_bytes = write(fd, buf + written_bytes, |
685 | IO_COUNT(size - written_bytes)); |
686 | if (cur_written_bytes > 0) { |
687 | written_bytes += (size_t)cur_written_bytes; |
688 | } |
689 | if (cur_written_bytes < 0) { |
690 | const int error = os_translate_sys_error(errno); |
691 | errno = 0; |
692 | if (non_blocking && error == UV_EAGAIN) { |
693 | break; |
694 | } else if (error == UV_EINTR || error == UV_EAGAIN) { |
695 | continue; |
696 | } else { |
697 | return error; |
698 | } |
699 | } |
700 | if (cur_written_bytes == 0) { |
701 | return UV_UNKNOWN; |
702 | } |
703 | } |
704 | return (ptrdiff_t)written_bytes; |
705 | } |
706 | |
707 | /// Copies a file from `path` to `new_path`. |
708 | /// |
709 | /// @see http://docs.libuv.org/en/v1.x/fs.html#c.uv_fs_copyfile |
710 | /// |
711 | /// @param path Path of file to be copied |
712 | /// @param path_new Path of new file |
713 | /// @param flags Bitwise OR of flags defined in <uv.h> |
714 | /// @return 0 on success, or libuv error code on failure. |
715 | int os_copy(const char *path, const char *new_path, int flags) |
716 | { |
717 | int r; |
718 | RUN_UV_FS_FUNC(r, uv_fs_copyfile, path, new_path, flags, NULL); |
719 | return r; |
720 | } |
721 | |
722 | /// Flushes file modifications to disk. |
723 | /// |
724 | /// @param fd the file descriptor of the file to flush to disk. |
725 | /// |
726 | /// @return 0 on success, or libuv error code on failure. |
727 | int os_fsync(int fd) |
728 | { |
729 | int r; |
730 | RUN_UV_FS_FUNC(r, uv_fs_fsync, fd, NULL); |
731 | g_stats.fsync++; |
732 | return r; |
733 | } |
734 | |
735 | /// Get stat information for a file. |
736 | /// |
737 | /// @return libuv return code, or -errno |
738 | static int os_stat(const char *name, uv_stat_t *statbuf) |
739 | FUNC_ATTR_NONNULL_ARG(2) |
740 | { |
741 | if (!name) { |
742 | return UV_EINVAL; |
743 | } |
744 | uv_fs_t request; |
745 | int result = uv_fs_stat(&fs_loop, &request, name, NULL); |
746 | *statbuf = request.statbuf; |
747 | uv_fs_req_cleanup(&request); |
748 | return result; |
749 | } |
750 | |
751 | /// Get the file permissions for a given file. |
752 | /// |
753 | /// @return libuv error code on error. |
754 | int32_t os_getperm(const char *name) |
755 | { |
756 | uv_stat_t statbuf; |
757 | int stat_result = os_stat(name, &statbuf); |
758 | if (stat_result == kLibuvSuccess) { |
759 | return (int32_t)statbuf.st_mode; |
760 | } else { |
761 | return stat_result; |
762 | } |
763 | } |
764 | |
765 | /// Set the permission of a file. |
766 | /// |
767 | /// @return `OK` for success, `FAIL` for failure. |
768 | int os_setperm(const char *const name, int perm) |
769 | FUNC_ATTR_NONNULL_ALL |
770 | { |
771 | int r; |
772 | RUN_UV_FS_FUNC(r, uv_fs_chmod, name, perm, NULL); |
773 | return (r == kLibuvSuccess ? OK : FAIL); |
774 | } |
775 | |
776 | /// Changes the owner and group of a file, like chown(2). |
777 | /// |
778 | /// @return 0 on success, or libuv error code on failure. |
779 | /// |
780 | /// @note If `owner` or `group` is -1, then that ID is not changed. |
781 | int os_chown(const char *path, uv_uid_t owner, uv_gid_t group) |
782 | { |
783 | int r; |
784 | RUN_UV_FS_FUNC(r, uv_fs_chown, path, owner, group, NULL); |
785 | return r; |
786 | } |
787 | |
788 | /// Changes the owner and group of the file referred to by the open file |
789 | /// descriptor, like fchown(2). |
790 | /// |
791 | /// @return 0 on success, or libuv error code on failure. |
792 | /// |
793 | /// @note If `owner` or `group` is -1, then that ID is not changed. |
794 | int os_fchown(int fd, uv_uid_t owner, uv_gid_t group) |
795 | { |
796 | int r; |
797 | RUN_UV_FS_FUNC(r, uv_fs_fchown, fd, owner, group, NULL); |
798 | return r; |
799 | } |
800 | |
801 | /// Check if a path exists. |
802 | /// |
803 | /// @return `true` if `path` exists |
804 | bool os_path_exists(const char_u *path) |
805 | { |
806 | uv_stat_t statbuf; |
807 | return os_stat((char *)path, &statbuf) == kLibuvSuccess; |
808 | } |
809 | |
810 | /// Sets file access and modification times. |
811 | /// |
812 | /// @see POSIX utime(2) |
813 | /// |
814 | /// @param path File path. |
815 | /// @param atime Last access time. |
816 | /// @param mtime Last modification time. |
817 | /// |
818 | /// @return 0 on success, or negative error code. |
819 | int os_file_settime(const char *path, double atime, double mtime) |
820 | { |
821 | int r; |
822 | RUN_UV_FS_FUNC(r, uv_fs_utime, path, atime, mtime, NULL); |
823 | return r; |
824 | } |
825 | |
826 | /// Check if a file is readable. |
827 | /// |
828 | /// @return true if `name` is readable, otherwise false. |
829 | bool os_file_is_readable(const char *name) |
830 | FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT |
831 | { |
832 | int r; |
833 | RUN_UV_FS_FUNC(r, uv_fs_access, name, R_OK, NULL); |
834 | return (r == 0); |
835 | } |
836 | |
837 | /// Check if a file is writable. |
838 | /// |
839 | /// @return `0` if `name` is not writable, |
840 | /// @return `1` if `name` is writable, |
841 | /// @return `2` for a directory which we have rights to write into. |
842 | int os_file_is_writable(const char *name) |
843 | FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT |
844 | { |
845 | int r; |
846 | RUN_UV_FS_FUNC(r, uv_fs_access, name, W_OK, NULL); |
847 | if (r == 0) { |
848 | return os_isdir((char_u *)name) ? 2 : 1; |
849 | } |
850 | return 0; |
851 | } |
852 | |
853 | /// Rename a file or directory. |
854 | /// |
855 | /// @return `OK` for success, `FAIL` for failure. |
856 | int os_rename(const char_u *path, const char_u *new_path) |
857 | FUNC_ATTR_NONNULL_ALL |
858 | { |
859 | int r; |
860 | RUN_UV_FS_FUNC(r, uv_fs_rename, (const char *)path, (const char *)new_path, |
861 | NULL); |
862 | return (r == kLibuvSuccess ? OK : FAIL); |
863 | } |
864 | |
865 | /// Make a directory. |
866 | /// |
867 | /// @return `0` for success, libuv error code for failure. |
868 | int os_mkdir(const char *path, int32_t mode) |
869 | FUNC_ATTR_NONNULL_ALL |
870 | { |
871 | int r; |
872 | RUN_UV_FS_FUNC(r, uv_fs_mkdir, path, mode, NULL); |
873 | return r; |
874 | } |
875 | |
876 | /// Make a directory, with higher levels when needed |
877 | /// |
878 | /// @param[in] dir Directory to create. |
879 | /// @param[in] mode Permissions for the newly-created directory. |
880 | /// @param[out] failed_dir If it failed to create directory, then this |
881 | /// argument is set to an allocated string containing |
882 | /// the name of the directory which os_mkdir_recurse |
883 | /// failed to create. I.e. it will contain dir or any |
884 | /// of the higher level directories. |
885 | /// |
886 | /// @return `0` for success, libuv error code for failure. |
887 | int os_mkdir_recurse(const char *const dir, int32_t mode, |
888 | char **const failed_dir) |
889 | FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT |
890 | { |
891 | // Get end of directory name in "dir". |
892 | // We're done when it's "/" or "c:/". |
893 | const size_t dirlen = strlen(dir); |
894 | char *const curdir = xmemdupz(dir, dirlen); |
895 | char *const past_head = (char *)get_past_head((char_u *)curdir); |
896 | char *e = curdir + dirlen; |
897 | const char *const real_end = e; |
898 | const char past_head_save = *past_head; |
899 | while (!os_isdir((char_u *)curdir)) { |
900 | e = (char *)path_tail_with_sep((char_u *)curdir); |
901 | if (e <= past_head) { |
902 | *past_head = NUL; |
903 | break; |
904 | } |
905 | *e = NUL; |
906 | } |
907 | while (e != real_end) { |
908 | if (e > past_head) { |
909 | *e = PATHSEP; |
910 | } else { |
911 | *past_head = past_head_save; |
912 | } |
913 | const size_t component_len = strlen(e); |
914 | e += component_len; |
915 | if (e == real_end |
916 | && memcnt(e - component_len, PATHSEP, component_len) == component_len) { |
917 | // Path ends with something like "////". Ignore this. |
918 | break; |
919 | } |
920 | int ret; |
921 | if ((ret = os_mkdir(curdir, mode)) != 0) { |
922 | *failed_dir = curdir; |
923 | return ret; |
924 | } |
925 | } |
926 | xfree(curdir); |
927 | return 0; |
928 | } |
929 | |
930 | /// Create a unique temporary directory. |
931 | /// |
932 | /// @param[in] template Template of the path to the directory with XXXXXX |
933 | /// which would be replaced by random chars. |
934 | /// @param[out] path Path to created directory for success, undefined for |
935 | /// failure. |
936 | /// @return `0` for success, non-zero for failure. |
937 | int os_mkdtemp(const char *template, char *path) |
938 | FUNC_ATTR_NONNULL_ALL |
939 | { |
940 | uv_fs_t request; |
941 | int result = uv_fs_mkdtemp(&fs_loop, &request, template, NULL); |
942 | if (result == kLibuvSuccess) { |
943 | STRNCPY(path, request.path, TEMP_FILE_PATH_MAXLEN); |
944 | } |
945 | uv_fs_req_cleanup(&request); |
946 | return result; |
947 | } |
948 | |
949 | /// Remove a directory. |
950 | /// |
951 | /// @return `0` for success, non-zero for failure. |
952 | int os_rmdir(const char *path) |
953 | FUNC_ATTR_NONNULL_ALL |
954 | { |
955 | int r; |
956 | RUN_UV_FS_FUNC(r, uv_fs_rmdir, path, NULL); |
957 | return r; |
958 | } |
959 | |
960 | /// Opens a directory. |
961 | /// @param[out] dir The Directory object. |
962 | /// @param path Path to the directory. |
963 | /// @returns true if dir contains one or more items, false if not or an error |
964 | /// occurred. |
965 | bool os_scandir(Directory *dir, const char *path) |
966 | FUNC_ATTR_NONNULL_ALL |
967 | { |
968 | int r = uv_fs_scandir(&fs_loop, &dir->request, path, 0, NULL); |
969 | if (r < 0) { |
970 | os_closedir(dir); |
971 | } |
972 | return r >= 0; |
973 | } |
974 | |
975 | /// Increments the directory pointer. |
976 | /// @param dir The Directory object. |
977 | /// @returns a pointer to the next path in `dir` or `NULL`. |
978 | const char *os_scandir_next(Directory *dir) |
979 | FUNC_ATTR_NONNULL_ALL |
980 | { |
981 | int err = uv_fs_scandir_next(&dir->request, &dir->ent); |
982 | return err != UV_EOF ? dir->ent.name : NULL; |
983 | } |
984 | |
985 | /// Frees memory associated with `os_scandir()`. |
986 | /// @param dir The directory. |
987 | void os_closedir(Directory *dir) |
988 | FUNC_ATTR_NONNULL_ALL |
989 | { |
990 | uv_fs_req_cleanup(&dir->request); |
991 | } |
992 | |
993 | /// Remove a file. |
994 | /// |
995 | /// @return `0` for success, non-zero for failure. |
996 | int os_remove(const char *path) |
997 | FUNC_ATTR_NONNULL_ALL |
998 | { |
999 | int r; |
1000 | RUN_UV_FS_FUNC(r, uv_fs_unlink, path, NULL); |
1001 | return r; |
1002 | } |
1003 | |
1004 | /// Get the file information for a given path |
1005 | /// |
1006 | /// @param path Path to the file. |
1007 | /// @param[out] file_info Pointer to a FileInfo to put the information in. |
1008 | /// @return `true` on success, `false` for failure. |
1009 | bool os_fileinfo(const char *path, FileInfo *file_info) |
1010 | FUNC_ATTR_NONNULL_ARG(2) |
1011 | { |
1012 | return os_stat(path, &(file_info->stat)) == kLibuvSuccess; |
1013 | } |
1014 | |
1015 | /// Get the file information for a given path without following links |
1016 | /// |
1017 | /// @param path Path to the file. |
1018 | /// @param[out] file_info Pointer to a FileInfo to put the information in. |
1019 | /// @return `true` on success, `false` for failure. |
1020 | bool os_fileinfo_link(const char *path, FileInfo *file_info) |
1021 | FUNC_ATTR_NONNULL_ARG(2) |
1022 | { |
1023 | if (path == NULL) { |
1024 | return false; |
1025 | } |
1026 | uv_fs_t request; |
1027 | int result = uv_fs_lstat(&fs_loop, &request, path, NULL); |
1028 | file_info->stat = request.statbuf; |
1029 | uv_fs_req_cleanup(&request); |
1030 | return (result == kLibuvSuccess); |
1031 | } |
1032 | |
1033 | /// Get the file information for a given file descriptor |
1034 | /// |
1035 | /// @param file_descriptor File descriptor of the file. |
1036 | /// @param[out] file_info Pointer to a FileInfo to put the information in. |
1037 | /// @return `true` on success, `false` for failure. |
1038 | bool os_fileinfo_fd(int file_descriptor, FileInfo *file_info) |
1039 | FUNC_ATTR_NONNULL_ALL |
1040 | { |
1041 | uv_fs_t request; |
1042 | int result = uv_fs_fstat(&fs_loop, &request, file_descriptor, NULL); |
1043 | file_info->stat = request.statbuf; |
1044 | uv_fs_req_cleanup(&request); |
1045 | return (result == kLibuvSuccess); |
1046 | } |
1047 | |
1048 | /// Compare the inodes of two FileInfos |
1049 | /// |
1050 | /// @return `true` if the two FileInfos represent the same file. |
1051 | bool os_fileinfo_id_equal(const FileInfo *file_info_1, |
1052 | const FileInfo *file_info_2) |
1053 | FUNC_ATTR_NONNULL_ALL |
1054 | { |
1055 | return file_info_1->stat.st_ino == file_info_2->stat.st_ino |
1056 | && file_info_1->stat.st_dev == file_info_2->stat.st_dev; |
1057 | } |
1058 | |
1059 | /// Get the `FileID` of a `FileInfo` |
1060 | /// |
1061 | /// @param file_info Pointer to the `FileInfo` |
1062 | /// @param[out] file_id Pointer to a `FileID` |
1063 | void os_fileinfo_id(const FileInfo *file_info, FileID *file_id) |
1064 | FUNC_ATTR_NONNULL_ALL |
1065 | { |
1066 | file_id->inode = file_info->stat.st_ino; |
1067 | file_id->device_id = file_info->stat.st_dev; |
1068 | } |
1069 | |
1070 | /// Get the inode of a `FileInfo` |
1071 | /// |
1072 | /// @deprecated Use `FileID` instead, this function is only needed in memline.c |
1073 | /// @param file_info Pointer to the `FileInfo` |
1074 | /// @return the inode number |
1075 | uint64_t os_fileinfo_inode(const FileInfo *file_info) |
1076 | FUNC_ATTR_NONNULL_ALL |
1077 | { |
1078 | return file_info->stat.st_ino; |
1079 | } |
1080 | |
1081 | /// Get the size of a file from a `FileInfo`. |
1082 | /// |
1083 | /// @return filesize in bytes. |
1084 | uint64_t os_fileinfo_size(const FileInfo *file_info) |
1085 | FUNC_ATTR_NONNULL_ALL |
1086 | { |
1087 | return file_info->stat.st_size; |
1088 | } |
1089 | |
1090 | /// Get the number of hardlinks from a `FileInfo`. |
1091 | /// |
1092 | /// @return number of hardlinks. |
1093 | uint64_t os_fileinfo_hardlinks(const FileInfo *file_info) |
1094 | FUNC_ATTR_NONNULL_ALL |
1095 | { |
1096 | return file_info->stat.st_nlink; |
1097 | } |
1098 | |
1099 | /// Get the blocksize from a `FileInfo`. |
1100 | /// |
1101 | /// @return blocksize in bytes. |
1102 | uint64_t os_fileinfo_blocksize(const FileInfo *file_info) |
1103 | FUNC_ATTR_NONNULL_ALL |
1104 | { |
1105 | return file_info->stat.st_blksize; |
1106 | } |
1107 | |
1108 | /// Get the `FileID` for a given path |
1109 | /// |
1110 | /// @param path Path to the file. |
1111 | /// @param[out] file_info Pointer to a `FileID` to fill in. |
1112 | /// @return `true` on sucess, `false` for failure. |
1113 | bool os_fileid(const char *path, FileID *file_id) |
1114 | FUNC_ATTR_NONNULL_ALL |
1115 | { |
1116 | uv_stat_t statbuf; |
1117 | if (os_stat(path, &statbuf) == kLibuvSuccess) { |
1118 | file_id->inode = statbuf.st_ino; |
1119 | file_id->device_id = statbuf.st_dev; |
1120 | return true; |
1121 | } |
1122 | return false; |
1123 | } |
1124 | |
1125 | /// Check if two `FileID`s are equal |
1126 | /// |
1127 | /// @param file_id_1 Pointer to first `FileID` |
1128 | /// @param file_id_2 Pointer to second `FileID` |
1129 | /// @return `true` if the two `FileID`s represent te same file. |
1130 | bool os_fileid_equal(const FileID *file_id_1, const FileID *file_id_2) |
1131 | FUNC_ATTR_NONNULL_ALL |
1132 | { |
1133 | return file_id_1->inode == file_id_2->inode |
1134 | && file_id_1->device_id == file_id_2->device_id; |
1135 | } |
1136 | |
1137 | /// Check if a `FileID` is equal to a `FileInfo` |
1138 | /// |
1139 | /// @param file_id Pointer to a `FileID` |
1140 | /// @param file_info Pointer to a `FileInfo` |
1141 | /// @return `true` if the `FileID` and the `FileInfo` represent te same file. |
1142 | bool os_fileid_equal_fileinfo(const FileID *file_id, |
1143 | const FileInfo *file_info) |
1144 | FUNC_ATTR_NONNULL_ALL |
1145 | { |
1146 | return file_id->inode == file_info->stat.st_ino |
1147 | && file_id->device_id == file_info->stat.st_dev; |
1148 | } |
1149 | |
1150 | #ifdef WIN32 |
1151 | # include <shlobj.h> |
1152 | |
1153 | /// When "fname" is the name of a shortcut (*.lnk) resolve the file it points |
1154 | /// to and return that name in allocated memory. |
1155 | /// Otherwise NULL is returned. |
1156 | char *os_resolve_shortcut(const char *fname) |
1157 | FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_MALLOC |
1158 | { |
1159 | HRESULT hr; |
1160 | IPersistFile *ppf = NULL; |
1161 | OLECHAR wsz[MAX_PATH]; |
1162 | char *rfname = NULL; |
1163 | IShellLinkW *pslw = NULL; |
1164 | WIN32_FIND_DATAW ffdw; |
1165 | |
1166 | // Check if the file name ends in ".lnk". Avoid calling CoCreateInstance(), |
1167 | // it's quite slow. |
1168 | if (fname == NULL) { |
1169 | return rfname; |
1170 | } |
1171 | const size_t len = strlen(fname); |
1172 | if (len <= 4 || STRNICMP(fname + len - 4, ".lnk" , 4) != 0) { |
1173 | return rfname; |
1174 | } |
1175 | |
1176 | CoInitialize(NULL); |
1177 | |
1178 | // create a link manager object and request its interface |
1179 | hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, |
1180 | &IID_IShellLinkW, (void **)&pslw); |
1181 | if (hr == S_OK) { |
1182 | wchar_t *p; |
1183 | const int r = utf8_to_utf16(fname, -1, &p); |
1184 | if (r != 0) { |
1185 | EMSG2("utf8_to_utf16 failed: %d" , r); |
1186 | } else if (p != NULL) { |
1187 | // Get a pointer to the IPersistFile interface. |
1188 | hr = pslw->lpVtbl->QueryInterface( |
1189 | pslw, &IID_IPersistFile, (void **)&ppf); |
1190 | if (hr != S_OK) { |
1191 | goto shortcut_errorw; |
1192 | } |
1193 | |
1194 | // "load" the name and resolve the link |
1195 | hr = ppf->lpVtbl->Load(ppf, p, STGM_READ); |
1196 | if (hr != S_OK) { |
1197 | goto shortcut_errorw; |
1198 | } |
1199 | |
1200 | # if 0 // This makes Vim wait a long time if the target does not exist. |
1201 | hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI); |
1202 | if (hr != S_OK) { |
1203 | goto shortcut_errorw; |
1204 | } |
1205 | # endif |
1206 | |
1207 | // Get the path to the link target. |
1208 | ZeroMemory(wsz, MAX_PATH * sizeof(wchar_t)); |
1209 | hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0); |
1210 | if (hr == S_OK && wsz[0] != NUL) { |
1211 | const int r2 = utf16_to_utf8(wsz, -1, &rfname); |
1212 | if (r2 != 0) { |
1213 | EMSG2("utf16_to_utf8 failed: %d" , r2); |
1214 | } |
1215 | } |
1216 | |
1217 | shortcut_errorw: |
1218 | xfree(p); |
1219 | goto shortcut_end; |
1220 | } |
1221 | } |
1222 | |
1223 | shortcut_end: |
1224 | // Release all interface pointers (both belong to the same object) |
1225 | if (ppf != NULL) { |
1226 | ppf->lpVtbl->Release(ppf); |
1227 | } |
1228 | if (pslw != NULL) { |
1229 | pslw->lpVtbl->Release(pslw); |
1230 | } |
1231 | |
1232 | CoUninitialize(); |
1233 | return rfname; |
1234 | } |
1235 | |
1236 | #endif |
1237 | |