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 };
int writeString(const char *data, uint8_t apiflags=TCP_WRITE_FLAG_COPY)
Writes string data directly to the TCP buffer.
Definition: TcpConnection.h:63
FileAttributes attr
Definition: Stat.h:78
Definition: FtpServerConnection.h:27
NameBuffer name
Name of file.
Definition: Stat.h:73
file_size_t size
Size of file in bytes.
Definition: Stat.h:74
#define debug_d
Definition: debug_progmem.h:108
#define _F(str)
Definition: FakePgmSpace.h:79
UserRole writeAccess
Definition: Access.h:77
The String class.
Definition: WString.h:136
const Stat & stat() const
Definition: Directory.h:148
IFS::FileSystem * getFileSystem()
TcpConnectionEvent
Definition: TcpConnection.h:26
bool isSet() const
Determine if setTime() has been called yet.
Definition: SystemClock.h:86
TimeStamp mtime
File modification time.
Definition: Stat.h:76
uint16_t length
OUT: length of name.
Definition: NameBuffer.h:85
FileSystem * getFileSystem() const
Definition: FsBase.h:81
FtpDataFileList(FtpServerConnection &connection, const String &path, bool namesOnly=false)
Definition: FtpDataFileList.h:28
SystemClockClass SystemClock
Global instance of system clock object.
bool setLength(size_t length)
set the string length accordingly, expanding if necessary
#define PSTR_ARRAY(name, str)
Define a flash string and load it into a named array buffer on the stack.
Definition: FakePgmSpace.h:181
void finishTransfer()
Definition: FtpDataStream.h:66
uint16_t getAvailableWriteSize()
Definition: TcpConnection.h:93
int m_snprintf(char *buf, int length, const char *fmt,...)
FtpServerConnection & control
Definition: FtpDataStream.h:91
uint16_t Year
Full Year number.
Definition: DateTime.h:283
void getStatLine(const FileStat &stat, String &line)
Definition: FtpDataFileList.h:79
Definition: FtpDataStream.h:40
Date and time class.
Definition: DateTime.h:79
File Status structure.
Definition: Stat.h:71
const User & getUser() const
Definition: FtpServerConnection.h:53
FtpDataStream(FtpServerConnection &control)
Definition: FtpDataStream.h:51
bool completed
Definition: FtpDataStream.h:92
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
void transferData(TcpConnectionEvent sourceEvent) override
Definition: FtpDataFileList.h:54
#define debug_w
Definition: debug_progmem.h:106
constexpr int FS_OK
Definition: Core/FileSystem.h:34
Wrapper class for enumerating a directory.
Definition: Directory.h:50
time_t now(TimeZone timeType=eTZ_Local) const
Get the current date and time.
ACL acl
Access Control.
Definition: Stat.h:77
UserRole readAccess
Definition: Access.h:76
uint32_t count() const
Definition: Directory.h:133
bool concat(const String &str)
Definition: WString.h:323
bool isDir() const
Is this a directory (or mountpoint) ?
Definition: Stat.h:110
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:243
bool open(const String &dirName=nullptr)
Open a directory and attach this stream object to it.
Definition: FtpDataFileList.h:17