first commit
This commit is contained in:
85
include/platform/filesystem/data_store.h
Normal file
85
include/platform/filesystem/data_store.h
Normal file
@ -0,0 +1,85 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIODATASTORE__
|
||||
#define __NIODATASTORE__
|
||||
|
||||
|
||||
#include "filesystem/io_handle.h"
|
||||
#include "container/nlist.h"
|
||||
#include "nstring/nstring.h"
|
||||
#include "thread/mutex.h"
|
||||
#include "unit/nunit.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
|
||||
/*!
|
||||
@short Data store.
|
||||
*/
|
||||
class DataStore
|
||||
{
|
||||
Threading::Mutex mutex;
|
||||
|
||||
SharedPtr <Base> io;
|
||||
size_t limit;
|
||||
|
||||
uint id_seed;
|
||||
String GetNewId();
|
||||
|
||||
public:
|
||||
|
||||
struct Entry
|
||||
{
|
||||
String id;
|
||||
size_t size;
|
||||
String user;
|
||||
|
||||
Entry() : size(0) {}
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
AutoList <Entry *> entries;
|
||||
|
||||
Entry *GetEntry(const String &id) const;
|
||||
|
||||
public:
|
||||
|
||||
Base *GetIO() const { return io.c_ptr(); }
|
||||
|
||||
const AutoList <Entry *> &GetEntries() const { return entries; }
|
||||
|
||||
/// Reserve space on the store, an empty id is returned if the store is full.
|
||||
virtual String Reserve(size_t size);
|
||||
/// Free a store alias.
|
||||
virtual bool Free(const String &id);
|
||||
|
||||
/// Store data on a reserved id.
|
||||
bool Store(const String &id, const void *data, size_t size, const char *user_data = 0);
|
||||
|
||||
/// Get entry size.
|
||||
size_t GetEntrySize(const String &id) const;
|
||||
|
||||
/// Get the current store size.
|
||||
size_t GetStoreSize() const;
|
||||
/// Get the store free space size.
|
||||
size_t GetFreeStore() const;
|
||||
|
||||
/// Restore store content from the storage fs.
|
||||
bool Load(const char *path = "store.db");
|
||||
/// Save store content to the storage fs.
|
||||
bool Save(const char *path = "store.db");
|
||||
|
||||
DataStore(Base *storage, size_t storage_limit = Units::MB(32)) : io(storage), limit(storage_limit), id_seed(0) {}
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIODATASTORE__
|
||||
73
include/platform/filesystem/filesystem.h
Normal file
73
include/platform/filesystem/filesystem.h
Normal file
@ -0,0 +1,73 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NFILESYSTEM__
|
||||
#define __NFILESYSTEM__
|
||||
|
||||
|
||||
#include "container/nlist.h"
|
||||
#include "filesystem/io_base.h"
|
||||
#include "nstring/nstring.h"
|
||||
#include "memory/nauto_ptr.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
class Handle;
|
||||
struct Base;
|
||||
|
||||
//
|
||||
struct MountPoint
|
||||
{
|
||||
String mount_point;
|
||||
SharedPtr <Base> io_sys;
|
||||
|
||||
MountPoint(const char *mount, Base *io) : mount_point(mount), io_sys(io) {}
|
||||
};
|
||||
|
||||
/*!
|
||||
@short I/O system.
|
||||
@author Emmanuel Julien (ejulien@nworks.fr)
|
||||
*/
|
||||
class Filesystem
|
||||
{
|
||||
AutoList <MountPoint *> mount_list;
|
||||
SharedList <Base *> root_mount;
|
||||
public:
|
||||
|
||||
String MapToAbsolute(const char *) const;
|
||||
String StripRootPath(const char *) const;
|
||||
|
||||
bool Mount(Base *, const char *mount_point = 0);
|
||||
void Unmount(const char *);
|
||||
void Unmount(Base *);
|
||||
void UnmountAll();
|
||||
|
||||
Base *GetIOSystem(const char *mount_point) const;
|
||||
const char *GetMountPoint(const Base *) const;
|
||||
|
||||
Handle *Open(const char *, Mode = ModeRead) const;
|
||||
void Close(Handle *) const;
|
||||
|
||||
bool MkDir(const char *) const;
|
||||
|
||||
bool Exists(const char *) const;
|
||||
bool Delete(const char *) const;
|
||||
|
||||
size_t FileSize(const char *) const;
|
||||
bool FileLoad(const char *, Array <char> &, bool verbose = true) const;
|
||||
bool FileSave(const char *, const Array <char> &) const;
|
||||
bool FileCopy(const char *src, const char *dst) const;
|
||||
bool FileMove(const char *src, const char *dst) const;
|
||||
~Filesystem() { UnmountAll(); }
|
||||
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NFILESYSTEM__
|
||||
173
include/platform/filesystem/ftp_lib.h
Normal file
173
include/platform/filesystem/ftp_lib.h
Normal file
@ -0,0 +1,173 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __FTPLIB__
|
||||
#define __FTPLIB__
|
||||
|
||||
|
||||
#if __PLATFORM_WINDOWS__
|
||||
#include <winsock.h>
|
||||
#endif
|
||||
#include "ntypes.h"
|
||||
|
||||
|
||||
/// FTP connection.
|
||||
struct FtpConnection
|
||||
{
|
||||
enum Direction
|
||||
{
|
||||
Upload = 0,
|
||||
Download
|
||||
};
|
||||
|
||||
int handle;
|
||||
bool ready;
|
||||
|
||||
char *buffer;
|
||||
int buffer_usage;
|
||||
|
||||
Direction direction;
|
||||
|
||||
void FreeBuffer();
|
||||
bool AllocateBuffer();
|
||||
|
||||
FtpConnection();
|
||||
~FtpConnection()
|
||||
{ FreeBuffer(); }
|
||||
};
|
||||
|
||||
/*
|
||||
@short FTP Library.
|
||||
*/
|
||||
class FtpLibrary
|
||||
{
|
||||
public:
|
||||
|
||||
enum ConnectionMode
|
||||
{
|
||||
ConnectionPORT = 0,
|
||||
ConnectionPASV
|
||||
};
|
||||
|
||||
enum TransferMode
|
||||
{
|
||||
TransferASCII = 'A',
|
||||
TransferBinary = 'I'
|
||||
};
|
||||
|
||||
enum AccessType
|
||||
{
|
||||
AccessDir = 0,
|
||||
AccessDirVerbose,
|
||||
AccessFileRead,
|
||||
AccessFileWrite,
|
||||
AccessFileReadAppend,
|
||||
AccessFileWriteAppend
|
||||
};
|
||||
|
||||
enum State
|
||||
{
|
||||
FtpError = 0,
|
||||
FtpOk,
|
||||
FtpSocketTimeout
|
||||
};
|
||||
|
||||
protected:
|
||||
|
||||
char response[256];
|
||||
|
||||
FtpConnection master_connection;
|
||||
ConnectionMode connection_mode;
|
||||
|
||||
size_t offset;
|
||||
bool correctpasv;
|
||||
|
||||
/// Check server PASV support.
|
||||
bool CheckPASVResponse(unsigned char *v);
|
||||
/// Read and verify a response from the server.
|
||||
bool CheckResponse(char c);
|
||||
|
||||
/// Wait for the socket to receive or flush data.
|
||||
State WaitSocket(FtpConnection *socket);
|
||||
/// Read a line of text.
|
||||
State ReadASCII(char *buf, int max, FtpConnection *socket, int &line_length);
|
||||
/// Write a line of text.
|
||||
State WriteASCII(char *buf, int length, FtpConnection *socket, int &wrote_length);
|
||||
|
||||
/// Send a command and wait for expected response.
|
||||
bool FtpSendCmd(const char *cmd, char expresp);
|
||||
/// Accept connection from server.
|
||||
bool FtpAcceptConnection(FtpConnection *connection);
|
||||
|
||||
/// Create a PORT connection for data transfer.
|
||||
FtpConnection *CreatePORTConnection(TransferMode mode, FtpConnection::Direction direction, char *connection_command);
|
||||
/// Create a PASV connection for data transfer.
|
||||
FtpConnection *CreatePASVConnection(TransferMode mode, FtpConnection::Direction direction, char *connection_command);
|
||||
/// Create a connection for data transfer.
|
||||
FtpConnection *CreateConnection(const char *path, AccessType type, TransferMode mode);
|
||||
/// Close connection.
|
||||
bool CloseConnection(FtpConnection *connection);
|
||||
|
||||
/// Generic data transfer function.
|
||||
State DataTransfer(const char *local_path, const char *remote_path, AccessType type, TransferMode mode);
|
||||
/// Read data from a connection.
|
||||
State ReadBinary(void *buffer, int max, FtpConnection *connection, int &length);
|
||||
/// Write data to a connection.
|
||||
State WriteBinary(void *buffer, int length, FtpConnection *connection);
|
||||
|
||||
public:
|
||||
|
||||
/// Set data connection transfer mode.
|
||||
void SetDataConnectionMode(ConnectionMode mode)
|
||||
{ connection_mode = mode; }
|
||||
|
||||
/// Get last server response received.
|
||||
const char *GetLastResponse() const
|
||||
{ return response; }
|
||||
|
||||
/// Connect to a remote server.
|
||||
bool Connect(const char *host);
|
||||
/// Upload file to server.
|
||||
State Upload(const char *local_path, const char *remote_path, TransferMode mode = TransferBinary, int offset = 0);
|
||||
/// Download file from server.
|
||||
State Download(const char *local_path, const char *remote_path, TransferMode mode = TransferBinary, int offset = 0);
|
||||
/// Quit server, close connection.
|
||||
bool Quit();
|
||||
|
||||
/// Change directory.
|
||||
bool ChangeDirectory(const char *path);
|
||||
/// Up directory.
|
||||
bool UpDirectory();
|
||||
/// Delete remote file.
|
||||
bool DeleteFile(const char *path);
|
||||
/// Get remote file size.
|
||||
int GetFileSize(const char *path, TransferMode mode = TransferBinary);
|
||||
|
||||
/// Download directory listing to a local file.
|
||||
bool Nlst(const char *outputfile, const char *path);
|
||||
|
||||
/// Login remote server.
|
||||
bool Login(const char *user, const char *password);
|
||||
|
||||
|
||||
/*
|
||||
Callback functions.
|
||||
*/
|
||||
virtual bool IdleCallback()
|
||||
{ return true; }
|
||||
virtual void SendCommandCallback(const char * /*command*/)
|
||||
{}
|
||||
virtual bool DataReadCallback(const FtpConnection * /*connection*/)
|
||||
{ return true; }
|
||||
virtual bool DataWriteCallback(const FtpConnection * /*connection*/)
|
||||
{ return true; }
|
||||
|
||||
FtpLibrary();
|
||||
virtual ~FtpLibrary();
|
||||
};
|
||||
|
||||
|
||||
#endif // __FTPLIB__
|
||||
66
include/platform/filesystem/io_alias.h
Normal file
66
include/platform/filesystem/io_alias.h
Normal file
@ -0,0 +1,66 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIOALIAS__
|
||||
#define __NIOALIAS__
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include "nstring/nstring.h"
|
||||
#include "container/nmap.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
class Handle;
|
||||
|
||||
/*!
|
||||
@short Alias proxy I/O.
|
||||
A simple proxy I/O to provide filename aliasing to another I/O system.
|
||||
@author Emmanuel Julien (ejulien@nworks.fr)
|
||||
*/
|
||||
class Alias : public Base
|
||||
{
|
||||
SharedPtr <Base> iofs;
|
||||
|
||||
public:
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
Map <String, String> alias_map;
|
||||
|
||||
String ResolveAlias(const char *path) const
|
||||
{
|
||||
Pair <String, String> *alias = alias_map.Get(path);
|
||||
return alias ? alias->value : path;
|
||||
}
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
virtual Handle *Open(const char *path, Mode mode = IORead)
|
||||
{ return iofs->Open(ResolveAlias(path), mode); }
|
||||
virtual void Close(Handle *h)
|
||||
{ iofs->Close(h); }
|
||||
|
||||
virtual bool Delete(const char *path)
|
||||
{ return iofs->Delete(ResolveAlias(path)); }
|
||||
|
||||
virtual size_t Tell(Handle *h)
|
||||
{ return iofs->Tell(h); }
|
||||
virtual size_t Seek(Handle *h, ptrdiff_t offset, SeekRef seek_ref = SeekCurrent)
|
||||
{ return iofs->Seek(h, offset, seek_ref); }
|
||||
|
||||
virtual size_t Read(Handle *h, void *p, size_t size)
|
||||
{ return iofs->Read(h, p, size); }
|
||||
virtual size_t Write(Handle *h, const void *p, size_t size)
|
||||
{ return iofs->Write(h, p, size); }
|
||||
|
||||
Alias(Base *io) : iofs(io) {}
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIOALIAS__
|
||||
80
include/platform/filesystem/io_base.h
Normal file
80
include/platform/filesystem/io_base.h
Normal file
@ -0,0 +1,80 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIOBASE__
|
||||
#define __NIOBASE__
|
||||
|
||||
|
||||
#include <stddef.h>
|
||||
#include "filesystem/io_mode.h"
|
||||
#include "memory/nshared_ptr.h"
|
||||
#include "container/narray.h"
|
||||
#include "alloc/ialloc.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
class String;
|
||||
|
||||
namespace IO {
|
||||
class Handle;
|
||||
|
||||
/*!
|
||||
@short I/O base.
|
||||
@author Emmanuel Julien (ejulien@nworks.fr)
|
||||
*/
|
||||
struct Base : public SharedObject
|
||||
{
|
||||
NPLACEMENT_NEW(Filesystem)
|
||||
|
||||
enum SeekRef
|
||||
{
|
||||
SeekStart = 0,
|
||||
SeekCurrent,
|
||||
SeekEnd
|
||||
};
|
||||
|
||||
enum Caps
|
||||
{
|
||||
IsCaseSensitive = (1 << 0),
|
||||
CanRead = (1 << 1),
|
||||
CanWrite = (1 << 2),
|
||||
CanSeek = (1 << 3),
|
||||
CanDelete = (1 << 4),
|
||||
CanMkDir = (1 << 5)
|
||||
};
|
||||
|
||||
bool FileLoad(const char *uri, Array <char> &buffer);
|
||||
bool FileSave(const char *uri, const Array <char> &buffer);
|
||||
|
||||
String FileHash(const char *uri);
|
||||
|
||||
virtual bool Exists(const char *);
|
||||
virtual String Hash(const char *);
|
||||
|
||||
virtual String MapToAbsolute(const char *uri) const;
|
||||
virtual String MapToRelative(const char *path) const;
|
||||
|
||||
virtual uint GetCaps() const = 0;
|
||||
|
||||
virtual Handle *Open(const char *, Mode = ModeRead) = 0;
|
||||
virtual void Close(Handle *) = 0;
|
||||
|
||||
virtual bool Delete(const char *) = 0;
|
||||
|
||||
virtual size_t Tell(Handle *) = 0;
|
||||
virtual size_t Seek(Handle *, ptrdiff_t offset, SeekRef = SeekCurrent) = 0;
|
||||
|
||||
virtual size_t Read(Handle *, void *, size_t) = 0;
|
||||
virtual size_t Write(Handle *, const void *, size_t) = 0;
|
||||
|
||||
virtual bool MkDir(const char *) = 0;
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIOBASE__
|
||||
87
include/platform/filesystem/io_buffer.h
Normal file
87
include/platform/filesystem/io_buffer.h
Normal file
@ -0,0 +1,87 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIOBUFFER__
|
||||
#define __NIOBUFFER__
|
||||
|
||||
|
||||
#include "filesystem/io_handle.h"
|
||||
#include "nstring/nstring.h"
|
||||
#include "memory/nauto_ptr.h"
|
||||
#include "container/narray.h"
|
||||
#include "unit/nunit.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
|
||||
//
|
||||
struct IOBuffer
|
||||
{
|
||||
Array <char> buffer;
|
||||
|
||||
size_t start_pos;
|
||||
size_t usage;
|
||||
|
||||
IOBuffer() : start_pos(0), usage(0) {}
|
||||
};
|
||||
|
||||
//
|
||||
class BufferHandle : public Handle
|
||||
{
|
||||
friend class Buffer;
|
||||
|
||||
AutoPtr <Handle> handle;
|
||||
size_t size; // file size on wrapped fs
|
||||
size_t pos; // position in source handle
|
||||
|
||||
IOBuffer read_buffer;
|
||||
|
||||
public:
|
||||
|
||||
virtual size_t GetSize() { return size; }
|
||||
|
||||
BufferHandle(Base *io, Handle *h) : Handle(io), handle(h), size(0), pos(0) {}
|
||||
};
|
||||
|
||||
/*!
|
||||
@short Buffered I/O filesystem wrapper.
|
||||
Implements a transparent read/write buffer on top of another filesystem.
|
||||
@author Emmanuel Julien (ejulien@nworks.fr)
|
||||
*/
|
||||
class Buffer : public Base
|
||||
{
|
||||
SharedPtr <Base> io;
|
||||
|
||||
size_t read_buffer_size,
|
||||
write_buffer_size;
|
||||
|
||||
public:
|
||||
|
||||
virtual uint GetCaps() const;
|
||||
|
||||
virtual Handle *Open(const char *, Mode = ModeRead);
|
||||
virtual void Close(Handle *);
|
||||
|
||||
virtual String Hash(const char *);
|
||||
virtual bool Delete(const char *);
|
||||
|
||||
virtual size_t Tell(Handle *);
|
||||
virtual size_t Seek(Handle *, ptrdiff_t offset, SeekRef = SeekCurrent);
|
||||
|
||||
virtual size_t Read(Handle *, void *, size_t);
|
||||
virtual size_t Write(Handle *, const void *, size_t);
|
||||
|
||||
virtual bool MkDir(const char *);
|
||||
|
||||
Buffer(Base *base, size_t read_buffer_size = Units::KB(4), size_t write_buffer_size = Units::KB(4));
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIOBUFFER__
|
||||
109
include/platform/filesystem/io_cache.h
Normal file
109
include/platform/filesystem/io_cache.h
Normal file
@ -0,0 +1,109 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIOCACHE__
|
||||
#define __NIOCACHE__
|
||||
|
||||
|
||||
#include "filesystem/data_store.h"
|
||||
#include "filesystem/io_handle.h"
|
||||
#include "memory/nauto_ptr.h"
|
||||
#include "container/nlist.h"
|
||||
#include "nstring/nstring.h"
|
||||
#include "time/ntime.h"
|
||||
#include "unit/nunit.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
|
||||
//
|
||||
class CacheHandle : public Handle
|
||||
{
|
||||
friend class Cache;
|
||||
|
||||
protected:
|
||||
|
||||
String path;
|
||||
AutoPtr <Handle> handle;
|
||||
|
||||
public:
|
||||
|
||||
CacheHandle(Base *io, const char *p, Handle *h) : Handle(io), path(p), handle(h) {}
|
||||
~CacheHandle();
|
||||
};
|
||||
|
||||
/*!
|
||||
@short Cached I/O filesystem wrapper.
|
||||
Implements a transparent file cache on top of another filesystem.
|
||||
@author Emmanuel Julien (ejulien@nworks.fr)
|
||||
*/
|
||||
class Cache : public Base
|
||||
{
|
||||
SharedPtr <Base> io;
|
||||
DataStore store;
|
||||
|
||||
public:
|
||||
|
||||
struct Entry
|
||||
{
|
||||
String path, id;
|
||||
String hash;
|
||||
|
||||
uint refc;
|
||||
size_t size;
|
||||
|
||||
Time last_use;
|
||||
|
||||
Entry() : refc(0), size(0) {}
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
Threading::Mutex mutex;
|
||||
|
||||
size_t cache_size;
|
||||
AutoList <Entry *> entries;
|
||||
|
||||
int ComputeEntryRecyclingScore(const Entry *) const;
|
||||
String ReserveOnStore(size_t size);
|
||||
|
||||
Entry *GetCacheEntry(const char *path) const;
|
||||
Entry *CreateCacheEntry(const char *path);
|
||||
bool UpdateCacheEntry(Entry *entry, Array <char> *preloaded_data = 0);
|
||||
bool DeleteCacheEntry(Entry *entry);
|
||||
|
||||
public:
|
||||
|
||||
virtual uint GetCaps() const;
|
||||
|
||||
virtual Handle *Open(const char *, Mode = ModeRead);
|
||||
virtual void Close(Handle *);
|
||||
|
||||
virtual bool Delete(const char *);
|
||||
|
||||
virtual size_t Tell(Handle *);
|
||||
virtual size_t Seek(Handle *, ptrdiff_t offset, SeekRef = SeekCurrent);
|
||||
|
||||
virtual size_t Read(Handle *, void *, size_t);
|
||||
virtual size_t Write(Handle *, const void *, size_t);
|
||||
|
||||
virtual bool MkDir(const char *);
|
||||
|
||||
/// Test if a given path is in cache.
|
||||
bool IsInCache(const char *path) const { return asbool(GetCacheEntry(path)); }
|
||||
|
||||
/// Reload cache state from its store.
|
||||
bool SynchronizeWithStore(const char *store_path = "store.db");
|
||||
|
||||
Cache(Base *base, Base *store, size_t cache_size = Units::MB(32));
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIOCACHE__
|
||||
70
include/platform/filesystem/io_cfile.h
Normal file
70
include/platform/filesystem/io_cfile.h
Normal file
@ -0,0 +1,70 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIOCFILE__
|
||||
#define __NIOCFILE__
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include "filesystem/io_handle.h"
|
||||
#include "nstring/nstring.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
|
||||
//
|
||||
class CFileHandle : public Handle
|
||||
{
|
||||
friend class CFile;
|
||||
|
||||
FILE *file;
|
||||
|
||||
CFileHandle(Base *);
|
||||
|
||||
public:
|
||||
|
||||
~CFileHandle();
|
||||
};
|
||||
|
||||
/*!
|
||||
@short C-File I/O.
|
||||
@author Emmanuel Julien (ejulien@nworks.fr)
|
||||
*/
|
||||
class CFile : public Base
|
||||
{
|
||||
String root;
|
||||
|
||||
public:
|
||||
|
||||
void SetRootPath(const char *);
|
||||
|
||||
virtual String MapToAbsolute(const char *) const;
|
||||
virtual String MapToRelative(const char *) const;
|
||||
|
||||
virtual uint GetCaps() const;
|
||||
|
||||
virtual Handle *Open(const char *, Mode = ModeRead);
|
||||
virtual void Close(Handle *);
|
||||
|
||||
virtual bool Delete(const char *);
|
||||
|
||||
virtual size_t Tell(Handle *);
|
||||
virtual size_t Seek(Handle *, ptrdiff_t offset, SeekRef = SeekCurrent);
|
||||
|
||||
virtual size_t Read(Handle *, void *, size_t);
|
||||
virtual size_t Write(Handle *, const void *, size_t);
|
||||
|
||||
virtual bool MkDir(const char *);
|
||||
|
||||
CFile(const char *root_path = 0);
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIOCFILE__
|
||||
73
include/platform/filesystem/io_crypto.h
Normal file
73
include/platform/filesystem/io_crypto.h
Normal file
@ -0,0 +1,73 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIOCRYPTO__
|
||||
#define __NIOCRYPTO__
|
||||
|
||||
|
||||
#include "filesystem/io_memory.h"
|
||||
#include "memory/nauto_ptr.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
|
||||
//
|
||||
class CryptoHandle : public Handle
|
||||
{
|
||||
friend class Crypto;
|
||||
|
||||
AutoPtr <Handle> cached_h;
|
||||
|
||||
String path;
|
||||
Mode io_mode;
|
||||
|
||||
CryptoHandle(Base *, Handle *, const char *, Mode);
|
||||
|
||||
public:
|
||||
|
||||
~CryptoHandle();
|
||||
};
|
||||
|
||||
/*!
|
||||
@short I/O Crypto proxy.
|
||||
@author Emmanuel Julien (ejulien@nworks.fr)
|
||||
*/
|
||||
class Crypto : public Base
|
||||
{
|
||||
String key;
|
||||
|
||||
SharedPtr <Base> wrapped_io;
|
||||
SharedPtr <Memory> cache_io;
|
||||
|
||||
void Encrypt(Array <char> &);
|
||||
void Decrypt(Array <char> &);
|
||||
|
||||
public:
|
||||
|
||||
virtual uint GetCaps() const;
|
||||
|
||||
virtual Handle *Open(const char *, Mode = ModeRead);
|
||||
virtual void Close(Handle *);
|
||||
|
||||
virtual bool Delete(const char *);
|
||||
|
||||
virtual size_t Tell(Handle *);
|
||||
virtual size_t Seek(Handle *, ptrdiff_t offset, SeekRef = SeekCurrent);
|
||||
|
||||
virtual size_t Read(Handle *, void *, size_t);
|
||||
virtual size_t Write(Handle *, const void *, size_t);
|
||||
|
||||
virtual bool MkDir(const char *);
|
||||
|
||||
Crypto(Base *, const char *key);
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIOCRYPTO__
|
||||
77
include/platform/filesystem/io_dispatcher.h
Normal file
77
include/platform/filesystem/io_dispatcher.h
Normal file
@ -0,0 +1,77 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIODISPATCH__
|
||||
#define __NIODISPATCH__
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include "filesystem/io_handle.h"
|
||||
#include "memory/nauto_ptr.h"
|
||||
#include "container/nlist.h"
|
||||
#include "nstring/nstring.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
|
||||
//
|
||||
class DispatcherHandle : public Handle
|
||||
{
|
||||
friend class Dispatcher;
|
||||
|
||||
AutoPtr <Handle> handle;
|
||||
|
||||
DispatcherHandle(Base *, Handle *);
|
||||
|
||||
public:
|
||||
|
||||
~DispatcherHandle();
|
||||
};
|
||||
|
||||
/*!
|
||||
@short Dispatcher I/O.
|
||||
@author Emmanuel Julien (ejulien@nworks.fr)
|
||||
*/
|
||||
class Dispatcher : public Base
|
||||
{
|
||||
struct DispatchFS
|
||||
{
|
||||
String prefix;
|
||||
SharedPtr <Base> fs;
|
||||
};
|
||||
|
||||
SharedList <Base *> roots;
|
||||
AutoList <DispatchFS *> mounts;
|
||||
|
||||
Base *Dispatch(String &, Mode);
|
||||
|
||||
public:
|
||||
|
||||
bool AddDispatch(Base *, const char *prefix = 0);
|
||||
|
||||
virtual uint GetCaps() const;
|
||||
|
||||
virtual Handle *Open(const char *, Mode = ModeRead);
|
||||
virtual void Close(Handle *);
|
||||
|
||||
virtual String Hash(const char *);
|
||||
virtual bool Delete(const char *);
|
||||
|
||||
virtual size_t Tell(Handle *);
|
||||
virtual size_t Seek(Handle *, ptrdiff_t offset, SeekRef = SeekCurrent);
|
||||
|
||||
virtual size_t Read(Handle *, void *, size_t);
|
||||
virtual size_t Write(Handle *, const void *, size_t);
|
||||
|
||||
virtual bool MkDir(const char *);
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIODISPATCH__
|
||||
71
include/platform/filesystem/io_handle.h
Normal file
71
include/platform/filesystem/io_handle.h
Normal file
@ -0,0 +1,71 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIOHANDLE__
|
||||
#define __NIOHANDLE__
|
||||
|
||||
|
||||
#include "filesystem/io_base.h"
|
||||
#include "memory/nweak_ptr.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
|
||||
//
|
||||
class Handle
|
||||
{
|
||||
WeakPtr <Base> io_sys;
|
||||
|
||||
public:
|
||||
|
||||
NPLACEMENT_NEW(Filesystem)
|
||||
|
||||
virtual size_t GetSize();
|
||||
|
||||
virtual size_t Rewind();
|
||||
virtual bool IsEOF();
|
||||
|
||||
virtual size_t Tell();
|
||||
virtual size_t Seek(ptrdiff_t, Base::SeekRef = Base::SeekCurrent);
|
||||
|
||||
virtual size_t Read(void *, size_t);
|
||||
virtual size_t Write(const void *, size_t);
|
||||
|
||||
template <class T> Handle &operator << (const T &v)
|
||||
{
|
||||
io_sys->Write(this, &v, sizeof(T));
|
||||
return *this;
|
||||
}
|
||||
template <class T> Handle &operator >> (T &v)
|
||||
{
|
||||
io_sys->Read(this, &v, sizeof(T));
|
||||
return *this;
|
||||
}
|
||||
Handle &operator << (const char *);
|
||||
Handle &operator << (char *);
|
||||
|
||||
template <class T> bool Write(const T &v)
|
||||
{ return asbool(io_sys->Write(this, &v, sizeof(T))); }
|
||||
template <class T> T Read()
|
||||
{
|
||||
T v;
|
||||
io_sys->Read(this, &v, sizeof(T));
|
||||
return v;
|
||||
}
|
||||
|
||||
Base *GetIOSystem() const
|
||||
{ return io_sys.c_ptr(); }
|
||||
|
||||
Handle(Base *);
|
||||
virtual ~Handle();
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIOHANDLE__
|
||||
50
include/platform/filesystem/io_handle_endian.h
Normal file
50
include/platform/filesystem/io_handle_endian.h
Normal file
@ -0,0 +1,50 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIOHANDLE_ENDIAN__
|
||||
#define __NIOHANDLE_ENDIAN__
|
||||
|
||||
|
||||
#include "filesystem/io_handle.h"
|
||||
#include "memory/endian.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
|
||||
/// IO Handle endian wrapper.
|
||||
class HandleEndian
|
||||
{
|
||||
AutoPtr <Handle> h;
|
||||
Endian::Config config;
|
||||
|
||||
public:
|
||||
|
||||
bool IsNull() const { return h.IsNull(); }
|
||||
bool IsValid() const { return h.IsValid(); }
|
||||
|
||||
Handle &GetIOHandle() const { return *h; }
|
||||
|
||||
template <class T> const HandleEndian &operator << (const T &v)
|
||||
{
|
||||
*h << Endian::ToHost(v, config);
|
||||
return *this;
|
||||
}
|
||||
template <class T> const HandleEndian &operator >> (T &v)
|
||||
{
|
||||
*h >> v;
|
||||
Endian::ToHost(&v, sizeof(T), config);
|
||||
return *this;
|
||||
}
|
||||
|
||||
HandleEndian(Handle *in_h, Endian::Config in_config = Endian::Little) : h(in_h), config(in_config) {}
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIOHANDLE_ENDIAN__
|
||||
42
include/platform/filesystem/io_handle_segment.h
Normal file
42
include/platform/filesystem/io_handle_segment.h
Normal file
@ -0,0 +1,42 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIOHANDLESEGMENT__
|
||||
#define __NIOHANDLESEGMENT__
|
||||
|
||||
|
||||
#include "filesystem/io_handle.h"
|
||||
#include "memory/nauto_ptr.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
|
||||
//
|
||||
class HandleSegment : public Handle
|
||||
{
|
||||
Handle *handle;
|
||||
size_t offset, size;
|
||||
size_t cursor;
|
||||
|
||||
public:
|
||||
|
||||
virtual size_t GetSize();
|
||||
|
||||
virtual size_t Tell();
|
||||
virtual size_t Seek(ptrdiff_t, Base::SeekRef = Base::SeekCurrent);
|
||||
|
||||
virtual size_t Read(void *, size_t);
|
||||
virtual size_t Write(const void *, size_t);
|
||||
|
||||
HandleSegment(Handle *h, size_t o, size_t s) : Handle(0), handle(h), offset(o), size(s), cursor(0) {}
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIOHANDLESEGMENT__
|
||||
86
include/platform/filesystem/io_memory.h
Normal file
86
include/platform/filesystem/io_memory.h
Normal file
@ -0,0 +1,86 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIOMEMORY__
|
||||
#define __NIOMEMORY__
|
||||
|
||||
|
||||
#include "filesystem/io_handle.h"
|
||||
#include "container/nlist.h"
|
||||
#include "nstring/nstring.h"
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
|
||||
//
|
||||
class MemoryFile
|
||||
{
|
||||
friend class Memory;
|
||||
|
||||
String uri;
|
||||
Array <char> data;
|
||||
size_t size; //< Might be != data.GetSize()
|
||||
|
||||
MemoryFile(const char *_uri = 0) : uri(_uri), size(0) {}
|
||||
|
||||
public:
|
||||
|
||||
const String &GetUri() const { return uri; }
|
||||
const Array <char> &GetData() const { return data; }
|
||||
size_t GetSize() const { return size; }
|
||||
};
|
||||
|
||||
//
|
||||
class MemoryHandle : public Handle
|
||||
{
|
||||
friend class Memory;
|
||||
|
||||
MemoryFile *file;
|
||||
size_t cursor;
|
||||
Mode mode;
|
||||
|
||||
MemoryHandle(Base *, MemoryFile *, Mode = ModeRead);
|
||||
|
||||
public:
|
||||
|
||||
~MemoryHandle();
|
||||
};
|
||||
|
||||
/*!
|
||||
@short Memory I/O.
|
||||
@author Emmanuel Julien (ejulien@nworks.fr)
|
||||
*/
|
||||
class Memory : public Base
|
||||
{
|
||||
AutoList <MemoryFile *> fat;
|
||||
|
||||
public:
|
||||
|
||||
/// Return the system file allocation table.
|
||||
const List <MemoryFile *> &GetFat() const { return fat; }
|
||||
|
||||
virtual uint GetCaps() const;
|
||||
|
||||
virtual Handle *Open(const char *, Mode = ModeRead);
|
||||
virtual void Close(Handle *);
|
||||
|
||||
virtual bool Delete(const char *);
|
||||
|
||||
virtual size_t Tell(Handle *);
|
||||
virtual size_t Seek(Handle *, ptrdiff_t offset, SeekRef = SeekCurrent);
|
||||
|
||||
virtual size_t Read(Handle *, void *, size_t);
|
||||
virtual size_t Write(Handle *, const void *, size_t);
|
||||
|
||||
virtual bool MkDir(const char *) { return false; }
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIOMEMORY__
|
||||
25
include/platform/filesystem/io_mode.h
Normal file
25
include/platform/filesystem/io_mode.h
Normal file
@ -0,0 +1,25 @@
|
||||
/* -----------------------------------------------------------------------------
|
||||
GSFramework
|
||||
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
||||
----------------------------------------------------------------------------- */
|
||||
|
||||
|
||||
#ifndef __NIOMODE__
|
||||
#define __NIOMODE__
|
||||
|
||||
|
||||
namespace GS {
|
||||
namespace IO {
|
||||
|
||||
enum Mode
|
||||
{
|
||||
ModeNone = 0,
|
||||
ModeRead,
|
||||
ModeWrite
|
||||
};
|
||||
|
||||
} // IO
|
||||
} // GS
|
||||
|
||||
|
||||
#endif // __NIOMODE__
|
||||
Reference in New Issue
Block a user