
#ifndef STORE_H
#define STORE_H

#include <pthread.h>
#include "Types.h"

class File;
class Server;
class Session;

class Store
{
	pthread_mutex_t mutex;
	File *spoolDir;
	int maxOpenSessions, sessionsNum, sessionsTotal;
	Session **sessions;
	Queue *queue;
	int32
		fsBlockSize, reservedBlocks,
		maxSizeInKBytes, sizeInKBytes;
	boolean *imageInSpoolDirFlags;

public:
	Store();
	~Store();
	Session *createSession(Server *server,
		const char *user, const char *password, const char *name,
		int overwrite);
	void getInfo(
		const char *user, const char *password, const char *name,
		int &files, int &tracks, int &size_KB);
	int getMaxSizeInMB() { return maxSizeInKBytes / 1024; }
	static File *getSessionDir(const char *sessionID);
	int getSessionsNum() { return sessionsNum; }
	int getSessionsTotal() { return sessionsTotal; }
	int getSizeInMB() { return sizeInKBytes / 1024; }
	int list(const char *userID, const char *userPassword,
		const char **sessionIDs, int max);
	char *makeSessionID(const char *user, const char *password,
		const char *name,
		char *ID, int IDSize);
	void removeSession(const char *ID);
	void unregister(Session *session);
	void write(const char *buf, int size, int fd);

private:
	Session *createSession2(Server *server,
		const char *ID, int overwrite);
	Session *createSession3(Server *server,
		const char *ID, int overwrite);
	void freeSpace();
	int getReservedBlocksForImageFile();
	int isSessionIDInUse(const char *sessionID);
	static void removeFile(const char *pathName);
	void removeSession2(const char *ID);
	void removeSessionLRU();
	int32 sizeToBlocks(int64 size);
	static void waitUntilAllFIFOsFilled();
};

extern Store *store;

#endif
