|
| 1 | +use std::fmt::Write; |
| 2 | +use std::fs::{create_dir_all, read_to_string, remove_file, rename, write}; |
| 3 | +use std::io::{Error, ErrorKind}; |
| 4 | +use std::path::Path; |
| 5 | + |
| 6 | +use super::Channel; |
| 7 | + |
| 8 | +use log::info; |
| 9 | + |
| 10 | +#[cfg(feature = "demo_mode")] |
| 11 | +mod imports { |
| 12 | + pub(super) const STATIC_CONF_PATH: &str = "demo_files/usr/lib/rauc/system.conf"; |
| 13 | + pub(super) const DYNAMIC_CONF_PATH: &str = "demo_files/run/rauc/system.conf"; |
| 14 | +} |
| 15 | + |
| 16 | +#[cfg(not(feature = "demo_mode"))] |
| 17 | +mod imports { |
| 18 | + pub(super) const STATIC_CONF_PATH: &str = "/usr/lib/rauc/system.conf"; |
| 19 | + pub(super) const DYNAMIC_CONF_PATH: &str = "/run/rauc/system.conf"; |
| 20 | +} |
| 21 | + |
| 22 | +use imports::*; |
| 23 | + |
| 24 | +const MAGIC_LINE: &str = "\n# <tacd-poll-section>\n"; |
| 25 | + |
| 26 | +fn poll_section( |
| 27 | + primary_channel: Option<&Channel>, |
| 28 | + polling: bool, |
| 29 | +) -> Result<Option<String>, std::fmt::Error> { |
| 30 | + // If no primary channel is configured or if polling is not enabled, |
| 31 | + // then we do not need a `[poll]` section at all. |
| 32 | + let primary_channel = match (primary_channel, polling) { |
| 33 | + (Some(pc), true) => pc, |
| 34 | + _ => return Ok(None), |
| 35 | + }; |
| 36 | + |
| 37 | + let mut section = String::new(); |
| 38 | + |
| 39 | + writeln!(&mut section)?; |
| 40 | + writeln!(&mut section, "[poll]")?; |
| 41 | + writeln!(&mut section, "source={}", primary_channel.url)?; |
| 42 | + |
| 43 | + if let Some(interval) = primary_channel.polling_interval { |
| 44 | + writeln!(&mut section, "interval-sec={}", interval.as_secs())?; |
| 45 | + } |
| 46 | + |
| 47 | + writeln!(&mut section, "candidate-criteria=different-version")?; |
| 48 | + |
| 49 | + Ok(Some(section)) |
| 50 | +} |
| 51 | + |
| 52 | +pub fn update_system_conf( |
| 53 | + primary_channel: Option<&Channel>, |
| 54 | + enable_polling: bool, |
| 55 | +) -> std::io::Result<bool> { |
| 56 | + let dynamic_conf = { |
| 57 | + match poll_section(primary_channel, enable_polling) { |
| 58 | + Ok(Some(ps)) => { |
| 59 | + // We use the config in /etc as a template ... |
| 60 | + let static_conf = read_to_string(STATIC_CONF_PATH)?; |
| 61 | + |
| 62 | + // ... and replace the line `# <tacd-poll-section>` with our |
| 63 | + // generated `[poll]` section. |
| 64 | + let dc = static_conf.replacen(MAGIC_LINE, &ps, 1); |
| 65 | + |
| 66 | + // The user may have decided not to include a `# <tacd-poll-section>` |
| 67 | + // line. In that case we do not need a dynamic config at all. |
| 68 | + if dc == static_conf { |
| 69 | + info!( |
| 70 | + "Rauc config {} did not contain magic line '{}'. Will not generate poll section.", |
| 71 | + STATIC_CONF_PATH, MAGIC_LINE |
| 72 | + ); |
| 73 | + |
| 74 | + None |
| 75 | + } else { |
| 76 | + Some(dc) |
| 77 | + } |
| 78 | + } |
| 79 | + _ => None, |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + /* Do we need a dynamic config in /run/rauc? |
| 84 | + * |
| 85 | + * If so, then is it actually different from what we already have? |
| 86 | + * If not, then there is no need to restart the daemon. |
| 87 | + * If it is, we write the new one and signal the need for a daemon |
| 88 | + * restart. |
| 89 | + * |
| 90 | + * If we do not need dynamic config, then try to delete the previous one. |
| 91 | + * If there was none, then the deamon does not have to be restarted. |
| 92 | + * If there was a dynamic config before, then we need to restart the |
| 93 | + * daemon. |
| 94 | + */ |
| 95 | + match dynamic_conf { |
| 96 | + Some(new) => match read_to_string(DYNAMIC_CONF_PATH) { |
| 97 | + Ok(old) if old == new => Ok(false), |
| 98 | + Err(err) if err.kind() != ErrorKind::NotFound => Err(err), |
| 99 | + Ok(_) | Err(_) => { |
| 100 | + let dynamic_conf_dir = Path::new(DYNAMIC_CONF_PATH) |
| 101 | + .parent() |
| 102 | + .ok_or_else(|| Error::other("Invalid dynamic config path"))?; |
| 103 | + |
| 104 | + let tmp_path = dynamic_conf_dir.join("system.conf.tacd-tmp"); |
| 105 | + |
| 106 | + create_dir_all(dynamic_conf_dir)?; |
| 107 | + |
| 108 | + write(&tmp_path, &new)?; |
| 109 | + rename(&tmp_path, DYNAMIC_CONF_PATH)?; |
| 110 | + |
| 111 | + Ok(true) |
| 112 | + } |
| 113 | + }, |
| 114 | + None => match remove_file(DYNAMIC_CONF_PATH) { |
| 115 | + Ok(_) => Ok(true), |
| 116 | + Err(err) if err.kind() == ErrorKind::NotFound => Ok(false), |
| 117 | + Err(err) => Err(err), |
| 118 | + }, |
| 119 | + } |
| 120 | +} |
0 commit comments