Applies a Create operation to create a new file. # Behavior 1. Checks if the trunk ID already exists (error if so, unless duplicates allowed) 2. Checks if the path is already taken (error if so) 3. Allocates a new inode 4. Creates the trunk entry in TRUNKS table 5. Updates PATH_TRUNK and INODE_TRUNK indexes # Errors - `TrunkAlreadyExists` - The trunk ID is already in use - `PathAlreadyExists`
(
txn: &mut T,
context: &mut ApplyContext,
trunk_id: TrunkId,
path: &str,
encoding: Option<Encoding>,
)
| 100 | /// - `TrunkAlreadyExists` - The trunk ID is already in use |
| 101 | /// - `PathAlreadyExists` - The path is occupied by another file |
| 102 | fn apply_create<T: MutCrdtTxnT>( |
| 103 | txn: &mut T, |
| 104 | context: &mut ApplyContext, |
| 105 | trunk_id: TrunkId, |
| 106 | path: &str, |
| 107 | encoding: Option<Encoding>, |
| 108 | ) -> ApplyResult<()> { |
| 109 | // Check for duplicate ID |
| 110 | if txn |
| 111 | .has_trunk(trunk_id) |
| 112 | .map_err(|e| storage_err(e, "checking trunk exists"))? |
| 113 | { |
| 114 | if context.options().allow_duplicate_ids() { |
| 115 | context.record_skipped(); |
| 116 | return Ok(()); |
| 117 | } |
| 118 | return Err(ApplyError::trunk_already_exists(trunk_id)); |
| 119 | } |
| 120 | |
| 121 | // Check for path collision |
| 122 | if let Some(existing) = txn |
| 123 | .get_trunk_by_path(path) |
| 124 | .map_err(|e| storage_err(e, "checking path exists"))? |
| 125 | { |
| 126 | return Err(ApplyError::path_already_exists(path, existing)); |
| 127 | } |
| 128 | |
| 129 | // Allocate inode and create trunk |
| 130 | let inode = txn |
| 131 | .alloc_inode() |
| 132 | .map_err(|e| storage_err(e, "allocating inode"))?; |
| 133 | let trunk = Trunk::new(trunk_id, inode, path.to_string(), encoding); |
| 134 | |
| 135 | txn.put_trunk(&trunk) |
| 136 | .map_err(|e| storage_err(e, "inserting trunk"))?; |
| 137 | |
| 138 | context.record_trunk_created(); |
| 139 | Ok(()) |
| 140 | } |
| 141 | |
| 142 | // Delete Operation |
| 143 |
no test coverage detected