1/*
2 * Copyright 2008, Haiku, Inc.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		François Revol <revol@free.fr>
7 */
8#ifndef STREAM_H
9#define STREAM_H
10
11#include "fatfs.h"
12#include "Volume.h"
13
14#include <sys/stat.h>
15
16#define CLUSTER_MAP_CACHE_SIZE 50
17
18class Node;
19
20struct file_map_run;
21
22namespace FATFS {
23
24class Stream {
25	public:
26		Stream(Volume &volume, uint32 chain, off_t size, const char *name);
27		~Stream();
28
29		status_t InitCheck();
30		Volume &GetVolume() const { return fVolume; }
31
32		status_t GetName(char *nameBuffer, size_t bufferSize) const;
33		status_t GetFileMap(struct file_map_run *runs, int32 *count);
34		off_t	Size() const { return fSize; }
35		uint32	FirstCluster() const { return fFirstCluster; }
36
37		void	SetSize(off_t size)		{ fSize = size; }
38
39		status_t ReadAt(off_t pos, void *buffer, size_t *length,
40					off_t *diskOffset = NULL);
41		status_t WriteAt(off_t pos, const void *buffer, size_t *length,
42					off_t *diskOffset = NULL);
43
44	private:
45		status_t		BuildClusterList();
46		status_t		_FindCluster(off_t pos, uint32& _cluster);
47		status_t		_FindOrCreateCluster(off_t pos, uint32& _cluster,
48							bool& _added);
49		status_t		FindBlock(off_t pos, off_t &block, off_t &offset);
50
51		Volume			&fVolume;
52		uint32			fFirstCluster;
53		uint32			fClusterCount;
54		//uint32			*fClusters; // [fClusterCount]
55		struct {
56			off_t block;
57			uint32 cluster;
58		}			fClusterMapCache[CLUSTER_MAP_CACHE_SIZE];
59		int			fClusterMapCacheLast;
60		off_t			fSize;
61		// we cache the name here, since FAT doesn't have inodes,
62		// let alone names inside.
63		char			fName[FATFS_NAME_LENGTH+1];
64};
65
66}	// namespace FATFS
67
68#endif	/* STREAM_H */
69