]>
Commit | Line | Data |
---|---|---|
1 | #include <config.h> | |
2 | ||
3 | #include <apt-pkg/hashes.h> | |
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> | |
10 | #include <apt-pkg/cachefile.h> | |
11 | ||
12 | #include <apt-private/private-output.h> | |
13 | #include <apt-private/private-sources.h> | |
14 | #include <apt-private/private-utils.h> | |
15 | ||
16 | #include <sys/types.h> | |
17 | #include <sys/stat.h> | |
18 | #include <stddef.h> | |
19 | #include <unistd.h> | |
20 | #include <iostream> | |
21 | #include <string> | |
22 | ||
23 | #include <apti18n.h> | |
24 | ||
25 | /* Interface discussion with donkult (for the future): | |
26 | apt [add-{archive,release,component}|edit|change-release|disable]-sources | |
27 | and be clever and work out stuff from the Release file | |
28 | */ | |
29 | ||
30 | // EditSource - EditSourcesList /*{{{*/ | |
31 | class APT_HIDDEN ScopedGetLock { | |
32 | public: | |
33 | int fd; | |
34 | ScopedGetLock(std::string const &filename) : fd(GetLock(filename)) {} | |
35 | ~ScopedGetLock() { close(fd); } | |
36 | }; | |
37 | bool EditSources(CommandLine &CmdL) | |
38 | { | |
39 | std::string sourceslist; | |
40 | if (CmdL.FileList[1] != NULL) | |
41 | { | |
42 | sourceslist = _config->FindDir("Dir::Etc::sourceparts") + CmdL.FileList[1]; | |
43 | if (!APT::String::Endswith(sourceslist, ".list")) | |
44 | sourceslist += ".list"; | |
45 | } else { | |
46 | sourceslist = _config->FindFile("Dir::Etc::sourcelist"); | |
47 | } | |
48 | HashString before; | |
49 | if (FileExists(sourceslist)) | |
50 | before.FromFile(sourceslist); | |
51 | else | |
52 | { | |
53 | FileFd filefd; | |
54 | if (filefd.Open(sourceslist, FileFd::Create | FileFd::WriteOnly, FileFd::None, 0644) == false) | |
55 | return false; | |
56 | } | |
57 | ||
58 | ScopedGetLock lock(sourceslist); | |
59 | if (lock.fd < 0) | |
60 | return false; | |
61 | ||
62 | bool res; | |
63 | bool file_changed = false; | |
64 | do { | |
65 | if (EditFileInSensibleEditor(sourceslist) == false) | |
66 | return false; | |
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)) | |
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) { | |
81 | std::string outs; | |
82 | strprintf(outs, _("Failed to parse %s. Edit again? "), sourceslist.c_str()); | |
83 | // FIXME: should we add a "restore previous" option here? | |
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 | } | |
94 | } | |
95 | } while (res == false); | |
96 | ||
97 | if (res == true && file_changed == true) | |
98 | { | |
99 | ioprintf( | |
100 | std::cout, _("Your '%s' file changed, please run 'apt-get update'."), | |
101 | sourceslist.c_str()); | |
102 | } | |
103 | return res; | |
104 | } | |
105 | /*}}}*/ |