Skip to content

Commit 637da9c

Browse files
committed
minor fixes
1 parent ab770ea commit 637da9c

File tree

11 files changed

+58
-45
lines changed

11 files changed

+58
-45
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ cd ios && pod install
4444
## Usage
4545

4646
```ts
47-
import { Mmfile } from 'react-native-mmfile';
47+
import { Mmfile } from '@d4l/react-native-mmfile';
4848

4949
// encode 'Hello World' to ArrayBuffer using UTF-8 encoding
5050
const data = new TextEncoder().encode('Hello World').buffer;
@@ -92,7 +92,6 @@ mkdir build
9292
cd build
9393
cmake ..
9494
cmake --build .
95-
./mmfile_tests
9695
ctest
9796
```
9897

cpp/HybridMmfilePackage.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,21 @@ bool HybridMmfilePackage::fileExists(const std::string& path)
2525
}
2626

2727
double HybridMmfilePackage::getFileSize(const std::string& path) {
28-
size_t size = getFileSizeFromName(getAbsolutePath(path));
29-
if (size == (size_t)-1)
28+
long long size = getFileSizeFromName(getAbsolutePath(path));
29+
if (size == -1)
3030
{
3131
throw std::runtime_error(std::string("Error getting file size for file: ") + path);
3232
}
3333
return size;
3434
}
3535

3636
double HybridMmfilePackage::getEncryptedFileSize(const std::string& path) {
37-
return getFileSize(getAbsolutePath(path)) - sizeof(EncryptedFileHeader);
37+
long long size = getFileSizeFromName(getAbsolutePath(path));
38+
if (size == -1 || (size > 0 && size < (long long)sizeof(EncryptedFileHeader)))
39+
{
40+
throw std::runtime_error(std::string("Error getting encrypted file size for file: ") + path);
41+
}
42+
return size == 0 ? 0 : getFileSize(getAbsolutePath(path)) - sizeof(EncryptedFileHeader);
3843
}
3944

4045
static std::vector<ReadDirItem> _readDir(const std::string& absPath) {

cpp/MMapEncryptedFile.h

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ template <> static inline uint32_t FixEndianness(uint32_t val) { return __builti
2525
template <> static inline uint64_t FixEndianness(uint64_t val) { return __builtin_bswap64(val); }
2626
#endif
2727

28-
static const char* MMapEncryptedFileErrors[] = {
29-
"",
30-
"Encrypted file doesn't have a full header",
31-
"Encrypted file header's magic number doesn't match",
32-
"Unsupported version",
33-
"Encrypted file is shorter than the header declares",
34-
"Wrong encryption key"
35-
};
36-
3728
class MMapEncryptedFile
3829
{
3930
public:
@@ -68,10 +59,10 @@ class MMapEncryptedFile
6859
void open(const std::string& filePath, const uint8_t *key, bool readOnly = false) {
6960
file_.open(filePath, readOnly);
7061
aes_.setKey(key);
71-
int result = readHeader();
72-
if (result != 0)
73-
{
74-
throw std::runtime_error(std::string("Failed to open encrypted file: ") + filePath + ", error: " + MMapEncryptedFileErrors[result]);
62+
try {
63+
readHeader();
64+
} catch (const std::exception& e) {
65+
throw std::runtime_error(std::string("Failed to open encrypted file: ") + filePath + ", error: " + e.what());
7566
}
7667
// resize the file to the correct size (for handling the case when the app crashes before the file is closed)
7768
if (!file_.readOnly()) [[likely]] {
@@ -173,36 +164,44 @@ class MMapEncryptedFile
173164

174165
static const uint16_t MAGIC = 0xDA7A;
175166

176-
int readHeader() {
167+
void readHeader() {
177168
if (file_.size() == 0) {
178169
initHeader();
179-
return 0;
170+
return;
180171
}
181172
if (file_.size() < sizeof(EncryptedFileHeader)) {
182-
return 1;
173+
throw std::runtime_error("Encrypted file doesn't have a full header, size: " + std::to_string(file_.size()));
183174
}
184175

185176
EncryptedFileHeader* header = reinterpret_cast<EncryptedFileHeader*>(file_.data());
186177
if (FixEndianness(header->magic) != MAGIC) {
187-
return 2;
178+
throw std::runtime_error("Encrypted file header's magic number doesn't match, expected: " +
179+
std::to_string(MAGIC) +
180+
", got: " +
181+
std::to_string(FixEndianness(header->magic)));
188182
}
183+
189184
if (FixEndianness(header->version) != 1) {
190-
return 3;
185+
throw std::runtime_error("Unsupported version: " + std::to_string(FixEndianness(header->version)));
191186
}
187+
192188
if (FixEndianness(header->size) + sizeof(EncryptedFileHeader) > file_.size()) {
193-
return 4;
189+
throw std::runtime_error("Encrypted file is shorter than the header declares, declared size: " +
190+
std::to_string(sizeof(EncryptedFileHeader)) + "+" +
191+
std::to_string(FixEndianness(header->size)) +
192+
", actual size: " +
193+
std::to_string(file_.size()));
194194
}
195195

196196
// decrypt data key
197197
uint8_t dataKey[16];
198198
aes_.decryptBlock(header->encryptedDataKey, dataKey);
199199

200200
if (FixEndianness(header->keyHash) != hashIVAndKey(header->iv, dataKey)) {
201-
return 5;
201+
return throw std::runtime_error("Wrong encryption key, hash mismatch");
202202
}
203203

204204
aesData_.setKey(dataKey);
205-
return 0;
206205
}
207206

208207
void initHeader() {

cpp/MMapFile.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
// #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "rtnmmrray", __VA_ARGS__))
1616

1717
// Helper functions
18-
static inline size_t getFileSizeFromName(const std::string& path) {
18+
static inline long long getFileSizeFromName(const std::string& path) {
1919
struct stat fileStat;
2020
if (stat(path.c_str(), &fileStat) != 0) {
2121
perror("fstat");
22-
return -1; // Return 0 to indicate an error
22+
return -1; // Return -1 to indicate an error
2323
}
24-
return fileStat.st_size;
24+
return S_ISDIR(fileStat.st_mode) ? 0 : fileStat.st_size; // Return 0 for directories, otherwise return file size
2525
}
2626

27-
static inline size_t getFileSizeFromFd(int fd) {
27+
static inline long long getFileSizeFromFd(int fd) {
2828
struct stat fileStat;
2929
if (fstat(fd, &fileStat) != 0) {
3030
perror("fstat");
31-
return -1; // Return 0 to indicate an error
31+
return -1; // Return -1 to indicate an error
3232
}
3333
return fileStat.st_size;
3434
}

docs/API.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
To use the library, you need to import it first:
66

77
```ts
8-
import { Mmfile } from 'react-native-mmfile';
8+
import { Mmfile } from '@d4l/react-native-mmfile';
99
```
1010

1111
The `Mmfile` object is a singleton with the following properties and methods:
@@ -52,7 +52,7 @@ interface ReadDirItem {
5252
### Example
5353

5454
```ts
55-
import { Mmfile } from 'react-native-mmfile';
55+
import { Mmfile } from '@d4l/react-native-mmfile';
5656

5757
// open a file
5858
let file = Mmfile.openMmfile('file1');

example/ios/Podfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ PODS:
88
- hermes-engine (0.78.1):
99
- hermes-engine/Pre-built (= 0.78.1)
1010
- hermes-engine/Pre-built (0.78.1)
11-
- NitroMmfile (1.0.1):
11+
- NitroMmfile (1.2.0):
1212
- DoubleConversion
1313
- glog
1414
- hermes-engine
@@ -1606,7 +1606,7 @@ DEPENDENCIES:
16061606
- fmt (from `../node_modules/react-native/third-party-podspecs/fmt.podspec`)
16071607
- glog (from `../node_modules/react-native/third-party-podspecs/glog.podspec`)
16081608
- hermes-engine (from `../node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec`)
1609-
- NitroMmfile (from `../node_modules/react-native-mmfile`)
1609+
- "NitroMmfile (from `../node_modules/@d4l/react-native-mmfile`)"
16101610
- NitroModules (from `../node_modules/react-native-nitro-modules`)
16111611
- RCT-Folly (from `../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`)
16121612
- RCT-Folly/Fabric (from `../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`)
@@ -1693,7 +1693,7 @@ EXTERNAL SOURCES:
16931693
:podspec: "../node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec"
16941694
:tag: hermes-2025-01-13-RNv0.78.0-a942ef374897d85da38e9c8904574f8376555388
16951695
NitroMmfile:
1696-
:path: "../node_modules/react-native-mmfile"
1696+
:path: "../node_modules/@d4l/react-native-mmfile"
16971697
NitroModules:
16981698
:path: "../node_modules/react-native-nitro-modules"
16991699
RCT-Folly:
@@ -1827,7 +1827,7 @@ SPEC CHECKSUMS:
18271827
fmt: a40bb5bd0294ea969aaaba240a927bd33d878cdd
18281828
glog: eb93e2f488219332457c3c4eafd2738ddc7e80b8
18291829
hermes-engine: f493b0a600aed5dc06532141603688a30a5b2f61
1830-
NitroMmfile: 1531b2170e6eff5c67cf6a2565afec63bb861534
1830+
NitroMmfile: 08850a97d9c752757760f8633af093a1b77c51dc
18311831
NitroModules: 5cc92d3b0baf1124ed8136a015924eea03db912e
18321832
RCT-Folly: e78785aa9ba2ed998ea4151e314036f6c49e6d82
18331833
RCTDeprecation: 082fbc90409015eac1366795a0b90f8b5cb28efe

example/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"test": "jest"
1111
},
1212
"dependencies": {
13-
"@d4l/react-native-mmfile": "file:d4l-react-native-mmfile-1.1.1.tgz",
13+
"@d4l/react-native-mmfile": "file:d4l-react-native-mmfile-1.2.0.tgz",
1414
"react": "19.0.0",
1515
"react-native": "0.78.1",
1616
"react-native-fs": "^2.20.0",

example/src/App.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
View,
99
} from 'react-native';
1010
import { MovingRectangle } from './MovingRectangle';
11-
import { Mmfile } from 'react-native-mmfile';
11+
import { Mmfile } from '@d4l/react-native-mmfile';
1212
import { MMKV } from 'react-native-mmkv';
1313

1414
import {prepareReactNativeFS, appendReactNativeFS} from './storages/ReactNativeFS';
@@ -207,11 +207,21 @@ export default function App() {
207207
onPress={async () => {
208208
console.log('readDir...');
209209
try {
210+
let f = Mmfile.openMmfile('test1.txt');
211+
const buffer = new ArrayBuffer(1234);
212+
f.clear();
213+
f.append(buffer);
214+
f.close();
215+
210216
const files = await Mmfile.readDir("");
211-
console.log('files', files);
217+
console.log('readDir()', files);
212218
// await Mmfile.unlink("");
213-
const files2 = await Mmfile.readDir("");
214-
console.log('files', files2);
219+
const files2 = Mmfile.readDirSync("");
220+
console.log('readDirSync()', files2);
221+
222+
// show popup with files
223+
alert('Files: ' + JSON.stringify(files2));
224+
215225
} catch (e) {
216226
console.error('Error reading directory:', e);
217227
}

example/src/storages/Mmfile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {openMmfile} from 'react-native-mmfile';
1+
import {openMmfile} from '@d4l/react-native-mmfile';
22

33
const path = 'k1';
44

example/src/storages/MmfileEncrypted.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {openEncryptedMmfile} from 'react-native-mmfile';
1+
import {openEncryptedMmfile} from '@d4l/react-native-mmfile';
22

33
const path = 'k3';
44
const aeskey = new Uint8Array([

0 commit comments

Comments
 (0)