6#include "dawn/io/fileio.hxx"
18static const char g_prefix_data[] =
"/data/";
19static const char g_prefix_tmp[] =
"/tmp/";
28 for (i = 0; i < desc.
getSize(); i++)
40 DAWNERR(
"unsupported fileio cfg 0x%08" PRIx32
"\n", item->
cfgid.
v);
50 if (nbytes >= PATH_MAX)
52 DAWNERR(
"file path too long: %zu bytes\n", nbytes);
56 std::memcpy(path, &item->
data, nbytes);
59 if (std::strstr(path,
"..") !=
nullptr)
61 DAWNERR(
"path traversal not allowed: %s\n", path);
65 if (std::strncmp(path, g_prefix_data,
sizeof(g_prefix_data) - 1) != 0 &&
66 std::strncmp(path, g_prefix_tmp,
sizeof(g_prefix_tmp) - 1) != 0)
68 DAWNERR(
"path not in allowed directory: %s\n", path);
78 const uint8_t *tmp =
reinterpret_cast<const uint8_t *
>(&item->
data);
84 DAWNERR(
"invalid file permission: %u\n", (
unsigned)perm);
94 DAWNERR(
"unsupported fileio cfg 0x%08" PRIx32
"\n", item->
cfgid.
v);
114 ret = configureDesc(
getDesc());
120 writeOnceLocked =
false;
130 flags = O_WRONLY | O_CREAT;
136 flags = O_RDWR | O_CREAT;
139 fd = open(path, flags, 0666);
142 DAWNERR(
"failed to open %s: %d\n", path, errno);
151 ret = fstat(fd, &st);
154 DAWNERR(
"fstat failed for %s: %d\n", path, errno);
160 fsize = (size_t)st.st_size;
164 ret = fstat(fd, &st);
167 DAWNERR(
"fstat failed for %s: %d\n", path, errno);
173 writeOnceLocked = st.st_size > 0;
204 if (lseek(fd, 0, SEEK_SET) < 0)
206 DAWNERR(
"lseek failed: %d\n", errno);
213 DAWNERR(
"read failed: %d\n", errno);
227 DAWNERR(
"write-once file already written: %s\n", path);
231 ret = ftruncate(fd, 0);
234 DAWNERR(
"ftruncate failed: %d\n", errno);
238 if (lseek(fd, 0, SEEK_SET) < 0)
240 DAWNERR(
"lseek failed: %d\n", errno);
247 DAWNERR(
"write failed: %d\n", errno);
251 fsize = (size_t)nwritten;
255 writeOnceLocked =
true;
275 if (lseek(fd, (off_t)offset, SEEK_SET) < 0)
277 DAWNERR(
"lseek failed: %d\n", errno);
284 DAWNERR(
"read failed: %d\n", errno);
299 DAWNERR(
"write-once file already written: %s\n", path);
305 ret = ftruncate(fd, 0);
308 DAWNERR(
"ftruncate failed: %d\n", errno);
313 if (lseek(fd, (off_t)offset, SEEK_SET) < 0)
315 DAWNERR(
"lseek failed: %d\n", errno);
322 DAWNERR(
"write failed: %d\n", errno);
326 endpos = offset + (size_t)nwritten;
327 if (offset == 0 || endpos > fsize)
334 writeOnceLocked =
true;
340#ifdef CONFIG_DAWN_IO_NOTIFY
Descriptor wrapper for individual object configuration.
size_t getSize() const
Get number of configuration items for this object.
SObjectCfg::SObjectCfgItem * objectCfgItemAtOffset(size_t offset) const
Get configuration item at specified offset.
virtual int getFd() const
Get file descriptor for notifications.
size_t cfgCmnOffset(const SObjectCfg::SObjectCfgItem *cfg)
Get offset of configuration item in descriptor.
@ IO_CLASS_ANY
Any I/O class.
@ IO_CLASS_FILE
File system I/O.
int setDataImpl(IODataCmn &data)
Set data implementation (override in derived classes).
@ IO_FILE_PERM_RW
Read-write.
@ IO_FILE_PERM_WRITE
Write-only.
@ IO_FILE_PERM_WRITE_ONCE
Write-once.
@ IO_FILE_PERM_READ
Read-only.
int getDataImpl(IODataCmn &data, size_t len)
Get data implementation (override in derived classes).
size_t getDataDim() const
Get data vector dimension.
int getDataAtImpl(IODataCmn &data, size_t len, size_t offset)
Get data at byte offset (override in seekable IOs).
int deinit()
De-initialize object.
@ IO_FILE_CFG_PATH
File path (null-terminated string, DTYPE_CHAR).
@ IO_FILE_CFG_PERM
Permission mode (EIOFilePerm, DTYPE_UINT8).
size_t getDataSize() const
Get data size in bytes.
bool isWrite() const
Check if IO supports write operations.
bool isRead() const
Check if IO supports read operations.
int setDataAtImpl(IODataCmn &data, size_t offset)
Set data at byte offset (override in seekable IOs).
int configure()
Configure object from descriptor data.
CDescObject & getDesc()
Get descriptor object for this object.
Out-of-tree user-extension hooks for Dawn.
Base interface for I/O data buffers (static and dynamic).
virtual void * getDataPtr(size_t batch=0)=0
Get pointer to data only (skips timestamp if present).
virtual size_t getDataSize()=0
Get data size in bytes.
Single configuration item within object.
ObjectCfgData_t data[]
Configuration data array (flexible, size from cfgid.s.size).
UObjectCfgId cfgid
Configuration ID header (type, class, id, size, rw, dtype).
ObjectCfgId v
Raw 32-bit ConfigID value (for storage, comparison).
uint32_t cls
Object class (bits 21-29, max 511).
uint32_t id
Configuration identifier (bits 0-4, max 31).
uint32_t size
Configuration data size in 32-bit words (bits 5-14, max 1023).
struct dawn::SObjectCfg::UObjectCfgId::@10 s
Bit-field structure for named member access.