Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
"regex": "cpp",
"stack": "cpp",
"__node_handle": "cpp",
"__bit_reference": "cpp"
"__bit_reference": "cpp",
"__config": "cpp"
},
"cmake.sourceDirectory": "${workspaceFolder}/linux/"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public interface IDataSerializer {
int BASE_BINARY_SIZE = 7;

int getMessageSize(StyxMessage message);
void serialize(StyxMessage message, IBufferWriter output) throws StyxException;
void serializeStat(StyxStat stat, IBufferWriter output) throws StyxException;
int getStatSerializedSize(StyxStat stat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import java.util.List;

public class StyxSerializerImpl implements IDataSerializer {
protected int getMessageSize(StyxMessage message) {
@Override
public int getMessageSize(StyxMessage message) {
var size = IDataSerializer.BASE_BINARY_SIZE;
if (message instanceof StyxTMessageFID) {
size += 4;
Expand Down Expand Up @@ -209,7 +210,6 @@ public void serializeStat(StyxStat stat, IBufferWriter output)
output.writeUInt16(stat.type());
output.writeUInt32(stat.dev());
serializeQid(stat.QID(), output);
// stat.QID().writeBinaryTo(output);
output.writeUInt32(stat.mode());
output.writeUInt32(DateToInt(stat.accessTime()));
output.writeUInt32(DateToInt(stat.modificationTime()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,54 @@ void testQidSerialization() throws StyxException {
buffer.get(data);
assertArrayEquals(expected, data);
}

@Test
void testSerializeStat() throws StyxException {
StyxStat stat = new StyxStat(
(short) 1,
2,
new StyxQID(QidType.QTFILE, 0x80, 0x90),
0x01,
new Date(1717171717L * 1000), // fixed date for reproducibility
new Date(1717171717L * 1000),
0x123,
"file",
"user",
"group",
"editor"
);
BufferWriterImpl output = new BufferWriterImpl(8192);
serializer.serializeStat(stat, output);

// Validate buffer size and some expected values
var buffer = output.getBuffer();
assertEquals(serializer.getStatSerializedSize(stat), output.getPosition());

byte[] expected = {
66, 0x00, // size - 2
1, 0x00, // type
0x02, 0x00, 0x00, 0x00, // dev
(byte) QidType.QTFILE,
(byte)0x80, 0x00, 0x00, 0x00, //9: qid.version[4]
(byte)0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //13: qid.path[8] 0x12309E51049E5104L
0x01, 0x00, 0x00, 0x00, // mode
0x05, (byte)0xF6, 89, 102, // atime
0x05, (byte)0xF6, 89, 102, // mtime
0x23, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // length
0x04, 0x00, // name length
'f', 'i', 'l', 'e', // name
0x04, 0x00, // uid length
'u', 's', 'e', 'r', // uid
0x05, 0x00, // gid length
'g', 'r', 'o', 'u', 'p', // gid
0x06, 0x00, // muid length
'e', 'd', 'i', 't', 'o', 'r' // muid
};

buffer.flip();
byte[] data = new byte[buffer.limit()];
// buffer.position(0);
buffer.get(data);
assertArrayEquals(expected, data);
}
}
4 changes: 4 additions & 0 deletions native/V2StyxLib/modules/l5/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ if(BUILD_MODULE_L5)
src/BufferWriterImpl.cpp
src/BufferReaderImpl.cpp
src/StyxMessage.cpp
src/StyxSerializerImpl.cpp
src/StyxQID.cpp
src/StyxStat.cpp
)
target_include_directories(module_l5 PUBLIC
$<INSTALL_INTERFACE:include>
Expand All @@ -14,6 +17,7 @@ if(BUILD_MODULE_L5)
add_executable(module_l5_tests
test/test_BufferWriterImpl.cpp
test/test_BufferReaderImpl.cpp
test/test_StyxSerializerImpl.cpp
)
target_include_directories(module_l5_tests PRIVATE ./include/)
target_link_libraries(module_l5_tests PRIVATE Catch2::Catch2WithMain module_l5)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include "serialization/IBufferReader.h"

class BufferReaderImpl : public IBufferReader {
class BufferReaderImpl : public styxlib::serialization::IBufferReader {
private:
const StyxBuffer buffer;
Styx::Size position;
Expand Down
23 changes: 13 additions & 10 deletions native/V2StyxLib/modules/l5/include/serialization/IBufferReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
#include <cstdint>
#include "data.h"

class IBufferReader {
public:
virtual ~IBufferReader() = default;
virtual uint8_t readUInt8() = 0;
virtual uint16_t readUInt16() = 0;
virtual uint32_t readUInt32() = 0;
virtual uint64_t readUInt64() = 0;
virtual StyxString readUTFString() = 0;
virtual Styx::Size read(uint8_t* data, Styx::Size count) = 0;
};
namespace styxlib::serialization {

class IBufferReader {
public:
virtual ~IBufferReader() = default;
virtual uint8_t readUInt8() = 0;
virtual uint16_t readUInt16() = 0;
virtual uint32_t readUInt32() = 0;
virtual uint64_t readUInt64() = 0;
virtual StyxString readUTFString() = 0;
virtual Styx::Size read(uint8_t* data, Styx::Size count) = 0;
};
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#pragma once
#include "l5/messages/StyxMessage.h"
#include "l5/structs/StyxStat.h"
#include "l5/serialization/IBufferWritter.h"
#include "messages/StyxMessage.h"
#include "structs/StyxStat.h"
#include "serialization/IBufferWriter.h"

class IDataSerializer {
public:
virtual void serialize(const StyxMessage &message, IBufferWriter *output) = 0;
virtual void serializeStat(const StyxStat &stat, IBufferWriter *output) = 0;
virtual void serialize(const styxlib::messages::StyxMessage &message,
IBufferWriter &output) = 0;
virtual void serializeStat(const StyxStat &stat, IBufferWriter &output) = 0;
virtual int getStatSerializedSize(const StyxStat &stat) = 0;
virtual int getQidSize() = 0;
virtual void serializeQid(const StyxQID &qid, IBufferWriter *output) = 0;
virtual void serializeQid(const StyxQID &qid, IBufferWriter &output) = 0;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include "serialization/IDataSerializer.h"

class StyxSerializerImpl : public IDataSerializer {
public:
StyxSerializerImpl() = default;
~StyxSerializerImpl() = default;
void serialize(const styxlib::messages::StyxMessage &message,
IBufferWriter &output) override;
void serializeStat(const StyxStat &stat, IBufferWriter &output) override;
int getStatSerializedSize(const StyxStat &stat) override;
int getQidSize() override;
void serializeQid(const StyxQID &qid, IBufferWriter &output) override;
};
8 changes: 6 additions & 2 deletions native/V2StyxLib/modules/l5/include/structs/StyxQID.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
#include <cstdint>

struct StyxQID {
public:
uint8_t type;
uint32_t version;
uint64_t path;
};

constexpr StyxQID(uint8_t t, uint32_t v, uint64_t p)
: type(t), version(v), path(p) {}

const static StyxQID EMPTY;
};
6 changes: 4 additions & 2 deletions native/V2StyxLib/modules/l5/include/structs/StyxStat.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "l5/structs/StyxQID.h"
#include "l5/data.h"
#include "structs/StyxQID.h"
#include "data.h"

struct StyxStat {
int type; //for kernel use
Expand All @@ -14,4 +14,6 @@ struct StyxStat {
StyxString userName; //owner name
StyxString groupName; //group name
StyxString modificationUser; //name of the user who last modified the file

const static StyxStat EMPTY;
};
4 changes: 4 additions & 0 deletions native/V2StyxLib/modules/l5/src/StyxQID.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "structs/StyxQID.h"
#include "enums/QidType.h"

const StyxQID StyxQID::EMPTY = {styxlib::enums::QTFILE, 0, 0};
44 changes: 44 additions & 0 deletions native/V2StyxLib/modules/l5/src/StyxSerializerImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "serialization/StyxSerializerImpl.h"

void StyxSerializerImpl::serialize(const styxlib::messages::StyxMessage &message,
IBufferWriter &output) {

Copy link

Copilot AI Jun 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The serialize function is currently an empty stub. Consider adding a TODO comment or implementation details to clarify if future work is expected.

Suggested change
// TODO: Implement serialization logic for StyxMessage into IBufferWriter.

Copilot uses AI. Check for mistakes.
}

void StyxSerializerImpl::serializeStat(const StyxStat &stat, IBufferWriter &output)
{
size_t size = getStatSerializedSize(stat);
output.writeUInt16(size - 2); // total size except first 2 bytes with size
output.writeUInt16(stat.type);
output.writeUInt32(stat.dev);
serializeQid(stat.QID, output);
output.writeUInt32(stat.mode);
output.writeUInt32(stat.accessTime);
output.writeUInt32(stat.modificationTime);
output.writeUInt64(stat.length);
output.writeUTFString(stat.name);
output.writeUTFString(stat.userName);
output.writeUTFString(stat.groupName);
output.writeUTFString(stat.modificationUser);
}

int StyxSerializerImpl::getStatSerializedSize(const StyxStat &stat)
{
return 28 + getQidSize()
+ 2 + stat.name.length()
+ 2 + stat.userName.length()
+ 2 + stat.groupName.length()
+ 2 + stat.modificationUser.length();
}

int StyxSerializerImpl::getQidSize()
{
return 13; // Size of StyxQID: 1 byte type, 4 bytes version, 8 bytes path
}

void StyxSerializerImpl::serializeQid(const StyxQID &qid, IBufferWriter &output)
{
output.writeUInt8(qid.type);
output.writeUInt32(qid.version);
output.writeUInt64(qid.path);
}
15 changes: 15 additions & 0 deletions native/V2StyxLib/modules/l5/src/StyxStat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "structs/StyxStat.h"

const StyxStat StyxStat::EMPTY = {
.type = 0,
.dev = 0,
.QID = StyxQID::EMPTY,
.mode = 0,
.accessTime = 0,
.modificationTime = 0,
.length = 0,
.name = "",
.userName = "",
.groupName = "",
.modificationUser = ""
};
82 changes: 82 additions & 0 deletions native/V2StyxLib/modules/l5/test/test_StyxSerializerImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <catch2/catch_test_macros.hpp>
#include "serialization/StyxSerializerImpl.h"
#include "structs/StyxStat.h"
#include "enums/QidType.h"
#include "serialization/BufferWriterImpl.h"

TEST_CASE("testGetStyxStatSize", "[StyxSerializationImpl]") {
StyxSerializerImpl impl;
REQUIRE(28 + 13 + 2 + 2 + 2 + 2 == impl.getStatSerializedSize(StyxStat::EMPTY));
}

TEST_CASE("testGetQidSize", "[StyxSerializationImpl]") {
StyxSerializerImpl impl;
REQUIRE(13 == impl.getQidSize());
}

TEST_CASE("testQidSerialization", "[StyxSerializationImpl]") {
StyxSerializerImpl serializer;
StyxQID qid(
styxlib::enums::QTDIR,
0x6A7470F1,
0x12309E51049E5104L
);
uint8_t expected[] = {
styxlib::enums::QTDIR,
0xF1, 0x70, 0x74, 0x6A, //9: qid.version[4] 0x6A7470F1
0x04, 0x51, 0x9E, 0x04, 0x51, 0x9E, 0x30, 0x12 //13: qid.path[8] 0x12309E51049E5104L
};
BufferWriterImpl outputBuffer(8192);
serializer.serializeQid(qid, outputBuffer);

REQUIRE(outputBuffer.getPosition() == serializer.getQidSize());
StyxBuffer buffer = outputBuffer.getBuffer();
REQUIRE(std::equal(std::begin(expected), std::end(expected), buffer));
}

TEST_CASE("testSerializeStat", "[StyxSerializationImpl]") {
StyxSerializerImpl serializer;

StyxStat stat {
.type = 1,
.dev = 2,
.QID = StyxQID(styxlib::enums::QTFILE, 0x80, 0x90),
.mode = 0x01,
.accessTime = 1717171717, // fixed date for reproducibility
.modificationTime = 1717171717,
.length = 0x123,
.name = "file",
.userName = "user",
.groupName = "group",
.modificationUser = "editor"
};
BufferWriterImpl output(8192);
serializer.serializeStat(stat, output);

// Validate buffer size and some expected values
StyxBuffer buffer = output.getBuffer();

REQUIRE(serializer.getStatSerializedSize(stat) == output.getPosition());

uint8_t expected[] = {
66, 0x00, // size - 2
1, 0x00, // type
0x02, 0x00, 0x00, 0x00, // dev
styxlib::enums::QTFILE,
0x80, 0x00, 0x00, 0x00, //9: qid.version[4]
0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //13: qid.path[8] 0x12309E51049E5104L
0x01, 0x00, 0x00, 0x00, // mode
0x05, 0xF6, 89, 102, // atime
0x05, 0xF6, 89, 102, // mtime
0x23, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // length
0x04, 0x00, // name length
'f', 'i', 'l', 'e', // name
0x04, 0x00, // uid length
'u', 's', 'e', 'r', // uid
0x05, 0x00, // gid length
'g', 'r', 'o', 'u', 'p', // gid
0x06, 0x00, // muid length
'e', 'd', 'i', 't', 'o', 'r' // muid
};
REQUIRE(std::equal(std::begin(expected), std::end(expected), buffer));
}
Loading