The Pedigree Project  0.1
Classes | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Member Functions | Friends | List of all members
File Class Reference

#include <File.h>

+ Inheritance diagram for File:
+ Collaboration diagram for File:

Classes

class  DataCacheKey
 
struct  MonitorTarget
 

Public Member Functions

 File ()
 
 File (const String &name, Time::Timestamp accessedTime, Time::Timestamp modifiedTime, Time::Timestamp creationTime, uintptr_t inode, class Filesystem *pFs, size_t size, File *pParent)
 
virtual ~File ()
 
virtual uint64_t read (uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true) final
 
virtual uint64_t write (uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true) final
 
virtual physical_uintptr_t getPhysicalPage (size_t offset)
 
virtual void returnPhysicalPage (size_t offset)
 
virtual void sync ()
 
virtual void sync (size_t offset, bool async)
 
Time::Timestamp getCreationTime ()
 
void setCreationTime (Time::Timestamp t)
 
Time::Timestamp getAccessedTime ()
 
void setAccessedTime (Time::Timestamp t)
 
Time::Timestamp getModifiedTime ()
 
void setModifiedTime (Time::Timestamp t)
 
String getName () const
 
void getName (String &s) const
 
virtual String getFullPath (bool bWithLabel=true)
 
virtual void truncate ()
 
size_t getSize ()
 
void setSize (size_t sz)
 
virtual bool isSymlink ()
 
virtual bool isDirectory ()
 
virtual bool isPipe () const
 
virtual bool isFifo () const
 
virtual bool isSocket () const
 
uintptr_t getInode () const
 
virtual void setInode (uintptr_t inode)
 
FilesystemgetFilesystem () const
 
void setFilesystem (Filesystem *pFs)
 
virtual void fileAttributeChanged ()
 
virtual void increaseRefCount (bool bIsWriter)
 
virtual void decreaseRefCount (bool bIsWriter)
 
void setPermissions (uint32_t perms)
 
uint32_t getPermissions () const
 
void setUid (size_t uid)
 
size_t getUid () const
 
void setGid (size_t gid)
 
size_t getGid () const
 
FilegetParent () const
 
virtual int select (bool bWriting=false, int timeout=0)
 
void monitor (Thread *pThread, Event *pEvent)
 
void cullMonitorTargets (Thread *pThread)
 
virtual bool supports (const size_t command) const
 
virtual int command (const size_t command, void *buffer)
 
virtual size_t getBlockSize () const
 
void enableDirect ()
 
void disableDirect ()
 
virtual void preallocate (size_t expectedSize, bool zero=true)
 
virtual Fileopen ()
 

Protected Member Functions

virtual bool isBytewise () const
 
virtual uint64_t readBytewise (uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
 
virtual uint64_t writeBytewise (uint64_t location, uint64_t size, uintptr_t buffer, bool bCanBlock=true)
 
virtual uintptr_t readBlock (uint64_t location)
 
virtual void writeBlock (uint64_t location, uintptr_t addr)
 
virtual void extend (size_t newSize)
 
virtual void extend (size_t newSize, uint64_t location, uint64_t size)
 
void dataChanged ()
 
void getFilesystemLabel (HugeStaticString &s)
 
virtual void pinBlock (uint64_t location)
 
virtual void unpinBlock (uint64_t location)
 
void evict (uint64_t location)
 
void setPermissionsOnly (uint32_t perms)
 
void setUidOnly (size_t uid)
 
void setGidOnly (size_t gid)
 

Static Protected Member Functions

static void writeCallback (CacheConstants::CallbackCause cause, uintptr_t loc, uintptr_t page, void *meta)
 

Protected Attributes

String m_Name
 
Time::Timestamp m_AccessedTime
 
Time::Timestamp m_ModifiedTime
 
Time::Timestamp m_CreationTime
 
uintptr_t m_Inode
 
class Filesystemm_pFilesystem
 
size_t m_Size
 
Filem_pParent
 
size_t m_nWriters
 
size_t m_nReaders
 
size_t m_Uid
 
size_t m_Gid
 
uint32_t m_Permissions
 
HashTable< DataCacheKey, uintptr_t > m_DataCache
 
bool m_bDirect
 
Cache m_FillCache
 
Mutex m_Lock
 
List< MonitorTarget * > m_MonitorTargets
 

Private Member Functions

 File (const File &file)
 
Fileoperator= (const File &)
 
uintptr_t getCachedPage (size_t block, bool locked=true)
 
void setCachedPage (size_t block, uintptr_t value, bool locked=true)
 
bool useFillCache () const
 
uintptr_t readIntoCache (uintptr_t block)
 

Friends

class Filesystem
 

Detailed Description

A File is a regular file - it is also the superclass of Directory, Symlink and Pipe.

Definition at line 66 of file File.h.

Constructor & Destructor Documentation

File::File ( )

Constructor, creates an invalid file.

Definition at line 68 of file File.cc.

Referenced by RtcFile::command(), FramebufferFile::command(), and NullFile::writeBytewise().

+ Here is the caller graph for this function:

File::File ( const File file)
private

Copy constructors are hidden - unused!

File::File ( const String name,
Time::Timestamp  accessedTime,
Time::Timestamp  modifiedTime,
Time::Timestamp  creationTime,
uintptr_t  inode,
class Filesystem pFs,
size_t  size,
File pParent 
)

Constructor, should be called only by a Filesystem.

Definition at line 84 of file File.cc.

References getBlockSize(), and setCachedPage().

File::~File ( )
virtual

Destructor - doesn't do anything.

Definition at line 111 of file File.cc.

Member Function Documentation

int File::command ( const size_t  command,
void *  buffer 
)
virtual

Handle a command.

Reimplemented in FramebufferFile, and RtcFile.

Definition at line 544 of file File.cc.

void File::cullMonitorTargets ( Thread pThread)

Walks the monitor-target queue, removing all for pThread .

Definition at line 691 of file File.cc.

void File::dataChanged ( )
protected

Internal function to notify all registered MonitorTargets.

Definition at line 654 of file File.cc.

References Scheduler::instance(), Thread::sendEvent(), and Scheduler::yield().

Referenced by Pipe::decreaseRefCount(), ConsolePhysicalFile::performInject(), PsAuxFile::select(), and Pipe::writeBytewise().

+ Here is the caller graph for this function:

void File::disableDirect ( )

Disables direct mode (use File-level cache).

Definition at line 559 of file File.cc.

void File::enableDirect ( )

Enables direct mode (no File-level cache).

Definition at line 554 of file File.cc.

void File::evict ( uint64_t  location)
protected

Removes the given location from the VFS-level File cache.

Each File offers read() and write(), which in turn call readBlock. The result from readBlock is cached at the File object level in such a way that requires notification from the File subclass when the address returned from readBlock is no longer valid.

Definition at line 634 of file File.cc.

References getBlockSize(), and setCachedPage().

void File::extend ( size_t  newSize)
protectedvirtual

Internal function to extend a file to be at least the given size.

Reimplemented in FatFile, and Ext2File.

Definition at line 615 of file File.cc.

Referenced by extend(), and write().

+ Here is the caller graph for this function:

void File::extend ( size_t  newSize,
uint64_t  location,
uint64_t  size 
)
protectedvirtual

Does the same thing as extend() but with an optional hint indicating that the upcoming write will fill an entire block. This hint can be used to do things like avoid zeroing the block that would otherwise be written over anyway.

Reimplemented in FatFile, and Ext2File.

Definition at line 621 of file File.cc.

References extend().

Time::Timestamp File::getAccessedTime ( )

Returns the time the file was last accessed.

Definition at line 389 of file File.cc.

Referenced by Iso9660Directory::cacheDirectoryContents().

+ Here is the caller graph for this function:

size_t File::getBlockSize ( ) const
virtual

Function to retrieve the block size returned by readBlock.

Note
This must be constant throughout the life of the file.

Reimplemented in ConsoleFile, Ext2File, Iso9660File, and RawFsFile.

Definition at line 549 of file File.cc.

References PhysicalMemoryManager::getPageSize().

Referenced by evict(), File(), getPhysicalPage(), read(), FatFile::readBlock(), readIntoCache(), returnPhysicalPage(), sync(), useFillCache(), write(), FatFile::writeBlock(), and writeCallback().

+ Here is the caller graph for this function:

uintptr_t File::getCachedPage ( size_t  block,
bool  locked = true 
)
private

Retrieve a page from our cache.

Definition at line 760 of file File.cc.

Referenced by getPhysicalPage(), and readIntoCache().

+ Here is the caller graph for this function:

Time::Timestamp File::getCreationTime ( )

Returns the time the file was created.

Definition at line 378 of file File.cc.

Referenced by Iso9660Directory::cacheDirectoryContents().

+ Here is the caller graph for this function:

void File::getFilesystemLabel ( HugeStaticString s)
protected

Internal function to get the filesystem label for this file.

Definition at line 713 of file File.cc.

References Filesystem::getVolumeLabel().

Referenced by getFullPath().

+ Here is the caller graph for this function:

String File::getFullPath ( bool  bWithLabel = true)
virtual
Time::Timestamp File::getModifiedTime ( )

Returns the time the file was last modified.

Definition at line 400 of file File.cc.

Referenced by Iso9660Directory::cacheDirectoryContents().

+ Here is the caller graph for this function:

String File::getName ( ) const
physical_uintptr_t File::getPhysicalPage ( size_t  offset)
virtual

Get the physical address for the given offset into the file. Returns (physical_uintptr_t) ~0 if the offset isn't in the cache.

Reimplemented in MemFile.

Definition at line 242 of file File.cc.

References getBlockSize(), getCachedPage(), VirtualAddressSpace::getMapping(), PhysicalMemoryManager::getPageSize(), Processor::information(), VirtualAddressSpace::isMapped(), LIKELY, Cache::lookup(), m_FillCache, Cache::pin(), pinBlock(), UNLIKELY, useFillCache(), and WARNING.

Referenced by MemoryMappedFile::setPermissions().

+ Here is the caller graph for this function:

bool File::isBytewise ( ) const
protectedvirtual

File subclasses can define this and return true if they require read() calls to perform actual data reads, and false if readBlock() is otherwise sufficient.

Reimplemented in Tty0File, UnixSocket, ConsoleFile, RtcFile, TextIO, ConstantFile, PtmxFile, Directory, UptimeFile, ZeroFile, Pipe, MountFile, NullFile, PciDevicesFile, Symlink, RandomFile, MeminfoFile, and PsAuxFile.

Definition at line 577 of file File.cc.

Referenced by Symlink::isSymlink(), read(), readBytewise(), write(), and writeBytewise().

+ Here is the caller graph for this function:

bool File::isDirectory ( )
virtual
bool File::isFifo ( ) const
virtual

Returns true if the File is actually a fifo.

Reimplemented in Pipe.

Definition at line 446 of file File.cc.

Referenced by Pipe::fromFile().

+ Here is the caller graph for this function:

bool File::isPipe ( ) const
virtual

Returns true if the File is actually a pipe.

Reimplemented in Pipe.

Definition at line 441 of file File.cc.

Referenced by Pipe::fromFile().

+ Here is the caller graph for this function:

bool File::isSocket ( ) const
virtual

Returns true if the File is actually a socket.

Reimplemented in UnixSocket.

Definition at line 451 of file File.cc.

Referenced by UnixSocketSyscalls::connect(), and UnixSocketSyscalls::sendto_msg().

+ Here is the caller graph for this function:

bool File::isSymlink ( )
virtual
void File::monitor ( Thread pThread,
Event pEvent 
)

Causes the event pEvent to be dispatched to pThread when activity occurs on this File. Activity includes the file becoming available for reading, writing or erroring.

Definition at line 683 of file File.cc.

Referenced by IoEvent::IoEvent().

+ Here is the caller graph for this function:

File * File::open ( )
virtual

Obtain the actual File object to use when opening this file.

This allows a File to exist and perform logic on an open() before returning either itself or some other File object. Some files require side effects as part of their opening, this facilitates that.

Reimplemented in Tty0File, and PtmxFile.

Definition at line 572 of file File.cc.

Referenced by Framebuffer::initialise().

+ Here is the caller graph for this function:

void File::pinBlock ( uint64_t  location)
protectedvirtual

Pins the given page.

If your File subclass uses a Cache for readBlock, this method should be implemented to call Cache::pin. The VFS layer calls this method when it determines it is about to give a physical page to an upper layer, and therefore will be unable to guarantee the virtual page's dirty status is a correct reflection of the page's state.

Reimplemented in FatFile, Ext2File, and RamFile.

Definition at line 626 of file File.cc.

Referenced by getPhysicalPage().

+ Here is the caller graph for this function:

void File::preallocate ( size_t  expectedSize,
bool  zero = true 
)
virtual

Optionally preallocates blocks to fit the given size.

Reimplemented in Ext2File.

Definition at line 564 of file File.cc.

uint64_t File::read ( uint64_t  location,
uint64_t  size,
uintptr_t  buffer,
bool  bCanBlock = true 
)
finalvirtual

Reads from the file.

Parameters
[in]bufferBuffer to write the read data into. Can be null, in which case the data can be found by calling getPhysicalPage.
[in]bCanBlockWhether or not the File can block when reading
Todo:
consider caching this still

Definition at line 116 of file File.cc.

References ERROR, getBlockSize(), PhysicalMemoryManager::getPageSize(), isBytewise(), readBytewise(), readIntoCache(), and useFillCache().

Referenced by Symlink::initialise(), PosixSubsystem::invoke(), PosixSubsystem::parseShebang(), and MemoryMappedFile::setPermissions().

+ Here is the caller graph for this function:

uintptr_t File::readBlock ( uint64_t  location)
protectedvirtual

Internal function to retrieve an aligned 512byte section of the file.

Reimplemented in FramebufferFile, FatFile, Iso9660File, Ext2File, RamFile, and RawFsFile.

Definition at line 602 of file File.cc.

References assert, ERROR, and getFullPath().

Referenced by readIntoCache().

+ Here is the caller graph for this function:

uint64_t File::readBytewise ( uint64_t  location,
uint64_t  size,
uintptr_t  buffer,
bool  bCanBlock = true 
)
protectedvirtual

Reads bytes from the file, if isBytewise() == true.

Reimplemented in ConsolePhysicalFile, ConsoleSlaveFile, Tty0File, ConsoleMasterFile, RtcFile, ConstantFile, PtmxFile, TextIO, UnixSocket, UptimeFile, ZeroFile, MountFile, NullFile, PciDevicesFile, Pipe, FatSymlink, RandomFile, Ext2Symlink, PsAuxFile, and MeminfoFile.

Definition at line 582 of file File.cc.

References FATAL, and isBytewise().

Referenced by Pipe::fromFile(), and read().

+ Here is the caller graph for this function:

uintptr_t File::readIntoCache ( uintptr_t  block)
private

Read the given block into the relevant cache.

Todo:
handle readBlock failing here

Definition at line 816 of file File.cc.

References ERROR, getBlockSize(), getCachedPage(), PhysicalMemoryManager::getPageSize(), Cache::insert(), m_FillCache, Cache::markNoLongerEditing(), NOTICE, readBlock(), setCachedPage(), and useFillCache().

Referenced by read(), and write().

+ Here is the caller graph for this function:

void File::returnPhysicalPage ( size_t  offset)
virtual

Specifies that the system is done with the physical page retrieved from getPhysicalPage now. Allows the physical page to be evicted from the file cache again.

Reimplemented in MemFile.

Definition at line 315 of file File.cc.

References getBlockSize(), PhysicalMemoryManager::getPageSize(), m_FillCache, Cache::release(), UNLIKELY, unpinBlock(), and useFillCache().

int File::select ( bool  bWriting = false,
int  timeout = 0 
)
virtual

Similar to POSIX's select() function

Returns
1 if ready for reading/writing, 0 otherwise
Note
Default implementation says always ready to read/write so be sure to override if that's not right

Reimplemented in ConsolePhysicalFile, TextIO, UnixSocket, ConsoleFile, Pipe, and PsAuxFile.

Definition at line 534 of file File.cc.

Referenced by Pipe::fromFile().

+ Here is the caller graph for this function:

void File::setAccessedTime ( Time::Timestamp  t)

Sets the time the file was last accessed.

Definition at line 394 of file File.cc.

Referenced by Ext2Filesystem::createNode().

+ Here is the caller graph for this function:

void File::setCachedPage ( size_t  block,
uintptr_t  value,
bool  locked = true 
)
private

Set a page in our cache.

Definition at line 778 of file File.cc.

References assert.

Referenced by evict(), File(), readIntoCache(), and writeCallback().

+ Here is the caller graph for this function:

void File::setCreationTime ( Time::Timestamp  t)

Sets the time the file was created.

Definition at line 383 of file File.cc.

void File::setGidOnly ( size_t  gid)
protected

Set GID without raising fileAttributeChanged.

Definition at line 649 of file File.cc.

Referenced by RtcFile::command(), FramebufferFile::command(), Ext2Directory::Ext2Directory(), Ext2File::Ext2File(), Ext2Symlink::Ext2Symlink(), and NullFile::writeBytewise().

+ Here is the caller graph for this function:

void File::setModifiedTime ( Time::Timestamp  t)

Sets the time the file was last modified.

Definition at line 405 of file File.cc.

Referenced by Ext2Filesystem::createNode().

+ Here is the caller graph for this function:

void File::setPermissionsOnly ( uint32_t  perms)
protected

Set permissions without raising fileAttributeChanged.

Definition at line 639 of file File.cc.

Referenced by RtcFile::command(), FramebufferFile::command(), Ext2Directory::Ext2Directory(), Ext2File::Ext2File(), Ext2Symlink::Ext2Symlink(), and NullFile::writeBytewise().

+ Here is the caller graph for this function:

void File::setUidOnly ( size_t  uid)
protected

Set UID without raising fileAttributeChanged.

Definition at line 644 of file File.cc.

Referenced by RtcFile::command(), FramebufferFile::command(), Ext2Directory::Ext2Directory(), Ext2File::Ext2File(), Ext2Symlink::Ext2Symlink(), and NullFile::writeBytewise().

+ Here is the caller graph for this function:

bool File::supports ( const size_t  command) const
virtual

Does this File object support the given integer-based command?

Reimplemented in FramebufferFile, and RtcFile.

Definition at line 539 of file File.cc.

void File::sync ( )
virtual

Sync all cached pages for the file back to disk.

Default implementation calls writeBlock; only override if your File subclass does not actually expose readBlock/writeBlock, or if it already overrides read() or write().

Definition at line 351 of file File.cc.

References getBlockSize(), and writeBlock().

void File::sync ( size_t  offset,
bool  async 
)
virtual

Trigger a sync of an inner cache back to disk.

Reimplemented in FatFile, and Ext2File.

Definition at line 374 of file File.cc.

void File::truncate ( )
virtual

Delete all data from the file.

Reimplemented in Ext2Symlink, Ext2File, Ext2Directory, FatDirectory, and RamFile.

Definition at line 568 of file File.cc.

void File::unpinBlock ( uint64_t  location)
protectedvirtual

Unpins the given page.

Reimplemented in FatFile, Ext2File, and RamFile.

Definition at line 630 of file File.cc.

Referenced by returnPhysicalPage().

+ Here is the caller graph for this function:

bool File::useFillCache ( ) const
private

Indicate whether the 'fill cache' is needed to handle sub-page block sizes.

Definition at line 804 of file File.cc.

References getBlockSize(), and PhysicalMemoryManager::getPageSize().

Referenced by getPhysicalPage(), read(), readIntoCache(), and returnPhysicalPage().

+ Here is the caller graph for this function:

uint64_t File::write ( uint64_t  location,
uint64_t  size,
uintptr_t  buffer,
bool  bCanBlock = true 
)
finalvirtual

Writes to the file.

Parameters
[in]bCanBlockWhether or not the File can block when reading
Todo:
consider caching this still

Definition at line 183 of file File.cc.

References ERROR, extend(), getBlockSize(), isBytewise(), readIntoCache(), writeBlock(), and writeBytewise().

Referenced by Ext2Filesystem::createNode(), FatFilesystem::createSymlink(), DevFs::initialise(), ConsolePhysicalFile::readBytewise(), and UnixSocketSyscalls::sendto_msg().

+ Here is the caller graph for this function:

void File::writeBlock ( uint64_t  location,
uintptr_t  addr 
)
protectedvirtual

Internal function to write a block retrieved with readBlock back to the file. The address of the block is provided for convenience.

Reimplemented in FatFile, and Ext2File.

Definition at line 611 of file File.cc.

Referenced by sync(), write(), and writeCallback().

+ Here is the caller graph for this function:

uint64_t File::writeBytewise ( uint64_t  location,
uint64_t  size,
uintptr_t  buffer,
bool  bCanBlock = true 
)
protectedvirtual

Writes bytes to the file, if isBytewise() == true.

Reimplemented in ConsolePhysicalFile, ConsoleSlaveFile, Tty0File, ConsoleMasterFile, RtcFile, ConstantFile, PtmxFile, TextIO, UnixSocket, UptimeFile, ZeroFile, MountFile, NullFile, PciDevicesFile, FatSymlink, Pipe, RandomFile, Ext2Symlink, PsAuxFile, and MeminfoFile.

Definition at line 592 of file File.cc.

References FATAL, and isBytewise().

Referenced by Pipe::fromFile(), and write().

+ Here is the caller graph for this function:

void File::writeCallback ( CacheConstants::CallbackCause  cause,
uintptr_t  loc,
uintptr_t  page,
void *  meta 
)
staticprotected

Called by a cache to write back changed data to disk.

File subclasses that use a Cache for readBlock can utilise this as the callback on their Cache instance to get a write-back notification.

Todo:
handle block size < 4K??

Definition at line 37 of file File.cc.

References getBlockSize(), PhysicalMemoryManager::getPageSize(), setCachedPage(), WARNING, and writeBlock().

Referenced by FatFile::FatFile().

+ Here is the caller graph for this function:

Member Data Documentation

Cache File::m_FillCache
protected

This cache is necessary to handle filesystems with block sizes that are smaller than the native page size. For these filesystems, to perform memory maps we read native page size blocks into this cache, and then return pages from it directly. This is expected to somewhat increase memory usage and reduce performance on non-natively-sized block sizes, but that's an acceptable compromise.

Definition at line 379 of file File.h.

Referenced by getPhysicalPage(), readIntoCache(), and returnPhysicalPage().


The documentation for this class was generated from the following files: