physfs.c
changeset 654 c0ae01de361d
parent 650 298b8bb26775
child 657 dad3b5c307a9
equal deleted inserted replaced
653:89d56fcb2b40 654:c0ae01de361d
  1642 
  1642 
  1643     return(retval);
  1643     return(retval);
  1644 } /* PHYSFS_isSymbolicLink */
  1644 } /* PHYSFS_isSymbolicLink */
  1645 
  1645 
  1646 
  1646 
  1647 static PHYSFS_file *doOpenWrite(const char *fname, int appending)
  1647 static PHYSFS_File *doOpenWrite(const char *fname, int appending)
  1648 {
  1648 {
  1649     void *opaque = NULL;
  1649     void *opaque = NULL;
  1650     FileHandle *fh = NULL;
  1650     FileHandle *fh = NULL;
  1651     DirHandle *h = NULL;
  1651     DirHandle *h = NULL;
  1652     const PHYSFS_Archiver *f;
  1652     const PHYSFS_Archiver *f;
  1685         fh->next = openWriteList;
  1685         fh->next = openWriteList;
  1686         openWriteList = fh;
  1686         openWriteList = fh;
  1687     } /* else */
  1687     } /* else */
  1688 
  1688 
  1689     __PHYSFS_platformReleaseMutex(stateLock);
  1689     __PHYSFS_platformReleaseMutex(stateLock);
  1690     return((PHYSFS_file *) fh);
  1690     return((PHYSFS_File *) fh);
  1691 } /* doOpenWrite */
  1691 } /* doOpenWrite */
  1692 
  1692 
  1693 
  1693 
  1694 PHYSFS_file *PHYSFS_openWrite(const char *filename)
  1694 PHYSFS_File *PHYSFS_openWrite(const char *filename)
  1695 {
  1695 {
  1696     return(doOpenWrite(filename, 0));
  1696     return(doOpenWrite(filename, 0));
  1697 } /* PHYSFS_openWrite */
  1697 } /* PHYSFS_openWrite */
  1698 
  1698 
  1699 
  1699 
  1700 PHYSFS_file *PHYSFS_openAppend(const char *filename)
  1700 PHYSFS_File *PHYSFS_openAppend(const char *filename)
  1701 {
  1701 {
  1702     return(doOpenWrite(filename, 1));
  1702     return(doOpenWrite(filename, 1));
  1703 } /* PHYSFS_openAppend */
  1703 } /* PHYSFS_openAppend */
  1704 
  1704 
  1705 
  1705 
  1706 PHYSFS_file *PHYSFS_openRead(const char *fname)
  1706 PHYSFS_File *PHYSFS_openRead(const char *fname)
  1707 {
  1707 {
  1708     FileHandle *fh = NULL;
  1708     FileHandle *fh = NULL;
  1709     int fileExists = 0;
  1709     int fileExists = 0;
  1710     DirHandle *i = NULL;
  1710     DirHandle *i = NULL;
  1711     fvoid *opaque = NULL;
  1711     fvoid *opaque = NULL;
  1746     fh->funcs = i->funcs;
  1746     fh->funcs = i->funcs;
  1747     fh->next = openReadList;
  1747     fh->next = openReadList;
  1748     openReadList = fh;
  1748     openReadList = fh;
  1749     __PHYSFS_platformReleaseMutex(stateLock);
  1749     __PHYSFS_platformReleaseMutex(stateLock);
  1750 
  1750 
  1751     return((PHYSFS_file *) fh);
  1751     return((PHYSFS_File *) fh);
  1752 } /* PHYSFS_openRead */
  1752 } /* PHYSFS_openRead */
  1753 
  1753 
  1754 
  1754 
  1755 static int closeHandleInOpenList(FileHandle **list, FileHandle *handle)
  1755 static int closeHandleInOpenList(FileHandle **list, FileHandle *handle)
  1756 {
  1756 {
  1761     for (i = *list; i != NULL; i = i->next)
  1761     for (i = *list; i != NULL; i = i->next)
  1762     {
  1762     {
  1763         if (i == handle)  /* handle is in this list? */
  1763         if (i == handle)  /* handle is in this list? */
  1764         {
  1764         {
  1765             PHYSFS_uint8 *tmp = handle->buffer;
  1765             PHYSFS_uint8 *tmp = handle->buffer;
  1766             rc = PHYSFS_flush((PHYSFS_file *) handle);
  1766             rc = PHYSFS_flush((PHYSFS_File *) handle);
  1767             if (rc)
  1767             if (rc)
  1768                 rc = handle->funcs->fileClose(handle->opaque);
  1768                 rc = handle->funcs->fileClose(handle->opaque);
  1769             if (!rc)
  1769             if (!rc)
  1770                 return(-1);
  1770                 return(-1);
  1771 
  1771 
  1785 
  1785 
  1786     return(0);
  1786     return(0);
  1787 } /* closeHandleInOpenList */
  1787 } /* closeHandleInOpenList */
  1788 
  1788 
  1789 
  1789 
  1790 int PHYSFS_close(PHYSFS_file *_handle)
  1790 int PHYSFS_close(PHYSFS_File *_handle)
  1791 {
  1791 {
  1792     FileHandle *handle = (FileHandle *) _handle;
  1792     FileHandle *handle = (FileHandle *) _handle;
  1793     int rc;
  1793     int rc;
  1794 
  1794 
  1795     __PHYSFS_platformGrabMutex(stateLock);
  1795     __PHYSFS_platformGrabMutex(stateLock);
  1851 
  1851 
  1852     return(retval);
  1852     return(retval);
  1853 } /* doBufferedRead */
  1853 } /* doBufferedRead */
  1854 
  1854 
  1855 
  1855 
  1856 PHYSFS_sint64 PHYSFS_read(PHYSFS_file *handle, void *buffer,
  1856 PHYSFS_sint64 PHYSFS_read(PHYSFS_File *handle, void *buffer,
  1857                           PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1857                           PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1858 {
  1858 {
  1859     FileHandle *fh = (FileHandle *) handle;
  1859     FileHandle *fh = (FileHandle *) handle;
  1860 
  1860 
  1861     BAIL_IF_MACRO(!fh->forReading, ERR_FILE_ALREADY_OPEN_W, -1);
  1861     BAIL_IF_MACRO(!fh->forReading, ERR_FILE_ALREADY_OPEN_W, -1);
  1864 
  1864 
  1865     return(fh->funcs->read(fh->opaque, buffer, objSize, objCount));
  1865     return(fh->funcs->read(fh->opaque, buffer, objSize, objCount));
  1866 } /* PHYSFS_read */
  1866 } /* PHYSFS_read */
  1867 
  1867 
  1868 
  1868 
  1869 static PHYSFS_sint64 doBufferedWrite(PHYSFS_file *handle, const void *buffer,
  1869 static PHYSFS_sint64 doBufferedWrite(PHYSFS_File *handle, const void *buffer,
  1870                                      PHYSFS_uint32 objSize,
  1870                                      PHYSFS_uint32 objSize,
  1871                                      PHYSFS_uint32 objCount)
  1871                                      PHYSFS_uint32 objCount)
  1872 {
  1872 {
  1873     FileHandle *fh = (FileHandle *) handle;
  1873     FileHandle *fh = (FileHandle *) handle;
  1874     
  1874     
  1884     BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, -1);
  1884     BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, -1);
  1885     return(fh->funcs->write(fh->opaque, buffer, objSize, objCount));
  1885     return(fh->funcs->write(fh->opaque, buffer, objSize, objCount));
  1886 } /* doBufferedWrite */
  1886 } /* doBufferedWrite */
  1887 
  1887 
  1888 
  1888 
  1889 PHYSFS_sint64 PHYSFS_write(PHYSFS_file *handle, const void *buffer,
  1889 PHYSFS_sint64 PHYSFS_write(PHYSFS_File *handle, const void *buffer,
  1890                            PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1890                            PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1891 {
  1891 {
  1892     FileHandle *fh = (FileHandle *) handle;
  1892     FileHandle *fh = (FileHandle *) handle;
  1893 
  1893 
  1894     BAIL_IF_MACRO(fh->forReading, ERR_FILE_ALREADY_OPEN_R, -1);
  1894     BAIL_IF_MACRO(fh->forReading, ERR_FILE_ALREADY_OPEN_R, -1);
  1897 
  1897 
  1898     return(fh->funcs->write(fh->opaque, buffer, objSize, objCount));
  1898     return(fh->funcs->write(fh->opaque, buffer, objSize, objCount));
  1899 } /* PHYSFS_write */
  1899 } /* PHYSFS_write */
  1900 
  1900 
  1901 
  1901 
  1902 int PHYSFS_eof(PHYSFS_file *handle)
  1902 int PHYSFS_eof(PHYSFS_File *handle)
  1903 {
  1903 {
  1904     FileHandle *fh = (FileHandle *) handle;
  1904     FileHandle *fh = (FileHandle *) handle;
  1905 
  1905 
  1906     if (!fh->forReading)  /* never EOF on files opened for write/append. */
  1906     if (!fh->forReading)  /* never EOF on files opened for write/append. */
  1907         return(0);
  1907         return(0);
  1909     /* eof if buffer is empty and archiver says so. */
  1909     /* eof if buffer is empty and archiver says so. */
  1910     return((fh->bufpos == fh->buffill) && (fh->funcs->eof(fh->opaque)));
  1910     return((fh->bufpos == fh->buffill) && (fh->funcs->eof(fh->opaque)));
  1911 } /* PHYSFS_eof */
  1911 } /* PHYSFS_eof */
  1912 
  1912 
  1913 
  1913 
  1914 PHYSFS_sint64 PHYSFS_tell(PHYSFS_file *handle)
  1914 PHYSFS_sint64 PHYSFS_tell(PHYSFS_File *handle)
  1915 {
  1915 {
  1916     FileHandle *fh = (FileHandle *) handle;
  1916     FileHandle *fh = (FileHandle *) handle;
  1917     PHYSFS_sint64 pos = fh->funcs->tell(fh->opaque);
  1917     PHYSFS_sint64 pos = fh->funcs->tell(fh->opaque);
  1918     PHYSFS_sint64 retval = fh->forReading ?
  1918     PHYSFS_sint64 retval = fh->forReading ?
  1919                             (pos - fh->buffill) + fh->bufpos :
  1919                             (pos - fh->buffill) + fh->bufpos :
  1920                             (pos + fh->buffill);
  1920                             (pos + fh->buffill);
  1921     return(retval);
  1921     return(retval);
  1922 } /* PHYSFS_tell */
  1922 } /* PHYSFS_tell */
  1923 
  1923 
  1924 
  1924 
  1925 int PHYSFS_seek(PHYSFS_file *handle, PHYSFS_uint64 pos)
  1925 int PHYSFS_seek(PHYSFS_File *handle, PHYSFS_uint64 pos)
  1926 {
  1926 {
  1927     FileHandle *fh = (FileHandle *) handle;
  1927     FileHandle *fh = (FileHandle *) handle;
  1928     BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, 0);
  1928     BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, 0);
  1929 
  1929 
  1930     if (fh->buffer && fh->forReading)
  1930     if (fh->buffer && fh->forReading)
  1944     fh->buffill = fh->bufpos = 0;
  1944     fh->buffill = fh->bufpos = 0;
  1945     return(fh->funcs->seek(fh->opaque, pos));
  1945     return(fh->funcs->seek(fh->opaque, pos));
  1946 } /* PHYSFS_seek */
  1946 } /* PHYSFS_seek */
  1947 
  1947 
  1948 
  1948 
  1949 PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_file *handle)
  1949 PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_File *handle)
  1950 {
  1950 {
  1951     FileHandle *fh = (FileHandle *) handle;
  1951     FileHandle *fh = (FileHandle *) handle;
  1952     return(fh->funcs->fileLength(fh->opaque));
  1952     return(fh->funcs->fileLength(fh->opaque));
  1953 } /* PHYSFS_filelength */
  1953 } /* PHYSFS_filelength */
  1954 
  1954 
  1955 
  1955 
  1956 int PHYSFS_setBuffer(PHYSFS_file *handle, PHYSFS_uint64 _bufsize)
  1956 int PHYSFS_setBuffer(PHYSFS_File *handle, PHYSFS_uint64 _bufsize)
  1957 {
  1957 {
  1958     FileHandle *fh = (FileHandle *) handle;
  1958     FileHandle *fh = (FileHandle *) handle;
  1959     PHYSFS_uint32 bufsize;
  1959     PHYSFS_uint32 bufsize;
  1960 
  1960 
  1961     BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, "buffer must fit in 32-bits", 0);
  1961     BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, "buffer must fit in 32-bits", 0);
  1997     fh->buffill = fh->bufpos = 0;
  1997     fh->buffill = fh->bufpos = 0;
  1998     return(1);
  1998     return(1);
  1999 } /* PHYSFS_setBuffer */
  1999 } /* PHYSFS_setBuffer */
  2000 
  2000 
  2001 
  2001 
  2002 int PHYSFS_flush(PHYSFS_file *handle)
  2002 int PHYSFS_flush(PHYSFS_File *handle)
  2003 {
  2003 {
  2004     FileHandle *fh = (FileHandle *) handle;
  2004     FileHandle *fh = (FileHandle *) handle;
  2005     PHYSFS_sint64 rc;
  2005     PHYSFS_sint64 rc;
  2006 
  2006 
  2007     if ((fh->forReading) || (fh->bufpos == fh->buffill))
  2007     if ((fh->forReading) || (fh->bufpos == fh->buffill))