24 #include <ibrcommon/Logger.h>
29 const std::string FatImageReader::TAG =
"FatImageReader";
32 : ibrcommon::IOException(create_message(errcode, operation, file)), _errorcode(errcode)
40 std::string FatImageReader::FatImageException::create_message(
int errcode,
const std::string &operation,
const ibrcommon::File &file)
43 ss << operation <<
" failed with " << errcode <<
" for " << file.getPath();
64 if (file.isRoot())
return true;
68 return d.dir_attr & DIR_ATTR_DIRECTORY;
69 }
catch (
const ibrcommon::IOException &e) {
77 if (file.isRoot())
return 0;
81 return d.dir_file_size;
82 }
catch (
const ibrcommon::IOException &e) {
90 if (file.isRoot())
return 0;
96 tm.tm_year = d.crttime.year - 1900;
97 tm.tm_mon = d.crttime.month - 1;
98 tm.tm_mday = d.crttime.day;
99 tm.tm_hour = d.crttime.hour;
100 tm.tm_min = d.crttime.min;
101 tm.tm_sec = d.crttime.sec / 2;
103 }
catch (
const ibrcommon::IOException &e) {
114 }
catch (
const ibrcommon::IOException &e) {
119 void FatImageReader::update(
const FATFile &path, dirent_t &d)
const throw (ibrcommon::IOException)
127 if (path.isRoot())
return;
130 byte* image_path =
const_cast<char *
>(_filename.getPath().c_str());
131 if ((ret = TFFS_mount(image_path, &htffs)) != TFFS_OK) {
132 throw FatImageException(ret,
"TFFS_mount", _filename);
136 FATFile parent = path.getParent();
139 const std::string dir_path = parent.getPath() + (parent.isRoot() ?
"" :
"/");
140 if ((ret = TFFS_opendir(htffs, const_cast<char *>(dir_path.c_str()), &hdir)) != TFFS_OK) {
141 IBRCOMMON_LOGGER_TAG(
TAG, error) <<
"TFFS_opendir failed" << IBRCOMMON_LOGGER_ENDL;
148 ret = TFFS_readdir(hdir, &d);
149 if (ret == TFFS_OK) {
150 if (d.d_name == path.getBasename()) {
161 if ((ret = TFFS_closedir(hdir)) != TFFS_OK) {
162 IBRCOMMON_LOGGER_TAG(
TAG, error) <<
"TFFS_closedir failed" << IBRCOMMON_LOGGER_ENDL;
167 if ((ret = TFFS_umount(htffs)) != TFFS_OK) {
168 throw FatImageException(ret,
"TFFS_umount", _filename);
172 throw ibrcommon::IOException(
"File not found");
190 byte* image_path =
const_cast<char *
>(_filename.getPath().c_str());
191 if ((ret = TFFS_mount(image_path, &htffs)) != TFFS_OK) {
196 const std::string dir_path = directory.getPath() + (directory.isRoot() ?
"" :
"/");
197 if ((ret = TFFS_opendir(htffs, const_cast<char *>(dir_path.c_str()), &hdir)) != TFFS_OK) {
198 IBRCOMMON_LOGGER_TAG(
TAG, error) <<
"TFFS_opendir failed" << IBRCOMMON_LOGGER_ENDL;
204 if ((ret = TFFS_readdir(hdir, &dirent)) == TFFS_OK)
206 files.push_back(directory.get(dirent.d_name));
208 else if (ret == ERR_TFFS_LAST_DIRENTRY) {
217 if ((ret = TFFS_closedir(hdir)) != TFFS_OK) {
218 IBRCOMMON_LOGGER_TAG(
TAG, error) <<
"TFFS_closedir failed" << IBRCOMMON_LOGGER_ENDL;
223 if ((ret = TFFS_umount(htffs)) != TFFS_OK) {
239 byte* path =
const_cast<char *
>(image.getPath().c_str());
240 if ((ret = TFFS_mount(path, &_htffs)) != TFFS_OK)
247 byte* file =
const_cast<char*
>(p.c_str());
248 if ((ret = TFFS_fopen(_htffs, file,
"r", &_hfile)) != TFFS_OK)
271 if (( ret = TFFS_fread(_hfile, buf_size, (
unsigned char*) buff)) < 0)
273 if( ret == ERR_TFFS_FILE_EOF)
return 0;
276 std::stringstream ss;
277 ss <<
"TFFS_fread " << ret;
278 throw ibrcommon::IOException(ss.str());
283 return (ret >= 0) ? (size_t)ret : 0;
291 if (TFFS_fclose(_hfile) != TFFS_OK) {
292 IBRCOMMON_LOGGER_TAG(
"FatFileHandle", error) <<
"TFFS_fclose failed" << IBRCOMMON_LOGGER_ENDL;
296 if (TFFS_umount(_htffs) != TFFS_OK) {
297 IBRCOMMON_LOGGER_TAG(
"FatFileHandle", error) <<
"TFFS_umount failed" << IBRCOMMON_LOGGER_ENDL;
FileHandle(const ibrcommon::File &image, const std::string &path)
bool isDirectory(const FATFile &file) const
FileHandle open(const FATFile &file) const
FatImageReader(const ibrcommon::File &filename)
virtual ~FatImageException()
void list(filelist &files) const
virtual ~FatImageReader()
FatImageException(int errcode, const std::string &operation, const ibrcommon::File &file)
time_t lastaccess(const FATFile &file) const
size_t read(unsigned char *buffer, size_t buf_size)
std::list< FATFile > filelist
bool exists(const FATFile &file) const
size_t size(const FATFile &file) const