CARDS 2.4.87
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-2015 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 logdir;
81  std::vector<std::string> locale;
82  std::vector<DirUrl> dirUrl;
83  std::vector<std::string> baseDir;
84  std::vector<std::string> archs;
85 };
86 
87 int getConfig(const char *fileName, Config& config);
88 void * getDatas ( void * var, FILE * file, long offset, size_t size, size_t nmemb);
89 std::string trimFileName(const std::string& filename);
90 time_t getEpochModifyTimeFile(const std::string& filename);
91 std::string getDateFromEpoch(const time_t& epoch);
92 std::string getModifyTimeFile(const std::string& filename);
93 bool checkFileExist(const std::string& filename);
94 bool checkFileEmpty(const std::string& filename);
95 bool checkRegularFile(const std::string& filename);
96 bool checkFileSignature(const std::string& filename, const std::string& signature);
97 bool checkFilesEqual(const std::string& file1, const std::string& file2);
98 bool checkPermissionsEqual(const std::string& file1, const std::string& file2);
99 bool createRecursiveDirs(const std::string& pathname);
100 void cleanupMetaFiles(const std::string& basedir);
101 void removeFile(const std::string& basedir, const std::string& filename);
102 int copyFile(const char * destFile, const char * origFile);
103 int findFile(std::set<std::string>& filesList, const std::string& basedir);
104 int findDir(itemList *filenameList, const char *path);
105 int findRecursiveFile(std::set<std::string>& filenameList, const char *filename, int spec);
106 int readFileStripSpace(itemList *fileContent, const char *fileName);
107 int readFile(itemList *fileContent, const char *fileName);
108 int parseFile(std::set<std::string>& fileContent, const char* fileName);
109 int parseFile(std::vector<std::string>& fileContent, const char* fileName);
110 bool findMD5sum(const std::string& fileName, unsigned char* result);
111 bool checkMD5sum(const char * fileName, const char * MD5Sum);
112 /* read a file
113  return it into a template container */
114 template <class T>
115 int parseFile( T& target, const char* fileName)
116 {
117  FILE *fp = fopen (fileName, "r");
118  if (!fp)
119  return -1;
120  const int length = BUFSIZ;
121  char input[length];
122  std::string line;
123  while (fgets(input, length, fp)) {
124  input[strlen(input)-1] = '\0';
125  line = input;
126  target.push_back(line);
127  }
128  fclose(fp);
129  return 0;
130 }
131 
132 
133 #endif /* FILE_UTILS_H */
134 // vim:set ts=2 :
Definition: libcards.h:481
Definition: file_utils.h:64
Definition: file_utils.h:69
Definition: file_utils.h:53