summaryrefslogtreecommitdiffstats
path: root/OvmfPkg/VirtioFsDxe/SimpleFsSetPosition.c
blob: a548012d9c09cc2d04d898c47aefc4a4350ee775 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/** @file
  EFI_FILE_PROTOCOL.SetPosition() member function for the Virtio Filesystem
  driver.

  Copyright (C) 2020, Red Hat, Inc.

  SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <Library/MemoryAllocationLib.h> // FreePool()

#include "VirtioFsDxe.h"

EFI_STATUS
EFIAPI
VirtioFsSimpleFileSetPosition (
  IN EFI_FILE_PROTOCOL *This,
  IN UINT64            Position
  )
{
  VIRTIO_FS_FILE                     *VirtioFsFile;
  VIRTIO_FS                          *VirtioFs;
  EFI_STATUS                         Status;
  VIRTIO_FS_FUSE_ATTRIBUTES_RESPONSE FuseAttr;

  VirtioFsFile = VIRTIO_FS_FILE_FROM_SIMPLE_FILE (This);

  //
  // Directories can only be rewound, per spec.
  //
  if (VirtioFsFile->IsDirectory) {
    if (Position != 0) {
      return EFI_UNSUPPORTED;
    }
    VirtioFsFile->FilePosition = 0;
    if (VirtioFsFile->FileInfoArray != NULL) {
      FreePool (VirtioFsFile->FileInfoArray);
      VirtioFsFile->FileInfoArray = NULL;
    }
    VirtioFsFile->SingleFileInfoSize = 0;
    VirtioFsFile->NumFileInfo        = 0;
    VirtioFsFile->NextFileInfo       = 0;
    return EFI_SUCCESS;
  }

  //
  // Regular file.
  //
  if (Position < MAX_UINT64) {
    //
    // Caller is requesting absolute file position.
    //
    VirtioFsFile->FilePosition = Position;
    return EFI_SUCCESS;
  }

  //
  // Caller is requesting a seek to EOF.
  //
  VirtioFs = VirtioFsFile->OwnerFs;
  Status = VirtioFsFuseGetAttr (VirtioFs, VirtioFsFile->NodeId, &FuseAttr);
  if (EFI_ERROR (Status)) {
    return Status;
  }
  VirtioFsFile->FilePosition = FuseAttr.Size;
  return EFI_SUCCESS;
}