Serialize and append one event, returning any I/O error. The complete record (JSON plus newline) is appended as one atomic O_APPEND write on a confined regular file, after reserving its full size against the retention cap under a nonblocking exclusive lock. Symlinks and non-regular files are refused; when the append lock is contended beyond a bounded retry budget the event is dropped.
(&self, kind: BridgeEventKind)
| 751 | /// Symlinks and non-regular files are refused; when the append lock is |
| 752 | /// contended beyond a bounded retry budget the event is dropped. |
| 753 | pub fn emit(&self, kind: BridgeEventKind) -> Result<(), std::io::Error> { |
| 754 | self.refresh_consent(); |
| 755 | if !self.consented.load(std::sync::atomic::Ordering::Relaxed) { |
| 756 | return Ok(()); |
| 757 | } |
| 758 | log_event(&kind); |
| 759 | // Serialize and bound the record before touching the filesystem: |
| 760 | // no oversized allocation or append can happen. |
| 761 | let event = BridgeEvent { |
| 762 | timestamp_ms: now_timestamp_ms(), |
| 763 | working_copy: self.working_copy.clone(), |
| 764 | kind, |
| 765 | }; |
| 766 | let json = serde_json::to_string(&event) |
| 767 | .map_err(|error| std::io::Error::new(std::io::ErrorKind::InvalidData, error))?; |
| 768 | let mut record = json.into_bytes(); |
| 769 | record.push(b'\n'); |
| 770 | if record.len() > MAX_BRIDGE_EVENT_RECORD_BYTES { |
| 771 | return Ok(()); |
| 772 | } |
| 773 | |
| 774 | // CB-13C F1: anchored no-follow traversal — every ancestor |
| 775 | // component from the anchor dot directory down is opened |
| 776 | // O_NOFOLLOW|O_DIRECTORY, so a symlinked `.atomic/bridge` (or any |
| 777 | // ancestor) cannot redirect the journal into an external |
| 778 | // directory. Only after the anchored walk succeeds is the final |
| 779 | // file opened O_NOFOLLOW. |
| 780 | self.open_anchored_ancestors()?; |
| 781 | #[cfg(unix)] |
| 782 | let mut file = std::fs::OpenOptions::new() |
| 783 | .read(true) |
| 784 | .create(true) |
| 785 | .append(true) |
| 786 | .custom_flags(OPEN_NO_FOLLOW | OPEN_NONBLOCK) |
| 787 | .open(&self.path)?; |
| 788 | #[cfg(not(unix))] |
| 789 | let mut file = std::fs::OpenOptions::new() |
| 790 | .read(true) |
| 791 | .create(true) |
| 792 | .append(true) |
| 793 | .open(&self.path)?; |
| 794 | // Confine to regular files: a FIFO or device node is refused |
| 795 | // instead of blocking or writing to it. |
| 796 | if !file.metadata()?.file_type().is_file() { |
| 797 | return Err(std::io::Error::new( |
| 798 | std::io::ErrorKind::Unsupported, |
| 799 | "event journal path is not a regular file", |
| 800 | )); |
| 801 | } |
| 802 | |
| 803 | // Nonblocking exclusive append lock (leaf lock: acquired only for |
| 804 | // the bounded repair/cap-check/append sequence, never while any |
| 805 | // other lock is held, so it cannot participate in a lock cycle). |
| 806 | let mut locked = false; |
| 807 | let mut backoff = Duration::from_micros(50); |
| 808 | for _ in 0..APPEND_LOCK_ATTEMPTS { |
| 809 | match file.try_lock_exclusive() { |
| 810 | Ok(()) => { |