embkernel
 All Classes Functions Variables Typedefs Groups Pages
FsEntry.cpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
5 
6 #include "Fs.hpp"
7 #include "FsEntry.hpp"
8 #include <string.h>
9 #include <stdlib.h>
10 
11 FsEntry::FsEntry() {
12  mSector = 0;
13  mFlags.value = 0;
14 }
15 
16 FsEntry::~FsEntry() {
17 
18 }
19 
20 FsDefs::RESULT FsEntry::open(const char* path, bool readOnly, bool createAlways, bool isDirectory) {
21  FsDefs::RESULT result = internalPrepare(path, isDirectory);
22  if (result != FsDefs::RES_SUCCESS) {
23  return result;
24  }
25 
26  mFlags.bits.readOnly = readOnly;
27  mFlags.bits.directory = isDirectory;
28  mFlags.bits.createAlways = createAlways;
29  result = Fs::open(path, this);
30  if (result != FsDefs::RES_SUCCESS) {
31  free(mSector);
32  mSector = 0;
33  }
34  else {
35  mFlags.bits.sectorCacheInvalid = true;
36  }
37  return result;
38 }
39 
40 FsDefs::RESULT FsEntry::del(const char* path, bool isDirectory) {
41  FsDefs::RESULT result = internalPrepare(path, isDirectory);
42  if (result != FsDefs::RES_SUCCESS) {
43  return result;
44  }
45 
46  mFlags.bits.directory = isDirectory;
47  result = Fs::open(path, this);
48  if (result != FsDefs::RES_SUCCESS) {
49  free(mSector);
50  mSector = 0;
51  return result;
52  }
53  result = Fs::del(this);
54  free(mSector);
55  mSector = 0;
56  return result;
57 }
58 
59 FsDefs::RESULT FsEntry::internalPrepare(const char* path, bool isDirectory) {
60  mFlags.value = 0;
61  if (mSector) {
62  close();
63  }
64  if (path[strlen(path) - 1] == '/') {
65  if (!isDirectory) {
66  return FsDefs::RES_INVALID_PATH;
67  }
68  }
69  else {
70  if (isDirectory) {
71  return FsDefs::RES_INVALID_PATH;
72  }
73  }
74  mSector = (FsDefs::SECTOR*) malloc(sizeof(FsDefs::SECTOR));
75  if (!mSector) {
76  return FsDefs::RES_OUT_OF_MEMORY;
77  }
78  return FsDefs::RES_SUCCESS;
79 }