1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
<?php
class sql_gentoo_profile extends sql_row_obj {
protected $table='gentoo_profiles', $primary_key=array('id'), $columns=array(
'id' => array (
'type' => 'TINYINT',
'length' => 3,
'unsigned' => true,
'not_null' => true,
'auto_increment' => true
),
'pkgdir' => array (
'type' => 'VARCHAR',
'length' => 255,
'not_null' => true,
'default' => '',
'unique' => true
),
'stage3' => array (
'type' => 'VARCHAR',
'length' => 255,
'not_null' => true
),
'name' => array (
'type' => 'VARCHAR',
'length' => 255,
'unique' => true
),
'order' => array (
'type' => 'TINYINT',
'length' => 4
),
'flags' => array (
'type' => 'VARCHAR',
'length' => 255,
'not_null' => true,
'default' => ''
),
'headers' => array (
'type' => 'TEXT',
'not_null' => true
)
);
private $headers_cache;
// Returns $this->headers as an array
public function &get_headers() {
if (isset($headers_cache)) return $headers_cache;
foreach (explode("\n", $this->headers) as $line) {
if (!$line) continue;
list($name, $val)=explode(': ', $line, 2);
$this->headers_cache[strtolower($name)]=$val;
}
return $this->headers_cache;
}
// Reads the data from the Packages file in $this->pkgdir
public function read_Packages($update_pkgs=false, $verbose=false) {
global $conf;
if (!is_readable($file="{$conf['pkgdir_root']}/$this->pkgdir/Packages")) {
throw_exception("Packages file doesn't exist for pkgdir $this->pkgdir");
}
$file=fopen($file, 'r');
$this->headers='';
while (!feof($file)) {
$line=rtrim(fgets($file));
if (strlen($line) == 0) {
break;
} else {
list($name, $val)=array_merge(explode(': ', $line, 2), array(null));
if ($val !== null) {
$this->headers.="$name: $val\n";
}
}
}
$p=array();
$cur=null;
$this->write();
while (!feof($file)) {
$line=rtrim(fgets($file));
if (strlen($line) == 0) {
unset($cur);
continue;
}
list($name, $val)=array_merge(explode(': ', $line, 2), array(null));
if ($name == 'CPV') {
if (preg_match('#^([^/-]+)([^/]*)/(.+?)-([^-]+)((?:-r[0-9]+)?)$#', $val, $match)) {
list(, $bcat, $lcat, $name, $ver, $r)=$match;
$ver.=$r;
} else {
debug("Unsplittable atom: $val");
continue;
}
if (isset($p[$bcat][$lcat][$name][$ver])) {
debug("Duplicate package $bcat$lcat/$name-$ver");
continue;
}
$p[$bcat][$lcat][$name][$ver]='';
$cur=&$p[$bcat][$lcat][$name][$ver];
} elseif (isset($cur, $val)) {
$cur.="$name: $val\n";
}
}
unset($cur);
$u=$d=$t=0;
if ($update_pkgs) {
global $S;
$r=$S['pdo']->query('SELECT * FROM `gentoo_packages` WHERE `profile`='.$this->id);
while ($pkg=$r->fetch(PDO::FETCH_ASSOC)) {
$pkg=new sql_gentoo_package($pkg);
if (isset($p[$pkg->bcat][$pkg->lcat][$pkg->name][$pkg->version])) {
$t++;
if ($pkg->data != $p[$pkg->bcat][$pkg->lcat][$pkg->name][$pkg->version]) {
$u++;
$pkg->data=$p[$pkg->bcat][$pkg->lcat][$pkg->name][$pkg->version];
if ($verbose) echo "U $pkg->bcat$pkg->lcat/$pkg->name/$pkg->version\n";
$pkg->write();
}
unset($p[$pkg->bcat][$pkg->lcat][$pkg->name][$pkg->version]);
} else {
$d++;
if ($verbose) echo "D $pkg->bcat$pkg->lcat/$pkg->name-$pkg->version\n";
$pkg->delete();
}
}
}
$n=0;
foreach ($p as $bcat => $lcats) {
foreach ($lcats as $lcat => $pkgs) {
foreach ($pkgs as $pkg => $vers) {
foreach ($vers as $ver => $data) {
$t++;
$n++;
$gp=new sql_gentoo_package(null, $this->id, $bcat, $lcat, $pkg, $ver, $data);
if ($verbose) echo "A $bcat$lcat/$pkg-$ver\n";
$gp->write();
}
}
}
}
return array($n, $u, $d, $t);
}
public function read_pkgsets($update=false) {
global $S;
$file=realpath(BACKEND.'/../gentoo_pkgsets.csv');
if (!is_readable($file)) return false;
$file=fopen($file, 'r');
if ($update) $exists=array();
while (!feof($file)) {
$add=array();
$pkgs=explode("\t", trim(fgets($file)));
if (substr($pkgs[0], 0, 1) == '#') continue;
$name=array_shift($pkgs);
$obj=new sql_gentoo_pkgset();
if ($update) {
$r=$S['pdo']->query('SELECT * FROM `gentoo_pkgsets` WHERE `profile`='.$this->id.' AND `name`="'.$name.'" LIMIT 1');
if ($r->rowCount())
$obj->from_array($r->fetch(PDO::FETCH_ASSOC), true);
}
foreach ($pkgs as $pkg) {
if ($pkg=sql_gentoo_package::from_atom($pkg, $this))
$add[]="$pkg->bcat$pkg->lcat/$pkg->name";
}
if (count($add)) {
$obj->profile=$this->id;
$obj->name=$name;
$obj->packages=implode("\n", $add);
$obj->write();
if ($update) $exists[]=$obj->id;
}
}
if ($update)
$S['pdo']->query('DELETE FROM `gentoo_pkgsets` WHERE `profile`='.$this->id.($exists?' AND `id` NOT IN ('.implode(',', $exists).')':''));
}
public function read_stage3($update=false) {
global $S;
if ($update) {
$S['pdo']->query('DELETE FROM `gentoo_basepkgs` WHERE `profile`='.$this->id);
$S['pdo']->query('DELETE FROM `gentoo_baseinit` WHERE `profile`='.$this->id);
}
$file=realpath(CACHE.'/stage3/'.$this->stage3);
if (!is_readable($file)) return false;
$opt='-tv'.(substr($file, -3) == 'bz2'?'j':'z').'f';
$prefix='./var/db/pkg/';
$files=explode("\n", is_readable("$file.CONTENTS")?file_get_contents("$file.CONTENTS"):shell_exec('tar '.$opt.' '.escapeshellarg($file)));
if (!is_file("$file.CONTENTS"))
file_put_contents("$file.CONTENTS", implode("\n", $files));
foreach ($files as $file) {
if (preg_match('#^[^.]+\./var/db/pkg/(.*/.*)/$#', $file, $match)) {
$pkg=new sql_gentoo_basepkg($this->id, $match[1]);
$pkg->write();
} elseif (preg_match('#^[^.]+\./etc/runlevels/([^/]+)/([^/]+) -> /etc/init\.d/#', $file, $match)) {
$init=new sql_gentoo_baseinit($this->id, $match[2], $match[1]);
$init->write();
}
}
return true;
}
public function &get_packages() {
global $S;
$r=$S['pdo']->query('SELECT * FROM `gentoo_packages` WHERE `profile`='.$this->id);
$p=array();
while ($pkg=$r->fetch(PDO::FETCH_ASSOC)) {
$pkg=new sql_gentoo_package($pkg);
$p[$pkg->bcat][$pkg->lcat][$pkg->name][$pkg->version]=$pkg->to_array();
}
return $p;
}
function get_arch() {
$arch=$this->get_headers();
$arch=explode(' ', $arch['accept_keywords']);
$arch=ltrim($arch[0], '~');
return $arch;
}
}
?>
|