flyshell 1.4.9__tar.gz → 1.4.11__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. {flyshell-1.4.9/src/flyshell.egg-info → flyshell-1.4.11}/PKG-INFO +9 -4
  2. {flyshell-1.4.9 → flyshell-1.4.11}/README.md +8 -3
  3. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/diskops.c +30 -0
  4. flyshell-1.4.11/c_src/diskops/posix/cp.c +247 -0
  5. flyshell-1.4.11/c_src/diskops/posix/mv.c +52 -0
  6. flyshell-1.4.11/c_src/diskops/win32/cp.c +298 -0
  7. flyshell-1.4.11/c_src/diskops/win32/mv.c +74 -0
  8. {flyshell-1.4.9 → flyshell-1.4.11}/pyproject.toml +0 -3
  9. flyshell-1.4.11/setup.py +49 -0
  10. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/constants.py +2 -3
  11. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/core/directory.py +3 -4
  12. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/native/diskops_handler.py +119 -0
  13. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/native/legacy/diskops.py +50 -0
  14. {flyshell-1.4.9 → flyshell-1.4.11/src/flyshell.egg-info}/PKG-INFO +9 -4
  15. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell.egg-info/SOURCES.txt +4 -2
  16. {flyshell-1.4.9 → flyshell-1.4.11}/tests/test_diskops_ext.py +79 -0
  17. {flyshell-1.4.9 → flyshell-1.4.11}/tests/test_diskops_lgc.py +32 -0
  18. flyshell-1.4.9/setup.py +0 -36
  19. flyshell-1.4.9/src/flyshell/builtin/diskops.py +0 -350
  20. flyshell-1.4.9/tests/test_diskops_old.py +0 -37
  21. {flyshell-1.4.9 → flyshell-1.4.11}/LICENSE +0 -0
  22. {flyshell-1.4.9 → flyshell-1.4.11}/MANIFEST.in +0 -0
  23. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/posix/cat.c +0 -0
  24. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/posix/dir.c +0 -0
  25. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/posix/mkdir.c +0 -0
  26. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/posix/rename.c +0 -0
  27. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/posix/rm.c +0 -0
  28. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/posix/rmdir.c +0 -0
  29. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/posix/touch.c +0 -0
  30. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/shared/dir_types.c +0 -0
  31. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/shared/dir_types.h +0 -0
  32. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/shared/native_errors.h +0 -0
  33. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/shared/win32_utf.h +0 -0
  34. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/win32/cat.c +0 -0
  35. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/win32/dir.c +0 -0
  36. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/win32/mkdir.c +0 -0
  37. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/win32/rename.c +0 -0
  38. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/win32/rm.c +0 -0
  39. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/win32/rmdir.c +0 -0
  40. {flyshell-1.4.9 → flyshell-1.4.11}/c_src/diskops/win32/touch.c +0 -0
  41. {flyshell-1.4.9 → flyshell-1.4.11}/setup.cfg +0 -0
  42. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/__init__.py +0 -0
  43. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/abstract/__init__.py +0 -0
  44. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/abstract/base_cmd.py +0 -0
  45. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/abstract/base_plugin.py +0 -0
  46. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/abstract/parent_cmd.py +0 -0
  47. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/accounts/__init__.py +0 -0
  48. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/accounts/auth.py +0 -0
  49. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/bin/__init__.py +0 -0
  50. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/builtin/__init__.py +0 -0
  51. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/builtin/fs.py +0 -0
  52. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/builtin/pkg.py +0 -0
  53. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/builtin/system.py +0 -0
  54. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/builtin/utils.py +0 -0
  55. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/core/__init__.py +0 -0
  56. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/core/console.py +0 -0
  57. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/core/data.py +0 -0
  58. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/main.py +0 -0
  59. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/native/__init__.py +0 -0
  60. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/native/diskops_bridge.py +0 -0
  61. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/native/legacy/__init__.py +0 -0
  62. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/startup/__init__.py +0 -0
  63. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/startup/deps.py +0 -0
  64. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/startup/env.py +0 -0
  65. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell/startup/loader.py +0 -0
  66. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell.egg-info/dependency_links.txt +0 -0
  67. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell.egg-info/entry_points.txt +0 -0
  68. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell.egg-info/requires.txt +0 -0
  69. {flyshell-1.4.9 → flyshell-1.4.11}/src/flyshell.egg-info/top_level.txt +0 -0
  70. {flyshell-1.4.9 → flyshell-1.4.11}/tests/test_auth.py +0 -0
  71. {flyshell-1.4.9 → flyshell-1.4.11}/tests/test_data.py +0 -0
  72. {flyshell-1.4.9 → flyshell-1.4.11}/tests/test_deps.py +0 -0
  73. {flyshell-1.4.9 → flyshell-1.4.11}/tests/test_directory.py +0 -0
  74. {flyshell-1.4.9 → flyshell-1.4.11}/tests/test_env.py +0 -0
  75. {flyshell-1.4.9 → flyshell-1.4.11}/tests/test_fs.py +0 -0
  76. {flyshell-1.4.9 → flyshell-1.4.11}/tests/test_loader.py +0 -0
  77. {flyshell-1.4.9 → flyshell-1.4.11}/tests/test_pkg.py +0 -0
  78. {flyshell-1.4.9 → flyshell-1.4.11}/tests/test_security.py +0 -0
  79. {flyshell-1.4.9 → flyshell-1.4.11}/tests/test_utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flyshell
3
- Version: 1.4.9
3
+ Version: 1.4.11
4
4
  Summary: A modular, zero-dependency command-line environment in Python.
5
5
  License-Expression: MIT
6
6
  Requires-Python: >=3.10
@@ -19,11 +19,11 @@ Dynamic: license-file
19
19
 
20
20
  ![Python Version](https://img.shields.io/badge/python-3.10%2B-blue.svg)
21
21
  ![Licence](https://img.shields.io/badge/license-MIT-green.svg)
22
- ![Platform](https://img.shields.io/badge/platform-Windows%20%7C%20macOS%20%7C%20Linux-lightgrey.svg)
22
+ ![Platform](https://img.shields.io/badge/platform-Windows%20%7C%20Linux%20(macOS%20source)-lightgrey.svg)
23
23
  ![Dependencies](https://img.shields.io/badge/dependencies-zero-brightgreen.svg)
24
- ![Tests](https://github.com/kianodev/flyshell/actions/workflows/build.yml/badge.svg)
24
+ ![Tests](https://github.com/kianodev/flyshell/actions/workflows/publish.yml/badge.svg)
25
25
 
26
- > Documentation last updated: v1.4.5
26
+ > Documentation last updated: v1.4.10
27
27
 
28
28
  A modular, zero-dependency command-line environment and extensible runtime interface built in Python with compiled C-extensions.
29
29
 
@@ -97,6 +97,11 @@ There are two methods to install Flyshell.
97
97
 
98
98
  ### Method 1: Install via `pip` (Best for General Use)
99
99
 
100
+ If you are using macOS, install Xcode Command Line Tools first:
101
+ ```bash
102
+ xcode-select --install
103
+ ```
104
+
100
105
  Install Flyshell through `pip`:
101
106
  ```bash
102
107
  pip install flyshell
@@ -2,11 +2,11 @@
2
2
 
3
3
  ![Python Version](https://img.shields.io/badge/python-3.10%2B-blue.svg)
4
4
  ![Licence](https://img.shields.io/badge/license-MIT-green.svg)
5
- ![Platform](https://img.shields.io/badge/platform-Windows%20%7C%20macOS%20%7C%20Linux-lightgrey.svg)
5
+ ![Platform](https://img.shields.io/badge/platform-Windows%20%7C%20Linux%20(macOS%20source)-lightgrey.svg)
6
6
  ![Dependencies](https://img.shields.io/badge/dependencies-zero-brightgreen.svg)
7
- ![Tests](https://github.com/kianodev/flyshell/actions/workflows/build.yml/badge.svg)
7
+ ![Tests](https://github.com/kianodev/flyshell/actions/workflows/publish.yml/badge.svg)
8
8
 
9
- > Documentation last updated: v1.4.5
9
+ > Documentation last updated: v1.4.10
10
10
 
11
11
  A modular, zero-dependency command-line environment and extensible runtime interface built in Python with compiled C-extensions.
12
12
 
@@ -80,6 +80,11 @@ There are two methods to install Flyshell.
80
80
 
81
81
  ### Method 1: Install via `pip` (Best for General Use)
82
82
 
83
+ If you are using macOS, install Xcode Command Line Tools first:
84
+ ```bash
85
+ xcode-select --install
86
+ ```
87
+
83
88
  Install Flyshell through `pip`:
84
89
  ```bash
85
90
  pip install flyshell
@@ -19,6 +19,10 @@ Result remove_dir(const char *path, bool force);
19
19
 
20
20
  Result remove_item(const char *path, bool recursive);
21
21
 
22
+ Result copy_item(const char *src, const char *dst, bool force);
23
+
24
+ Result move_item(const char *src, const char *dst, bool force);
25
+
22
26
  static PyObject *method_ping(PyObject *self, PyObject *args) {
23
27
  return PyLong_FromLong(42);
24
28
  }
@@ -138,6 +142,30 @@ static PyObject *method_rm(PyObject *self, PyObject *args) {
138
142
  return Py_BuildValue("(is)", (int)res, "");
139
143
  }
140
144
 
145
+ static PyObject *method_cp(PyObject *self, PyObject *args) {
146
+ const char *src;
147
+ const char *dst;
148
+ int force = 0;
149
+ if (!PyArg_ParseTuple(args, "ss|p", &src, &dst, &force)) {
150
+ return NULL;
151
+ }
152
+
153
+ Result res = copy_item(src, dst, force != 0);
154
+ return Py_BuildValue("(is)", (int)res, "");
155
+ }
156
+
157
+ static PyObject *method_mv(PyObject *self, PyObject *args) {
158
+ const char *src;
159
+ const char *dst;
160
+ int force = 0;
161
+ if (!PyArg_ParseTuple(args, "ss|p", &src, &dst, &force)) {
162
+ return NULL;
163
+ }
164
+
165
+ Result res = move_item(src, dst, force != 0);
166
+ return Py_BuildValue("(is)", (int)res, "");
167
+ }
168
+
141
169
  static PyMethodDef DiskopsMethods[] = {
142
170
  {"ping", method_ping, METH_NOARGS, "Health check ping."},
143
171
  {"cat", method_cat, METH_VARARGS, "Read file content natively."},
@@ -147,6 +175,8 @@ static PyMethodDef DiskopsMethods[] = {
147
175
  {"rename", method_rename, METH_VARARGS, "Rename file or directory natively."},
148
176
  {"rmdir", method_rmdir, METH_VARARGS, "Remove directory natively."},
149
177
  {"rm", method_rm, METH_VARARGS, "Remove file or directory natively."},
178
+ {"cp", method_cp, METH_VARARGS, "Copy file or directory natively."},
179
+ {"mv", method_mv, METH_VARARGS, "Move file or directory natively."},
150
180
  {NULL, NULL, 0, NULL},
151
181
  };
152
182
 
@@ -0,0 +1,247 @@
1
+ // c_src/diskops/posix/cp.c
2
+
3
+ #define _POSIX_C_SOURCE 200809L
4
+ #define _GNU_SOURCE
5
+
6
+ #include "../shared/native_errors.h"
7
+ #include <dirent.h>
8
+ #include <errno.h>
9
+ #include <fcntl.h>
10
+ #include <stdbool.h>
11
+ #include <stdio.h>
12
+ #include <stdlib.h>
13
+ #include <string.h>
14
+ #include <sys/stat.h>
15
+ #include <unistd.h>
16
+
17
+ #define CP_CHUNK_SIZE 32768
18
+
19
+ static bool is_subpath_posix(const char *parent, const char *child) {
20
+ char *real_parent = realpath(parent, NULL);
21
+ if (!real_parent) {
22
+ return false;
23
+ }
24
+
25
+ char *child_copy = strdup(child);
26
+ if (!child_copy) {
27
+ free(real_parent);
28
+ return false;
29
+ }
30
+
31
+ char *slash = strrchr(child_copy, '/');
32
+ char *real_child_parent = NULL;
33
+ if (slash) {
34
+ if (slash == child_copy) {
35
+ real_child_parent = realpath("/", NULL);
36
+ } else {
37
+ *slash = '\0';
38
+ real_child_parent = realpath(child_copy, NULL);
39
+ }
40
+ } else {
41
+ real_child_parent = realpath(".", NULL);
42
+ }
43
+ free(child_copy);
44
+
45
+ if (!real_child_parent) {
46
+ free(real_parent);
47
+ return false;
48
+ }
49
+
50
+ size_t plen = strlen(real_parent);
51
+ bool sub = false;
52
+ if (strcmp(real_parent, "/") == 0) {
53
+ sub = true;
54
+ } else if (strncmp(real_parent, real_child_parent, plen) == 0 &&
55
+ (real_child_parent[plen] == '\0' || real_child_parent[plen] == '/')) {
56
+ sub = true;
57
+ }
58
+
59
+ free(real_parent);
60
+ free(real_child_parent);
61
+ return sub;
62
+ }
63
+
64
+ static Result copy_file_posix(const char *src, const char *dst, const struct stat *src_st,
65
+ bool force) {
66
+ struct stat dst_st;
67
+ if (lstat(dst, &dst_st) == 0) {
68
+ if (src_st->st_dev == dst_st.st_dev && src_st->st_ino == dst_st.st_ino) {
69
+ return ERR_SAME_FILE;
70
+ }
71
+ if (S_ISDIR(dst_st.st_mode)) {
72
+ return ERR_IS_DIR;
73
+ }
74
+ if (!force) {
75
+ return ERR_EXISTS;
76
+ }
77
+ }
78
+
79
+ int src_fd = open(src, O_RDONLY);
80
+ if (src_fd < 0) {
81
+ return map_posix_errno(errno);
82
+ }
83
+
84
+ int dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, src_st->st_mode & 0777);
85
+ if (dst_fd < 0) {
86
+ int err = errno;
87
+ close(src_fd);
88
+ return map_posix_errno(err);
89
+ }
90
+
91
+ char *buf = (char *)malloc(CP_CHUNK_SIZE);
92
+ if (!buf) {
93
+ close(src_fd);
94
+ close(dst_fd);
95
+ return ERR_IO;
96
+ }
97
+
98
+ Result res = SUCCESS;
99
+ for (;;) {
100
+ ssize_t n_read = read(src_fd, buf, CP_CHUNK_SIZE);
101
+ if (n_read < 0) {
102
+ if (errno == EINTR) {
103
+ continue;
104
+ }
105
+ res = map_posix_errno(errno);
106
+ break;
107
+ }
108
+ if (n_read == 0) {
109
+ break;
110
+ }
111
+
112
+ size_t written = 0;
113
+ while (written < (size_t)n_read) {
114
+ ssize_t n_written = write(dst_fd, buf + written, (size_t)n_read - written);
115
+ if (n_written < 0) {
116
+ if (errno == EINTR) {
117
+ continue;
118
+ }
119
+ res = map_posix_errno(errno);
120
+ break;
121
+ }
122
+ written += (size_t)n_written;
123
+ }
124
+ if (res != SUCCESS) {
125
+ break;
126
+ }
127
+ }
128
+
129
+ free(buf);
130
+
131
+ if (res == SUCCESS) {
132
+ (void)fchmod(dst_fd, src_st->st_mode & 0777);
133
+ struct timespec times[2];
134
+ #ifdef __APPLE__
135
+ times[0] = src_st->st_atimespec;
136
+ times[1] = src_st->st_mtimespec;
137
+ #else
138
+ times[0] = src_st->st_atim;
139
+ times[1] = src_st->st_mtim;
140
+ #endif
141
+ (void)futimens(dst_fd, times);
142
+ }
143
+
144
+ close(src_fd);
145
+ if (close(dst_fd) != 0 && res == SUCCESS) {
146
+ res = map_posix_errno(errno);
147
+ }
148
+ return res;
149
+ }
150
+
151
+ static Result copy_dir_recursive_posix(const char *src, const char *dst, const struct stat *src_st,
152
+ bool force) {
153
+ struct stat dst_st;
154
+ if (lstat(dst, &dst_st) == 0) {
155
+ if (src_st->st_dev == dst_st.st_dev && src_st->st_ino == dst_st.st_ino) {
156
+ return ERR_SAME_FILE;
157
+ }
158
+ if (!S_ISDIR(dst_st.st_mode)) {
159
+ return ERR_NOT_DIR;
160
+ }
161
+ if (!force) {
162
+ return ERR_EXISTS;
163
+ }
164
+ } else if (mkdir(dst, src_st->st_mode & 0777) != 0) {
165
+ return map_posix_errno(errno);
166
+ }
167
+
168
+ DIR *dir = opendir(src);
169
+ if (!dir) {
170
+ return map_posix_errno(errno);
171
+ }
172
+
173
+ size_t src_len = strlen(src);
174
+ size_t dst_len = strlen(dst);
175
+ bool src_slash = (src_len > 0 && src[src_len - 1] == '/') != 0;
176
+ bool dst_slash = (dst_len > 0 && dst[dst_len - 1] == '/') != 0;
177
+
178
+ struct dirent *entry;
179
+ while ((entry = readdir(dir)) != NULL) {
180
+ if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
181
+ continue;
182
+ }
183
+
184
+ size_t name_len = strlen(entry->d_name);
185
+ size_t s_child_len = src_len + (src_slash ? 0 : 1) + name_len + 1;
186
+ size_t d_child_len = dst_len + (dst_slash ? 0 : 1) + name_len + 1;
187
+
188
+ char *s_child = (char *)malloc(s_child_len);
189
+ char *d_child = (char *)malloc(d_child_len);
190
+ if (!s_child || !d_child) {
191
+ free(s_child);
192
+ free(d_child);
193
+ closedir(dir);
194
+ return ERR_IO;
195
+ }
196
+
197
+ snprintf(s_child, s_child_len, src_slash ? "%s%s" : "%s/%s", src, entry->d_name);
198
+ snprintf(d_child, d_child_len, dst_slash ? "%s%s" : "%s/%s", dst, entry->d_name);
199
+
200
+ struct stat child_st;
201
+ if (stat(s_child, &child_st) != 0) {
202
+ int err = errno;
203
+ free(s_child);
204
+ free(d_child);
205
+ closedir(dir);
206
+ return map_posix_errno(err);
207
+ }
208
+
209
+ Result res;
210
+ if (S_ISDIR(child_st.st_mode)) {
211
+ res = copy_dir_recursive_posix(s_child, d_child, &child_st, force);
212
+ } else {
213
+ res = copy_file_posix(s_child, d_child, &child_st, force);
214
+ }
215
+
216
+ free(s_child);
217
+ free(d_child);
218
+
219
+ if (res != SUCCESS) {
220
+ closedir(dir);
221
+ return res;
222
+ }
223
+ }
224
+
225
+ closedir(dir);
226
+ return SUCCESS;
227
+ }
228
+
229
+ Result copy_item(const char *src, const char *dst, bool force) {
230
+ if (!src || src[0] == '\0' || !dst || dst[0] == '\0') {
231
+ return ERR_INVALID_PATH;
232
+ }
233
+
234
+ struct stat src_st;
235
+ if (stat(src, &src_st) != 0) {
236
+ return map_posix_errno(errno);
237
+ }
238
+
239
+ if (S_ISDIR(src_st.st_mode)) {
240
+ if (is_subpath_posix(src, dst)) {
241
+ return ERR_INVALID_PATH;
242
+ }
243
+ return copy_dir_recursive_posix(src, dst, &src_st, force);
244
+ }
245
+
246
+ return copy_file_posix(src, dst, &src_st, force);
247
+ }
@@ -0,0 +1,52 @@
1
+ // c_src/diskops/posix/mv.c
2
+
3
+ #include "../shared/native_errors.h"
4
+ #include <errno.h>
5
+ #include <stdbool.h>
6
+ #include <stdio.h>
7
+ #include <sys/stat.h>
8
+
9
+ Result copy_item(const char *src, const char *dst, bool force);
10
+ Result remove_item(const char *path, bool recursive);
11
+
12
+ Result move_item(const char *src, const char *dst, bool force) {
13
+ if (!src || src[0] == '\0' || !dst || dst[0] == '\0') {
14
+ return ERR_INVALID_PATH;
15
+ }
16
+
17
+ struct stat src_st;
18
+ if (lstat(src, &src_st) != 0) {
19
+ return map_posix_errno(errno);
20
+ }
21
+
22
+ struct stat dst_st;
23
+ if (lstat(dst, &dst_st) == 0) {
24
+ if (src_st.st_dev == dst_st.st_dev && src_st.st_ino == dst_st.st_ino) {
25
+ return ERR_SAME_FILE;
26
+ }
27
+ if (!force) {
28
+ return ERR_EXISTS;
29
+ }
30
+ if (S_ISDIR(dst_st.st_mode)) {
31
+ Result rm_res = remove_item(dst, true);
32
+ if (rm_res != SUCCESS) {
33
+ return rm_res;
34
+ }
35
+ }
36
+ }
37
+
38
+ if (rename(src, dst) == 0) {
39
+ return SUCCESS;
40
+ }
41
+
42
+ if (errno != EXDEV) {
43
+ return map_posix_errno(errno);
44
+ }
45
+
46
+ Result cp_res = copy_item(src, dst, true);
47
+ if (cp_res != SUCCESS) {
48
+ return cp_res;
49
+ }
50
+
51
+ return remove_item(src, true);
52
+ }
@@ -0,0 +1,298 @@
1
+ // c_src/diskops/win32/cp.c
2
+
3
+ #include "../shared/native_errors.h"
4
+ #include "../shared/win32_utf.h"
5
+ #include <stdbool.h>
6
+ #include <stdlib.h>
7
+ #include <string.h>
8
+ #include <windows.h>
9
+
10
+ #define CP_CHUNK_SIZE 32768
11
+
12
+ static bool is_same_file_win32(const wchar_t *wsrc, const wchar_t *wdst) {
13
+ HANDLE h1 = CreateFileW(wsrc, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
14
+ OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
15
+ if (h1 == INVALID_HANDLE_VALUE) {
16
+ return false;
17
+ }
18
+
19
+ HANDLE h2 = CreateFileW(wdst, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
20
+ OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
21
+ if (h2 == INVALID_HANDLE_VALUE) {
22
+ CloseHandle(h1);
23
+ return false;
24
+ }
25
+
26
+ BY_HANDLE_FILE_INFORMATION info1;
27
+ BY_HANDLE_FILE_INFORMATION info2;
28
+ bool same = false;
29
+ if (GetFileInformationByHandle(h1, &info1) != 0 &&
30
+ GetFileInformationByHandle(h2, &info2) != 0) {
31
+ same = (info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber &&
32
+ info1.nFileIndexHigh == info2.nFileIndexHigh &&
33
+ info1.nFileIndexLow == info2.nFileIndexLow) != 0;
34
+ }
35
+
36
+ CloseHandle(h1);
37
+ CloseHandle(h2);
38
+ return same;
39
+ }
40
+
41
+ static bool is_subpath_win32(const wchar_t *wsrc, const wchar_t *wdst) {
42
+ DWORD src_len = GetFullPathNameW(wsrc, 0, NULL, NULL);
43
+ DWORD dst_len = GetFullPathNameW(wdst, 0, NULL, NULL);
44
+ if (src_len == 0 || dst_len == 0) {
45
+ return false;
46
+ }
47
+
48
+ wchar_t *full_src = (wchar_t *)malloc((size_t)src_len * sizeof(wchar_t));
49
+ wchar_t *full_dst = (wchar_t *)malloc((size_t)dst_len * sizeof(wchar_t));
50
+ if (!full_src || !full_dst) {
51
+ free(full_src);
52
+ free(full_dst);
53
+ return false;
54
+ }
55
+
56
+ GetFullPathNameW(wsrc, src_len, full_src, NULL);
57
+ GetFullPathNameW(wdst, dst_len, full_dst, NULL);
58
+
59
+ size_t slen = wcslen(full_src);
60
+ while (slen > 3 && (full_src[slen - 1] == L'\\' || full_src[slen - 1] == L'/')) {
61
+ full_src[--slen] = L'\0';
62
+ }
63
+
64
+ bool sub = false;
65
+ if (_wcsnicmp(full_src, full_dst, slen) == 0 &&
66
+ (full_dst[slen] == L'\0' || full_dst[slen] == L'\\' || full_dst[slen] == L'/')) {
67
+ sub = true;
68
+ }
69
+
70
+ free(full_src);
71
+ free(full_dst);
72
+ return sub;
73
+ }
74
+
75
+ static Result copy_file_win32(const wchar_t *wsrc, const wchar_t *wdst, DWORD src_attrs,
76
+ bool force) {
77
+ DWORD dst_attrs = GetFileAttributesW(wdst);
78
+ if (dst_attrs != INVALID_FILE_ATTRIBUTES) {
79
+ if (is_same_file_win32(wsrc, wdst)) {
80
+ return ERR_SAME_FILE;
81
+ }
82
+ if ((dst_attrs & FILE_ATTRIBUTE_DIRECTORY) != 0) {
83
+ return ERR_IS_DIR;
84
+ }
85
+ if (!force) {
86
+ return ERR_EXISTS;
87
+ }
88
+ if ((dst_attrs & FILE_ATTRIBUTE_READONLY) != 0) {
89
+ SetFileAttributesW(wdst, dst_attrs & ~FILE_ATTRIBUTE_READONLY);
90
+ }
91
+ }
92
+
93
+ HANDLE h_src =
94
+ CreateFileW(wsrc, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
95
+ NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
96
+ if (h_src == INVALID_HANDLE_VALUE) {
97
+ return map_win32_error(GetLastError());
98
+ }
99
+
100
+ HANDLE h_dst = CreateFileW(wdst, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
101
+ src_attrs & ~FILE_ATTRIBUTE_DIRECTORY, NULL);
102
+ if (h_dst == INVALID_HANDLE_VALUE) {
103
+ DWORD err = GetLastError();
104
+ CloseHandle(h_src);
105
+ return map_win32_error(err);
106
+ }
107
+
108
+ char *buf = (char *)malloc(CP_CHUNK_SIZE);
109
+ if (!buf) {
110
+ CloseHandle(h_src);
111
+ CloseHandle(h_dst);
112
+ return ERR_IO;
113
+ }
114
+
115
+ Result res = SUCCESS;
116
+ for (;;) {
117
+ DWORD bytes_read = 0;
118
+ if (ReadFile(h_src, buf, CP_CHUNK_SIZE, &bytes_read, NULL) == 0) {
119
+ res = map_win32_error(GetLastError());
120
+ break;
121
+ }
122
+ if (bytes_read == 0) {
123
+ break;
124
+ }
125
+
126
+ DWORD total_written = 0;
127
+ while (total_written < bytes_read) {
128
+ DWORD bytes_written = 0;
129
+ if (WriteFile(h_dst, buf + total_written, bytes_read - total_written, &bytes_written,
130
+ NULL) == 0) {
131
+ res = map_win32_error(GetLastError());
132
+ break;
133
+ }
134
+ total_written += bytes_written;
135
+ }
136
+ if (res != SUCCESS) {
137
+ break;
138
+ }
139
+ }
140
+
141
+ free(buf);
142
+
143
+ if (res == SUCCESS) {
144
+ FILETIME ft_create;
145
+ FILETIME ft_access;
146
+ FILETIME ft_write;
147
+ if (GetFileTime(h_src, &ft_create, &ft_access, &ft_write) != 0) {
148
+ SetFileTime(h_dst, &ft_create, &ft_access, &ft_write);
149
+ }
150
+ }
151
+
152
+ CloseHandle(h_src);
153
+ CloseHandle(h_dst);
154
+ return res;
155
+ }
156
+
157
+ static Result copy_dir_recursive_win32(const wchar_t *wsrc, const wchar_t *wdst, bool force) {
158
+ DWORD dst_attrs = GetFileAttributesW(wdst);
159
+ if (dst_attrs != INVALID_FILE_ATTRIBUTES) {
160
+ if (is_same_file_win32(wsrc, wdst)) {
161
+ return ERR_SAME_FILE;
162
+ }
163
+ if ((dst_attrs & FILE_ATTRIBUTE_DIRECTORY) == 0) {
164
+ return ERR_NOT_DIR;
165
+ }
166
+ if (!force) {
167
+ return ERR_EXISTS;
168
+ }
169
+ } else if (CreateDirectoryW(wdst, NULL) == 0) {
170
+ return map_win32_error(GetLastError());
171
+ }
172
+
173
+ size_t slen = wcslen(wsrc);
174
+ size_t dlen = wcslen(wdst);
175
+ bool s_sep = (slen > 0 && (wsrc[slen - 1] == L'\\' || wsrc[slen - 1] == L'/')) != 0;
176
+ bool d_sep = (dlen > 0 && (wdst[dlen - 1] == L'\\' || wdst[dlen - 1] == L'/')) != 0;
177
+
178
+ size_t pat_len = slen + (s_sep ? 1 : 2) + 1;
179
+ wchar_t *wpattern = (wchar_t *)malloc(pat_len * sizeof(wchar_t));
180
+ if (!wpattern) {
181
+ return ERR_IO;
182
+ }
183
+
184
+ memcpy(wpattern, wsrc, slen * sizeof(wchar_t));
185
+ size_t ppos = slen;
186
+ if (!s_sep) {
187
+ wpattern[ppos++] = L'\\';
188
+ }
189
+ wpattern[ppos++] = L'*';
190
+ wpattern[ppos] = L'\0';
191
+
192
+ WIN32_FIND_DATAW find_data;
193
+ HANDLE h_find = FindFirstFileW(wpattern, &find_data);
194
+ DWORD find_err = GetLastError();
195
+ free(wpattern);
196
+
197
+ if (h_find == INVALID_HANDLE_VALUE) {
198
+ return map_win32_error(find_err);
199
+ }
200
+
201
+ do {
202
+ if (wcscmp(find_data.cFileName, L".") == 0 || wcscmp(find_data.cFileName, L"..") == 0) {
203
+ continue;
204
+ }
205
+
206
+ size_t nlen = wcslen(find_data.cFileName);
207
+ size_t sc_len = slen + (s_sep ? 0 : 1) + nlen + 1;
208
+ size_t dc_len = dlen + (d_sep ? 0 : 1) + nlen + 1;
209
+
210
+ wchar_t *ws_child = (wchar_t *)malloc(sc_len * sizeof(wchar_t));
211
+ wchar_t *wd_child = (wchar_t *)malloc(dc_len * sizeof(wchar_t));
212
+ if (!ws_child || !wd_child) {
213
+ free(ws_child);
214
+ free(wd_child);
215
+ FindClose(h_find);
216
+ return ERR_IO;
217
+ }
218
+
219
+ memcpy(ws_child, wsrc, slen * sizeof(wchar_t));
220
+ size_t spos = slen;
221
+ if (!s_sep) {
222
+ ws_child[spos++] = L'\\';
223
+ }
224
+ memcpy(ws_child + spos, find_data.cFileName, (nlen + 1) * sizeof(wchar_t));
225
+
226
+ memcpy(wd_child, wdst, dlen * sizeof(wchar_t));
227
+ size_t dpos = dlen;
228
+ if (!d_sep) {
229
+ wd_child[dpos++] = L'\\';
230
+ }
231
+ memcpy(wd_child + dpos, find_data.cFileName, (nlen + 1) * sizeof(wchar_t));
232
+
233
+ Result res;
234
+ if ((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
235
+ res = copy_dir_recursive_win32(ws_child, wd_child, force);
236
+ } else {
237
+ res = copy_file_win32(ws_child, wd_child, find_data.dwFileAttributes, force);
238
+ }
239
+
240
+ free(ws_child);
241
+ free(wd_child);
242
+
243
+ if (res != SUCCESS) {
244
+ FindClose(h_find);
245
+ return res;
246
+ }
247
+ } while (FindNextFileW(h_find, &find_data) != 0);
248
+
249
+ DWORD loop_err = GetLastError();
250
+ FindClose(h_find);
251
+
252
+ if (loop_err != ERROR_NO_MORE_FILES && loop_err != ERROR_SUCCESS) {
253
+ return map_win32_error(loop_err);
254
+ }
255
+
256
+ return SUCCESS;
257
+ }
258
+
259
+ Result copy_item(const char *src, const char *dst, bool force) {
260
+ if (!src || src[0] == '\0' || !dst || dst[0] == '\0') {
261
+ return ERR_INVALID_PATH;
262
+ }
263
+
264
+ wchar_t *wsrc = utf8_to_wide(src);
265
+ if (!wsrc) {
266
+ return ERR_INVALID_PATH;
267
+ }
268
+
269
+ wchar_t *wdst = utf8_to_wide(dst);
270
+ if (!wdst) {
271
+ free(wsrc);
272
+ return ERR_INVALID_PATH;
273
+ }
274
+
275
+ DWORD src_attrs = GetFileAttributesW(wsrc);
276
+ if (src_attrs == INVALID_FILE_ATTRIBUTES) {
277
+ DWORD err = GetLastError();
278
+ free(wsrc);
279
+ free(wdst);
280
+ return map_win32_error(err);
281
+ }
282
+
283
+ Result res;
284
+ if ((src_attrs & FILE_ATTRIBUTE_DIRECTORY) != 0) {
285
+ if (is_subpath_win32(wsrc, wdst)) {
286
+ free(wsrc);
287
+ free(wdst);
288
+ return ERR_INVALID_PATH;
289
+ }
290
+ res = copy_dir_recursive_win32(wsrc, wdst, force);
291
+ } else {
292
+ res = copy_file_win32(wsrc, wdst, src_attrs, force);
293
+ }
294
+
295
+ free(wsrc);
296
+ free(wdst);
297
+ return res;
298
+ }