~~Title: Reading and Writing File Data~~ As mentioned above, the VFS Plugin API defines a filesystem-like interface for reading and writing of files within the plugin's namespace. There are four key functions that need to be implemented in order to function as a useful plugin (or three for a read-only plugin) - [[:reference:plugins:vfs_plugins:vfs_createfile|VFS_CreateFile]], [[:reference:plugins:vfs_plugins:vfs_readfile|VFS_ReadFile]], [[:reference:plugins:vfs_plugins:vfs_writefile|VFS_WriteFile]] and [[:reference:plugins:vfs_plugins:vfs_closefile|VFS_CloseFile]]. An optional fifth function is [[:reference:plugins:vfs_plugins:vfs_seekfile|VFS_SeekFile]] which is not required, but it is recommended you provide it if possible. HANDLE VFS_CreateFile( HANDLE hVFSData, LPVFSFUNCDATA lpFuncData, LPTSTR lpszFile, DWORD dwMode, DWORD dwFlagsAndAttr, DWORD dwFlags, LPFILETIME lpFT ) The **hVFSData** parameter is your instance handle, returned from [[:reference:plugins:vfs_plugins:vfs_create|VFS_Create]]. This parameter is passed to almost all of the functions in the plugin. The **lpFuncData** parameter is a general purpose data pointer that currently is not used. Many functions accept this parameter and it may be used in future revisions of the API to provide additional information or callback parameters to your functions. For now you should ignore this parameter - it will usually be NULL. **lpszFile** is the full pathname of the file to create or open. **dwMode** specifies the required operation - it will equal either **GENERIC_WRITE** or **GENERIC_READ** (but never both - Opus never requests read/write access to a file.) If **GENERIC_WRITE** is specified you should attempt to create the requested file; if **GENERIC_READ** is specified you should attempt to open an existing file. **dwFlagsAndAttr** is equivalent to the same parameter in the Windows **CreateFile** function. It specifies the file attributes that the newly created file should be given (if **GENERIC_WRITE** was specified.) and also may contain flags controlling the behavior of the new file. The file attributes will usually be set to **FILE_ATTRIBUTE_NORMAL**. If your namespace does not support the concept of file attributes or does not support all the standard Windows attributes you are free to ignore this parameter. Currently the only flags that Opus uses at times are **FILE_FLAG_SEQUENTIAL_SCAN** and **FILE_FLAG_BACKUP_SEMANTICS** - you are free to ignore both of these flags unless it makes sense for your plugin to support them. **dwFlags** is a flags field that combines the "creation disposition" parameter of the Windows **CreateFile** function with several Opus-specific flags. Again, you are free to ignore any of these flags if they do not make sense to your plugin. The "creation disposition" parameters are **CREATE_NEW**, **CREATE_ALWAYS**, **OPEN_EXISTING**, **OPEN_ALWAYS** and **TRUNCATE_EXISTING**. You can retrieve the creation disposition value using the special mask value **VFSCREATEF_MODEMASK**. If the creation disposition value is not specified (ie, the value is 0) you should assume "create new" for a write operation and "open existing" for a read operation. The Opus-specific flags for this parameter are: |VFSCREATEF_RESUME|file is being opened to resume a transfer| |VFSCREATEF_RECURSIVECOPY|a recursive copy operation is in progress| |VFSCREATEF_THUMBNAIL|Opus wants to generate a thumbnail image| |VFSCREATEF_IGNOREIFSLOW|if reading the file will take a long time you should not open it and return failure instead| |VFSCREATEF_BUFFERED|the file is being buffered| The value you return from [[:reference:plugins:vfs_plugins:vfs_createfile|VFS_CreateFile]] must be a data structure you allocate that can serve to identify the file in question. You must cast this value to a **HANDLE** and Opus treats it as a black-box - it does not know what the value points to, but merely saves the handle and passes it back to your other file functions. The [[:reference:plugins:vfs_plugins:vfs_readfile|VFS_ReadFile]] and [[:reference:plugins:vfs_plugins:vfs_writefile|VFS_WriteFile]] functions are analogous to the **ReadFile** and **WriteFile** Windows API functions. BOOL VFS_ReadFile( HANDLE hVFSData, LPVFSFUNCDATA lpFuncData, HANDLE hFile, LPVOID lpData, DWORD dwSize, LPDWORD lpdwReadSize ) The parameters for this call are mostly self-explanatory. **hVFSData** is your instance handle. **lpFuncData** is not currently used. **hFile** is your file handle, returned by the call to [[:reference:plugins:vfs_plugins:vfs_createfile|VFS_CreateFile]]. **lpData** points to an area of memory that Opus wants you to return file data in. **dwSize** indicates the requested data amount to read, and **lpdwReadSize** points to a DWORD so you can tell Opus the actual amount of data you read. Similar to the Windows **ReadFile** function, you do not need to read all requested data. You should not only return as much data as you can, up to and including **dwSize** bytes. Return the actual amount of data you did read in the value pointed to be **lpdwReadSize**. You should return **TRUE** from this function if you were successfully able to read any data at all. If an error occurred or the end-of-file has been reached, you should return **FALSE**. Do not return **FALSE** if you read any data - even if you were only able to read one byte, you should still return **TRUE**. The [[:reference:plugins:vfs_plugins:vfs_writefile|VFS_WriteFile]] function is very similar to the [[:reference:plugins:vfs_plugins:vfs_readfile|VFS_ReadFile]] function, except it has an additional **BOOL fFlush** parameter. This will be set to **TRUE** if Opus wants you to flush any cached data to the file. If your plugin does not perform write caching you should ignore this parameter. If your plugin is read-only and does not support creation of or writing to files at all, you should not provide a [[:reference:plugins:vfs_plugins:vfs_writefile|VFS_WriteFile]] function, and you should make sure you set the **VFSCAPABILITY_READONLY** capabilities flag. If your plugin supports random seeking within files you should implement the [[:reference:plugins:vfs_plugins:vfs_seekfile|VFS_SeekFile]] function. Even if you only support forwards seeking you should implement this function and simply return an error for seek attempts that you can't handle.