Documentation
use e_utils::{
  fs::{FileExt, FileOptionsExt, FileShare},
  parse::AutoPath as _,
  system::time::{TimezoneStrategy, LOG_FILE_DATE_FORMAT},
};
use std::{
  fs,
  io::{self, Write},
  path::Path,
  time::Duration,
};

/// 只读不可删除
fn main() -> Result<(), io::Error> {
  let now = TimezoneStrategy::UseUtc
    .get_now()
    .format(LOG_FILE_DATE_FORMAT);
  let fpath = Path::new("logs").join(format!("test.{}.log", now));
  fpath.auto_create_dir().unwrap();
  let mut f = fs::OpenOptions::new()
    .create(true)
    .write(true)
    .read(true)
    .append(true)
    .lock_share(FileShare::Read)
    .open(&fpath)?;
  f.write("locked\n".as_bytes())?;
  for i in 0..30 {
    let _ = f
      .write(format!("{i}.locke write\n").as_bytes())
      .inspect_err(|e| eprintln!("{}", e));
    if i > 20 {
      let _ = f.unlock();
    }
    let x = f.allocated_size()?;
    println!("allocated -> {x}");
    std::thread::sleep(Duration::from_millis(500));
  }
  Ok(())
}