| 1 |
/** |
|---|
| 2 |
* Update the sources list from the given path, specifically modifying the |
|---|
| 3 |
* given tool |
|---|
| 4 |
* |
|---|
| 5 |
* Authors: |
|---|
| 6 |
* Gregor Richards |
|---|
| 7 |
* |
|---|
| 8 |
* License: |
|---|
| 9 |
* Copyright (c) 2006 Gregor Richards |
|---|
| 10 |
* |
|---|
| 11 |
* Permission is hereby granted, free of charge, to any person obtaining a |
|---|
| 12 |
* copy of this software and associated documentation files (the "Software"), |
|---|
| 13 |
* to deal in the Software without restriction, including without limitation |
|---|
| 14 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
|---|
| 15 |
* and/or sell copies of the Software, and to permit persons to whom the |
|---|
| 16 |
* Software is furnished to do so, subject to the following conditions: |
|---|
| 17 |
* |
|---|
| 18 |
* The above copyright notice and this permission notice shall be included in |
|---|
| 19 |
* all copies or substantial portions of the Software. |
|---|
| 20 |
* |
|---|
| 21 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 22 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 23 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 24 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 25 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|---|
| 26 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|---|
| 27 |
* DEALINGS IN THE SOFTWARE. |
|---|
| 28 |
*/ |
|---|
| 29 |
|
|---|
| 30 |
module updateSources; |
|---|
| 31 |
|
|---|
| 32 |
import std.file; |
|---|
| 33 |
import std.path; |
|---|
| 34 |
import std.process; |
|---|
| 35 |
import std.stdio; |
|---|
| 36 |
import std.string; |
|---|
| 37 |
|
|---|
| 38 |
version (Unix) { |
|---|
| 39 |
import std.c.unix.unix; |
|---|
| 40 |
} else version (linux) { |
|---|
| 41 |
import std.c.linux.linux; |
|---|
| 42 |
} else static assert(0); |
|---|
| 43 |
|
|---|
| 44 |
alias std.file.chdir chdir; |
|---|
| 45 |
alias std.file.getcwd getcwd; |
|---|
| 46 |
|
|---|
| 47 |
extern (C) int daemon(int, int); |
|---|
| 48 |
extern (C) int geteuid(); |
|---|
| 49 |
extern (C) int setreuid(int, int); |
|---|
| 50 |
|
|---|
| 51 |
int main(char[][] args) |
|---|
| 52 |
{ |
|---|
| 53 |
// args: directory tool |
|---|
| 54 |
if (args.length != 3) { |
|---|
| 55 |
writefln("Improper invocation."); |
|---|
| 56 |
return 1; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
char[] srcDir = args[1]; |
|---|
| 60 |
char[] updPkg = args[2]; |
|---|
| 61 |
|
|---|
| 62 |
// get our correct home directory |
|---|
| 63 |
passwd *pwbuf; |
|---|
| 64 |
pwbuf = getpwuid(geteuid()); |
|---|
| 65 |
if (pwbuf is null) { |
|---|
| 66 |
writefln("Failed to get home directory"); |
|---|
| 67 |
return 1; |
|---|
| 68 |
} |
|---|
| 69 |
chdir(toString(pwbuf.pw_dir)); |
|---|
| 70 |
setreuid(geteuid(), -1); |
|---|
| 71 |
|
|---|
| 72 |
// 1) sanitize |
|---|
| 73 |
if (updPkg[0] == '.' || |
|---|
| 74 |
find(updPkg, '/') != -1) { |
|---|
| 75 |
writefln("Invalid package name."); |
|---|
| 76 |
return 1; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
// 2) daemonize (if possible, ignore error but will always be running /something/) |
|---|
| 80 |
daemon(1, 0); |
|---|
| 81 |
|
|---|
| 82 |
// 3) make sure nothing is out of date |
|---|
| 83 |
system("svn up sources"); |
|---|
| 84 |
|
|---|
| 85 |
// 4) remove old mirrored cruft |
|---|
| 86 |
system("rm -rf mirror/" ~ updPkg ~ "*/"); |
|---|
| 87 |
|
|---|
| 88 |
// 5) Update the source.list |
|---|
| 89 |
char[] sourceList; |
|---|
| 90 |
foreach (pkg; listdir(srcDir)) { |
|---|
| 91 |
if (pkg.length < 1 || |
|---|
| 92 |
pkg[0] == '.' || |
|---|
| 93 |
pkg == "privileges") continue; |
|---|
| 94 |
|
|---|
| 95 |
// add it |
|---|
| 96 |
sourceList ~= cast(char[]) std.file.read( |
|---|
| 97 |
srcDir ~ "/" ~ pkg) ~ "\n"; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
char[] origcwd = getcwd(); |
|---|
| 101 |
chdir("sources"); |
|---|
| 102 |
std.file.write("source.list", sourceList); |
|---|
| 103 |
|
|---|
| 104 |
// 6) Update pkgs.list (and in turn the mirror) |
|---|
| 105 |
system("./pkgslist"); |
|---|
| 106 |
chdir(origcwd); |
|---|
| 107 |
|
|---|
| 108 |
// 7) svn commit everything |
|---|
| 109 |
system("svn commit sources -m ''"); |
|---|
| 110 |
system("./up.sh"); |
|---|
| 111 |
|
|---|
| 112 |
return 0; |
|---|
| 113 |
} |
|---|