CARDS 2.4.121
Package manager for the NuTyX GNU/Linux distribution
file_utils.h
1 //
2 // file_utils.h
3 //
4 // Copyright (c) 2002 by Johannes Winkelmann
5 // Copyright (c) 2013 - 2020 by NuTyX team (http://nutyx.org)
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21 //
22 
23 #ifndef FILE_UTILS_H
24 #define FILE_UTILS_H
25 
26 #include "md5.h"
27 #include "string_utils.h"
28 
29 #include <utime.h>
30 #include <dirent.h>
31 #include <sys/param.h>
32 #include <sys/file.h>
33 #include <unistd.h>
34 #include <libgen.h>
35 #include <err.h>
36 
37 #define S_CARDS_MODE 0755
38 
39 #define WS_NONE 0
40 #define WS_RECURSIVE (1 << 0)
41 #define WS_DEFAULT WS_RECURSIVE
42 #define WS_FOLLOWLINK (1 << 1) /* follow symlinks */
43 #define WS_DOTFILES (1 << 2) /* per unix convention, .file is hidden */
44 #define WS_MATCHDIRS (1 << 3) /* if pattern is used on dir names too */
45 
46 enum {
47  WALK_OK = 0,
48  WALK_BADPATTERN,
49  WALK_NAMETOOLONG,
50  WALK_BADIO
51 };
52 
53 struct InfoFile
54 {
55  std::string url;
56  std::string filename;
57  std::string dirname;
58  std::string md5sum;
59  long int filetime;
60  utimbuf acmodtime;
61  FILE *stream;
62 };
63 
64 struct DirUrl {
65  std::string Dir;
66  std::string Url;
67 };
68 
69 struct Config {
70  Config() {}
71  std::string hostname;
72  std::string database;
73  std::string username;
74  std::string password;
75  std::string socket;
76  std::string collection;
77  std::string name;
78  std::string version;
79  std::string arch;
80  std::string url;
81  std::string logdir;
82  std::vector<std::string> group;
83  std::vector<DirUrl> dirUrl;
84  std::vector<std::string> baseDir;
85  std::vector<std::string> archs;
86 };
87 
88 int getConfig(const char *fileName, Config& config);
89 void * getDatas ( void * var, FILE * file, long offset, size_t size, size_t nmemb);
90 std::string trimFileName(const std::string& filename);
91 time_t getEpochModifyTimeFile(const std::string& filename);
92 std::string getDateFromEpoch(const time_t& epoch);
93 std::string getModifyTimeFile(const std::string& filename);
94 bool checkFileExist(const std::string& filename);
95 bool checkFileEmpty(const std::string& filename);
96 bool checkRegularFile(const std::string& filename);
97 bool checkFileSignature(const std::string& filename, const std::string& signature);
98 bool checkFilesEqual(const std::string& file1, const std::string& file2);
99 bool checkPermissionsEqual(const std::string& file1, const std::string& file2);
100 bool createRecursiveDirs(const std::string& pathname);
101 void cleanupMetaFiles(const std::string& basedir);
102 void removeFile(const std::string& basedir, const std::string& filename);
103 int copyFile(const char * destFile, const char * origFile);
104 int findFile(std::set<std::string>& filesList, const std::string& basedir);
105 int findDir(itemList *filenameList, const char *path);
106 int findRecursiveFile(std::set<std::string>& filenameList, const char *filename, int spec);
107 int readFileStripSpace(itemList *fileContent, const char *fileName);
108 int readFile(itemList *fileContent, const char *fileName);
109 int parseFile(std::set<std::string>& fileContent, const char* fileName);
110 int parseFile(std::vector<std::string>& fileContent, const char* fileName);
111 int parseFile(std::string& Depends, const char* key, const char* fileName);
112 bool findMD5sum(const std::string& fileName, unsigned char* result);
113 bool checkMD5sum(const char * fileName, const char * MD5Sum);
114 /* read a file
115  return it into a template container */
116 template <class T>
117 int parseFile( T& target, const char* fileName)
118 {
119  FILE *fp = fopen (fileName, "r");
120  if (!fp)
121  return -1;
122  const int length = BUFSIZ;
123  char input[length];
124  std::string line;
125  while (fgets(input, length, fp)) {
126  input[strlen(input)-1] = '\0';
127  line = input;
128  target.push_back(line);
129  }
130  fclose(fp);
131  return 0;
132 }
133 
134 
135 #endif /* FILE_UTILS_H */
136 // vim:set ts=2 :
itemList
Definition: libcards.h:484
DirUrl
Definition: file_utils.h:64
Config
Definition: file_utils.h:69
InfoFile
Definition: file_utils.h:54