Create a new mapping using the current configuration
(mut self)
| 102 | |
| 103 | /// Create a new mapping using the current configuration |
| 104 | pub fn create(mut self) -> Result<Shmem, ShmemError> { |
| 105 | if self.size == 0 { |
| 106 | return Err(ShmemError::MapSizeZero); |
| 107 | } |
| 108 | |
| 109 | if let Some(ref flink_path) = self.flink_path { |
| 110 | if !self.overwrite_flink && flink_path.is_file() { |
| 111 | return Err(ShmemError::LinkExists); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Create the mapping |
| 116 | let mapping = match self.os_id { |
| 117 | None => { |
| 118 | // Generate random ID until one works |
| 119 | loop { |
| 120 | let cur_id = format!("/shmem_{:X}", rand::random::<u64>()); |
| 121 | match os_impl::create_mapping(&cur_id, self.size) { |
| 122 | Err(ShmemError::MappingIdExists) => continue, |
| 123 | Ok(m) => break m, |
| 124 | Err(e) => { |
| 125 | return Err(e); |
| 126 | } |
| 127 | }; |
| 128 | } |
| 129 | } |
| 130 | Some(ref specific_id) => os_impl::create_mapping(specific_id, self.size)?, |
| 131 | }; |
| 132 | debug!("Created shared memory mapping '{}'", mapping.unique_id); |
| 133 | |
| 134 | // Create flink |
| 135 | if let Some(ref flink_path) = self.flink_path { |
| 136 | debug!("Creating file link that points to mapping"); |
| 137 | let mut open_options: OpenOptions = OpenOptions::new(); |
| 138 | open_options.write(true); |
| 139 | |
| 140 | if self.overwrite_flink { |
| 141 | open_options.create(true).truncate(true); |
| 142 | } else { |
| 143 | open_options.create_new(true); |
| 144 | } |
| 145 | |
| 146 | match open_options.open(flink_path) { |
| 147 | Ok(mut f) => { |
| 148 | // write the shmem uid asap |
| 149 | if let Err(e) = f.write(mapping.unique_id.as_bytes()) { |
| 150 | let _ = std::fs::remove_file(flink_path); |
| 151 | return Err(ShmemError::LinkWriteFailed(e)); |
| 152 | } |
| 153 | } |
| 154 | Err(e) if e.kind() == ErrorKind::AlreadyExists => { |
| 155 | return Err(ShmemError::LinkExists) |
| 156 | } |
| 157 | Err(e) => return Err(ShmemError::LinkCreateFailed(e)), |
| 158 | } |
| 159 | |
| 160 | debug!( |
| 161 | "Created file link '{}' with id '{}'", |