CARDS 2.4.140
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
46enum {
47 WALK_OK = 0,
48 WALK_BADPATTERN,
49 WALK_NAMETOOLONG,
50 WALK_BADIO
51};
52
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
64struct DirUrl {
65 std::string Dir;
66 std::string Url;
67};
68
69struct 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
88int getConfig(const char *fileName, Config& config);
89void * getDatas ( void * var, FILE * file, long offset, size_t size, size_t nmemb);
90std::string trimFileName(const std::string& filename);
91time_t getEpochModifyTimeFile(const std::string& filename);
92std::string getDateFromEpoch(const time_t& epoch);
93std::string getModifyTimeFile(const std::string& filename);
94bool checkFileExist(const std::string& filename);
95bool checkFileEmpty(const std::string& filename);
96bool checkRegularFile(const std::string& filename);
97bool checkFileSignature(const std::string& filename, const std::string& signature);
98bool checkFilesEqual(const std::string& file1, const std::string& file2);
99bool checkPermissionsEqual(const std::string& file1, const std::string& file2);
100bool createRecursiveDirs(const std::string& pathname);
101void cleanupMetaFiles(const std::string& basedir);
102void removeFile(const std::string& basedir, const std::string& filename);
103int copyFile(const char * destFile, const char * origFile);
104int findFile(std::set<std::string>& filesList, const std::string& basedir);
105int findDir(itemList *filenameList, const char *path);
106int findRecursiveFile(std::set<std::string>& filenameList, const char *filename, int spec);
107int readFileStripSpace(itemList *fileContent, const char *fileName);
108int readFile(itemList *fileContent, const char *fileName);
109int parseFile(std::set<std::string>& fileContent, const char* fileName);
110int parseFile(std::vector<std::string>& fileContent, const char* fileName);
111int parseFile(std::string& Depends, const char* key, const char* fileName);
112bool findMD5sum(const std::string& fileName, unsigned char* result);
113bool checkMD5sum(const char * fileName, const char * MD5Sum);
114/* read a file
115 return it into a template container */
116template <class T>
117int 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 :
Definition: file_utils.h:69
Definition: file_utils.h:64
Definition: file_utils.h:54
Definition: libcards.h:484