Skip to content

Commit

Permalink
Merge pull request #1 from mvidner/Code-11-SP2
Browse files Browse the repository at this point in the history
L3 bnc#769081 and a cleanup of related code
  • Loading branch information
mchf committed Jul 2, 2012
2 parents 7483759 + f5e2058 commit ff3ca28
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 56 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
2.17.172
2.17.173
8 changes: 8 additions & 0 deletions package/yast2-network.changes
@@ -1,3 +1,11 @@
-------------------------------------------------------------------
Fri Jun 29 16:54:11 CEST 2012 - mvidner@suse.cz

- Don't remove /etc/hosts lines containing repeated spaces
(bnc#769081).
- Hosts.ycp: encapsulated by making variables private.
- 2.17.173

-------------------------------------------------------------------
Wed Mar 7 11:10:31 CET 2012 - mvidner@suse.cz

Expand Down
10 changes: 1 addition & 9 deletions src/clients/host.ycp
Expand Up @@ -25,14 +25,6 @@ import "Wizard";
import "CommandLine";
import "RichText";

/**
* Return a modification status
* @return true if data was modified
*/
define boolean Modified() {
return Host::modified;
}

include "network/runtime.ycp";
include "network/services/host.ycp";

Expand All @@ -50,7 +42,7 @@ any HostGUI() {
any ret = HostsMainDialog(true);
y2debug("ret == %1", ret);

if(ret == `next && Host::modified) {
if(ret == `next && Host::GetModified()) {
Host::Write();
/* Not needed? RestartNetwork(); */
}
Expand Down
10 changes: 0 additions & 10 deletions src/clients/host_auto.ycp
Expand Up @@ -31,14 +31,6 @@ import "Wizard";

include "network/services/host.ycp";

/**
* Return a modification status
* @return true if data was modified
*/
define boolean Modified() {
return Host::modified;
}

any ret = nil;
string func = "";
map param = $[];
Expand All @@ -58,7 +50,6 @@ if(func == "Summary") {
}
/* Reset configuration */
else if (func == "Reset") {
Host::modified = false;
Host::Import($[]);
ret = $[];
}
Expand Down Expand Up @@ -104,7 +95,6 @@ else if (func == "Packages") {
else if (func == "Write") {
import "Progress";
boolean progress_orig = Progress::set (false);
Host::write_only = true;
ret = Host::Write();
Progress::set (progress_orig);
}
Expand Down
8 changes: 3 additions & 5 deletions src/lan/address.ycp
Expand Up @@ -787,7 +787,7 @@ define any AddressDialog() {

fwzone_initial = fwzone;

list <string> host_list = Host::hosts[LanItems::ipaddr]:[];
list <string> host_list = Host::Names(LanItems::ipaddr);
if ( size( host_list) > 1)
{
y2milestone("More than one hostname for single IP detected, using the first one only");
Expand Down Expand Up @@ -1090,9 +1090,8 @@ define any AddressDialog() {
boolean ip_changed = ( LanItems::ipaddr != settings["IPADDR"]:"" );
if (ip_changed)
{
Host::hosts[LanItems::ipaddr] = [];
Host::SetNames(LanItems::ipaddr, []);
y2milestone("IP has changed");
Host::SetModified();
}

LanItems::ipaddr = settings["IPADDR"]:"";
Expand All @@ -1103,10 +1102,9 @@ define any AddressDialog() {
if ( (hostname_initial != settings["HOSTNAME"]:"") || ip_changed)
{
if (settings["HOSTNAME"]:"" == "" )
Host::hosts[LanItems::ipaddr] = [];
Host::SetNames(LanItems::ipaddr, []);
else
Host::Update(hostname_initial, settings["HOSTNAME"]:"", [ settings["IPADDR"]:""]);
Host::SetModified();
}
}
else
Expand Down
74 changes: 55 additions & 19 deletions src/modules/Host.ycp
Expand Up @@ -26,12 +26,50 @@ include "network/routines.ycp";
* keys: IPs, (But #35671 suggests that repeating IPs is valid)
* values: names, the first one is the canonical one
*/
global map<string, list<string> > hosts = $[];
map<string, list<string> > hosts = $[];

/**
* Data was modified?
*/
global boolean modified = false;
boolean modified = false;

/**
* Remove all entries from the host table.
*/
global void Clear() {
hosts = $[];
modified = true;
}

/**
* @return a map: address->list of names
*/
global map<string, list<string> > NameMap() {
return hosts;
}

/**
* @return list of names for that address
*/
global list<string> Names(string address) {
return hosts[address]:[];
}

/**
* Give address a new list of names.
*/
global void SetNames(string address, list<string> names) {
hosts[address] = names;
modified = true;
}

/**
* Add another name to the list for address (which may be empty so far)
*/
global void AddName(string address, string name) {
hosts[address] = add(hosts[address]:[], name);
modified = true;
}

/**
* All hosts read at the start
Expand All @@ -43,11 +81,6 @@ map<string,any> hosts_init = $[];
*/
string hosts_file = "/etc/hosts";

/**
* Only write configuration
*/
global boolean write_only = false;

boolean initialized = false;

global boolean NeedDummyIP() {
Expand Down Expand Up @@ -230,22 +263,25 @@ global define boolean Update(string oldhn, string newhn, list<string> iplist) {
modified = true;

string nick = Hostname::SplitFQ(newhn)[0]:"";
string oldnick = Hostname::SplitFQ(oldhn)[0]:"";

/* Remove old hostname from hosts */
// list oldhnlist = [];
foreach (string ip, list<string> hs, hosts, {
list <list <string> > wrk = maplist(string s, hs, {
return splitstring(s," ");
});
wrk = filter (list <string> lst, wrk, {
return (!contains(lst, oldhn));
// if wrk contains "" because of multiple spaces,
// the loop would remove innocent lines (bnc#769081)
if (oldhn != "") {
foreach (string ip, list<string> hs, hosts, {
list <list <string> > wrk = maplist(string s, hs, {
return splitstring(s," ");
});
wrk = filter (list <string> lst, wrk, {
return (!contains(lst, oldhn));
});

hosts[ip] = maplist(list <string> lst, wrk, {
return mergestring(lst, " ");
});
});

hosts[ip] = maplist(list <string> lst, wrk, {
return mergestring(lst, " ");
});
});
}

/* Resurect the rest of oldhnlist without old hostname */
// FIXME: maybe
Expand Down
22 changes: 10 additions & 12 deletions src/services/host.ycp
Expand Up @@ -73,10 +73,11 @@ define symbol HostsMainDialog(boolean standalone) {
list<term> table_items = [];
list <string> deleted_items = [];

y2debug("hosts=%1", Host::hosts);
map<string, list<string> > hosts = Host::NameMap();
y2debug("hosts=%1", hosts);

/* make ui items from the hosts list */
maplist(string host, list<string> names, (map<string, list<string> >) Host::hosts, {
maplist(string host, list<string> names, hosts, {
if(size(names) < 1) {
y2error("Invalid host: %1, (%2)", host, names);
return;
Expand Down Expand Up @@ -238,30 +239,27 @@ define symbol HostsMainDialog(boolean standalone) {
return true;
});
UI::ChangeWidget(`id(`table), `Items, table_items);
Host::modified = true;
Host::SetModified();
continue;
}
else if(ret == `back) {
break;
}
else if(ret == `next) {
/* check_ */
if(Host::modified){
Host::hosts=$[];
if(Host::GetModified()){
Host::Clear();
foreach(term row, table_items, {
string value = mergestring( prepend( Punycode::EncodePunycodes( [row[3]:""]),
Punycode::EncodeDomainName( row[2]:"" ) ), " " );
string key = row[1]:"";
if(!haskey(Host::hosts, key))
Host::hosts[key]=[ value ];
else
Host::hosts[key] = add(Host::hosts[key]:[], value);
Host::AddName(key, value);
});
//deleted entries need to be set to [],
//so that ini-agent does not keep them in
//config file (#455862)
foreach( string d, deleted_items, {
Host::hosts[d] = [];
Host::SetNames(d, []);
});
}
break;
Expand All @@ -274,7 +272,7 @@ define symbol HostsMainDialog(boolean standalone) {


y2debug("table_items=%1", table_items);
y2debug("hosts=%1", Host::hosts);
y2debug("hosts=%1", Host::NameMap());

return (symbol) ret;
}
Expand Down Expand Up @@ -374,7 +372,7 @@ define term HostDialog(integer id, term entry) {
UI::CloseDialog();
if(ret != `ok) return nil;

Host::modified = true;
Host::SetModified();
return host;
}

Expand Down
Empty file.
7 changes: 7 additions & 0 deletions testsuite/tests/Host-twospaces.out
@@ -0,0 +1,7 @@
Read .target.tmpdir "/tmp"
Dump Read
Read .target.size "/etc/hosts" 1
Dir .etc.hosts: ["10.0.0.1", "127.0.0.1"]
Read .etc.hosts."10.0.0.1" ["somehost.example.com notice-two-spaces"]
Read .etc.hosts."127.0.0.1" ["localhost localhost.localdomain"]
Return true
32 changes: 32 additions & 0 deletions testsuite/tests/Host-twospaces.ycp
@@ -0,0 +1,32 @@
//bnc#769081: two spaces and an empty oldhostname would remove a line
{

include "testsuite.ycp";

map READ = $[
"target" : $[
"size" : 1,
"tmpdir" : "/tmp",
],
"etc" : $[
"hosts" : $[
"127.0.0.1" : ["localhost localhost.localdomain"],
"10.0.0.1" : ["somehost.example.com notice-two-spaces"],
],
],
];

TESTSUITE_INIT([READ], nil);

import "Assert";
import "Host";
import "Progress";
Progress::off();

DUMP("Read");
TEST(``(Host::Read()), [READ], nil);
list<string> names = Host::Names("10.0.0.1");
Host::Update("", "newname", ["10.0.0.42"]);
list<string> new_names = Host::Names("10.0.0.1");
Assert::Equal(names, new_names);
}

0 comments on commit ff3ca28

Please sign in to comment.