SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Shell_Fs.cc
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
9 #include "swcdb/fs/Interface.h"
10 #include <iomanip>
11 
12 
13 namespace SWC { namespace Utils { namespace shell {
14 
16  : Interface("\033[32mSWC-DB(\033[36mfs-" +
17  Env::Config::settings()->get_str("swc.fs") +
18  "\033[32m)\033[33m> \033[00m",
19  "/tmp/.swc-cli-fs-" +
20  Env::Config::settings()->get_str("swc.fs") + "-history",
21  CLI::FILESYSTEM) {
22 
25  FS::fs_type(Env::Config::settings()->get_str("swc.fs"))
26  );
27 
28  add_option(
29  "list",
30  {"list directory contents",
31  "list 'path';"},
32  [ptr=this](const std::string& cmd){ return ptr->ls(cmd); },
33  "(?i)^(ls|list)"
34  );
35 }
36 
39  #if defined(SWC_ENABLE_SANITIZER)
40  std::this_thread::sleep_for(std::chrono::seconds(2));
42  #endif
43 }
44 
45 bool Fs::ls(const std::string& cmd) {
46 
47  std::string path;
48  RE2::FullMatch(cmd, "(?i)^(ls|list)\\s+(.*)$", nullptr, &path);
49  if(path.front() == '"' || path.front() == '\'')
50  path = path.substr(1, path.size()-2);
51 
52  SWC_PRINT << "Listing path='" << path << "':" << SWC_PRINT_CLOSE;
53  FS::DirentList entries;
54  Env::FsInterface::fs()->readdir(err, path, entries);
55  if(err)
56  return error("Problem reading '"+path+"'");
57 
58  size_t lname = 0;
61  dirs.reserve(entries.size());
62  files.reserve(entries.size());
63  for(auto& entry : entries) {
64  if(entry.name.size() > lname)
65  lname = entry.name.size();
66 
67  auto& tmp = entry.is_dir ? dirs : files;
68  for(auto it = tmp.cbegin(); ; ++it) {
69  if(it == tmp.cend() ||
71  reinterpret_cast<const uint8_t*>((*it)->name.c_str()),
72  (*it)->name.size(),
73  reinterpret_cast<const uint8_t*>(entry.name.c_str()),
74  entry.name.size() ) ) {
75  tmp.insert(it, &entry);
76  break;
77  }
78  }
79  }
80 
81  time_t t_secs;
82  char modified[20];
83 
84  SWC_PRINT << "Directories=" << dirs.size() << ":\n";
85  for(auto& entry : dirs) {
86  t_secs = entry->last_modification_time;
87  std::strftime(modified, 20, "%Y/%m/%d %H:%M:%S", std::gmtime(&t_secs));
88 
89  SWC_LOG_OSTREAM << std::left << " "
90  << std::left << std::setw(lname + 2)
91  << format("'%s'", entry->name.c_str())
92  << std::left << " modified=" << modified
93  << '\n';
94  }
95 
96  SWC_LOG_OSTREAM << "Files=" << files.size() << ":\n";
97  for(auto& entry : files) {
98  t_secs = entry->last_modification_time;
99  std::strftime(modified, 20, "%Y/%m/%d %H:%M:%S", std::gmtime(&t_secs));
100 
101  SWC_LOG_OSTREAM << std::left << " "
102  << std::left << std::setw(lname + 2)
103  << format("'%s'", entry->name.c_str())
104  << std::left << " modified=" << modified
105  << " size=" << entry->length
106  << '\n';
107  }
108 
110  return true;
111 }
112 
113 
114 
115 }}} // namespace Utils::shell
SWC::Env::FsInterface::init
static void init(const SWC::Config::Settings::Ptr &settings, FS::Type typ)
Definition: Interface.cc:513
SWC::Utils::shell::Fs::ls
bool ls(const std::string &cmd)
Definition: Shell_Fs.cc:45
SWC::Utils::shell::Interface::add_option
void add_option(const char *a_name, Core::Vector< std::string > &&a_desc, OptCall_t &&a_call, const char *a_re)
Definition: Shell.cc:249
SWC_LOG_OSTREAM
#define SWC_LOG_OSTREAM
Definition: Logger.h:44
SWC::FS::fs_type
Type fs_type(const std::string &fs_name)
Definition: FileSystem.cc:19
SWC::Env::Config::settings
static SWC::Config::Settings::Ptr & settings()
Definition: Settings.h:128
SWC::Env::FsInterface::interface
static SWC_CAN_INLINE FS::Interface::Ptr & interface() noexcept
Definition: Interface.h:150
SWC::Condition::lt_volume
SWC_CAN_INLINE bool lt_volume(const uint8_t *p1, uint32_t p1_len, const uint8_t *p2, uint32_t p2_len) noexcept
Definition: Comparators.h:222
SWC::Utils::shell::Interface::err
int err
Definition: Shell.h:54
SWC::Utils::shell::CLI
CLI
Definition: Shell.h:28
SWC::Utils::shell::Interface::error
bool error(const std::string &message)
Definition: Shell.cc:345
SWC_PRINT
#define SWC_PRINT
Definition: Logger.h:167
SWC_PRINT_CLOSE
#define SWC_PRINT_CLOSE
Definition: Logger.h:171
SWC::Utils::shell::Interface
Definition: Shell.h:41
SWC::Utils::shell::Fs::~Fs
virtual ~Fs()
Definition: Shell_Fs.cc:37
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Utils::shell::FILESYSTEM
@ FILESYSTEM
Definition: Shell.h:33
SWC::Env::FsInterface::fs
static SWC_CAN_INLINE FS::FileSystem::Ptr & fs() noexcept
Definition: Interface.h:155
Comparators.h
Shell_Fs.h
SWC::Utils::shell::Fs::Fs
Fs()
Definition: Shell_Fs.cc:15
SWC::format
std::string format(const char *fmt,...) __attribute__((format(printf
Definition: String.cc:17
SWC::Core::Vector< Dirent >
SWC::Core::Vector::size
constexpr SWC_CAN_INLINE size_type size() const noexcept
Definition: Vector.h:189
Interface.h
SWC::Core::Vector::insert
SWC_CAN_INLINE iterator insert(size_type offset, ArgsT &&... args)
Definition: Vector.h:367
SWC::Env::FsInterface::reset
static void reset() noexcept
Definition: Interface.h:159
SWC::Core::Vector::reserve
SWC_CAN_INLINE void reserve(size_type cap)
Definition: Vector.h:288