FtpDataFileList.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * FtpDataFileList.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include "FtpDataStream.h"
14 #include <DateTime.h>
15 #include <SystemClock.h>
16 
18 {
19 public:
20  explicit FtpDataFileList(FtpServerConnection& connection, const String& path, bool namesOnly = false)
21  : FtpDataStream(connection), dir(connection.getFileSystem()), namesOnly(namesOnly)
22  {
23  auto fs = dir.getFileSystem();
24  assert(fs != nullptr);
25  auto& stat = const_cast<FileStat&>(dir.stat());
26  auto err = fs->stat(path, stat);
27  if(err != FS_OK) {
28  completed = true;
29  return;
30  }
31 
32  if(stat.isDir()) {
33  dir.open(path);
34  statValid = dir.next();
35  } else {
36  statValid = true;
37  }
38 
39  if(SystemClock.isSet()) {
40  year = DateTime(SystemClock.now()).Year;
41  } else {
42  debug_w("[FTP] Warning: system clock hasn't been set!");
43  }
44  }
45 
46  void transferData(TcpConnectionEvent sourceEvent) override
47  {
48  if(completed) {
49  return;
50  }
51 
52  String line;
53  while(statValid) {
54  getStatLine(dir.stat(), line);
55  line += "\r\n";
56  if(line.length() > getAvailableWriteSize()) {
57  return;
58  }
59  int written = writeString(line);
60  if(written < 0) {
61  return;
62  }
63  statValid = dir.next();
64  }
65 
66  debug_d("sent file list: %u", dir.count());
67  completed = true;
69  }
70 
71  void getStatLine(const FileStat& stat, String& line)
72  {
73  line.setLength(0);
74  if(!namesOnly) {
75  auto& user = control.getUser();
76  DateTime dt{stat.mtime};
77  String timestr = dt.format((dt.Year == year) ? _F("%b %e %H:%M") : _F("%b %e %Y"));
78  char buf[128];
79  PSTR_ARRAY(fmt, "--- %-6u %s ");
80  m_snprintf(buf, sizeof(buf), fmt, stat.size, timestr.c_str());
81  buf[0] = stat.isDir() ? 'd' : '-';
82  buf[1] = (user.role >= stat.acl.readAccess) ? 'r' : '-';
83  buf[2] = stat.attr[FileAttribute::ReadOnly] && (user.role >= stat.acl.writeAccess) ? 'w' : '-';
84  line = buf;
85  }
86  line.concat(stat.name, stat.name.length);
87  if(stat.isDir()) {
88  line += '/';
89  }
90  }
91 
92 private:
93  IFS::Directory dir;
94  uint16_t year{0};
95  bool namesOnly;
96  bool statValid{false};
97 };
Date and time class.
Definition: DateTime.h:136
uint16_t Year
Full Year number.
Definition: DateTime.h:363
Definition: FtpDataFileList.h:18
FtpDataFileList(FtpServerConnection &connection, const String &path, bool namesOnly=false)
Definition: FtpDataFileList.h:20
void transferData(TcpConnectionEvent sourceEvent) override
Definition: FtpDataFileList.h:46
void getStatLine(const FileStat &stat, String &line)
Definition: FtpDataFileList.h:71
Definition: FtpDataStream.h:41
FtpServerConnection & control
Definition: FtpDataStream.h:83
void finishTransfer()
Definition: FtpDataStream.h:58
bool completed
Definition: FtpDataStream.h:84
Definition: FtpServerConnection.h:28
const User & getUser() const
Definition: FtpServerConnection.h:53
Wrapper class for enumerating a directory.
Definition: Directory.h:32
const Stat & stat() const
Definition: Directory.h:110
bool open(const String &dirName=nullptr)
Open a directory and attach this stream object to it.
uint32_t count() const
Definition: Directory.h:95
FileSystem * getFileSystem() const
Definition: FsBase.h:63
The String class.
Definition: WString.h:137
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:617
bool concat(const String &str)
Definition: WString.h:324
bool setLength(size_t length)
set the string length accordingly, expanding if necessary
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:244
time_t now(TimeZone timeType=eTZ_Local) const
Get the current date and time.
bool isSet() const
Determine if setTime() has been called yet.
Definition: SystemClock.h:86
int writeString(const char *data, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
Writes string data directly to the TCP buffer.
Definition: TcpConnection.h:67
uint16_t getAvailableWriteSize()
Definition: TcpConnection.h:97
#define debug_d
Definition: debug_progmem.h:100
#define debug_w
Definition: debug_progmem.h:98
IFS::FileSystem * getFileSystem()
Get the currently active file system, if any.
Definition: Core/FileSystem.h:64
constexpr int FS_OK
Definition: Core/FileSystem.h:34
#define PSTR_ARRAY(name, str)
Define a flash string and load it into a named array buffer on the stack.
Definition: FakePgmSpace.h:186
#define _F(str)
Definition: FakePgmSpace.h:97
SystemClockClass SystemClock
Global instance of system clock object.
TcpConnectionEvent
Definition: TcpConnection.h:26
int m_snprintf(char *buf, int length, const char *fmt,...)
UserRole writeAccess
Definition: Access.h:37
UserRole readAccess
Definition: Access.h:36
uint16_t length
OUT: length of name.
Definition: NameBuffer.h:47
File Status structure.
Definition: Stat.h:52
bool isDir() const
Is this a directory (or mountpoint) ?
Definition: Stat.h:91
ACL acl
Access Control.
Definition: Stat.h:58
TimeStamp mtime
File modification time.
Definition: Stat.h:57
NameBuffer name
Name of file.
Definition: Stat.h:54
FileAttributes attr
Definition: Stat.h:59
file_size_t size
Size of file in bytes.
Definition: Stat.h:55