1 // Copyright 2022 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 use crate::ffi::ClientResponseReadable; 16 use std::fs::File; 17 /// Implements handler for pcap operations 18 use std::io::Write; 19 use std::path::PathBuf; 20 use tracing::error; 21 22 pub struct FileHandler { 23 pub file: File, 24 pub path: PathBuf, 25 } 26 27 impl ClientResponseReadable for FileHandler { 28 // function to handle writing each chunk to file handle_chunk(&self, chunk: &[u8])29 fn handle_chunk(&self, chunk: &[u8]) { 30 (&self.file) 31 .write_all(chunk) 32 .unwrap_or_else(|_| panic!("Unable to write to file: {}", self.path.display())); 33 } 34 // function to handle error response handle_error(&self, error_code: u32, error_message: &str)35 fn handle_error(&self, error_code: u32, error_message: &str) { 36 error!( 37 "Handling error code: {}, msg: {}, on file: {}", 38 error_code, 39 error_message, 40 self.path.display() 41 ); 42 } 43 } 44