aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/file.cpp111
-rw-r--r--src/file.h27
-rw-r--r--src/nids.cpp15
-rw-r--r--src/tests/archive.cpp2
-rw-r--r--src/tests/archive2.cpp2
-rw-r--r--src/tests/bzip2.cpp4
-rw-r--r--src/tests/nids.cpp11
-rw-r--r--src/tests/taf.cpp8
-rw-r--r--src/unit/file.cpp8
-rw-r--r--src/unit/taf.cpp4
10 files changed, 116 insertions, 76 deletions
diff --git a/src/file.cpp b/src/file.cpp
index 3419216..5049bbb 100644
--- a/src/file.cpp
+++ b/src/file.cpp
@@ -10,30 +10,24 @@
10#include <sys/types.h> 10#include <sys/types.h>
11#include <sys/stat.h> 11#include <sys/stat.h>
12#include <fcntl.h> 12#include <fcntl.h>
13#include <unistd.h>
13 14
14namespace Bu { subExceptionDef( FileException ) } 15namespace Bu { subExceptionDef( FileException ) }
15 16
16Bu::File::File( const char *sName, const char *sFlags ) 17Bu::File::File( const Bu::FString &sName, int iFlags ) :
18 fd( -1 )
17{ 19{
18 fh = fopen( sName, sFlags ); 20 fd = ::open( sName.getStr(), getPosixFlags( iFlags ) );
19 if( fh == NULL ) 21 if( fd < 0 )
20 { 22 {
21 throw Bu::FileException( errno, "%s: %s", strerror(errno), sName ); 23 throw Bu::FileException( errno, "%s: %s",
24 strerror(errno), sName.getStr() );
22 } 25 }
23} 26}
24 27
25Bu::File::File( const Bu::FString &sName, const char *sFlags ) 28Bu::File::File( int fd ) :
29 fd( fd )
26{ 30{
27 fh = fopen( sName.getStr(), sFlags );
28 if( fh == NULL )
29 {
30 throw Bu::FileException( errno, "%s: %s", strerror(errno), sName.getStr() );
31 }
32}
33
34Bu::File::File( int fd, const char *sFlags )
35{
36 fh = fdopen( fd, sFlags );
37} 31}
38 32
39Bu::File::~File() 33Bu::File::~File()
@@ -43,69 +37,68 @@ Bu::File::~File()
43 37
44void Bu::File::close() 38void Bu::File::close()
45{ 39{
46 if( fh ) 40 if( fd >= 0 )
47 { 41 {
48 fclose( fh ); 42 if( ::close( fd ) )
49 fh = NULL; 43 {
44 throw Bu::FileException( errno, "%s",
45 strerror(errno) );
46 }
47 fd = -1;
50 } 48 }
51} 49}
52 50
53size_t Bu::File::read( void *pBuf, size_t nBytes ) 51size_t Bu::File::read( void *pBuf, size_t nBytes )
54{ 52{
55 if( !fh ) 53 if( fd < 0 )
56 throw FileException("File not open."); 54 throw FileException("File not open.");
57 55
58 int nAmnt = fread( pBuf, 1, nBytes, fh ); 56 return ::read( fd, pBuf, nBytes );
59
60 //if( nAmnt == 0 )
61 // throw FileException("End of file.");
62
63 return nAmnt;
64} 57}
65 58
66size_t Bu::File::write( const void *pBuf, size_t nBytes ) 59size_t Bu::File::write( const void *pBuf, size_t nBytes )
67{ 60{
68 if( !fh ) 61 if( fd < 0 )
69 throw FileException("File not open."); 62 throw FileException("File not open.");
70 63
71 return fwrite( pBuf, 1, nBytes, fh ); 64 return ::write( fd, pBuf, nBytes );
72} 65}
73 66
74long Bu::File::tell() 67long Bu::File::tell()
75{ 68{
76 if( !fh ) 69 if( fd < 0 )
77 throw FileException("File not open."); 70 throw FileException("File not open.");
78 71
79 return ftell( fh ); 72 return lseek( fd, 0, SEEK_CUR );
80} 73}
81 74
82void Bu::File::seek( long offset ) 75void Bu::File::seek( long offset )
83{ 76{
84 if( !fh ) 77 if( fd < 0 )
85 throw FileException("File not open."); 78 throw FileException("File not open.");
86 79
87 fseek( fh, offset, SEEK_CUR ); 80 lseek( fd, offset, SEEK_CUR );
88} 81}
89 82
90void Bu::File::setPos( long pos ) 83void Bu::File::setPos( long pos )
91{ 84{
92 if( !fh ) 85 if( fd < 0 )
93 throw FileException("File not open."); 86 throw FileException("File not open.");
94 87
95 fseek( fh, pos, SEEK_SET ); 88 lseek( fd, pos, SEEK_SET );
96} 89}
97 90
98void Bu::File::setPosEnd( long pos ) 91void Bu::File::setPosEnd( long pos )
99{ 92{
100 if( !fh ) 93 if( fd < 0 )
101 throw FileException("File not open."); 94 throw FileException("File not open.");
102 95
103 fseek( fh, pos, SEEK_END ); 96 lseek( fd, pos, SEEK_END );
104} 97}
105 98
106bool Bu::File::isEOS() 99bool Bu::File::isEOS()
107{ 100{
108 return feof( fh ); 101 return false;
109} 102}
110 103
111bool Bu::File::canRead() 104bool Bu::File::canRead()
@@ -145,13 +138,13 @@ void Bu::File::setBlocking( bool bBlocking )
145#else 138#else
146 if( bBlocking ) 139 if( bBlocking )
147 fcntl( 140 fcntl(
148 fileno( fh ), 141 fd,
149 F_SETFL, fcntl( fileno( fh ), F_GETFL, 0 )&(~O_NONBLOCK) 142 F_SETFL, fcntl( fd, F_GETFL, 0 )&(~O_NONBLOCK)
150 ); 143 );
151 else 144 else
152 fcntl( 145 fcntl(
153 fileno( fh ), 146 fd,
154 F_SETFL, fcntl( fileno( fh ), F_GETFL, 0 )|O_NONBLOCK 147 F_SETFL, fcntl( fd, F_GETFL, 0 )|O_NONBLOCK
155 ); 148 );
156#endif 149#endif
157} 150}
@@ -159,22 +152,52 @@ void Bu::File::setBlocking( bool bBlocking )
159#ifndef WIN32 152#ifndef WIN32
160void Bu::File::truncate( long nSize ) 153void Bu::File::truncate( long nSize )
161{ 154{
162 ftruncate( fileno( fh ), nSize ); 155 ftruncate( fd, nSize );
163} 156}
164 157
165void Bu::File::chmod( mode_t t ) 158void Bu::File::chmod( mode_t t )
166{ 159{
167 fchmod( fileno( fh ), t ); 160 fchmod( fd, t );
168} 161}
169#endif 162#endif
170 163
171void Bu::File::flush() 164void Bu::File::flush()
172{ 165{
173 fflush( fh ); 166 // There is no flushing with direct I/O...
167 //fflush( fh );
174} 168}
175 169
176bool Bu::File::isOpen() 170bool Bu::File::isOpen()
177{ 171{
178 return (fh != NULL); 172 return (fd > -1);
173}
174
175int Bu::File::getPosixFlags( int iFlags )
176{
177 int iRet = 0;
178 switch( (iFlags&ReadWrite) )
179 {
180 // According to posix, O_RDWR is not necesarilly O_RDONLY|O_WRONLY, so
181 // lets be proper and use the right value in the right place.
182 case Read: iRet = O_RDONLY; break;
183 case Write: iRet = O_WRONLY; break;
184 case ReadWrite: iRet = O_RDWR; break;
185 default:
186 throw FileException(
187 "You must specify Read, Write, or both when opening a file.");
188 }
189
190 if( (iFlags&Create) )
191 iRet |= O_CREAT;
192 if( (iFlags&Append) )
193 iRet |= O_APPEND;
194 if( (iFlags&Truncate) )
195 iRet |= O_TRUNC;
196 if( (iFlags&NonBlock) )
197 iRet |= O_NONBLOCK;
198 if( (iFlags&Exclusive) )
199 iRet |= O_EXCL;
200
201 return iRet;
179} 202}
180 203
diff --git a/src/file.h b/src/file.h
index e48e6f1..40419b0 100644
--- a/src/file.h
+++ b/src/file.h
@@ -10,7 +10,6 @@
10 10
11#include <stdint.h> 11#include <stdint.h>
12#include <sys/types.h> 12#include <sys/types.h>
13#include <stdlib.h>
14 13
15#include "bu/stream.h" 14#include "bu/stream.h"
16#include "bu/fstring.h" 15#include "bu/fstring.h"
@@ -27,9 +26,8 @@ namespace Bu
27 class File : public Bu::Stream 26 class File : public Bu::Stream
28 { 27 {
29 public: 28 public:
30 File( const char *sName, const char *sFlags ); 29 File( const Bu::FString &sName, int iFlags );
31 File( const Bu::FString &sName, const char *sFlags ); 30 File( int fd );
32 File( int fd, const char *sFlags );
33 virtual ~File(); 31 virtual ~File();
34 32
35 virtual void close(); 33 virtual void close();
@@ -56,6 +54,19 @@ namespace Bu
56 virtual bool isBlocking(); 54 virtual bool isBlocking();
57 virtual void setBlocking( bool bBlocking=true ); 55 virtual void setBlocking( bool bBlocking=true );
58 56
57 enum {
58 // Flags
59 Read = 0x01, //< Open file for reading
60 Write = 0x02, //< Open file for writing
61 Create = 0x04, //< Create file if it doesn't exist
62 Truncate = 0x08, //< Truncate file if it does exist
63 Append = 0x10, //< Always append on every write
64 NonBlock = 0x20, //< Open file in non-blocking mode
65 Exclusive = 0x44, //< Create file, if it exists then fail
66
67 // Helpful mixes
68 ReadWrite = 0x03 //< Open for reading and writing
69 };
59 /** 70 /**
60 * Create a temp file and return its handle 71 * Create a temp file and return its handle
61 *@param sName (Bu::FString) Give in the form: "/tmp/tmpfileXXXXXXXX" 72 *@param sName (Bu::FString) Give in the form: "/tmp/tmpfileXXXXXXXX"
@@ -65,11 +76,11 @@ namespace Bu
65 *@returns (Bu::File) A file object representing your temp file. 76 *@returns (Bu::File) A file object representing your temp file.
66 */ 77 */
67#ifndef WIN32 78#ifndef WIN32
68 inline static Bu::File tempFile( Bu::FString &sName, const char *sFlags ) 79 inline static Bu::File tempFile( Bu::FString &sName, int /*iFlags*/ )
69 { 80 {
70 int afh_d = mkstemp( sName.getStr() ); 81 int afh_d = mkstemp( sName.getStr() );
71 82
72 return Bu::File( afh_d, sFlags ); 83 return Bu::File( afh_d );
73 } 84 }
74 85
75 /** 86 /**
@@ -87,8 +98,10 @@ namespace Bu
87#endif 98#endif
88 99
89 private: 100 private:
90 FILE *fh; 101 int getPosixFlags( int iFlags );
91 102
103 private:
104 int fd;
92 }; 105 };
93} 106}
94 107
diff --git a/src/nids.cpp b/src/nids.cpp
index d0cb843..a6596a2 100644
--- a/src/nids.cpp
+++ b/src/nids.cpp
@@ -3,7 +3,7 @@
3#include "bu/nidsstream.h" 3#include "bu/nidsstream.h"
4#include <stdio.h> 4#include <stdio.h>
5 5
6#define NIDS_MAGIC_CODE "\xFF\xC3\x99\xBD" 6#define NIDS_MAGIC_CODE ((unsigned char *)"\xFF\xC3\x99\xBD")
7 7
8namespace Bu 8namespace Bu
9{ 9{
@@ -36,8 +36,10 @@ Bu::Nids::Nids( Bu::Stream &sStore ) :
36 sStore.isOpen()?"yes":"no" 36 sStore.isOpen()?"yes":"no"
37 ); 37 );
38 printf("sizeof(Block) = %db\n", sizeof(Block) ); 38 printf("sizeof(Block) = %db\n", sizeof(Block) );
39 39 printf("Magic: %02X%02X%02X%02X\n",
40 40 NIDS_MAGIC_CODE[0], NIDS_MAGIC_CODE[1],
41 NIDS_MAGIC_CODE[2], NIDS_MAGIC_CODE[3]
42 );
41} 43}
42 44
43Bu::Nids::~Nids() 45Bu::Nids::~Nids()
@@ -46,13 +48,16 @@ Bu::Nids::~Nids()
46 48
47void Bu::Nids::initialize() 49void Bu::Nids::initialize()
48{ 50{
49 char buf[4]; 51 unsigned char buf[4];
50 sStore.read( buf, 4 ); 52 if( sStore.read( buf, 4 ) < 4 )
53 throw NidsException("Input stream appears to be empty.");
51 if( memcmp( buf, NIDS_MAGIC_CODE, 4 ) ) 54 if( memcmp( buf, NIDS_MAGIC_CODE, 4 ) )
52 { 55 {
53 throw NidsException( 56 throw NidsException(
54 "Stream does not appear to be a valid NIDS format."); 57 "Stream does not appear to be a valid NIDS format.");
55 } 58 }
59
60
56} 61}
57 62
58void Bu::Nids::initialize( int iBlockSize, int iPreAllocate ) 63void Bu::Nids::initialize( int iBlockSize, int iPreAllocate )
diff --git a/src/tests/archive.cpp b/src/tests/archive.cpp
index 698d37c..131d4de 100644
--- a/src/tests/archive.cpp
+++ b/src/tests/archive.cpp
@@ -12,7 +12,7 @@ using namespace Bu;
12 12
13int main() 13int main()
14{ 14{
15 File f("test.dat", "wb"); 15 File f("test.dat", File::Write );
16 Archive ar( f, Archive::save ); 16 Archive ar( f, Archive::save );
17 17
18 std::string s("Hello there"); 18 std::string s("Hello there");
diff --git a/src/tests/archive2.cpp b/src/tests/archive2.cpp
index 6d3c2c1..ab02d04 100644
--- a/src/tests/archive2.cpp
+++ b/src/tests/archive2.cpp
@@ -82,7 +82,7 @@ void write()
82{ 82{
83 C *c = new C; 83 C *c = new C;
84 84
85 Bu::File f( "test.archive", "wb"); 85 Bu::File f( "test.archive", Bu::File::Write );
86 Bu::Archive ar( f, Bu::Archive::save ); 86 Bu::Archive ar( f, Bu::Archive::save );
87 ar << c; 87 ar << c;
88} 88}
diff --git a/src/tests/bzip2.cpp b/src/tests/bzip2.cpp
index de7c034..77dc064 100644
--- a/src/tests/bzip2.cpp
+++ b/src/tests/bzip2.cpp
@@ -19,10 +19,10 @@ int main( int argc, char *argv[] )
19 char buf[1024]; 19 char buf[1024];
20 size_t nRead; 20 size_t nRead;
21 21
22 Bu::File f( argv[0], "wb" ); 22 Bu::File f( argv[0], Bu::File::Write );
23 Bu::BZip2 bz2( f ); 23 Bu::BZip2 bz2( f );
24 24
25 Bu::File fin( argv[1], "rb"); 25 Bu::File fin( argv[1], Bu::File::Read );
26 26
27 for(;;) 27 for(;;)
28 { 28 {
diff --git a/src/tests/nids.cpp b/src/tests/nids.cpp
index 4856883..f50fde5 100644
--- a/src/tests/nids.cpp
+++ b/src/tests/nids.cpp
@@ -10,17 +10,16 @@ int main( int argc, char *argv[] )
10 return 1; 10 return 1;
11 } 11 }
12 12
13 Bu::File fOut( argv[1], "wb+"); 13 Bu::File fOut( argv[1], Bu::File::ReadWrite );
14 Bu::Nids n( fOut ); 14 Bu::Nids n( fOut );
15 15
16// n.initialize( 120, 5 ); 16// n.initialize( 120, 5 );
17 17
18 Bu::NidsStream s = n.openStream( n.createStream() ); 18 Bu::NidsStream s = n.openStream( n.createStream() );
19/* 19
20 Bu::FString sBuf( 350 ); 20// Bu::FString sBuf( 350 );
21 memset( sBuf.getStr(), 'a', 350 ); 21// memset( sBuf.getStr(), 'a', 350 );
22 s.write( sBuf ); 22// s.write( sBuf );
23 */
24 23
25 return 0; 24 return 0;
26} 25}
diff --git a/src/tests/taf.cpp b/src/tests/taf.cpp
index 281b9c4..859ecfc 100644
--- a/src/tests/taf.cpp
+++ b/src/tests/taf.cpp
@@ -13,13 +13,13 @@ int main( int argc, char *argv[] )
13{ 13{
14 if( argc == 1 ) 14 if( argc == 1 )
15 { 15 {
16 Bu::File f("test.taf", "rb"); 16 Bu::File f("test.taf", Bu::File::Read );
17 Bu::TafReader tr( f ); 17 Bu::TafReader tr( f );
18 18
19 Bu::TafGroup *pGroup = tr.readGroup(); 19 Bu::TafGroup *pGroup = tr.readGroup();
20 20
21 { 21 {
22 Bu::File fo("out.taf", "wb"); 22 Bu::File fo("out.taf", Bu::File::Write );
23 Bu::TafWriter tw( fo ); 23 Bu::TafWriter tw( fo );
24 tw.writeGroup( pGroup ); 24 tw.writeGroup( pGroup );
25 } 25 }
@@ -28,13 +28,13 @@ int main( int argc, char *argv[] )
28 } 28 }
29 else if( argc == 3 ) 29 else if( argc == 3 )
30 { 30 {
31 Bu::File f( argv[1], "rb"); 31 Bu::File f( argv[1], Bu::File::Read );
32 Bu::TafReader tr( f ); 32 Bu::TafReader tr( f );
33 33
34 Bu::TafGroup *pGroup = tr.readGroup(); 34 Bu::TafGroup *pGroup = tr.readGroup();
35 35
36 { 36 {
37 Bu::File fo( argv[2], "wb"); 37 Bu::File fo( argv[2], Bu::File::Write );
38 Bu::TafWriter tw( fo ); 38 Bu::TafWriter tw( fo );
39 tw.writeGroup( pGroup ); 39 tw.writeGroup( pGroup );
40 } 40 }
diff --git a/src/unit/file.cpp b/src/unit/file.cpp
index a22239d..cc19fac 100644
--- a/src/unit/file.cpp
+++ b/src/unit/file.cpp
@@ -31,7 +31,7 @@ public:
31 // 31 //
32 void writeFull() 32 void writeFull()
33 { 33 {
34 Bu::File sf("testfile1", "wb"); 34 Bu::File sf("testfile1", Bu::File::Write );
35 for( int c = 0; c < 256; c++ ) 35 for( int c = 0; c < 256; c++ )
36 { 36 {
37 unsigned char ch = (unsigned char)c; 37 unsigned char ch = (unsigned char)c;
@@ -49,7 +49,7 @@ public:
49 49
50 void readBlocks() 50 void readBlocks()
51 { 51 {
52 Bu::File sf("testfile1", "rb"); 52 Bu::File sf("testfile1", Bu::File::Read );
53 unsigned char buf[50]; 53 unsigned char buf[50];
54 size_t total = 0; 54 size_t total = 0;
55 for(;;) 55 for(;;)
@@ -74,7 +74,7 @@ public:
74 { 74 {
75 try 75 try
76 { 76 {
77 Bu::File sf("doesn'texist", "rb"); 77 Bu::File sf("doesn'texist", Bu::File::Read );
78 unitFailed("No exception thrown"); 78 unitFailed("No exception thrown");
79 } 79 }
80 catch( Bu::FileException &e ) 80 catch( Bu::FileException &e )
@@ -85,7 +85,7 @@ public:
85 85
86 void readError2() 86 void readError2()
87 { 87 {
88 Bu::File sf("testfile1", "rb"); 88 Bu::File sf("testfile1", Bu::File::Read );
89 char buf[256]; 89 char buf[256];
90 int r = sf.read( buf, 256 ); 90 int r = sf.read( buf, 256 );
91 unitTest( r == 256 ); 91 unitTest( r == 256 );
diff --git a/src/unit/taf.cpp b/src/unit/taf.cpp
index 4e22ab1..0a97eba 100644
--- a/src/unit/taf.cpp
+++ b/src/unit/taf.cpp
@@ -29,14 +29,14 @@ public:
29 { 29 {
30#define FN_TMP ("/tmp/tmpXXXXXXXX") 30#define FN_TMP ("/tmp/tmpXXXXXXXX")
31 Bu::FString sFnTmp(FN_TMP); 31 Bu::FString sFnTmp(FN_TMP);
32 Bu::File fOut = Bu::File::tempFile( sFnTmp, "wb" ); 32 Bu::File fOut = Bu::File::tempFile( sFnTmp, Bu::File::Write );
33 const char *data = 33 const char *data =
34"{test: name=\"Bob\"}" 34"{test: name=\"Bob\"}"
35; 35;
36 fOut.write(data,strlen(data)); 36 fOut.write(data,strlen(data));
37 fOut.close(); 37 fOut.close();
38 38
39 Bu::File fIn(sFnTmp.getStr(), "rb"); 39 Bu::File fIn(sFnTmp.getStr(), Bu::File::Read );
40 Bu::TafReader tr(fIn); 40 Bu::TafReader tr(fIn);
41 41
42 Bu::TafGroup *tn = tr.readGroup(); 42 Bu::TafGroup *tn = tr.readGroup();