use super::*;
use crate::{config::Config, store::Store};
use anyhow::Result;
use clap::Subcommand;
use serde_json::{Value, json};
use std::{
path::PathBuf,
sync::{Arc, atomic::AtomicBool},
};
#[derive(Debug, Clone, Subcommand)]
pub enum Action {
Chapters {
id: String,
},
Bookmarks {
id: String,
},
Bookmark {
id: String,
#[arg(long)]
time: Option<String>,
#[arg(long, default_value = "")]
note: String,
},
BookmarkEdit {
id: String,
#[arg(long)]
note: Option<String>,
},
Settings {
id: String,
key: Option<String>,
value: Option<String>,
},
Jump {
id: String,
time: String,
},
Sleep {
#[arg(default_value = "30")]
value: String,
},
Library {
#[command(flatten)]
options: LibraryQuery,
},
Save {
id: String,
#[arg(long)]
remove: bool,
},
Dismiss {
id: String,
},
MarkBatch {
#[arg(required = true)]
ids: Vec<String>,
#[arg(long)]
unplayed: bool,
},
Resume,
Relative {
seconds: f64,
},
Browse {
#[arg(long, default_value = "")]
country: String,
},
Categories {
#[arg(long, default_value = "")]
country: String,
},
Charts {
#[arg(long, default_value = "")]
country: String,
#[arg(long, value_parser = directory::category_id)]
category: Option<String>,
#[arg(long, default_value_t = 25, value_parser = clap::value_parser!(u16).range(1..=100))]
limit: u16,
#[arg(long)]
random: bool,
},
Search {
query: String,
#[arg(long, default_value = "")]
country: String,
#[arg(long,default_value_t=25,value_parser=clap::value_parser!(u16).range(1..=200))]
limit: u16,
},
Discover {
url: String,
},
Preview {
url: String,
},
Add {
url: String,
},
List {
#[command(flatten)]
page: crate::cli::Page,
#[arg(long, default_value = "")]
query: String,
},
Episodes {
id: String,
#[arg(long, default_value = "")]
query: String,
#[command(flatten)]
page: crate::cli::Page,
},
Refresh {
id: Option<String>,
},
Unsubscribe {
id: String,
},
Play {
id: String,
#[arg(long)]
from_start: bool,
#[arg(long)]
speed: Option<f64>,
},
Mark {
id: String,
#[arg(value_parser=["played","unplayed"])]
state: String,
},
Subtitles {
id: String,
#[arg(long, conflicts_with = "transcribe")]
file: Option<PathBuf>,
#[arg(long)]
transcribe: bool,
},
Download {
id: String,
},
Downloads {
#[command(subcommand)]
action: DownloadAction,
},
Queue {
#[command(subcommand)]
action: QueueAction,
},
Import {
file: PathBuf,
#[arg(long)]
dry_run: bool,
},
Export {
file: PathBuf,
#[arg(long)]
overwrite: bool,
},
Seek {
time: String,
},
Speed {
rate: f64,
},
AutoNext {
#[arg(value_parser=["on","off"])]
state: String,
},
}
#[derive(Debug, Clone, Subcommand)]
pub enum DownloadAction {
List,
Stats,
Pause { id: String },
Resume { id: String },
Cancel { id: String },
Remove { id: String },
}
#[derive(Debug, Clone, Subcommand)]
pub enum QueueAction {
Start {
id: String,
#[arg(long)]
auto_next: bool,
},
Next {
id: String,
#[arg(skip)]
after: Option<String>,
},
List,
Add {
id: String,
},
Remove {
id: String,
},
Move {
id: String,
position: usize,
},
#[command(skip)]
MoveAdjacent {
id: String,
up: bool,
},
Clear,
Play {
#[arg(long)]
auto_next: bool,
},
}
impl Action {
pub(crate) fn polls_browse(&self) -> bool {
matches!(
self,
Self::List { .. }
| Self::Bookmarks { .. }
| Self::Library { .. }
| Self::Episodes { .. }
| Self::Downloads {
action: DownloadAction::List
}
| Self::Queue {
action: QueueAction::List
}
)
}
pub(crate) fn is_query(&self) -> bool {
matches!(
self,
Self::Chapters { .. }
| Self::Bookmarks { .. }
| Self::Library { .. }
| Self::List { .. }
| Self::Episodes { .. }
| Self::Search { .. }
| Self::Browse { .. }
| Self::Categories { .. }
| Self::Charts { .. }
| Self::Discover { .. }
| Self::Preview { .. }
| Self::Downloads {
action: DownloadAction::List
}
| Self::Queue {
action: QueueAction::List
}
)
}
pub(crate) fn episode_id_mut(&mut self) -> Option<&mut String> {
match self {
Self::Jump { id, .. }
| Self::Chapters { id }
| Self::Bookmarks { id }
| Self::Bookmark { id, .. }
| Self::Save { id, .. }
| Self::Dismiss { id }
| Self::Play { id, .. }
| Self::Mark { id, .. }
| Self::Subtitles { id, .. }
| Self::Download { id } => Some(id),
Self::Queue {
action:
QueueAction::Add { id }
| QueueAction::Remove { id }
| QueueAction::Start { id, .. }
| QueueAction::Next { id, .. }
| QueueAction::Move { id, .. }
| QueueAction::MoveAdjacent { id, .. },
} => Some(id),
Self::Downloads {
action:
DownloadAction::Pause { id }
| DownloadAction::Resume { id }
| DownloadAction::Cancel { id }
| DownloadAction::Remove { id },
} => Some(id),
_ => None,
}
}
}
pub fn valid_speed(rate: f64) -> Result<f64> {
if !rate.is_finite() || !(0.5..=3.).contains(&rate) {
return Err(error(crate::t!("podcast_invalid_speed")));
}
Ok(rate)
}
pub fn parse_time(value: &str) -> Result<u64> {
fn millis(value: &str) -> Option<u64> {
let (clock, fraction) = value
.split_once('.')
.map_or((value, None), |(c, f)| (c, Some(f)));
let mut seconds = 0u64;
for (index, field) in clock.split(':').enumerate() {
if index >= 3 || field.is_empty() || !field.bytes().all(|c| c.is_ascii_digit()) {
return None;
}
let part = field.parse::<u64>().ok()?;
if index > 0 && part >= 60 {
return None;
}
seconds = seconds.checked_mul(60)?.checked_add(part)?;
}
let mut result = seconds.checked_mul(1000)?;
if let Some(fraction) = fraction {
if fraction.is_empty() || !fraction.bytes().all(|c| c.is_ascii_digit()) {
return None;
}
for (digit, weight) in fraction.bytes().zip([100, 10, 1]) {
result = result.checked_add(u64::from(digit - b'0') * weight)?;
}
}
(result <= i64::MAX as u64).then_some(result)
}
millis(value).ok_or_else(|| error(crate::t!("podcast_invalid_time")))
}
#[cfg(test)]
mod time_tests {
#[test]
fn validates_user_clock_fields_before_combining_them() {
for (v, ms) in [
("65", 65000),
("01:02.3456", 62345),
("2:03:04", 7384000),
("9223372036854775.807", i64::MAX as u64),
] {
assert_eq!(super::parse_time(v).unwrap(), ms);
}
for v in [
"1:-1",
"1:2:3:4",
"1:60",
"+2",
"1e3",
"1.5:20",
"1::20",
"NaN",
"9223372036854775.808",
] {
assert!(super::parse_time(v).is_err(), "{v}");
}
}
}
pub(super) fn page_result<T: Serialize>(items: &[T], total: usize, p: crate::cli::Page) -> Value {
json!({"items":items,"total":total,"offset":p.offset,"limit":if p.all {None}else{Some(p.limit)},"has_more":p.offset.saturating_add(items.len())<total})
}
pub fn refresh(
config: &Config,
store: &Store,
id: Option<&str>,
stop: &Arc<AtomicBool>,
) -> Result<Value> {
if stop.load(std::sync::atomic::Ordering::Relaxed) {
return Err(DomainError::new(ErrorCode::Cancelled).into());
}
let feeds = if let Some(id) = id {
vec![store.podcast_feed(id)?]
} else {
store.podcast_feeds()?
};
refresh_feeds(config, store, &feeds, stop)
}
pub(super) fn refresh_feeds(
config: &Config,
store: &Store,
feeds: &[Feed],
stop: &Arc<AtomicBool>,
) -> Result<Value> {
let mut results = vec![];
for batch in feeds.chunks(4) {
let fetched = std::thread::scope(|scope| {
let tasks: Vec<_> = batch
.iter()
.map(|f| scope.spawn(move || fetch(config, &f.url, Some(f), stop)))
.collect();
tasks
.into_iter()
.map(|t| {
t.join()
.unwrap_or_else(|_| Err(error(crate::t!("podcast_network"))))
})
.collect::<Vec<_>>()
});
for (f, fetched) in batch.iter().cloned().zip(fetched) {
if stop.load(std::sync::atomic::Ordering::Relaxed) {
return Err(DomainError::new(ErrorCode::Cancelled).into());
}
let result = fetched.and_then(|p| {
if stop.load(std::sync::atomic::Ordering::Relaxed) {
return Err(DomainError::new(ErrorCode::Cancelled).into());
}
store.podcast_refresh_merge(&f, p)
});
match result {
Ok(()) => {
if stop.load(std::sync::atomic::Ordering::Relaxed) {
return Err(DomainError::new(ErrorCode::Cancelled).into());
}
results.push(json!({"id":f.id,"title":f.title,"ok":true}));
}
Err(e) => {
if e.downcast_ref::<DomainError>()
.is_some_and(|e| e.code == ErrorCode::Cancelled)
{
return Err(e);
}
if stop.load(std::sync::atomic::Ordering::Relaxed) {
return Err(DomainError::new(ErrorCode::Cancelled).into());
}
let mut f = f;
f.error = Some(e.to_string());
f.failures = f.failures.saturating_add(1);
f.refreshed_at = Some(crate::timestamp::now());
store.podcast_refresh_error(&f)?;
results
.push(json!({"id":f.id,"title":f.title,"ok":false,"error":e.to_string()}));
}
}
}
}
Ok(json!(results))
}
pub(super) fn episode_value(
store: &Store,
e: &Episode,
is_file: impl FnOnce(&str) -> bool,
) -> Result<Value> {
let feed = store.podcast_feed(&e.feed_id)?;
let (saved, queued) = store.conn.query_row(
"SELECT EXISTS(SELECT 1 FROM podcast_saved WHERE episode_id=?1),
EXISTS(SELECT 1 FROM podcast_queue WHERE episode_id=?1)",
[&e.id],
|r| Ok((r.get(0)?, r.get(1)?)),
)?;
Ok(episode_details(
e,
&feed.title,
&feed.author,
saved,
queued,
store.podcast_download(&e.id)?.as_ref(),
is_file,
))
}
pub(super) fn episode_details(
e: &Episode,
feed_title: &str,
author: &str,
saved: bool,
queued: bool,
download: Option<&Download>,
is_file: impl FnOnce(&str) -> bool,
) -> Value {
let mut value = json!(e);
value["author"] = json!(author);
value["feed_title"] = json!(feed_title);
value["saved"] = json!(saved);
value["queued"] = json!(queued);
if let Some(d) = download {
value["download_state"] = json!(if d.phase() == DownloadState::Complete
&& d.offline_path_if(e, is_file).is_none()
{
"stale"
} else {
&d.state
});
}
value
}
pub fn manage(
config: &Config,
store: &Store,
action: Action,
stop: &Arc<AtomicBool>,
) -> Result<Value> {
let files = Arc::new(super::download_files::DownloadFiles::new(stop.clone()));
manage_with_files(config, store, action, stop, &files)
}
pub(super) fn manage_with_files(
config: &Config,
store: &Store,
mut action: Action,
stop: &Arc<AtomicBool>,
files: &Arc<super::download_files::DownloadFiles>,
) -> Result<Value> {
if let Some(id) = action.episode_id_mut() {
*id = store.podcast_episode_id(id)?;
}
Ok(match action {
Action::Chapters { id } => {
super::extras::chapters(config, &store.podcast_episode(&id)?, stop)?
}
Action::Bookmarks { id } => {
let rows: Vec<_> = store.podcast_bookmarks(&id)?.iter().map(|b| json!({"id":b.episode_id,"bookmark_id":b.id,"title":b.note,"position_ms":b.position_ms,"kind":"bookmark"})).collect();
json!({"items":rows,"total":rows.len()})
}
Action::Bookmark { id, time, note } => json!(
store.podcast_add_bookmark(
&id,
parse_time(
time.as_deref()
.ok_or_else(|| error(crate::t!("podcast_invalid_time")))?
)?,
¬e
)?
),
Action::BookmarkEdit { id, note } => {
store.podcast_edit_bookmark(&id, note.as_deref())?;
json!({"id":id,"removed":note.is_none()})
}
Action::Settings { id, key, value } => match (key, value) {
(None, None) => json!(store.podcast_settings(&id)?),
(Some(key), Some(value)) => json!(store.podcast_set_setting(&id, &key, &value)?),
_ => return Err(error(crate::t!("podcast_invalid_setting"))),
},
Action::Jump { .. } | Action::Sleep { .. } => {
return Err(error(crate::t!("podcast_playback_only")));
}
Action::Library { options } => store.podcast_library_with_files(&options, files)?,
Action::Save { id, remove } => {
store.podcast_save_episode(&id, remove)?;
json!({"id":id,"saved":!remove})
}
Action::Dismiss { id } => {
store.podcast_dismiss_inbox(&id)?;
json!({"id":id,"dismissed":true})
}
Action::MarkBatch { ids, unplayed } => {
let tx = rusqlite::Transaction::new_unchecked(
&store.conn,
rusqlite::TransactionBehavior::Immediate,
)?;
for id in &ids {
let mut e = store.podcast_episode(id)?;
e.mark(!unplayed);
Store::write_podcast_progress(&tx, &e.id, &e.progress())?;
}
tx.commit()?;
json!({"marked":ids.len()})
}
Action::Resume | Action::Relative { .. } => {
return Err(error(crate::t!("podcast_playback_only")));
}
Action::Browse { country } => directory::home(&directory::country(config, &country)?),
Action::Categories { country } => {
directory::categories(&directory::country(config, &country)?)
}
Action::Charts {
country,
category,
limit,
random,
} => directory::charts(
config,
store,
&directory::country(config, &country)?,
category.as_deref(),
limit,
random,
stop,
)?,
Action::Search {
query,
country,
limit,
} => {
let country = directory::country(config, &country)?;
let mut rows = search(config, &query, &country, limit.into(), stop)?;
directory::annotate_subscriptions(store, &mut rows)?;
json!(rows)
}
Action::Discover { url } => json!(discover(config, &url, stop)?),
Action::Preview { url } => {
let subscription_url = url.clone();
let resolved = if feed::is_apple_url(&url) {
let urls = discover(config, &url, stop)?;
if urls.len() != 1 {
return Err(error(crate::t!("podcast_no_public_feed")));
}
urls[0].clone()
} else {
url
};
let mut value = json!(fetch(config, &resolved, None, stop)?);
value["subscription_url"] = json!(subscription_url);
value
}
Action::Add { url } => {
let urls = discover(config, &url, stop)?;
if urls.len() != 1 {
return Err(error(crate::t!("podcast_choose_feed")));
}
let p = fetch(config, &urls[0], None, stop)?
.ok_or_else(|| error(crate::t!("podcast_invalid_feed")))?;
if stop.load(std::sync::atomic::Ordering::Relaxed) {
return Err(DomainError::new(ErrorCode::Cancelled).into());
}
json!(store.podcast_subscribe(&p, &url)?)
}
Action::List { page: p, query } => store.read_snapshot(|store| {
let query = query.to_lowercase();
let feeds: Vec<_> = store
.podcast_feeds()?
.into_iter()
.filter(|feed| feed.title.to_lowercase().contains(&query))
.collect();
let total = feeds.len();
let limit = if p.all { total } else { usize::from(p.limit) };
let mut rows = Vec::new();
for feed in feeds.into_iter().skip(p.offset).take(limit) {
let mut value = json!(feed);
value["unplayed_count"] = json!(store.podcast_unplayed_count(&feed.id)?);
value["sort"] = json!(store.podcast_settings(&feed.id)?.sort);
rows.push(value);
}
Ok(page_result(&rows, total, p))
})?,
Action::Episodes { id, query, page: p } => {
let (items, total) = store.podcast_episodes_page(&id, &query, &p)?;
let items = items
.iter()
.map(|e| episode_value(store, e, |path| files.is_file(path)))
.collect::<Result<Vec<_>>>()?;
let mut value = page_result(&items, total, p);
value["feed_title"] = json!(store.podcast_feed(&id)?.title);
value
}
Action::Refresh { id } => refresh(config, store, id.as_deref(), stop)?,
Action::Unsubscribe { id } => {
store.podcast_unsubscribe(&id)?;
json!({"id":id,"subscribed":false})
}
Action::Mark { id, state } => {
let mut e = store.podcast_episode(&id)?;
e.mark(state == "played");
store.podcast_save_progress(&id, &e.progress())?;
json!(store.podcast_episode(&id)?)
}
Action::Subtitles {
id,
file,
transcribe,
} => {
super::transcript::manage(config, store, &id, file.as_deref(), transcribe, stop, None)?
}
Action::Download { id } => json!(super::download::foreground(config, store, &id, stop)?),
Action::Downloads { action } => match action {
DownloadAction::Stats => {
let rows = store.podcast_downloads()?;
let bytes: u64 = rows.iter().map(|d| d.bytes).sum();
let directory = config
.settings
.podcasts
.directory
.clone()
.unwrap_or_else(|| config.data_dir.join("podcasts"));
json!({"downloads":rows.len(),"bytes":bytes,"completed":rows.iter().filter(|d| d.phase() == DownloadState::Complete).count(),"available_bytes":fs2::available_space(directory).ok()})
}
DownloadAction::List => {
let mut rows = Vec::new();
for d in store.podcast_downloads()? {
let mut value =
episode_value(store, &store.podcast_episode(&d.episode_id)?, |path| {
files.is_file(path)
})?;
value
.as_object_mut()
.unwrap()
.extend(json!(d).as_object().unwrap().clone());
rows.push(value);
}
json!(rows)
}
DownloadAction::Resume { id } => {
json!(super::download::foreground(config, store, &id, stop)?)
}
DownloadAction::Pause { ref id }
| DownloadAction::Cancel { ref id }
| DownloadAction::Remove { ref id } => {
let d = store.podcast_download_transaction(id, |current| {
let mut d = current.ok_or_else(|| error(crate::t!("podcast_not_found")))?;
if matches!(action, DownloadAction::Remove { .. }) {
super::download::remove(config, store, &mut d)?;
return Ok(d);
}
if d.phase() == DownloadState::Complete {
return Ok(d);
}
d.set_phase(if matches!(action, DownloadAction::Pause { .. }) {
DownloadState::Paused
} else {
DownloadState::Cancelled
});
d.retry_at = None;
d.generation = d.generation.saturating_add(1);
store.podcast_save_download(&d)?;
Ok(d)
})?;
json!(d)
}
},
Action::Queue {
action: QueueAction::Start { .. },
} => return Err(error(crate::t!("podcast_playback_only"))),
Action::Queue {
action: QueueAction::List,
} => files.read_page(store, |store| {
let items = store
.podcast_queue()?
.iter()
.map(|e| episode_value(store, e, |path| files.observed_file(path)))
.collect::<Result<Vec<_>>>()?;
Ok(json!(items))
})?,
Action::Queue { action } => {
json!(store.podcast_queue_update(action)?)
}
Action::Export { file, overwrite } => {
let feeds = store.podcast_feeds()?;
let mut text = String::from(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><opml version=\"2.0\"><head><title>radio-cli</title></head><body>",
);
for f in &feeds {
text.push_str(&format!(
"<outline type=\"rss\" text=\"{}\" xmlUrl=\"{}\" htmlUrl=\"{}\"/>",
quick_xml::escape::escape(&f.title),
quick_xml::escape::escape(&f.url),
quick_xml::escape::escape(&f.website)
));
}
text.push_str("</body></opml>");
write_export(&file, overwrite, |f| {
std::io::Write::write_all(f, text.as_bytes())
})?;
json!({"exported":feeds.len()})
}
Action::Import { file, dry_run } => {
if stop.load(std::sync::atomic::Ordering::Relaxed) {
return Err(DomainError::new(ErrorCode::Cancelled).into());
}
use std::io::Read;
let mut data = Vec::new();
std::fs::File::open(file)?
.take(10 * 1024 * 1024 + 1)
.read_to_end(&mut data)?;
let urls = opml(&data)?;
if dry_run {
json!({"urls":urls})
} else {
let mut results = vec![];
for batch in urls.chunks(4) {
let fetched = std::thread::scope(|scope| {
let tasks: Vec<_> = batch
.iter()
.map(|u| scope.spawn(move || fetch(config, u, None, stop)))
.collect();
tasks
.into_iter()
.map(|t| {
t.join()
.unwrap_or_else(|_| Err(error(crate::t!("podcast_network"))))
})
.collect::<Vec<_>>()
});
for (u, fetched) in batch.iter().zip(fetched) {
if stop.load(std::sync::atomic::Ordering::Relaxed) {
return Err(DomainError::new(ErrorCode::Cancelled).into());
}
match fetched.and_then(|p| {
if stop.load(std::sync::atomic::Ordering::Relaxed) {
return Err(DomainError::new(ErrorCode::Cancelled).into());
}
store.podcast_subscribe(
&p.ok_or_else(|| error(crate::t!("podcast_invalid_feed")))?,
u,
)
}) {
Ok(f) => results.push(json!({"id":f.id,"ok":true})),
Err(e) => {
if e.downcast_ref::<DomainError>()
.is_some_and(|e| e.code == ErrorCode::Cancelled)
{
return Err(e);
}
if stop.load(std::sync::atomic::Ordering::Relaxed) {
return Err(DomainError::new(ErrorCode::Cancelled).into());
}
results.push(json!({"ok":false,"error":e.to_string()}));
}
}
}
}
json!(results)
}
}
Action::AutoNext { state } => {
store.set_meta("podcast_auto_next", &state)?;
json!({"auto_next":state=="on"})
}
Action::Speed { rate } => {
valid_speed(rate)?;
store.set_meta("podcast_speed", &rate.to_string())?;
json!({"speed":rate})
}
Action::Seek { .. } | Action::Play { .. } => {
return Err(error(crate::t!("podcast_playback_only")));
}
})
}
fn write_export(
path: &std::path::Path,
overwrite: bool,
write: impl FnOnce(&mut std::fs::File) -> std::io::Result<()>,
) -> Result<()> {
use std::io::ErrorKind;
use std::os::unix::fs::PermissionsExt;
let mut target = path.to_owned();
let mut builder = tempfile::Builder::new();
let mut existing = None;
if overwrite {
match std::fs::symlink_metadata(path) {
Ok(_) => {
target = path.canonicalize()?;
anyhow::ensure!(
std::fs::metadata(&target)?.is_file(),
"{}",
crate::t!("podcast_file_exists")
);
existing = Some(std::fs::OpenOptions::new().write(true).open(&target)?);
}
Err(e) if e.kind() == ErrorKind::NotFound => (),
Err(e) => return Err(e.into()),
}
}
if existing.is_none() {
builder.permissions(std::fs::Permissions::from_mode(0o666));
}
let directory = target
.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or(std::path::Path::new("."));
let mut temp = builder.tempfile_in(directory)?;
let permissions = existing
.as_ref()
.map(|source| crate::files::prepare_replacement_permissions(source, temp.as_file()))
.transpose()?;
write(temp.as_file_mut())?;
if let Some(permissions) = permissions {
temp.as_file().set_permissions(permissions)?;
}
temp.as_file().sync_all()?;
if existing.is_some() {
temp.persist(target)?;
} else {
temp.persist_noclobber(target)?;
}
Ok(())
}
#[cfg(test)]
mod export_tests {
use super::*;
use std::{
io::Write,
os::unix::fs::{PermissionsExt, symlink},
};
#[cfg(target_os = "linux")]
#[test]
fn overwrite_preserves_named_acl_entries_and_removes_inherited_grants() -> Result<()> {
use rustix::fs::{XattrFlags, getxattr, removexattr, setxattr};
use rustix::io::Errno;
let named_reader = |uid: u32| {
let mut bytes = 2u32.to_le_bytes().to_vec();
for (tag, permissions, id) in [
(1u16, 6u16, u32::MAX), (2, 4, uid), (4, 0, u32::MAX), (16, 4, u32::MAX), (32, 0, u32::MAX), ] {
bytes.extend(tag.to_le_bytes());
bytes.extend(permissions.to_le_bytes());
bytes.extend(id.to_le_bytes());
}
bytes
};
let dir = tempfile::tempdir()?;
let target = dir.path().join("feeds.opml");
std::fs::write(&target, b"old")?;
let access = c"system.posix_acl_access";
let original = named_reader(65534);
match setxattr(&target, access, &original, XattrFlags::empty()) {
Err(Errno::NOTSUP) => return Ok(()),
result => result?,
}
setxattr(
dir.path(),
c"system.posix_acl_default",
&named_reader(65533),
XattrFlags::empty(),
)?;
for extended in [true, false] {
if !extended {
removexattr(&target, access)?;
}
let mode = std::fs::metadata(&target)?.permissions().mode();
write_export(&target, true, |f| f.write_all(b"new"))?;
assert_eq!(std::fs::metadata(&target)?.permissions().mode(), mode);
let mut bytes = [0; 128];
let acl = getxattr(&target, access, &mut bytes[..]);
if extended {
assert_eq!(mode & 0o777, 0o640);
assert_eq!(&bytes[..acl?], original);
} else {
assert_eq!(acl, Err(Errno::NODATA));
}
assert_eq!(std::fs::read(&target)?, b"new");
}
Ok(())
}
#[cfg(target_os = "macos")]
#[test]
fn overwrite_preserves_darwin_access_entries() -> Result<()> {
use std::process::Command;
let dir = tempfile::tempdir()?;
let target = dir.path().join("feeds.opml");
std::fs::write(&target, b"old")?;
anyhow::ensure!(
Command::new("/bin/chmod")
.args(["+a", "everyone allow read"])
.arg(&target)
.status()?
.success()
);
let entries = || -> Result<Vec<String>> {
let listing = Command::new("/bin/ls").arg("-le").arg(&target).output()?;
anyhow::ensure!(listing.status.success());
Ok(String::from_utf8(listing.stdout)?
.lines()
.skip(1)
.map(str::to_owned)
.collect())
};
let before = entries()?;
assert!(!before.is_empty());
write_export(&target, true, |f| f.write_all(b"new"))?;
assert_eq!(entries()?, before);
Ok(())
}
#[test]
fn interrupted_export_preserves_the_old_file_and_its_symlink() -> Result<()> {
let dir = tempfile::tempdir()?;
let file = dir.path().join("old");
let link = dir.path().join("link");
std::fs::write(&file, b"old")?;
std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o4640))?;
symlink(&file, &link)?;
assert!(
write_export(&link, true, |f| {
f.write_all(b"incomplete")?;
Err(std::io::Error::other("disk full"))
})
.is_err()
);
assert_eq!(std::fs::read(&link)?, b"old");
write_export(&link, true, |f| f.write_all(b"new"))?;
assert!(link.is_symlink());
assert_eq!(std::fs::read(&file)?, b"new");
assert_eq!(
std::fs::metadata(file)?.permissions().mode() & 0o7777,
0o4640
);
Ok(())
}
}
fn opml(data: &[u8]) -> Result<Vec<String>> {
use quick_xml::{Reader, events::Event};
if data.len() > 10 * 1024 * 1024 {
return Err(error(crate::t!("podcast_limit")));
}
let mut reader = Reader::from_reader(data);
let mut depth = 0;
let mut root = false;
let mut urls = vec![];
let mut seen = std::collections::HashSet::new();
loop {
let event = reader
.read_event()
.map_err(|_| error(crate::t!("podcast_invalid_feed")))?;
match &event {
Event::Start(e) | Event::Empty(e) => {
if depth == 0 && (root || e.local_name().as_ref() != "opml") {
return Err(error(crate::t!("podcast_invalid_feed")));
}
root = true;
if e.local_name().as_ref() == "outline" {
for a in e.attributes() {
let a = a.map_err(|_| error(crate::t!("podcast_invalid_feed")))?;
if a.key.as_ref() == "xmlUrl" {
let value = a
.normalized_value(quick_xml::XmlVersion::Implicit1_0)
.map_err(|_| error(crate::t!("podcast_invalid_feed")))?;
let u = super::feed::url(&value, None)?.to_string();
if seen.insert(u.clone()) {
urls.push(u);
}
}
}
}
if matches!(event, Event::Start(_)) {
depth += 1;
}
if depth > 64 || urls.len() > 10000 {
return Err(error(crate::t!("podcast_limit")));
}
}
Event::End(_) => {
if depth == 0 {
return Err(error(crate::t!("podcast_invalid_feed")));
}
depth -= 1;
}
Event::DocType(_) => return Err(error(crate::t!("podcast_invalid_feed"))),
Event::Eof => break,
_ => {}
}
}
if !root || depth != 0 {
return Err(error(crate::t!("podcast_invalid_feed")));
}
Ok(urls)
}