]> git.saurik.com Git - apt.git/blame - apt-private/private-sources.cc
Avoid wedging the entire system if recoverable :/.
[apt.git] / apt-private / private-sources.cc
CommitLineData
453b82a3 1#include <config.h>
9e6b13f3
MV
2
3#include <apt-pkg/hashes.h>
453b82a3
DK
4#include <apt-pkg/strutl.h>
5#include <apt-pkg/configuration.h>
6#include <apt-pkg/sourcelist.h>
7#include <apt-pkg/cmndline.h>
8#include <apt-pkg/error.h>
9#include <apt-pkg/fileutl.h>
503c7d59 10#include <apt-pkg/cachefile.h>
453b82a3
DK
11
12#include <apt-private/private-output.h>
13#include <apt-private/private-sources.h>
14#include <apt-private/private-utils.h>
9e6b13f3 15
01047752
DK
16#include <sys/types.h>
17#include <sys/stat.h>
453b82a3
DK
18#include <stddef.h>
19#include <unistd.h>
20#include <iostream>
21#include <string>
22
23#include <apti18n.h>
9e6b13f3 24
a8f671c1 25/* Interface discussion with donkult (for the future):
503c7d59 26 apt [add-{archive,release,component}|edit|change-release|disable]-sources
a8f671c1
MV
27 and be clever and work out stuff from the Release file
28*/
29
503c7d59
DK
30// EditSource - EditSourcesList /*{{{*/
31class APT_HIDDEN ScopedGetLock {
32public:
33 int fd;
34 ScopedGetLock(std::string const &filename) : fd(GetLock(filename)) {}
35 ~ScopedGetLock() { close(fd); }
36};
9e6b13f3
MV
37bool EditSources(CommandLine &CmdL)
38{
a8f671c1
MV
39 std::string sourceslist;
40 if (CmdL.FileList[1] != NULL)
cf993341
MV
41 {
42 sourceslist = _config->FindDir("Dir::Etc::sourceparts") + CmdL.FileList[1];
43 if (!APT::String::Endswith(sourceslist, ".list"))
44 sourceslist += ".list";
45 } else {
a8f671c1 46 sourceslist = _config->FindFile("Dir::Etc::sourcelist");
cf993341 47 }
9e6b13f3 48 HashString before;
a8f671c1
MV
49 if (FileExists(sourceslist))
50 before.FromFile(sourceslist);
01047752
DK
51 else
52 {
53 FileFd filefd;
54 if (filefd.Open(sourceslist, FileFd::Create | FileFd::WriteOnly, FileFd::None, 0644) == false)
55 return false;
56 }
9e6b13f3 57
503c7d59
DK
58 ScopedGetLock lock(sourceslist);
59 if (lock.fd < 0)
c189f87d 60 return false;
fd789740 61
503c7d59
DK
62 bool res;
63 bool file_changed = false;
9e6b13f3 64 do {
503c7d59
DK
65 if (EditFileInSensibleEditor(sourceslist) == false)
66 return false;
01047752
DK
67 if (before.empty())
68 {
69 struct stat St;
70 if (stat(sourceslist.c_str(), &St) == 0 && St.st_size == 0)
71 RemoveFile("edit-sources", sourceslist);
72 }
73 else if (FileExists(sourceslist) && !before.VerifyFile(sourceslist))
503c7d59
DK
74 {
75 file_changed = true;
76 pkgCacheFile::RemoveCaches();
77 }
78 pkgCacheFile CacheFile;
79 res = CacheFile.BuildCaches(nullptr);
80 if (res == false || _error->empty(GlobalError::WARNING) == false) {
fd789740
DK
81 std::string outs;
82 strprintf(outs, _("Failed to parse %s. Edit again? "), sourceslist.c_str());
cf993341 83 // FIXME: should we add a "restore previous" option here?
503c7d59
DK
84 if (YnPrompt(outs.c_str(), true) == false)
85 {
86 if (res == false && _error->PendingError() == false)
87 {
88 CacheFile.Close();
89 pkgCacheFile::RemoveCaches();
90 res = CacheFile.BuildCaches(nullptr);
91 }
92 break;
93 }
9e6b13f3 94 }
9e6b13f3
MV
95 } while (res == false);
96
503c7d59
DK
97 if (res == true && file_changed == true)
98 {
fd789740
DK
99 ioprintf(
100 std::cout, _("Your '%s' file changed, please run 'apt-get update'."),
9e6b13f3 101 sourceslist.c_str());
9e6b13f3 102 }
503c7d59 103 return res;
9e6b13f3
MV
104}
105 /*}}}*/