rename moves 'oldParentID/oldName' to 'newParentID/newName'. If 'newParentID/newName' already exists, it is deleted. See NOTE on node.go:Rename.
( ctx context.Context, oldParentID, newParentID uint64, oldName, newName string, )
| 203 | // If 'newParentID/newName' already exists, it is deleted. |
| 204 | // See NOTE on node.go:Rename. |
| 205 | func (cfs CFS) rename( |
| 206 | ctx context.Context, oldParentID, newParentID uint64, oldName, newName string, |
| 207 | ) error { |
| 208 | if oldParentID == newParentID && oldName == newName { |
| 209 | return nil |
| 210 | } |
| 211 | |
| 212 | const deleteNamespace = `DELETE FROM fs_namespace WHERE (parentID, name) = (?, ?)` |
| 213 | const insertNamespace = `INSERT INTO fs_namespace VALUES (?, ?, ?)` |
| 214 | const updateNamespace = `UPDATE fs_namespace SET id = ? WHERE (parentID, name) = (?, ?)` |
| 215 | const deleteInode = `DELETE FROM fs_inode WHERE id = ?` |
| 216 | |
| 217 | // Lookup source inode. |
| 218 | srcObject, err := getInode(cfs.db, oldParentID, oldName) |
| 219 | if err != nil { |
| 220 | return err |
| 221 | } |
| 222 | |
| 223 | // Lookup destination inode. |
| 224 | destObject, err := getInode(cfs.db, newParentID, newName) |
| 225 | if err != nil && err != sql.ErrNoRows { |
| 226 | return err |
| 227 | } |
| 228 | |
| 229 | // Check that the rename is allowed. |
| 230 | if err := validateRename(cfs.db, srcObject, destObject); err != nil { |
| 231 | return err |
| 232 | } |
| 233 | |
| 234 | err = client.ExecuteTx(ctx, cfs.db, nil /* txopts */, func(tx *sql.Tx) error { |
| 235 | // At this point we know the following: |
| 236 | // - srcObject is not nil |
| 237 | // - destObject may be nil. If not, its inode can be deleted. |
| 238 | if destObject == nil { |
| 239 | // No new object: use INSERT. |
| 240 | if _, err := tx.Exec(deleteNamespace, oldParentID, oldName); err != nil { |
| 241 | return err |
| 242 | } |
| 243 | |
| 244 | if _, err := tx.Exec(insertNamespace, newParentID, newName, srcObject.ID); err != nil { |
| 245 | return err |
| 246 | } |
| 247 | } else { |
| 248 | // Destination exists. |
| 249 | if _, err := tx.Exec(deleteNamespace, oldParentID, oldName); err != nil { |
| 250 | return err |
| 251 | } |
| 252 | |
| 253 | if _, err := tx.Exec(updateNamespace, srcObject.ID, newParentID, newName); err != nil { |
| 254 | return err |
| 255 | } |
| 256 | |
| 257 | if _, err := tx.Exec(deleteInode, destObject.ID); err != nil { |
| 258 | return err |
| 259 | } |
| 260 | } |
| 261 | return nil |
| 262 | }) |
no test coverage detected