summaryrefslogtreecommitdiffstats
path: root/trial/direct_bt/dbt_utils.hpp
blob: 47e6c7dbb5125f2211e1552508902736b8303fd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2022 Gothel Software e.K.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef DBT_UTILS_HPP_
#define DBT_UTILS_HPP_

#include <cstring>
#include <string>
#include <memory>
#include <cstdint>
#include <cstdio>

#include "dbt_constants.hpp"

#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>

class file_stats {
    private:
        std::string fname_;
        bool access_, exists_, is_link_, is_file_, is_dir_;
        int errno_res_;

    public:
        file_stats(const std::string& fname, const bool use_lstat=true) noexcept
        : fname_(fname), access_(true), exists_(true),
          is_link_(false), is_file_(false), is_dir_(false),
          errno_res_(0)
        {
            struct stat s;
            int stat_res;
            if( use_lstat ) {
                stat_res = ::lstat(fname.c_str(), &s);
            } else {
                stat_res = ::stat(fname.c_str(), &s);
            }
            if( 0 != stat_res ) {
                switch( errno ) {
                    case EACCES:
                        access_ = false;
                        break;
                    case ENOENT:
                        exists_ = false;
                        break;
                    default:
                        break;
                }
                if( access_ && exists_ ) {
                    errno_res_ = errno;
                }
            } else {
                is_link_ = S_ISLNK( s.st_mode );
                is_file_ = S_ISREG( s.st_mode );
                is_dir_ = S_ISDIR( s.st_mode );
            }
        }

        int errno_res() const noexcept { return errno_res_; }
        bool ok()  const noexcept { return 0 == errno_res_; }

        bool has_access() const noexcept { return access_; }
        bool exists() const noexcept { return exists_; }

        bool is_link() const noexcept { return is_link_; }
        bool is_file() const noexcept { return is_file_; }
        bool is_dir() const noexcept { return is_dir_; }

        std::string to_string() const noexcept {
            const std::string e = 0 != errno_res_ ? ", "+std::string(::strerror(errno_res_)) : "";
            return "stat['"+fname_+"', access "+std::to_string(access_)+", exists "+std::to_string(exists_)+
                    ", link "+std::to_string(is_link_)+", file "+std::to_string(is_file_)+
                    ", dir "+std::to_string(is_dir_)+", errno "+std::to_string(errno_res_)+e+"]";
        }
};

class file_utils {
    public:
        static bool mkdir(const std::string& name) {
            file_stats fstats(name);

            if( !fstats.ok() ) {
                jau::fprintf_td(stderr, "mkdir stat failed: %s\n", fstats.to_string().c_str());
                return false;
            }
            if( fstats.is_dir() ) {
                jau::fprintf_td(stderr, "mkdir failed: %s, dir already exists\n", fstats.to_string().c_str());
                return true;
            } else if( !fstats.exists() ) {
                const int dir_err = ::mkdir(name.c_str(), S_IRWXU | S_IRWXG ); // excluding others
                if ( 0 != dir_err ) {
                    jau::fprintf_td(stderr, "mkdir failed: %s, errno %d (%s)\n", fstats.to_string().c_str(), errno, strerror(errno));
                    return false;
                } else {
                    return true;
                }
            } else {
                jau::fprintf_td(stderr, "mkdir failed: %s, exists but is no dir\n", fstats.to_string().c_str());
                return false;
            }
        }

        static std::vector<std::string> get_file_list(const std::string& dname) {
            std::vector<std::string> res;
            DIR *dir;
            struct dirent *ent;
            if( ( dir = ::opendir( dname.c_str() ) ) != nullptr ) {
              while ( ( ent = ::readdir( dir ) ) != NULL ) {
                  std::string fname( ent->d_name );
                  if( "." != fname && ".." != fname ) { // avoid '.' and '..'
                      res.push_back( dname + "/" + fname ); // full path
                  }
              }
              ::closedir (dir);
            } else {
                // could not open directory
                file_stats fstats(dname);
                jau::fprintf_td(stderr, "get_file_list failed: %s, errno %d (%s)\n",
                        fstats.to_string().c_str(), errno, strerror(errno));
            }
            return res;
        }

        /**
         *
         * @param file
         * @param recursive
         * @return true only if the file or the directory with content has been deleted, otherwise false
         */
        static bool remove(const std::string& file, const bool recursive) {
            file_stats fstats_parent(file);
            bool rm_parent = true;
            if( fstats_parent.is_dir() ) {
                std::vector<std::string> contents = get_file_list(file);
                if ( contents.size() > 0 ) {
                    for (const std::string& f : contents) {
                        file_stats fstats(f);
                        if( !fstats.ok() ) {
                            jau::fprintf_td(stderr, "remove: stat failed: %s\n", fstats.to_string().c_str());
                            return false;
                        }
                        if( !fstats.exists() ) {
                            jau::fprintf_td(stderr, "remove: listed entity not existing: %s\n", fstats.to_string().c_str());
                            // try to continue ..
                        } else if( fstats.is_link() ) {
                            jau::fprintf_td(stderr, "remove: listed entity is link (drop): %s\n", fstats.to_string().c_str());
                        } else if( fstats.is_dir() ) {
                            if( recursive ) {
                                rm_parent = remove(f, true) && rm_parent;
                            } else {
                                // can't empty contents -> can't rm 'file'
                                rm_parent = false;
                            }
                        } else if( fstats.is_file() ) {
                            const int res = ::remove( f.c_str() );
                            rm_parent = 0 == res && rm_parent;
                            if( 0 != res ) {
                                jau::fprintf_td(stderr, "remove.1 failed: %s, res %d, errno %d (%s)\n",
                                        fstats.to_string().c_str(), res, errno, strerror(errno));
                            }
                        }
                    }
                }
            }
            if( rm_parent ) {
                const int res = ::remove( file.c_str() );
                if( 0 != res ) {
                    jau::fprintf_td(stderr, "remove.2 failed: %s, res %d, errno %d (%s)\n",
                            fstats_parent.to_string().c_str(), res, errno, strerror(errno));
                }
                return 0 == res;
            }
            return false;
        }
};

class DBTUtils {
    public:

        static bool mkdirKeyFolder() {
            if( file_utils::mkdir( DBTConstants::CLIENT_KEY_PATH ) ) {
                if( file_utils::mkdir( DBTConstants::SERVER_KEY_PATH ) ) {
                    return true;
                }
            }
            return false;
        }


        static bool rmKeyFolder() {
            if( file_utils::remove( DBTConstants::CLIENT_KEY_PATH, true ) ) {
                if( file_utils::remove( DBTConstants::SERVER_KEY_PATH, true ) ) {
                    return true;
                }
            }
            return false;
        }
};

#endif /* DBT_UTILS_HPP_ */