Annotation of solidite/utils/mkdir.cpp, revision 1.1.1.1

1.1       user        1: /* Copyright (c) 2026 The Solidite Developers
                      2:  *
                      3:  * This program is free software: you can redistribute it and/or modify it under
                      4:  * the terms of the GNU Affero General Public License as published by the Free
                      5:  * Software Foundation, either version 3 of the License, or (at your option)
                      6:  * any later version.
                      7:  *
                      8:  * This program is distributed in the hope that it will be useful, but WITHOUT
                      9:  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
                     10:  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
                     11:  * details.
                     12:  *
                     13:  * You should have received a copy of the GNU Affero General Public License
                     14:  * along with this program. If not, see <https://www.gnu.org/licenses/>. */
                     15: 
                     16: #include <bitset>
                     17: #include <filesystem>
                     18: #include <iostream>
                     19: #include <locale>
                     20: #include <sstream>
                     21: #include <string>
                     22: #include <vector>
                     23: 
                     24: #include <sys/stat.h>
                     25: 
                     26: int main(int argc, char* argv[]) {
                     27:   // locale work
                     28:   std::locale::global(std::locale(""));
                     29:   std::cerr.imbue(std::locale()); 
                     30: 
                     31:   std::vector<std::string> directories;
                     32:   std::bitset<4> options = 0b00;
                     33:   /* bitset[0]=do not process arguments further (-- encountered) 
                     34:    * bitset[1]=-p flag, create intermediate directories
                     35:    * bitset[2]=ignore -m XXX for internal use to not process the mode argument
                     36:    *           as a directory to create
                     37:    * bitset[3]=-m flag is chosen to change mode*/
                     38:   uint_fast16_t mode = 777;
                     39: 
                     40:   for (int i = 1; i < argc; ++i) {
                     41:     if (options.test(2) == 1) {
                     42:       options.reset(2);
                     43:       continue;
                     44:       // ignore the next XXX argument if -m XXX
                     45:     }
                     46: 
                     47:     const std::string &ARG = argv[i];
                     48:     if (options.test(0) == 0) {
                     49:       if (ARG == "--") {
                     50:         options.set(0);
                     51:         continue;
                     52:       }
                     53: 
                     54:       if (ARG[0] == '-') {
                     55:         if (ARG.find('p') != std::string::npos)
                     56:           options.set(1);
                     57:         if (ARG.find('m') != std::string::npos) {
                     58:           if (i + 1 > argc - 1) {
                     59:             std::cerr << "octal integer required after -m\n";
                     60:             return 1;
                     61:           }
                     62:           std::istringstream(ARG) >> std::oct >> mode;
                     63:           options.set(2); // set ignore XXX argument if -m XXX
                     64:           options.set(3);
                     65:         }
                     66:       } else
                     67:         directories.push_back(ARG);
                     68:     }
                     69:   }
                     70: 
                     71:   for (std::string directory : directories) {
                     72:     if (options.test(1) == 0)
                     73:       std::filesystem::create_directory(directory);
                     74:     else
                     75:       std::filesystem::create_directories(directory);
                     76:     if (options.test(3) == 1) {
                     77:       try {
                     78:         chmod(directory.c_str(), mode);
                     79:       } catch (...) {
                     80:         std::cerr << "failed to change permissions: " + directory + '\n';
                     81:         return 1;
                     82:       }
                     83:     }
                     84:   }
                     85:   return 0;
                     86: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>