00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #define _LARGEFILE64_SOURCE
00027 #include <sys/types.h>
00028 #include <unistd.h>
00029
00030 #include <vsidevice.h>
00031 #include <vsierror.h>
00032 #include <errno.h>
00033 #include <sys/stat.h>
00034 #include <fcntl.h>
00035 #include <sys/ioctl.h>
00036 #include <sys/syscall.h>
00037
00038 #include <linux/unistd.h>
00039 #include <linux/fs.h>
00040
00041 #include <string.h>
00042
00043
00044 #include <fstream>
00045 #include <iostream>
00046
00047 #include "RioError.h"
00048
00049 using namespace std;
00050
00051
00052
00053
00054
00055 CvsiDevice::CvsiDevice()
00056 {
00057 m_Handle = -1;
00058 m_SectorSize = 0;
00059 m_Size = 0;
00060 }
00061
00062
00063 CvsiDevice::~CvsiDevice()
00064 {
00065 Close();
00066 }
00067
00068
00069 int CvsiDevice::Close()
00070 {
00071 if( m_Handle != -1 )
00072 {
00073 close( m_Handle );
00074 }
00075
00076 return (0);
00077 }
00078
00079
00080 int CvsiDevice::Open( const char* deviceName )
00081 {
00082
00083 m_Handle = open( deviceName, O_RDWR );
00084
00085 if( m_Handle == -1)
00086 {
00087 RioErr << "CvsiDevice: Could not open device "<< deviceName << " with RDRW permissions, trying to open it with RDONLY permission" << endl
00088 << strerror(errno) << endl;
00089 m_Handle = open( deviceName, O_RDONLY);
00090
00091 if( m_Handle == -1)
00092 {
00093 RioErr << "CvsiDevice: Could not open device "<< deviceName << " with RDONLY permission" << endl
00094 << strerror(errno) << endl;
00095 return( VSI_ERROR_DEVICE_NOT_EXIST);
00096 }
00097 }
00098
00099
00100 m_SectorSize = 512;
00101
00102 off_t disknosect;
00103 int rc;
00104 rc = ioctl(m_Handle, BLKGETSIZE, &disknosect);
00105 if(rc != 0)
00106 {
00107 if(errno == ENOTTY)
00108 {
00109 rc = lseek(m_Handle, 0, SEEK_END);
00110 if(rc == -1)
00111 {
00112 Close();
00113 RioErr << "CvsiDevice: seek error." << endl;
00114 return(VSI_ERROR_DEVICE_SEEK);
00115 }
00116 m_Size = rc;
00117 }
00118 else
00119 {
00120 Close();
00121 RioErr << "CvsiDevice: Could not get Device size." << endl;
00122 return(VSI_ERROR_DEVICE_GETSIZE);
00123 }
00124 }
00125 else
00126 {
00127 m_Size = ( (u64) disknosect) * m_SectorSize;
00128 }
00129
00130 return(0);
00131 }
00132
00133
00134
00135 int CvsiDevice::OpenDeviceAgain( const char* deviceName, u64 Size )
00136 {
00137
00138 m_Handle = open( deviceName, O_RDWR );
00139
00140 if( m_Handle == -1)
00141 {
00142 RioErr << "CvsiDevice: Could not open device "<< deviceName << " with RDRW permissions, trying to open it with RDONLY permission" << endl
00143 << strerror(errno) << endl;
00144 m_Handle = open( deviceName, O_RDONLY);
00145
00146 if( m_Handle == -1)
00147 {
00148 RioErr << "CvsiDevice: Could not open device "<< deviceName << " with RDONLY permission" << endl
00149 << strerror(errno) << endl;
00150 return( VSI_ERROR_DEVICE_NOT_EXIST);
00151 }
00152 }
00153
00154 m_SectorSize = 512;
00155 m_Size = Size;
00156
00157 return(0);
00158 }
00159
00160
00161 int CvsiDevice::Write( u64 startingByte, int numberBytes, char * data )
00162 {
00163 int numberBytesWritten = 0;
00164
00165
00166 s64 seekresult;
00167
00168 if( ( startingByte >= m_Size ) ||
00169 ( startingByte >= m_Size + numberBytes ) ||
00170 ( numberBytes < 0 ))
00171 {
00172 return (VSI_ERROR_DEVICE_POSITION);
00173 }
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 seekresult = lseek64( m_Handle, startingByte, SEEK_SET );
00185
00186 if( seekresult < 0 )
00187 {
00188 return(VSI_ERROR_DEVICE_SEEK);
00189 }
00190
00191
00192 numberBytesWritten = write(m_Handle, data, numberBytes);
00193
00194 if(numberBytesWritten != numberBytes)
00195 {
00196 return( VSI_ERROR_DEVICE_WRITE );
00197 }
00198
00199 return(0);
00200 }
00201
00202
00203 int CvsiDevice::Read( u64 startingByte, int numberBytes, char* data )
00204 {
00205 int numberBytesRead = 0;
00206
00207
00208 s64 seekresult;
00209
00210 if( ( startingByte >= m_Size ) ||
00211 ( startingByte >= m_Size + numberBytes ) ||
00212 ( numberBytes < 0 ))
00213 {
00214 return ( VSI_ERROR_DEVICE_POSITION );
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 seekresult = lseek64( m_Handle, startingByte, SEEK_SET );
00227
00228 if( seekresult < 0 )
00229 {
00230 return(VSI_ERROR_DEVICE_SEEK);
00231 }
00232
00233
00234 numberBytesRead = read(m_Handle, data, numberBytes);
00235
00236 if(numberBytesRead != numberBytes)
00237 {
00238 return( VSI_ERROR_DEVICE_READ);
00239 }
00240
00241 return(0);
00242 }
00243
00244
00245 int CvsiDevice::SectorSize()
00246 {
00247 return m_SectorSize;
00248 }
00249
00250
00251 u64 CvsiDevice::Size()
00252 {
00253 return m_Size;
00254 }