diff --git a/include/messageFormat.h b/include/messageFormat.h index f66335fbf..637fabf1d 100644 --- a/include/messageFormat.h +++ b/include/messageFormat.h @@ -106,7 +106,7 @@ enum Opcodes { TRUNCATE -------- command format: - 1-byte opcode|4-byte filename length|filename|off64_t length + 1-byte opcode|off64_t length|4-byte filename length|filename response format: diff --git a/src/ListDirectoryTask.cpp b/src/ListDirectoryTask.cpp index e92162ae8..e79cc2c49 100644 --- a/src/ListDirectoryTask.cpp +++ b/src/ListDirectoryTask.cpp @@ -59,9 +59,9 @@ bool ListDirectoryTask::writeString(uint8_t buf, int *offset, int size, const st void ListDirectoryTask::run() { bool success; - uint8_t buf[1024]; + uint8_t buf[1024] = {0}; - if (getLength() > 1024) { + if (getLength() > 1023) { handleError("ListDirectoryTask read", ENAMETOOLONG); return; } @@ -71,7 +71,7 @@ void ListDirectoryTask::run() cmd_overlay *cmd = (cmd_overlay *) buf; vector listing; - // IOC->listDirectory(path, &listing) + // IOC->listDirectory(cmd->path, &listing) // bogus response listing.push_back("dummy1"); diff --git a/src/ListDirectoryTask.h b/src/ListDirectoryTask.h index 832b89569..19e6e2eb5 100644 --- a/src/ListDirectoryTask.h +++ b/src/ListDirectoryTask.h @@ -18,9 +18,9 @@ class ListDirectoryTask : public PosixTask private: ListDirectoryTask(); - void writeString(uint8_t buf, int *offset, int size, const std::string &str) + void writeString(uint8_t buf, int *offset, int size, const std::string &str); struct cmd_overlay { - uint flen; + uint plen; char path[]; }; }; diff --git a/src/PingTask.cpp b/src/PingTask.cpp index e69de29bb..4be407096 100644 --- a/src/PingTask.cpp +++ b/src/PingTask.cpp @@ -0,0 +1,20 @@ + +#include "PingTask.h" + +namespace storagemanager +{ + +PingTask::PingTask(int sock, uint len) : PosixTask(sock, len) +{ +} + +PingTask::~PingTask() +{ +} + +void PingTask::run() +{ + // not much to check on for Milestone 1 + uint32_t buf[3] = { SM_MSG_START, 4, 0 }; // generic success response + write((uint8_t *) buf, 12); +} diff --git a/src/PingTask.h b/src/PingTask.h index e69de29bb..52aed4c2a 100644 --- a/src/PingTask.h +++ b/src/PingTask.h @@ -0,0 +1,24 @@ + + +#ifndef PINGTASK_H_ +#define PINGTASK_H_ + +#include "PosixTask.h" + +namespace storagemanager +{ + +class PingTask : PosixTask +{ + public: + PingTask(int sock, uint length); + virtual ~PingTask(); + + void run(); + + private: + PingTask(); +}; + +} +#endif diff --git a/src/PosixTask.cpp b/src/PosixTask.cpp index 362fb2b3f..a82dda7d8 100644 --- a/src/PosixTask.cpp +++ b/src/PosixTask.cpp @@ -19,6 +19,7 @@ PosixTask::PosixTask(int _sock, uint _length) : PosixTask::~PosixTask() { + comsumeMsg(); if (!socketReturned) returnSocket(); } @@ -60,11 +61,15 @@ uint PosixTask::getLength() return totalLength; } +// todo, need this to return an int instead of a bool b/c it modifies the length of the read bool PosixTask::read(uint8_t *buf, uint length) { if (length > remainingLengthForCaller) length = remainingLengthForCaller; + if (length == 0) + return false; + uint count = 0; int err; @@ -156,4 +161,24 @@ bool PosixTask::write(const vector &buf) return write(&buf[0], buf.size()); } +void PosixTask::consumeMsg() +{ + uint8_t buf[1024]; + int err; + + bufferLen = 0; + bufferPos = 0; + remainingLengthForCaller = 0; + + while (remainingLengthInStream > 0) + { + err = ::read(sock, buf, min(remainingLengthInStream, 1024)); + if (err <= 0) { + remainingLengthInStream = 0; + break; + } + remainingLengthInStream -= err; + } +} + } diff --git a/src/PosixTask.h b/src/PosixTask.h index c3d9cff66..0f0b61e65 100644 --- a/src/PosixTask.h +++ b/src/PosixTask.h @@ -18,6 +18,7 @@ class PosixTask int read(uint8_t *buf, uint length); bool write(const std::vector &buf); bool write(void *buf, uint length); + void consumeMsg(); // drains the remaining portion of the message uint getLength(); // returns the total length of the msg uint getRemainingLength(); // returns the remaining length from the caller's perspective void handleError(const char *name, int errCode); diff --git a/src/StatTask.cpp b/src/StatTask.cpp index e69de29bb..552f73d39 100644 --- a/src/StatTask.cpp +++ b/src/StatTask.cpp @@ -0,0 +1,41 @@ + +#include "StatTask.h" + +using namespace std; + +namespace storagemanager +{ + +StatTask::StatTask(int sock, uint len) : PosixTask(sock, len) +{ +} + +StatTask::~StatTask() +{ +} + +void StatTask::run() +{ + bool success; + uint8_t buf[1024] = {0}; + + if (getLength() > 1023) { + handleError("StatTask read", ENAMETOOLONG); + return; + } + + success = read(buf, getLength()); + check_error("StatTask read"); + cmd_overlay *cmd = (cmd_overlay *) buf; + + struct stat _stat; + // IOC->stat(cmd->path, &_stat); + + // bogus response + memset(&_stat, 0, sizeof(_stat)); + uint32_t *buf32 = (uint32_t *) buf; + buf32[0] = SM_MSG_START; + buf32[1] = sizeof(_stat); + memcpy(&buf[SM_HEADER_LEN], &_stat, sizeof(_stat)); + write(buf, SM_HEADER_LEN + sizeof(_stat)); +} diff --git a/src/StatTask.h b/src/StatTask.h index e69de29bb..7e71edad6 100644 --- a/src/StatTask.h +++ b/src/StatTask.h @@ -0,0 +1,29 @@ + +#ifndef STATTASK_H_ +#define STATTASK_H_ + +#include "PosixTask.h" + +namespace storagemanager +{ + +class StatTask : public PosixTask +{ + public: + StatTask(int sock, uint length); + virtual ~StatTask(); + + void run(); + + private: + StatTask(); + + struct cmd_overlay { + uint plen; + char path[]; + }; +}; + + +} +#endif diff --git a/src/TruncateTask.cpp b/src/TruncateTask.cpp index e69de29bb..6edfa0658 100644 --- a/src/TruncateTask.cpp +++ b/src/TruncateTask.cpp @@ -0,0 +1,39 @@ + +#include "TruncateTask.h" + +using namespace std; + +namespace storagemanager +{ + +TruncateTask::TruncateTask(int sock, uint len) : PosixTask(sock, len) +{ +} + +TruncateTask::~TruncateTask() +{ +} + +void TruncateTask::run() +{ + bool success; + uint8_t buf[1024] = {0}; + + if (getLength() > 1023) { + handleError("TruncateTask read", ENAMETOOLONG); + return; + } + + success = read(buf, getLength()); + check_error("TruncateTask read"); + cmd_overlay *cmd = (cmd_overlay *) buf; + + // IOC->truncate(cmd->filename, cmd->newSize); + + // generic success msg + uint32_t *buf32 = buf; + buf32[0] = SM_MSG_START; + buf32[1] = 4; + buf32[2] = 0; + write(buf, 12); +} diff --git a/src/TruncateTask.h b/src/TruncateTask.h index e69de29bb..7a56df7d0 100644 --- a/src/TruncateTask.h +++ b/src/TruncateTask.h @@ -0,0 +1,30 @@ + +#ifndef TRUNCATETASK_H_ +#define TRUNCATETASK_H_ + +#include "PosixTask.h" + +namespace storagemanager +{ + +class TruncateTask : public PosixTask +{ + public: + TruncateTask(int sock, uint length); + virtual ~TruncateTask(); + + void run(); + + private: + TruncateTask(); + + struct cmd_overlay { + off64_t newSize; + uint flen; + char filename[]; + }; +}; + + +} +#endif diff --git a/src/UnlinkTask.cpp b/src/UnlinkTask.cpp index e69de29bb..c88f6dad9 100644 --- a/src/UnlinkTask.cpp +++ b/src/UnlinkTask.cpp @@ -0,0 +1,39 @@ + +#include "UnlinkTask.h" + +using namespace std; + +namespace storagemanager +{ + +UnlinkTask::UnlinkTask(int sock, uint len) : PosixTask(sock, len) +{ +} + +UnlinkTask::~UnlinkTask() +{ +} + +void UnlinkTask::run() +{ + bool success; + uint8_t buf[1024] = {0}; + + if (getLength() > 1023) { + handleError("UnlinkTask read", ENAMETOOLONG); + return; + } + + success = read(buf, getLength()); + check_error("UnlinkTask read"); + cmd_overlay *cmd = (cmd_overlay *) buf; + + // IOC->unlink(cmd->filename); + + // generic success msg + uint32_t *buf32 = buf; + buf32[0] = SM_MSG_START; + buf32[1] = 4; + buf32[2] = 0; + write(buf, 12); +} diff --git a/src/UnlinkTask.h b/src/UnlinkTask.h index e69de29bb..61e515a9a 100644 --- a/src/UnlinkTask.h +++ b/src/UnlinkTask.h @@ -0,0 +1,29 @@ + +#ifndef UNLINKTASK_H_ +#define UNLINKTASK_H_ + +#include "PosixTask.h" + +namespace storagemanager +{ + +class UnlinkTask : public PosixTask +{ + public: + UnlinkTask(int sock, uint length); + virtual ~UnlinkTask(); + + void run(); + + private: + UnlinkTask(); + + struct cmd_overlay { + uint flen; + char filename[]; + }; +}; + + +} +#endif