merge 'after squeeze release'-stuff
[ntk/apt.git] / ftparchive / writer.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: writer.h,v 1.4.2.2 2003/12/26 22:55:43 mdz Exp $
4 /* ######################################################################
5
6 Writer
7
8 The file writer classes. These write various types of output, sources,
9 packages and contents.
10
11 ##################################################################### */
12 /*}}}*/
13 #ifndef WRITER_H
14 #define WRITER_H
15
16
17 #include <string>
18 #include <stdio.h>
19 #include <iostream>
20 #include <vector>
21 #include <map>
22 #include <set>
23
24 #include "cachedb.h"
25 #include "multicompress.h"
26 #include "override.h"
27 #include "apt-ftparchive.h"
28
29 using std::string;
30 using std::cout;
31 using std::endl;
32 using std::vector;
33 using std::map;
34
35 class FTWScanner
36 {
37 protected:
38 vector<string> Patterns;
39 string Arch;
40 const char *OriginalPath;
41 bool ErrorPrinted;
42
43 // Stuff for the delinker
44 bool NoLinkAct;
45
46 static FTWScanner *Owner;
47 static int ScannerFTW(const char *File,const struct stat *sb,int Flag);
48 static int ScannerFile(const char *File, bool const &ReadLink);
49
50 bool Delink(string &FileName,const char *OriginalPath,
51 unsigned long &Bytes,off_t const &FileSize);
52
53 inline void NewLine(unsigned const &Priority)
54 {
55 if (ErrorPrinted == false && Quiet <= Priority)
56 {
57 c1out << endl;
58 ErrorPrinted = true;
59 }
60 }
61
62 public:
63
64 unsigned long DeLinkLimit;
65 string InternalPrefix;
66
67 virtual bool DoPackage(string FileName) = 0;
68 bool RecursiveScan(string const &Dir);
69 bool LoadFileList(string const &BaseDir,string const &File);
70 void ClearPatterns() { Patterns.clear(); };
71 void AddPattern(string const &Pattern) { Patterns.push_back(Pattern); };
72 void AddPattern(char const *Pattern) { Patterns.push_back(Pattern); };
73 void AddPatterns(std::vector<std::string> const &patterns) { Patterns.insert(Patterns.end(), patterns.begin(), patterns.end()); };
74 bool SetExts(string const &Vals);
75
76 FTWScanner(string const &Arch = string());
77 virtual ~FTWScanner() {};
78 };
79
80 class TranslationWriter
81 {
82 MultiCompress *Comp;
83 FILE *Output;
84 std::set<string> Included;
85 unsigned short RefCounter;
86
87 public:
88 void IncreaseRefCounter() { ++RefCounter; };
89 unsigned short DecreaseRefCounter() { return (RefCounter == 0) ? 0 : --RefCounter; };
90 unsigned short GetRefCounter() const { return RefCounter; };
91 bool DoPackage(string const &Pkg, string const &Desc, string const &MD5);
92
93 TranslationWriter(string const &File, string const &TransCompress, mode_t const &Permissions);
94 TranslationWriter() : Comp(NULL), Output(NULL), RefCounter(0) {};
95 ~TranslationWriter();
96 };
97
98 class PackagesWriter : public FTWScanner
99 {
100 Override Over;
101 CacheDB Db;
102
103 public:
104
105 // Some flags
106 bool DoMD5;
107 bool DoSHA1;
108 bool DoSHA256;
109 bool DoAlwaysStat;
110 bool NoOverride;
111 bool DoContents;
112 bool LongDescription;
113
114 // General options
115 string PathPrefix;
116 string DirStrip;
117 FILE *Output;
118 struct CacheDB::Stats &Stats;
119 TranslationWriter *TransWriter;
120
121 inline bool ReadOverride(string const &File) {return Over.ReadOverride(File);};
122 inline bool ReadExtraOverride(string const &File)
123 {return Over.ReadExtraOverride(File);};
124 virtual bool DoPackage(string FileName);
125
126 PackagesWriter(string const &DB,string const &Overrides,string const &ExtOverrides=string(),
127 string const &Arch=string());
128 virtual ~PackagesWriter() {};
129 };
130
131 class ContentsWriter : public FTWScanner
132 {
133 CacheDB Db;
134
135 GenContents Gen;
136
137 public:
138
139 // General options
140 FILE *Output;
141 struct CacheDB::Stats &Stats;
142 string Prefix;
143
144 bool DoPackage(string FileName,string Package);
145 virtual bool DoPackage(string FileName)
146 {return DoPackage(FileName,string());};
147 bool ReadFromPkgs(string const &PkgFile,string const &PkgCompress);
148
149 void Finish() {Gen.Print(Output);};
150 inline bool ReadyDB(string const &DB) {return Db.ReadyDB(DB);};
151
152 ContentsWriter(string const &DB, string const &Arch = string());
153 virtual ~ContentsWriter() {};
154 };
155
156 class SourcesWriter : public FTWScanner
157 {
158 Override BOver;
159 Override SOver;
160 char *Buffer;
161 unsigned long BufSize;
162
163 public:
164
165 bool NoOverride;
166
167 // General options
168 string PathPrefix;
169 string DirStrip;
170 FILE *Output;
171 struct CacheDB::Stats Stats;
172
173 virtual bool DoPackage(string FileName);
174
175 SourcesWriter(string const &BOverrides,string const &SOverrides,
176 string const &ExtOverrides=string());
177 virtual ~SourcesWriter() {free(Buffer);};
178 };
179
180 class ReleaseWriter : public FTWScanner
181 {
182 public:
183 ReleaseWriter(string const &DB);
184 virtual bool DoPackage(string FileName);
185 void Finish();
186
187 FILE *Output;
188 // General options
189 string PathPrefix;
190 string DirStrip;
191
192 protected:
193 struct CheckSum
194 {
195 string MD5;
196 string SHA1;
197 string SHA256;
198 // Limited by FileFd::Size()
199 unsigned long size;
200 ~CheckSum() {};
201 };
202 map<string,struct CheckSum> CheckSums;
203 };
204
205 #endif