1// File.cpp
2
3#include "AllocationInfo.h"
4#include "File.h"
5#include "SizeIndex.h"
6#include "Volume.h"
7
8// constructor
9File::File(Volume *volume)
10	: Node(volume, NODE_TYPE_FILE),
11	  DataContainer(volume)
12{
13}
14
15// destructor
16File::~File()
17{
18}
19
20// ReadAt
21status_t
22File::ReadAt(off_t offset, void *buffer, size_t size, size_t *bytesRead)
23{
24	status_t error = DataContainer::ReadAt(offset, buffer, size, bytesRead);
25	// TODO: update access time?
26	return error;
27}
28
29// WriteAt
30status_t
31File::WriteAt(off_t offset, const void *buffer, size_t size,
32			  size_t *bytesWritten)
33{
34	off_t oldSize = DataContainer::GetSize();
35	status_t error = DataContainer::WriteAt(offset, buffer, size,
36											bytesWritten);
37	MarkModified();
38	// update the size index, if our size has changed
39	if (oldSize != DataContainer::GetSize()) {
40		if (SizeIndex *index = GetVolume()->GetSizeIndex())
41			index->Changed(this, oldSize);
42	}
43	return error;
44}
45
46// SetSize
47status_t
48File::SetSize(off_t newSize)
49{
50	status_t error = B_OK;
51	off_t oldSize = DataContainer::GetSize();
52	if (newSize != oldSize) {
53		error = DataContainer::Resize(newSize);
54		MarkModified();
55		// update the size index
56		if (SizeIndex *index = GetVolume()->GetSizeIndex())
57			index->Changed(this, oldSize);
58	}
59	return error;
60}
61
62// GetSize
63off_t
64File::GetSize() const
65{
66	return DataContainer::GetSize();
67}
68
69// GetAllocationInfo
70void
71File::GetAllocationInfo(AllocationInfo &info)
72{
73	info.AddFileAllocation(GetSize());
74}
75
76