Repository navigation
Expand file tree
/
Copy pathnanoHAL_StorageOperation.cpp
More file actions
174 lines (146 loc) · 5.38 KB
/
Copy pathnanoHAL_StorageOperation.cpp
File metadata and controls
174 lines (146 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
#include <nanoHAL.h>
#include <nanoHAL_v2.h>
#include <nanoWeak.h>
#include <nanoCLR_Runtime.h>
#include <nanoCLR_Types.h>
#include <nanoPAL_FileSystem.h>
#include <nanoCLR_FileStream.h>
#include <nanoHAL_StorageOperation.h>
#include <nf_sys_io_filesystem.h>
#include <WireProtocol_MonitorCommands.h>
#if CONFIG_NF_FEATURE_HAS_ACCESSIBLE_STORAGE
uint32_t HAL_StorageOperation(uint8_t operation, uint32_t dataLength, uint32_t offset, uint8_t *data)
{
char *storageName = NULL;
char *rootName = NULL;
char *relativePath = NULL;
uint8_t *fileData = NULL;
uint32_t rootNameLength = 0;
FileSystemVolume *volume = NULL;
StorageOperationErrorCode errorCode = StorageOperationErrorCode::NoError;
// point to the storage name in the data buffer
storageName = (char *)data;
// split the path into root name and relative path
if (FAILED(CLR_RT_FileStream::SplitFilePath(storageName, rootName, rootNameLength, relativePath)))
{
errorCode = StorageOperationErrorCode::PlatformError;
goto done;
}
// find the volume for this path
volume = FileSystemVolumeList::FindVolume(rootName, rootNameLength);
if (volume == NULL)
{
errorCode = StorageOperationErrorCode::PlatformError;
goto done;
}
// point to the file data in the data buffer (after the storage name and null terminator)
fileData = (uint8_t *)(data + hal_strlen_s(storageName) + 1);
if (operation == StorageOperation_Monitor::StorageOperation_Write)
{
void *fileHandle = NULL;
char dirPath[FS_MAX_DIRECTORY_LENGTH];
char *lastSeparator;
int bytesWritten = 0;
HRESULT deleteResult;
// extract parent directory from relative path and create it if needed
snprintf(dirPath, sizeof(dirPath), "%s", relativePath);
lastSeparator = strrchr(dirPath, '\\');
if (lastSeparator == NULL)
{
lastSeparator = strrchr(dirPath, '/');
}
if (lastSeparator != NULL && lastSeparator != dirPath)
{
*lastSeparator = '\0';
if (FAILED(volume->CreateDirectory(dirPath)))
{
errorCode = StorageOperationErrorCode::WriteError;
goto done;
}
}
// Open() below doesn't truncate, so remove an existing file to start the write from an empty one
deleteResult = volume->Delete(relativePath, false);
// a missing file, or a volume without Delete, is the normal case; any other failure would leave
// the previous content in place and the write would produce a file with a stale tail
if (FAILED(deleteResult) && deleteResult != CLR_E_FILE_NOT_FOUND && deleteResult != CLR_E_NOT_SUPPORTED)
{
errorCode = StorageOperationErrorCode::WriteError;
goto done;
}
// open the file (creates it, if it doesn't exist)
if (FAILED(volume->Open(relativePath, FileAccess_Write, fileHandle)))
{
errorCode = StorageOperationErrorCode::WriteError;
goto done;
}
// write the data
if (FAILED(volume->Write(fileHandle, fileData, (int)dataLength, &bytesWritten)))
{
errorCode = StorageOperationErrorCode::WriteError;
volume->Close(fileHandle);
goto done;
}
// close file
volume->Close(fileHandle);
if ((uint32_t)bytesWritten != dataLength)
{
// failed to write expected number of bytes
errorCode = StorageOperationErrorCode::WriteError;
}
}
else if (operation == StorageOperation_Monitor::StorageOperation_Append)
{
void *fileHandle = NULL;
int64_t position;
int bytesWritten = 0;
// open the file
if (FAILED(volume->Open(relativePath, FileAccess_Write, fileHandle)))
{
errorCode = StorageOperationErrorCode::WriteError;
goto done;
}
// seek to the end, to append
if (FAILED(volume->Seek(fileHandle, 0, SEEKORIGIN_END, &position)))
{
errorCode = StorageOperationErrorCode::WriteError;
volume->Close(fileHandle);
goto done;
}
// validate that the seek position matches the offset provided
if (position != offset)
{
errorCode = StorageOperationErrorCode::WriteError;
volume->Close(fileHandle);
goto done;
}
// append the data
if (FAILED(volume->Write(fileHandle, fileData, (int)dataLength, &bytesWritten)))
{
errorCode = StorageOperationErrorCode::WriteError;
volume->Close(fileHandle);
goto done;
}
// close file
volume->Close(fileHandle);
if ((uint32_t)bytesWritten != dataLength)
{
// failed to write expected number of bytes
errorCode = StorageOperationErrorCode::WriteError;
}
}
else if (operation == StorageOperation_Monitor::StorageOperation_Delete)
{
// remove the file
if (FAILED(volume->Delete(relativePath, false)))
{
errorCode = StorageOperationErrorCode::DeleteError;
}
}
done:
return errorCode;
}
#endif // CONFIG_NF_FEATURE_HAS_ACCESSIBLE_STORAGE