1 /* Copyright (C) 2019 MariaDB Corporation
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License
5    as published by the Free Software Foundation; version 2 of
6    the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16    MA 02110-1301, USA. */
17 
18 #include <sys/types.h>
19 #include "SMFileSystem.h"
20 #include "SMComm.h"
21 #include "sm_exceptions.h"
22 
23 using namespace std;
24 
25 namespace idbdatafile
26 {
27 
SMFileSystem()28 SMFileSystem::SMFileSystem() : IDBFileSystem(IDBFileSystem::CLOUD)
29 {
30     SMComm::get();   // get SMComm running
31 }
32 
~SMFileSystem()33 SMFileSystem::~SMFileSystem()
34 {
35 }
36 
mkdir(const char * path)37 int SMFileSystem::mkdir(const char *path)
38 {
39     return 0;
40 }
41 
size(const char * filename) const42 off64_t SMFileSystem::size(const char *filename) const
43 {
44     struct stat _stat;
45 
46     SMComm *smComm = SMComm::get();
47     int err = smComm->stat(filename, &_stat);
48     if (err)
49         return err;
50 
51     return _stat.st_size;
52 }
53 
compressedSize(const char * filename) const54 off64_t SMFileSystem::compressedSize(const char *filename) const
55 {
56     // Yikes, punting on this one.
57     throw NotImplementedYet(__func__);
58 }
59 
remove(const char * filename)60 int SMFileSystem::remove(const char *filename)
61 {
62     SMComm *comm = SMComm::get();
63     return comm->unlink(filename);
64 }
65 
rename(const char * oldFile,const char * newFile)66 int SMFileSystem::rename(const char *oldFile, const char *newFile)
67 {
68     int err = copyFile(oldFile, newFile);
69     if (err)
70         return err;
71     err = this->remove(oldFile);
72     return err;
73 }
74 
exists(const char * filename) const75 bool SMFileSystem::exists(const char *filename) const
76 {
77     struct stat _stat;
78     SMComm *comm = SMComm::get();
79 
80     int err = comm->stat(filename, &_stat);
81     return (err == 0);
82 }
83 
listDirectory(const char * pathname,std::list<std::string> & contents) const84 int SMFileSystem::listDirectory(const char* pathname, std::list<std::string>& contents) const
85 {
86     SMComm *comm = SMComm::get();
87     return comm->listDirectory(pathname, &contents);
88 }
89 
isDir(const char * path) const90 bool SMFileSystem::isDir(const char *path) const
91 {
92     SMComm *comm = SMComm::get();
93     struct stat _stat;
94 
95     int err = comm->stat(path, &_stat);
96     if (err != 0)
97         return false;   // reasonable to throw here?  todo, look at what the other classes do.
98     return (_stat.st_mode & S_IFDIR);
99 }
100 
copyFile(const char * src,const char * dest) const101 int SMFileSystem::copyFile(const char *src, const char *dest) const
102 {
103     SMComm *comm = SMComm::get();
104     return comm->copyFile(src, dest);
105 }
106 
filesystemIsUp() const107 bool SMFileSystem::filesystemIsUp() const
108 {
109     SMComm *comm = SMComm::get();
110     return (comm->ping() == 0);
111 }
112 
filesystemSync() const113 bool SMFileSystem::filesystemSync() const
114 {
115     SMComm *comm = SMComm::get();
116     return (comm->sync() == 0);
117 }
118 }
119