|
| 1 | +--- |
| 2 | +sidebar_position: 17 |
| 3 | +--- |
| 4 | + |
| 5 | +# SMB Module |
| 6 | + |
| 7 | +## SMB Module - samba |
| 8 | + |
| 9 | +This module will automatically process the URL using [`string.encode_uri`](./exstring.md#-url-encoding-stringencode_uristringencode_uri_component) by default. |
| 10 | + |
| 11 | +The conventional URL format for the SMB protocol is `smb://[username[:password]@]server_address[:server_port]/share[/path]`. |
| 12 | + |
| 13 | +Functions marked with 🚥 will **yield** in the [**Thread Module**](./thread.md). Before these function calls return, other **threads** may get a chance to run. |
| 14 | + |
| 15 | +### Samba Client \(**samba\.client**\) |
| 16 | + |
| 17 | +*SMB Client* is a type of [Lua *userdata*](https://www.lua.org/manual/5.3/manual.html#2.1) that contains configuration information for accessing SMB server shared folders, including username and password. |
| 18 | + |
| 19 | +#### Declaration |
| 20 | + |
| 21 | +```lua |
| 22 | +client = samba.client(workgroup, username, password) |
| 23 | +``` |
| 24 | + |
| 25 | +```lua |
| 26 | +client = samba.client { |
| 27 | + workgroup = workgroup, |
| 28 | + username = username, |
| 29 | + password = password, |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +#### Parameters and Return Values |
| 34 | + |
| 35 | +- workgroup |
| 36 | + - *string*, typically this would be `WORKGROUP` |
| 37 | +- username *string* |
| 38 | +- password *string* |
| 39 | +- client *SMB Client* |
| 40 | + |
| 41 | +#### Description |
| 42 | + |
| 43 | +All operations on this client will use the above information for authentication, eliminating the need to pass username and password through the URL. |
| 44 | + |
| 45 | +#### Example |
| 46 | + |
| 47 | +```lua title="samba.client" |
| 48 | +client = samba.client { |
| 49 | + workgroup = "WORKGROUP", |
| 50 | + username = 'guest', |
| 51 | + password = 'mypassword123' |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### Check if a Value is an SMB Client \(**samba\.is**\) |
| 56 | + |
| 57 | +#### Declaration |
| 58 | + |
| 59 | +```lua |
| 60 | +is_samba_client = samba.is(value_to_check) |
| 61 | +``` |
| 62 | + |
| 63 | +#### Parameters and Return Values |
| 64 | + |
| 65 | +- value_to_check *any type* |
| 66 | +- is_samba_client *boolean* |
| 67 | + |
| 68 | +### 🚥 List Directory Contents on SMB Server \(**samba\:list**\) |
| 69 | + |
| 70 | +#### Declaration |
| 71 | + |
| 72 | +```lua |
| 73 | +directory_contents, error_message = client:list(URL) |
| 74 | +``` |
| 75 | + |
| 76 | +#### Parameters and Return Values |
| 77 | + |
| 78 | +- client *SMB Client* |
| 79 | +- URL |
| 80 | + - *string*, directory URL on the SMB server starting with `smb://` |
| 81 | +- directory_contents |
| 82 | + - *array*, contents of the **URL** directory, where each element is a *table* containing information such as file name, file size, file type, etc. Returns `nil` when the directory cannot be accessed |
| 83 | +- error_message |
| 84 | + - *string*, returns the error description when the directory cannot be accessed |
| 85 | + |
| 86 | +#### Example |
| 87 | + |
| 88 | +```lua title="samba:list" |
| 89 | +tab, err = smbclient:list('smb://WORKGROUP/Documents/JSTColorPicker') |
| 90 | +``` |
| 91 | + |
| 92 | +#### Example Output |
| 93 | + |
| 94 | +```lua |
| 95 | +{ |
| 96 | + [1] = { |
| 97 | + modification = 1654506149.0, -- Last modification timestamp |
| 98 | + name = "TagList.sqlite-wal", -- File name |
| 99 | + path = "smb://WORKGROUP/Documents/JSTColorPicker/TagList.sqlite-wal", -- Full file path |
| 100 | + size = 74192, -- File size |
| 101 | + type = "file", -- File type, either "file" or "dir" |
| 102 | + access = 1654533327.0, -- Last access timestamp |
| 103 | + mode = 33252, |
| 104 | + creation = 1654506149.0, -- Creation timestamp |
| 105 | + }, |
| 106 | + [2] = { |
| 107 | + modification = 1656912977.0, |
| 108 | + name = "TagList.sqlite-shm", |
| 109 | + path = "smb://WORKGROUP/Documents/JSTColorPicker/TagList.sqlite-shm", |
| 110 | + size = 32768, |
| 111 | + type = "file", |
| 112 | + access = 1656912977.0, |
| 113 | + mode = 33252, |
| 114 | + creation = 1656913376.0, |
| 115 | + }, |
| 116 | + ... |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +### 🚥 Create Directory on SMB Server \(**samba\:mkdir**\) |
| 121 | + |
| 122 | +#### Declaration |
| 123 | + |
| 124 | +```lua |
| 125 | +directory_attributes, error_message = client:mkdir(URL) |
| 126 | +``` |
| 127 | + |
| 128 | +#### Parameters and Return Values |
| 129 | + |
| 130 | +- client *SMB Client* |
| 131 | +- URL |
| 132 | + - *string*, directory URL on the SMB server starting with `smb://` |
| 133 | +- directory_attributes |
| 134 | + - *table*, directory attributes of **URL**, containing information such as file name, file size, file type, etc. Returns `nil` when the directory cannot be created |
| 135 | +- error_message |
| 136 | + - *string*, returns the error description when the operation fails |
| 137 | + |
| 138 | +:::caution Limitation |
| 139 | +This function does not support recursive creation of multi-level directories. |
| 140 | +::: |
| 141 | + |
| 142 | +#### Example |
| 143 | + |
| 144 | +```lua title="samba:mkdir" |
| 145 | +tab, err = smbclient:mkdir('smb://WORKGROUP/Documents/JSTColorPicker/test_dir') |
| 146 | +``` |
| 147 | + |
| 148 | +#### Example Output |
| 149 | + |
| 150 | +```lua |
| 151 | +{ |
| 152 | + modification = 1657130592.0, |
| 153 | + name = "test_dir", |
| 154 | + path = "smb://WORKGROUP/Documents/JSTColorPicker/test_dir", |
| 155 | + size = 0, |
| 156 | + type = "dir", |
| 157 | + access = 1657130592.0, |
| 158 | + mode = 16877, |
| 159 | + creation = 1657130592.0, |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +### 🚥 Create Empty File on SMB Server \(**samba\:touch**\) |
| 164 | + |
| 165 | +#### Declaration |
| 166 | + |
| 167 | +```lua |
| 168 | +success, error_message = client:touch(URL) |
| 169 | +``` |
| 170 | + |
| 171 | +#### Parameters and Return Values |
| 172 | + |
| 173 | +- client *SMB Client* |
| 174 | +- URL |
| 175 | + - *string*, file URL on the SMB server starting with `smb://` |
| 176 | +- success *boolean* |
| 177 | +- error_message |
| 178 | + - *string*, returns the error description when the operation fails |
| 179 | + |
| 180 | +#### Example |
| 181 | + |
| 182 | +```lua title="samba:touch" |
| 183 | +ok, err = smbclient:touch('smb://WORKGROUP/Documents/JSTColorPicker/test_dir/aaa') |
| 184 | +``` |
| 185 | + |
| 186 | +### 🚥 Delete File or Empty Directory on SMB Server \(**samba\:remove**\) |
| 187 | + |
| 188 | +#### Declaration |
| 189 | + |
| 190 | +```lua |
| 191 | +success, error_message = client:remove(URL) |
| 192 | +``` |
| 193 | + |
| 194 | +#### Parameters and Return Values |
| 195 | + |
| 196 | +- client *SMB Client* |
| 197 | +- URL |
| 198 | + - *string*, file or empty directory URL on the SMB server starting with `smb://` |
| 199 | +- success *boolean* |
| 200 | +- error_message |
| 201 | + - *string*, returns the error description when the operation fails |
| 202 | + |
| 203 | +#### Description |
| 204 | + |
| 205 | +When deleting a directory, the directory must be empty. Attempting to delete a non-empty directory will result in a `directory not empty` error. |
| 206 | + |
| 207 | +#### Example |
| 208 | + |
| 209 | +```lua title="samba:remove" |
| 210 | +ok, err = smbclient:remove('smb://WORKGROUP/Documents/JSTColorPicker/test_dir') |
| 211 | +``` |
| 212 | + |
| 213 | +### 🚥 Recursively Delete Directory on SMB Server \(**samba\:rmdir**\) |
| 214 | + |
| 215 | +#### Declaration |
| 216 | + |
| 217 | +```lua |
| 218 | +success, error_message = client:rmdir(URL) |
| 219 | +``` |
| 220 | + |
| 221 | +#### Parameters and Return Values |
| 222 | + |
| 223 | +- client *SMB Client* |
| 224 | +- URL |
| 225 | + - *string*, directory URL on the SMB server starting with `smb://` |
| 226 | +- success *boolean* |
| 227 | +- error_message |
| 228 | + - *string*, returns the error description when the deletion fails |
| 229 | + |
| 230 | +#### Description |
| 231 | + |
| 232 | +Recursively deletes all files and subdirectories under the **URL** directory. |
| 233 | + |
| 234 | +#### Example |
| 235 | + |
| 236 | +```lua title="samba:rmdir" |
| 237 | +ok, err = smbclient:rmdir('smb://WORKGROUP/Documents/JSTColorPicker/test_dir') |
| 238 | +``` |
| 239 | + |
| 240 | +### 🚥 Rename File or Directory on SMB Server \(**samba\:rename**\) |
| 241 | + |
| 242 | +#### Declaration |
| 243 | + |
| 244 | +```lua |
| 245 | +success, error_message = client:rename(old_URL, new_URL) |
| 246 | +``` |
| 247 | + |
| 248 | +#### Parameters and Return Values |
| 249 | + |
| 250 | +- client *SMB Client* |
| 251 | +- old_URL, new_URL |
| 252 | + - *string*, file or directory URL on the SMB server starting with `smb://` |
| 253 | +- success *boolean* |
| 254 | +- error_message |
| 255 | + - *string*, returns the error description when the operation fails |
| 256 | + |
| 257 | +#### Description |
| 258 | + |
| 259 | +Renames (moves) the file or directory from **old_URL** to **new_URL**. |
| 260 | + |
| 261 | +#### Example |
| 262 | + |
| 263 | +```lua title="samba:rename" |
| 264 | +ok, err = smbclient:rename('smb://WORKGROUP/Documents/JSTColorPicker/.DS_Store', 'smb://WORKGROUP/Documents/JSTColorPicker/DS_Store') |
| 265 | +``` |
| 266 | + |
| 267 | +### 🚥 Copy File on SMB Server \(**samba\:copy**\) |
| 268 | + |
| 269 | +#### Declaration |
| 270 | + |
| 271 | +```lua |
| 272 | +success, error_message = client:copy(source_URL, destination_URL[, overwrite]) |
| 273 | +``` |
| 274 | + |
| 275 | +#### Parameters and Return Values |
| 276 | + |
| 277 | +- client *SMB Client* |
| 278 | +- source_URL, destination_URL |
| 279 | + - *string*, file URL on the SMB server starting with `smb://` |
| 280 | +- overwrite |
| 281 | + - *boolean*, whether to delete and then write if **destination_URL** already exists |
| 282 | +- success *boolean* |
| 283 | +- error_message |
| 284 | + - *string*, returns the error description when the operation fails |
| 285 | + |
| 286 | +#### Description |
| 287 | + |
| 288 | +Copies the file from **source_URL** to **destination_URL**. |
| 289 | + |
| 290 | +:::caution Limitations |
| 291 | + |
| 292 | +- This function only supports copying regular files, not recursive copying of directories. |
| 293 | + - **source_URL** and **destination_URL** must be file URLs, not directory URLs. |
| 294 | + - The parent directory of **destination_URL** must already exist. |
| 295 | +- This function does not currently support Server-Side Copy operations. |
| 296 | + - The current implementation downloads to local storage first, then uploads to the server. |
| 297 | + |
| 298 | +::: |
| 299 | + |
| 300 | +#### Example |
| 301 | + |
| 302 | +```lua title="samba:copy" |
| 303 | +ok, err = smbclient:copy('smb://WORKGROUP/Downloads/Archive.zip', 'smb://WORKGROUP/Downloads/Archive_1.zip', true) -- Overwrite |
| 304 | +``` |
| 305 | + |
| 306 | +### 🚥 Download File or Directory from SMB Server \(**samba\:download**\) |
| 307 | + |
| 308 | +#### Declaration |
| 309 | + |
| 310 | +```lua |
| 311 | +success, error_message = client:download(remote_URL, local_path[, progress_callback]) |
| 312 | +``` |
| 313 | + |
| 314 | +#### Parameters and Return Values |
| 315 | + |
| 316 | +- client *SMB Client* |
| 317 | +- remote_URL |
| 318 | + - *string*, file or directory URL on the SMB server starting with `smb://` |
| 319 | +- local_path |
| 320 | + - *string*, local file or directory path |
| 321 | +- progress_callback |
| 322 | + - *function*, *optional*, used to receive download progress callbacks. When the return value is `true`, the download is terminated. The parameters passed to the callback function are: |
| 323 | + - current_file_attributes *table* |
| 324 | + - current_file_transferred_bytes *integer* |
| 325 | +- success *boolean* |
| 326 | +- error_message |
| 327 | + - *string*, returns the error description when the operation fails |
| 328 | + |
| 329 | +```lua title="Current File Attributes Structure" |
| 330 | +{ |
| 331 | + modification = 1654506149.0, -- Last modification timestamp |
| 332 | + name = "TagList.sqlite-wal", -- File name |
| 333 | + path = "smb://WORKGROUP/Documents/JSTColorPicker/TagList.sqlite-wal", -- Full file path |
| 334 | + size = 74192, -- File size |
| 335 | + type = "file", -- File type, either "file" or "dir" |
| 336 | + access = 1654533327.0, -- Last access timestamp |
| 337 | + mode = 33252, |
| 338 | + creation = 1654506149.0, -- Creation timestamp |
| 339 | +} |
| 340 | +``` |
| 341 | + |
| 342 | +#### Description |
| 343 | + |
| 344 | +If **remote_URL** points to a remote directory, all files and subdirectories under the directory will be downloaded **recursively**. |
| 345 | + |
| 346 | +#### Example |
| 347 | + |
| 348 | +```lua title="samba:download" |
| 349 | +-- Local path supports both relative and absolute paths |
| 350 | +ok, err = smbclient:download('smb://WORKGROUP/Documents/JSTColorPicker', 'JSTColorPicker') |
| 351 | +``` |
| 352 | + |
| 353 | +### 🚥 Upload File or Directory to SMB Server \(**samba\:upload**\) |
| 354 | + |
| 355 | +#### Declaration |
| 356 | + |
| 357 | +```lua |
| 358 | +success, error_message = client:upload(local_path, remote_URL[, progress_callback]) |
| 359 | +``` |
| 360 | + |
| 361 | +#### Parameters and Return Values |
| 362 | + |
| 363 | +- client *SMB Client* |
| 364 | +- local_path |
| 365 | + - *string*, local file or directory path |
| 366 | +- remote_URL |
| 367 | + - *string*, file or directory URL on the SMB server starting with `smb://` |
| 368 | +- progress_callback |
| 369 | + - *function*, *optional*, used to receive upload progress callbacks. When the return value is `true`, the upload is terminated. The parameters passed to the callback function are: |
| 370 | + - current_file_attributes |
| 371 | + - *table*, refer to the **Current File Attributes Structure** in [`samba:download`](#download-file-or-directory-from-smb-server-sambadownload) |
| 372 | + - current_file_transferred_bytes *integer* |
| 373 | +- success *boolean* |
| 374 | +- error_message |
| 375 | + - *string*, returns the error description when the operation fails |
| 376 | + |
| 377 | +#### Description |
| 378 | + |
| 379 | +If **local_path** points to a local directory, all files and subdirectories under the directory will be uploaded **recursively**. |
| 380 | + |
| 381 | +#### Example |
| 382 | + |
| 383 | +```lua title="samba:upload" |
| 384 | +ok, err = smbclient:upload('plugins', 'smb://WORKGROUP/Documents/plugins') |
| 385 | +``` |
0 commit comments