Skip to content

Commit

Permalink
coreos: let aleph file be a symlink
Browse files Browse the repository at this point in the history
Once osbuild/osbuild#1475 gets picked up and
starts getting used the .coreos-aleph-version.json will be a symlink.
Let's canonicalize and find the actual file before dropping into
sysroot.open_file_optional() because the underlying code for that uses
O_NOFOLLOW and will give an ELOOP (too many symbolic links) error.
  • Loading branch information
dustymabe committed Dec 18, 2023
1 parent 663d0e4 commit 504697a
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/coreos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

use anyhow::Result;
use anyhow::{Context, Result};
use chrono::prelude::*;
use openat_ext::OpenatDirExt;
use serde::{Deserialize, Serialize};
use std::fs::{canonicalize, symlink_metadata};
use std::path::Path;

#[derive(Serialize, Deserialize, Clone, Debug, Hash, Ord, PartialOrd, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
Expand All @@ -30,7 +32,21 @@ const ALEPH_PATH: &str = "/sysroot/.coreos-aleph-version.json";

pub(crate) fn get_aleph_version() -> Result<Option<AlephWithTimestamp>> {
let sysroot = openat::Dir::open("/")?;
if let Some(statusf) = sysroot.open_file_optional(ALEPH_PATH)? {
let mut path = ALEPH_PATH;
if !Path::new(ALEPH_PATH).exists() {
return Ok(None);
}
let target;
let is_link = symlink_metadata(path)
.with_context(|| format!("reading metadata for {}", path))?
.file_type()
.is_symlink();
if is_link {
target =
canonicalize(path).with_context(|| format!("getting absolute path to {}", path))?;
path = target.to_str().unwrap()
}
if let Some(statusf) = sysroot.open_file_optional(path)? {
let meta = statusf.metadata()?;
let bufr = std::io::BufReader::new(statusf);
let aleph: Aleph = serde_json::from_reader(bufr)?;
Expand Down

0 comments on commit 504697a

Please sign in to comment.