embkernel
 All Classes Functions Variables Typedefs Groups Pages
FsFile.cpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
5 
6 #include "FsFile.hpp"
7 #include <stdlib.h>
8 
9 FsFile::FsFile() {
10 }
11 
12 FsFile::~FsFile() {
13  close();
14 }
15 
16 FsDefs::RESULT FsFile::open(const char* path, bool readOnly) {
17  return FsEntry::open(path, readOnly, false, false);
18 }
19 
20 FsDefs::RESULT FsFile::create(const char* path, bool readOnly) {
21  return FsEntry::open(path, readOnly, true, false);
22 }
23 
24 FsDefs::RESULT FsFile::read(void* buffer, size_t len, size_t* transfered) {
25  if (!mSector) {
26  return FsDefs::RES_ENTRY_NOT_OPEN;
27  }
28  *transfered = 0;
29  if (len <= 0) {
30  return FsDefs::RES_SUCCESS;
31  }
32  return Fs::read(buffer, len, transfered, this);
33 }
34 
35 FsDefs::RESULT FsFile::write(const void* buffer, size_t len, size_t* transfered) {
36  if (!mSector) {
37  return FsDefs::RES_ENTRY_NOT_OPEN;
38  }
39  if (mFlags.bits.readOnly) {
40  return FsDefs::RES_DENIED;
41  }
42  if (len <= 0) {
43  return FsDefs::RES_SUCCESS;
44  }
45  mFlags.bits.unflushedSector = true;
46  return Fs::write(buffer, len, transfered, this);
47 }
48 
49 FsDefs::RESULT FsFile::seek(size_t position) {
50  if (!mSector) {
51  return FsDefs::RES_ENTRY_NOT_OPEN;
52  }
53  FsDefs::RESULT result = Fs::flush(this);
54  if (result == FsDefs::RES_SUCCESS) {
55  result = Fs::seek(this, position);
56  }
57  return result;
58 }
59 
60 FsDefs::RESULT FsFile::flush() {
61  FsDefs::RESULT result = Fs::flush(this);
62  return result;
63 }
64 
65 FsDefs::RESULT FsFile::close() {
66  FsDefs::RESULT result = FsDefs::RES_ENTRY_NOT_OPEN;
67  if (mSector) {
68  result = Fs::flush(this);
69  free(mSector);
70  mSector = 0;
71  }
72  return result;
73 }
74 
75 FsDefs::RESULT FsFile::del(const char* path) {
76  return FsEntry::del(path, false);
77 }
78 
79 uint32_t FsFile::getSize() {
80  if (!mSector) {
81  return 0;
82  }
83  return mSize;
84 }
85