Write writes data to 'n'. It may overwrite existing data, or grow it.
(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse)
| 285 | |
| 286 | // Write writes data to 'n'. It may overwrite existing data, or grow it. |
| 287 | func (n *Node) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error { |
| 288 | if !n.isRegular() { |
| 289 | return fuse.Errno(syscall.EINVAL) |
| 290 | } |
| 291 | if req.Offset < 0 { |
| 292 | return fuse.Errno(syscall.EINVAL) |
| 293 | } |
| 294 | if len(req.Data) == 0 { |
| 295 | return nil |
| 296 | } |
| 297 | |
| 298 | n.mu.Lock() |
| 299 | defer n.mu.Unlock() |
| 300 | |
| 301 | newSize := uint64(req.Offset) + uint64(len(req.Data)) |
| 302 | if newSize > maxSize { |
| 303 | return fuse.Errno(syscall.EFBIG) |
| 304 | } |
| 305 | |
| 306 | // Store the current size in case we need to rollback. |
| 307 | originalSize := n.Size |
| 308 | |
| 309 | // Wrap everything inside a transaction. |
| 310 | err := client.ExecuteTx(ctx, n.cfs.db, nil /* txopts */, func(tx *sql.Tx) error { |
| 311 | |
| 312 | // Update blocks. They will be added as needed. |
| 313 | if err := write(tx, n.ID, n.Size, uint64(req.Offset), req.Data); err != nil { |
| 314 | return err |
| 315 | } |
| 316 | |
| 317 | if newSize > originalSize { |
| 318 | // This was an append, commit the size change. |
| 319 | n.Size = newSize |
| 320 | if err := updateNode(tx, n); err != nil { |
| 321 | return err |
| 322 | } |
| 323 | } |
| 324 | return nil |
| 325 | }) |
| 326 | |
| 327 | if err != nil { |
| 328 | // Reset our size. |
| 329 | log.Print(err) |
| 330 | n.Size = originalSize |
| 331 | return err |
| 332 | } |
| 333 | |
| 334 | // We always write everything. |
| 335 | resp.Size = len(req.Data) |
| 336 | return nil |
| 337 | } |
| 338 | |
| 339 | // Read reads data from 'n'. |
| 340 | func (n *Node) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { |
nothing calls this directly
no test coverage detected