sftp_server_file.c
Go to the documentation of this file.
1 /**
2  * @file sftp_server_file.c
3  * @brief File operations
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2019-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneSSH Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL SFTP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "ssh/ssh.h"
36 #include "sftp/sftp_server.h"
37 #include "sftp/sftp_server_file.h"
38 #include "sftp/sftp_server_misc.h"
39 #include "debug.h"
40 
41 //Check SSH stack configuration
42 #if (SFTP_SERVER_SUPPORT == ENABLED)
43 
44 
45 /**
46  * @brief Canonicalize a given path name to an absolute path
47  * @param[in] session Handle referencing an SFTP session
48  * @param[in] path Path name to be canonicalized
49  * @param[out] name Name in canonical form
50  * @return Error code
51  **/
52 
54  const SshString *path, SftpName *name)
55 {
56  error_t error;
57  const char_t *p;
58  SftpServerContext *context;
59 
60  //Point to the SFTP server context
61  context = session->context;
62 
63  //Clear file attributes
64  osMemset(name, 0, sizeof(SftpName));
65 
66  //Retrieve the full pathname
67  error = sftpServerGetPath(session, path, context->path,
69  //Any error to report?
70  if(error)
71  return error;
72 
73  //Strip the root directory from the pathname
74  p = sftpServerStripRootDir(session, context->path);
75 
76  //The name structure contains the name in canonical form
77  name->filename.value = p;
78  name->filename.length = osStrlen(p);
79 
80  //Successful processing
81  return NO_ERROR;
82 }
83 
84 
85 /**
86  * @brief Retrieve file attributes
87  * @param[in] session Handle referencing an SFTP session
88  * @param[in] path Path name of the file for which status is to be returned
89  * @param[out] attributes File attributes
90  * @return Error code
91  **/
92 
94  const SshString *path, SftpFileAttrs *attributes)
95 {
96  error_t error;
97  uint_t perm;
98  FsFileStat fileStat;
99  SftpServerContext *context;
100 
101  //Point to the SFTP server context
102  context = session->context;
103 
104  //Clear file attributes
105  osMemset(attributes, 0, sizeof(SftpFileAttrs));
106 
107  //Retrieve the full pathname
108  error = sftpServerGetPath(session, path, context->path,
110  //Any error to report?
111  if(error)
112  return error;
113 
114  //Retrieve permissions for the specified file
115  perm = sftpServerGetFilePermissions(session, context->path);
116  //Insufficient access rights?
117  if((perm & SFTP_FILE_PERM_READ) == 0)
118  return ERROR_ACCESS_DENIED;
119 
120  //Retrieve the attributes of the specified file
121  error = fsGetFileStat(context->path, &fileStat);
122  //Any error to report?
123  if(error)
124  return error;
125 
126  //File type
127  if((fileStat.attributes & FS_FILE_ATTR_DIRECTORY) != 0)
128  {
130  }
131  else
132  {
134  }
135 
136  //Size of the file
137  attributes->size = fileStat.size;
138 
139  //File permissions
140  if((fileStat.attributes & FS_FILE_ATTR_READ_ONLY) != 0)
141  {
142  attributes->permissions = SFTP_MODE_IRUSR;
143  }
144  else
145  {
146  attributes->permissions = SFTP_MODE_IRUSR | SFTP_MODE_IWUSR;
147  }
148 
149  //Modification time
150  attributes->mtime = fileStat.modified;
151  attributes->atime = fileStat.modified;
152 
153  //Attribute bits
154  if((fileStat.attributes & FS_FILE_ATTR_READ_ONLY) != 0)
155  {
157  }
158 
159  if((fileStat.attributes & FS_FILE_ATTR_SYSTEM) != 0)
160  {
162  }
163 
164  if((fileStat.attributes & FS_FILE_ATTR_HIDDEN) != 0)
165  {
167  }
168 
169  if((fileStat.attributes & FS_FILE_ATTR_ARCHIVE) != 0)
170  {
172  }
173 
174  //Specify which of the attribute fields are present
177 
178  //Successful processing
179  return NO_ERROR;
180 }
181 
182 
183 /**
184  * @brief Retrieve file attributes
185  * @param[in] session Handle referencing an SFTP session
186  * @param[in] handle Opaque value that identifies the file
187  * @param[out] attributes File attributes
188  * @return Error code
189  **/
190 
192  const SshBinaryString *handle, SftpFileAttrs *attributes)
193 {
194  error_t error;
195  SshString path;
196  SftpFileObject *fileObject;
197 
198  //The SSH_FXP_OPEN request returns a handle which may be used to access
199  //the file later
200  fileObject = sftpServerFindFile(session, handle);
201 
202  //Valid handle?
203  if(fileObject != NULL)
204  {
205  //Get full path name
206  path.value = sftpServerStripRootDir(session, fileObject->path);
207  path.length = osStrlen(path.value);
208 
209  //Retrieve the attributes of the specified file
210  error = sftpServerGetFileStat(session, &path, attributes);
211  }
212  else
213  {
214  //The supplied handle is not valid
215  error = ERROR_INVALID_HANDLE;
216  }
217 
218  //Return status code
219  return error;
220 }
221 
222 
223 /**
224  * @brief Modify file attributes
225  * @param[in] session Handle referencing an SFTP session
226  * @param[in] path Path name of the file
227  * @param[in] attributes File attributes
228  * @return Error code
229  **/
230 
232  const SshString *path, const SftpFileAttrs *attributes)
233 {
234  error_t error;
235  uint_t perm;
236  SftpServerContext *context;
237 
238  //Point to the SFTP server context
239  context = session->context;
240 
241  //Retrieve the full pathname
242  error = sftpServerGetPath(session, path, context->path,
244  //Any error to report?
245  if(error)
246  return error;
247 
248  //Retrieve permissions for the specified file
249  perm = sftpServerGetFilePermissions(session, context->path);
250  //Insufficient access rights?
251  if((perm & SFTP_FILE_PERM_READ) == 0)
252  return ERROR_ACCESS_DENIED;
253 
254  //Modify file attributes
255  return NO_ERROR;
256 }
257 
258 
259 /**
260  * @brief Modify file attributes
261  * @param[in] session Handle referencing an SFTP session
262  * @param[in] handle Opaque value that identifies the file
263  * @param[in] attributes File attributes
264  * @return Error code
265  **/
266 
268  const SshBinaryString *handle, const SftpFileAttrs *attributes)
269 {
270  error_t error;
271  SftpFileObject *fileObject;
272 
273  //The SSH_FXP_OPEN request returns a handle which may be used to access
274  //the file later
275  fileObject = sftpServerFindFile(session, handle);
276 
277  //Valid handle?
278  if(fileObject != NULL)
279  {
280  //Modify file attributes
281  error = NO_ERROR;
282  }
283  else
284  {
285  //The supplied handle is not valid
286  error = ERROR_INVALID_HANDLE;
287  }
288 
289  //Return status code
290  return error;
291 }
292 
293 
294 /**
295  * @brief Rename the specified file
296  * @param[in] session Handle referencing an SFTP session
297  * @param[in] oldPath Pathname of the file to be renamed
298  * @param[in] newPath New filename
299  * @return Error code
300  **/
301 
303  const SshString *oldPath, const SshString *newPath)
304 {
305  error_t error;
306  uint_t perm;
307  char_t *path;
308  SftpServerContext *context;
309 
310  //Point to the SFTP server context
311  context = session->context;
312  //Point to the scratch buffer
313  path = (char_t *) session->buffer;
314 
315  //Retrieve the full pathname of the file to be renamed
316  error = sftpServerGetPath(session, oldPath, context->path,
318  //Any error to report?
319  if(error)
320  return error;
321 
322  //Retrieve permissions for the specified file
323  perm = sftpServerGetFilePermissions(session, context->path);
324  //Insufficient access rights?
325  if((perm & SFTP_FILE_PERM_WRITE) == 0)
326  return ERROR_ACCESS_DENIED;
327 
328  //Retrieve the full pathname of the new filename
329  error = sftpServerGetPath(session, newPath, path,
331  //Any error to report?
332  if(error)
333  return error;
334 
335  //Retrieve permissions for the specified file
336  perm = sftpServerGetFilePermissions(session, path);
337  //Insufficient access rights?
338  if((perm & SFTP_FILE_PERM_WRITE) == 0)
339  return ERROR_ACCESS_DENIED;
340 
341  //Rename the specified file
342  error = fsRenameFile(context->path, path);
343 
344  //Return status code
345  return error;
346 }
347 
348 
349 /**
350  * @brief Remove a file
351  * @param[in] session Handle referencing an SFTP session
352  * @param[in] path Pathname of the file to be removed
353  * @return Error code
354  **/
355 
357  const SshString *path)
358 {
359  error_t error;
360  uint_t perm;
361  SftpServerContext *context;
362 
363  //Point to the SFTP server context
364  context = session->context;
365 
366  //Retrieve the full pathname
367  error = sftpServerGetPath(session, path, context->path,
369  //Any error to report?
370  if(error)
371  return error;
372 
373  //Retrieve permissions for the specified file
374  perm = sftpServerGetFilePermissions(session, context->path);
375  //Insufficient access rights?
376  if((perm & SFTP_FILE_PERM_WRITE) == 0)
377  return ERROR_ACCESS_DENIED;
378 
379  //Delete the specified file
380  error = fsDeleteFile(context->path);
381 
382  //Return status code
383  return error;
384 }
385 
386 
387 /**
388  * @brief Open a file
389  * @param[in] session Handle referencing an SFTP session
390  * @param[in] path Path name of the file
391  * @param[in] pflags Bitmask that specifies the desired access mode
392  * @param[in] attributes Initial attributes for the file
393  * @param[out] handle Opaque value that identifies the file
394  * @return Error code
395  **/
396 
398  uint32_t pflags, const SftpFileAttrs *attributes, uint32_t *handle)
399 {
400  error_t error;
401  uint_t i;
402  uint_t perm;
403  uint_t mode;
404  SftpServerContext *context;
405  SftpFileObject *fileObject;
406  FsFileStat fileStat;
407 
408  //Point to the SFTP server context
409  context = session->context;
410 
411  //Retrieve the full pathname
412  error = sftpServerGetPath(session, path, context->path,
414  //Any error to report?
415  if(error)
416  return error;
417 
418  //Retrieve permissions for the specified file
419  perm = sftpServerGetFilePermissions(session, context->path);
420 
421  //Check if the file is opened for reading or writing
422  if((pflags & SSH_FXF_WRITE) != 0)
423  {
424  //Insufficient access rights?
425  if((perm & SFTP_FILE_PERM_WRITE) == 0)
426  return ERROR_ACCESS_DENIED;
427 
428  //Just for sanity
429  fileStat.size = 0;
430  }
431  else
432  {
433  //Insufficient access rights?
434  if((perm & SFTP_FILE_PERM_READ) == 0)
435  return ERROR_ACCESS_DENIED;
436 
437  //Retrieve the attributes of the specified file
438  error = fsGetFileStat(context->path, &fileStat);
439  //Any error to report?
440  if(error)
441  return error;
442 
443  //Check file type
444  if((fileStat.attributes & FS_FILE_ATTR_DIRECTORY) != 0)
445  return ERROR_FILE_NOT_FOUND;
446  }
447 
448  //Loop through file objects
449  for(i = 0; i < context->numFileObjects; i++)
450  {
451  //Point to the current file object
452  fileObject = &context->fileObjects[i];
453 
454  //Unused file object?
455  if(fileObject->type == SSH_FILEXFER_TYPE_INVALID)
456  {
457  break;
458  }
459  }
460 
461  //Any file object available for use?
462  if(i < context->numFileObjects)
463  {
464  //The 'pflags' field is a bitmask that specifies the desired access mode
465  if((pflags & SSH_FXF_WRITE) != 0)
466  {
467  mode = FS_FILE_MODE_WRITE;
468  }
469  else
470  {
471  mode = FS_FILE_MODE_READ;
472  }
473 
474  if((pflags & SSH_FXF_CREAT) != 0)
475  {
476  mode |= FS_FILE_MODE_CREATE;
477  }
478 
479  if((pflags & SSH_FXF_TRUNC) != 0)
480  {
481  mode |= FS_FILE_MODE_TRUNC;
482  }
483 
484  //Open the specified file
485  fileObject->file = fsOpenFile(context->path, mode);
486 
487  //Valid handle?
488  if(fileObject->file != NULL)
489  {
490  //Initialize file object
491  fileObject->type = SSH_FILEXFER_TYPE_REGULAR;
492  fileObject->session = session;
493  fileObject->size = fileStat.size;
494  fileObject->offset = 0;
495  fileObject->dir = NULL;
496 
497  //Save path name
498  osStrcpy(fileObject->path, context->path);
499 
500  //Generate a unique handle
501  fileObject->handle = sftpServerGenerateHandle(session);
502 
503  //Check if the file is opened for appending
504  if((pflags & SSH_FXF_APPEND) != 0)
505  {
506  //Move file pointer position to the end of the file
507  error = fsSeekFile(fileObject->file, 0, FS_SEEK_END);
508  }
509  else
510  {
511  //Move file pointer position to the beginning of the file
512  error = NO_ERROR;
513  }
514 
515  //Check status code
516  if(!error)
517  {
518  //The SSH_FXP_OPEN request returns a handle which may be used to
519  //access the file later
520  *handle = fileObject->handle;
521  }
522  else
523  {
524  //Clean up side effects
525  fsCloseFile(fileObject->file);
526  fileObject->file = NULL;
527 
528  //Mark the entry as free
529  fileObject->type = SSH_FILEXFER_TYPE_INVALID;
530  }
531  }
532  else
533  {
534  //The specified path name does not exist
535  error = ERROR_OPEN_FAILED;
536  }
537  }
538  else
539  {
540  //The file object table runs out of space
541  error = ERROR_OUT_OF_RESOURCES;
542  }
543 
544  //Return status code
545  return error;
546 }
547 
548 
549 /**
550  * @brief Write the specified file
551  * @param[in] session Handle referencing an SFTP session
552  * @param[in] handle Opaque value that identifies the file
553  * @param[in] offset Offset from the beginning of the file where to start
554  * writing
555  * @param[in] data Data to be written
556  * @param[in] fragLen Number of bytes available on hand
557  * @param[in] totalLen Total length of the data, in bytes
558  * @return Error code
559  **/
560 
562  const SshBinaryString *handle, uint64_t offset, const uint8_t *data,
563  uint32_t fragLen, uint32_t totalLen)
564 {
565  error_t error;
566  SftpFileObject *fileObject;
567 
568  //Initialize status code
569  error = NO_ERROR;
570 
571  //Save the length of the payload data
572  session->dataLen = totalLen;
573  session->bufferLen = MIN(totalLen, SFTP_SERVER_BUFFER_SIZE);
574  session->bufferPos = fragLen;
575 
576  //The SSH_FXP_OPEN request returns a handle which may be used to access
577  //the file later
578  fileObject = sftpServerFindFile(session, handle);
579 
580  //Valid handle?
581  if(fileObject != NULL)
582  {
583  //Check file offset
584  if(offset != fileObject->offset)
585  {
586  //Move the file pointer position to the specified offset
587  error = fsSeekFile(fileObject->file, (int_t) offset, FS_SEEK_SET);
588  }
589 
590  //Check status code
591  if(!error)
592  {
593  //Set up data transfer
594  session->file = fileObject->file;
595  fileObject->offset = offset + session->dataLen;
596  }
597  }
598  else
599  {
600  //The supplied handle is not valid
601  error = ERROR_INVALID_HANDLE;
602  }
603 
604  //Move the data to the beginning of the buffer
605  osMemmove(session->buffer, data, fragLen);
606 
607  //Save the status of the write operation
608  session->requestStatus = error;
609  //Consume the payload data before returning the SSH_FXP_STATUS response
611 
612  //Successful processing
613  return NO_ERROR;
614 }
615 
616 
617 /**
618  * @brief Write data to the specified file
619  * @param[in] session Handle referencing an SFTP session
620  * @return Error code
621  **/
622 
624 {
625  //Any data pending in the buffer?
626  if(session->bufferLen > 0)
627  {
628  //Check the status of the write operation
629  if(session->requestStatus == NO_ERROR)
630  {
631  //Write data to the specified file
632  session->requestStatus = fsWriteFile(session->file, session->buffer,
633  session->bufferLen);
634  }
635 
636  //Number of bytes left to process
637  session->dataLen -= session->bufferLen;
638 
639  //Set up next data transfer
640  session->bufferLen = MIN(session->dataLen, SFTP_SERVER_BUFFER_SIZE);
641  session->bufferPos = 0;
642  }
643 
644  //Consume the payload data before returning the SSH_FXP_STATUS response
645  return NO_ERROR;
646 }
647 
648 
649 /**
650  * @brief Read the specified file
651  * @param[in] session Handle referencing an SFTP session
652  * @param[in] handle Opaque value that identifies the file
653  * @param[in] offset Offset relative to the beginning of the file from where
654  * to start reading
655  * @param[in,out] length Maximum number of bytes to read (input value) and
656  * actual length of the data (output value)
657  * @return Error code
658  **/
659 
661  const SshBinaryString *handle, uint64_t offset, uint32_t *length)
662 {
663  error_t error;
664  uint64_t n;
665  SftpFileObject *fileObject;
666 
667  //Initialize status code
668  error = NO_ERROR;
669 
670  //The SSH_FXP_OPEN request returns a handle which may be used to access
671  //the file later
672  fileObject = sftpServerFindFile(session, handle);
673 
674  //Valid handle?
675  if(fileObject != NULL)
676  {
677  //Sanity check
678  if(offset < fileObject->size)
679  {
680  //Check file offset
681  if(offset != fileObject->offset)
682  {
683  //Move the file pointer position to the specified offset
684  error = fsSeekFile(fileObject->file, (int_t) offset, FS_SEEK_SET);
685  }
686 
687  //Check status code
688  if(!error)
689  {
690  //The server reads as many bytes as it can from the file, up to
691  //the specified length
692  n = MIN(*length, fileObject->size - offset);
693 
694  //Set up data transfer
695  session->file = fileObject->file;
696  session->dataLen = (size_t) n;
697  fileObject->offset = offset + session->dataLen;
698 
699  //Return the actual length of the payload data
700  *length = session->dataLen;
701  }
702  else
703  {
704  //Terminate the data transfer
705  error = ERROR_END_OF_STREAM;
706  }
707  }
708  else
709  {
710  //The supplied offset is not valid
711  error = ERROR_END_OF_STREAM;
712  }
713  }
714  else
715  {
716  //The supplied handle is not valid
717  error = ERROR_INVALID_HANDLE;
718  }
719 
720  //Return status code
721  return error;
722 }
723 
724 
725 /**
726  * @brief Read data from the specified file
727  * @param[in] session Handle referencing an SFTP session
728  * @return error
729  **/
730 
732 {
733  error_t error;
734  size_t n;
735  size_t length;
736 
737  //Read an integral number of blocks
738  length = SFTP_SERVER_BUFFER_SIZE - session->bufferLen;
739  length = length - (length % 512);
740 
741  //Limit the number of bytes to read at a time
742  length = MIN(length, session->dataLen);
743 
744  //Read data from the specified file
745  error = fsReadFile(session->file, session->buffer + session->bufferLen,
746  length, &n);
747 
748  //Check status code
749  if(!error)
750  {
751  //Sanity check
752  if(n == length)
753  {
754  //Number of bytes left to process
755  session->dataLen -= n;
756 
757  //Update the length of the buffer
758  session->bufferLen += n;
759  session->bufferPos = 0;
760  }
761  else
762  {
763  //Report an error
764  error = ERROR_READ_FAILED;
765  }
766  }
767 
768  //Return status code
769  return error;
770 }
771 
772 
773 /**
774  * @brief Close a file
775  * @param[in] session Handle referencing an SFTP session
776  * @param[in] handle File handle returned by SSH_FXP_OPEN
777  * @return Error code
778  **/
779 
781  const SshBinaryString *handle)
782 {
783  error_t error;
784  SftpFileObject *fileObject;
785 
786  //The SSH_FXP_OPEN request returns a handle which may be used to access
787  //the file later
788  fileObject = sftpServerFindFile(session, handle);
789 
790  //Any matching directory?
791  if(fileObject != NULL)
792  {
793  //Close file
794  fsCloseFile(fileObject->file);
795  fileObject->file = NULL;
796 
797  //Mark the entry as free
798  fileObject->type = SSH_FILEXFER_TYPE_INVALID;
799 
800  //Successful processing
801  error = NO_ERROR;
802  }
803  else
804  {
805  //The supplied handle is not valid
806  error = ERROR_INVALID_HANDLE;
807  }
808 
809  //Return status code
810  return error;
811 }
812 
813 
814 /**
815  * @brief Find the file that matches a given handle
816  * @param[in] session Handle referencing an SFTP session
817  * @param[in] handle Opaque variable-length string
818  * @return Pointer to the matching file object
819  **/
820 
822  const SshBinaryString *handle)
823 {
824  uint_t i;
825  SftpServerContext *context;
826  SftpFileObject *fileObject;
827 
828  //Point to the SFTP server context
829  context = session->context;
830 
831  //Valid handle?
832  if(handle->length == sizeof(uint32_t))
833  {
834  //Loop through file objects
835  for(i = 0; i < context->numFileObjects; i++)
836  {
837  //Point to the current file object
838  fileObject = &context->fileObjects[i];
839 
840  //The handle can identify a file or a directory
841  if(fileObject->type == SSH_FILEXFER_TYPE_REGULAR &&
842  fileObject->session == session &&
843  fileObject->handle == LOAD32BE(handle->value))
844  {
845  //The handle matches the current file object
846  return fileObject;
847  }
848  }
849  }
850 
851  //The handle does not match any active file object
852  return NULL;
853 }
854 
855 #endif
signed int int_t
Definition: compiler_port.h:49
unsigned int uint_t
Definition: compiler_port.h:50
char char_t
Definition: compiler_port.h:48
#define LOAD32BE(p)
Definition: cpu_endian.h:210
Debugging facilities.
uint8_t n
error_t
Error codes.
Definition: error.h:43
@ ERROR_FILE_NOT_FOUND
Definition: error.h:156
@ ERROR_OUT_OF_RESOURCES
Definition: error.h:64
@ ERROR_INVALID_HANDLE
Definition: error.h:280
@ ERROR_END_OF_STREAM
Definition: error.h:210
@ ERROR_ACCESS_DENIED
Definition: error.h:148
@ ERROR_OPEN_FAILED
Definition: error.h:75
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_READ_FAILED
Definition: error.h:222
uint8_t data[]
Definition: ethernet.h:222
@ FS_FILE_MODE_CREATE
Definition: fs_port.h:74
@ FS_FILE_MODE_READ
Definition: fs_port.h:72
@ FS_FILE_MODE_TRUNC
Definition: fs_port.h:75
@ FS_FILE_MODE_WRITE
Definition: fs_port.h:73
@ FS_FILE_ATTR_HIDDEN
Definition: fs_port.h:58
@ FS_FILE_ATTR_READ_ONLY
Definition: fs_port.h:57
@ FS_FILE_ATTR_DIRECTORY
Definition: fs_port.h:61
@ FS_FILE_ATTR_ARCHIVE
Definition: fs_port.h:62
@ FS_FILE_ATTR_SYSTEM
Definition: fs_port.h:59
@ FS_SEEK_END
Definition: fs_port.h:87
@ FS_SEEK_SET
Definition: fs_port.h:85
error_t fsSeekFile(FsFile *file, int_t offset, uint_t origin)
Move to specified position in file.
error_t fsDeleteFile(const char_t *path)
Delete a file.
void fsCloseFile(FsFile *file)
Close a file.
error_t fsRenameFile(const char_t *oldPath, const char_t *newPath)
Rename the specified file.
error_t fsGetFileStat(const char_t *path, FsFileStat *fileStat)
Retrieve the attributes of the specified file.
error_t fsWriteFile(FsFile *file, void *data, size_t length)
Write data to the specified file.
FsFile * fsOpenFile(const char_t *path, uint_t mode)
Open the specified file for reading or writing.
error_t fsReadFile(FsFile *file, void *data, size_t size, size_t *length)
Read data from the specified file.
uint8_t p
Definition: ndp.h:300
#define osMemset(p, value, length)
Definition: os_port.h:135
#define osMemmove(dest, src, length)
Definition: os_port.h:147
#define MIN(a, b)
Definition: os_port.h:63
#define osStrlen(s)
Definition: os_port.h:165
#define osStrcpy(s1, s2)
Definition: os_port.h:207
uint8_t attributes[]
Definition: radius.h:88
char_t name[]
@ SSH_FILEXFER_TYPE_INVALID
Definition: sftp_common.h:189
@ SSH_FILEXFER_TYPE_DIRECTORY
Definition: sftp_common.h:191
@ SSH_FILEXFER_TYPE_REGULAR
Definition: sftp_common.h:190
#define SFTP_MODE_IRUSR
Definition: sftp_common.h:93
#define SSH_FXF_APPEND
Definition: sftp_common.h:44
#define SSH_FXF_WRITE
Definition: sftp_common.h:43
#define SSH_FILEXFER_ATTR_SIZE
Definition: sftp_common.h:50
#define SSH_FILEXFER_ATTR_PERMISSIONS
Definition: sftp_common.h:52
#define SSH_FILEXFER_ATTR_FLAGS_SYSTEM
Definition: sftp_common.h:70
#define SSH_FILEXFER_ATTR_ACMODTIME
Definition: sftp_common.h:53
#define SSH_FILEXFER_ATTR_FLAGS_READONLY
Definition: sftp_common.h:69
#define SSH_FXF_TRUNC
Definition: sftp_common.h:46
#define SSH_FILEXFER_ATTR_FLAGS_ARCHIVE
Definition: sftp_common.h:73
#define SSH_FXF_CREAT
Definition: sftp_common.h:45
#define SFTP_MODE_IWUSR
Definition: sftp_common.h:92
#define SSH_FILEXFER_ATTR_FLAGS_HIDDEN
Definition: sftp_common.h:71
SFTP server.
#define SFTP_SERVER_MAX_PATH_LEN
Definition: sftp_server.h:109
@ SFTP_FILE_PERM_READ
Definition: sftp_server.h:146
@ SFTP_FILE_PERM_WRITE
Definition: sftp_server.h:147
#define SftpServerSession
Definition: sftp_server.h:120
#define SftpServerContext
Definition: sftp_server.h:116
@ SFTP_SERVER_SESSION_STATE_RECEIVING_DATA
Definition: sftp_server.h:160
#define SFTP_SERVER_BUFFER_SIZE
Definition: sftp_server.h:88
error_t sftpServerReadData(SftpServerSession *session)
Read data from the specified file.
error_t sftpServerWriteFile(SftpServerSession *session, const SshBinaryString *handle, uint64_t offset, const uint8_t *data, uint32_t fragLen, uint32_t totalLen)
Write the specified file.
error_t sftpServerSetFileStatEx(SftpServerSession *session, const SshBinaryString *handle, const SftpFileAttrs *attributes)
Modify file attributes.
error_t sftpServerGetFileStat(SftpServerSession *session, const SshString *path, SftpFileAttrs *attributes)
Retrieve file attributes.
error_t sftpServerGetRealPath(SftpServerSession *session, const SshString *path, SftpName *name)
Canonicalize a given path name to an absolute path.
error_t sftpServerCloseFile(SftpServerSession *session, const SshBinaryString *handle)
Close a file.
error_t sftpServerRenameFile(SftpServerSession *session, const SshString *oldPath, const SshString *newPath)
Rename the specified file.
error_t sftpServerOpenFile(SftpServerSession *session, const SshString *path, uint32_t pflags, const SftpFileAttrs *attributes, uint32_t *handle)
Open a file.
error_t sftpServerRemoveFile(SftpServerSession *session, const SshString *path)
Remove a file.
error_t sftpServerReadFile(SftpServerSession *session, const SshBinaryString *handle, uint64_t offset, uint32_t *length)
Read the specified file.
SftpFileObject * sftpServerFindFile(SftpServerSession *session, const SshBinaryString *handle)
Find the file that matches a given handle.
error_t sftpServerGetFileStatEx(SftpServerSession *session, const SshBinaryString *handle, SftpFileAttrs *attributes)
Retrieve file attributes.
error_t sftpServerWriteData(SftpServerSession *session)
Write data to the specified file.
error_t sftpServerSetFileStat(SftpServerSession *session, const SshString *path, const SftpFileAttrs *attributes)
Modify file attributes.
File operations.
uint_t sftpServerGetFilePermissions(SftpServerSession *session, const char_t *path)
Get permissions for the specified file or directory.
uint32_t sftpServerGenerateHandle(SftpServerSession *session)
Generate a unique handle.
const char_t * sftpServerStripRootDir(SftpServerSession *session, const char_t *path)
Strip root dir from specified pathname.
error_t sftpServerGetPath(SftpServerSession *session, const SshString *path, char_t *fullPath, size_t maxLen)
Retrieve the full pathname.
Helper functions for SFTP server.
Secure Shell (SSH)
File status.
Definition: fs_port.h:96
uint32_t attributes
Definition: fs_port.h:97
uint32_t size
Definition: fs_port.h:98
DateTime modified
Definition: fs_port.h:99
File attributes.
Definition: sftp_common.h:247
File or directory object.
Definition: sftp_server.h:186
FsFile * file
File pointer.
Definition: sftp_server.h:193
uint64_t offset
Offset within the file.
Definition: sftp_server.h:192
SftpFileType type
File type.
Definition: sftp_server.h:187
FsDir * dir
Directory pointer.
Definition: sftp_server.h:194
uint32_t handle
Opaque value that identifies the file.
Definition: sftp_server.h:190
SftpServerSession * session
Pointer to the SFTP session.
Definition: sftp_server.h:188
char_t path[SFTP_SERVER_MAX_PATH_LEN+1]
Path name.
Definition: sftp_server.h:189
uint64_t size
Size of the file.
Definition: sftp_server.h:191
Name structure.
Definition: sftp_common.h:265
Binary string.
Definition: ssh_types.h:67
const uint8_t * value
Definition: ssh_types.h:68
size_t length
Definition: ssh_types.h:69
String.
Definition: ssh_types.h:56
const char_t * value
Definition: ssh_types.h:57
size_t length
Definition: ssh_types.h:58
uint8_t length
Definition: tcp.h:368