initial commit
This commit is contained in:
54
include/fs/dir.h
Normal file
54
include/fs/dir.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "type.h"
|
||||
|
||||
namespace fs
|
||||
{
|
||||
void mkDir(const std::string& _p);
|
||||
void mkDirRec(const std::string& _p);
|
||||
void delDir(const std::string& _p);
|
||||
bool dirNotEmpty(const std::string& _dir);
|
||||
bool isDir(const std::string& _path);
|
||||
|
||||
//threadInfo is optional. Only for updating task status.
|
||||
void copyDirToDir(const std::string& src, const std::string& dst, threadInfo *t);
|
||||
void copyDirToDirThreaded(const std::string& src, const std::string& dst);
|
||||
void copyDirToDirCommit(const std::string& src, const std::string& dst, const std::string& dev, threadInfo *t);
|
||||
void copyDirToDirCommitThreaded(const std::string& src, const std::string& dst, const std::string& dev);
|
||||
void getDirProps(const std::string& path, unsigned& dirCount, unsigned& fileCount, uint64_t& totalSize);
|
||||
|
||||
class dirItem
|
||||
{
|
||||
public:
|
||||
dirItem(const std::string& pathTo, const std::string& sItem);
|
||||
std::string getItm() const { return itm; }
|
||||
std::string getName() const;
|
||||
std::string getExt() const;
|
||||
bool isDir() const { return dir; }
|
||||
|
||||
private:
|
||||
std::string itm;
|
||||
bool dir = false;
|
||||
};
|
||||
|
||||
//Just retrieves a listing for _path and stores it in item vector
|
||||
class dirList
|
||||
{
|
||||
public:
|
||||
dirList() = default;
|
||||
dirList(const std::string& _path);
|
||||
void reassign(const std::string& _path);
|
||||
void rescan();
|
||||
|
||||
std::string getItem(int index) const { return item[index].getItm(); }
|
||||
std::string getItemExt(int index) const { return item[index].getExt(); }
|
||||
bool isDir(int index) const { return item[index].isDir(); }
|
||||
unsigned getCount() const { return item.size(); }
|
||||
fs::dirItem *getDirItemAt(unsigned int _ind) { return &item[_ind]; }
|
||||
|
||||
private:
|
||||
std::string path;
|
||||
std::vector<dirItem> item;
|
||||
};
|
||||
}
|
||||
64
include/fs/file.h
Normal file
64
include/fs/file.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
#include <switch.h>
|
||||
#include <dirent.h>
|
||||
#include <minizip/zip.h>
|
||||
#include <minizip/unzip.h>
|
||||
|
||||
#include "fs.h"
|
||||
#include "data.h"
|
||||
|
||||
namespace fs
|
||||
{
|
||||
//Copy args are optional and only used if passed and threaded
|
||||
void copyFile(const std::string& src, const std::string& dst, threadInfo *t);
|
||||
void copyFileThreaded(const std::string& src, const std::string& dst);
|
||||
void copyFileCommit(const std::string& src, const std::string& dst, const std::string& dev, threadInfo *t);
|
||||
void copyFileCommitThreaded(const std::string& src, const std::string& dst, const std::string& dev);
|
||||
void fileDrawFunc(void *a);
|
||||
|
||||
//deletes file
|
||||
void delfile(const std::string& _p);
|
||||
|
||||
//Dumps all titles for current user
|
||||
void dumpAllUserSaves();
|
||||
|
||||
void getShowFileProps(const std::string& _path);
|
||||
void getShowDirProps(const std::string& _path);
|
||||
|
||||
bool fileExists(const std::string& _path);
|
||||
//Returns file size
|
||||
size_t fsize(const std::string& _f);
|
||||
|
||||
class dataFile
|
||||
{
|
||||
public:
|
||||
dataFile(const std::string& _path);
|
||||
~dataFile();
|
||||
void close(){ fclose(f); }
|
||||
|
||||
bool isOpen() const { return opened; }
|
||||
|
||||
bool readNextLine(bool proc);
|
||||
//Finds where variable name ends. When a '(' or '=' is hit. Strips spaces
|
||||
void procLine();
|
||||
std::string getLine() const { return line; }
|
||||
std::string getName() const { return name; }
|
||||
//Reads until ';', ',', or '\n' is hit and returns as string.
|
||||
std::string getNextValueStr();
|
||||
int getNextValueInt();
|
||||
|
||||
private:
|
||||
FILE *f;
|
||||
std::string line, name;
|
||||
size_t lPos = 0;
|
||||
bool opened = false;
|
||||
};
|
||||
|
||||
void logOpen();
|
||||
void logWrite(const char *fmt, ...);
|
||||
void logClose();
|
||||
}
|
||||
100
include/fs/fsfile.h
Normal file
100
include/fs/fsfile.h
Normal file
@@ -0,0 +1,100 @@
|
||||
#pragma once
|
||||
|
||||
#include <switch.h>
|
||||
#include <stdint.h>
|
||||
|
||||
//Bare minimum wrapper around switch fs for JKSV
|
||||
#define FS_SEEK_SET 0
|
||||
#define FS_SEEK_CUR 1
|
||||
#define FS_SEEK_END 2
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
typedef struct
|
||||
{
|
||||
FsFile _f;
|
||||
Result error;
|
||||
s64 offset, fsize;
|
||||
} FSFILE;
|
||||
|
||||
int fsremove(const char *_p);
|
||||
Result fsDelDirRec(const char *_p);
|
||||
|
||||
char *getDeviceFromPath(char *dev, size_t _max, const char *path);
|
||||
char *getFilePath(char *pathOut, size_t _max, const char *path);
|
||||
|
||||
bool fsMkDir(const char *_p);
|
||||
|
||||
/*Opens file. Device is fetched from path. Libnx romfs doesn't work with this.
|
||||
Mode needs to be:
|
||||
FsOpenMode_Read
|
||||
FsOpenMode_Write
|
||||
FsOpenMode_Append
|
||||
*/
|
||||
bool fsfcreate(const char *_p, int64_t crSize);
|
||||
|
||||
FSFILE *fsfopen(const char *_p, uint32_t mode);
|
||||
|
||||
/*Same as above, but FsFileSystem _s is used. Path cannot have device in it*/
|
||||
FSFILE *fsfopenWithSystem(FsFileSystem *_s, const char *_p, uint32_t mode);
|
||||
|
||||
//Closes _f
|
||||
inline void fsfclose(FSFILE *_f)
|
||||
{
|
||||
if(_f != NULL)
|
||||
{
|
||||
fsFileClose(&_f->_f);
|
||||
free(_f);
|
||||
}
|
||||
}
|
||||
|
||||
//Seeks like stdio
|
||||
inline void fsfseek(FSFILE *_f, int offset, int origin)
|
||||
{
|
||||
switch(origin)
|
||||
{
|
||||
case FS_SEEK_SET:
|
||||
_f->offset = offset;
|
||||
break;
|
||||
|
||||
case FS_SEEK_CUR:
|
||||
_f->offset += offset;
|
||||
break;
|
||||
|
||||
case FS_SEEK_END:
|
||||
_f->offset = offset + _f->fsize;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//Returns offset
|
||||
inline size_t fsftell(FSFILE *_f) { return _f->offset; }
|
||||
|
||||
//Writes buf to file. Automatically resizes _f to fit buf
|
||||
size_t fsfwrite(const void *buf, size_t sz, size_t count, FSFILE *_f);
|
||||
|
||||
//Reads to buff
|
||||
inline size_t fsfread(void *buf, size_t sz, size_t count, FSFILE *_f)
|
||||
{
|
||||
uint64_t read = 0;
|
||||
_f->error = fsFileRead(&_f->_f, _f->offset, buf, sz * count, 0, &read);
|
||||
_f->offset += read;
|
||||
return read;
|
||||
}
|
||||
|
||||
//Gets byte from file
|
||||
inline char fsfgetc(FSFILE *_f)
|
||||
{
|
||||
char ret = 0;
|
||||
uint64_t read = 0;
|
||||
_f->error = fsFileRead(&_f->_f, _f->offset++, &ret, 1, 0, &read);
|
||||
return ret;
|
||||
}
|
||||
|
||||
//Writes byte to file
|
||||
inline void fsfputc(int ch, FSFILE *_f) { fsfwrite(&ch, 1, 1, _f); }
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
46
include/fs/fstype.h
Normal file
46
include/fs/fstype.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include "data.h"
|
||||
#include "type.h"
|
||||
#include "ldn.h"
|
||||
|
||||
namespace fs
|
||||
{
|
||||
typedef struct
|
||||
{
|
||||
std::string src, dst, dev;
|
||||
zipFile z;
|
||||
unzFile unz;
|
||||
LDN::LDNCommunicate *comm;
|
||||
bool cleanup = false, trimZipPath = false;
|
||||
uint8_t trimZipPlaces = 0;
|
||||
uint64_t offset = 0;
|
||||
threadStatus *thrdStatus;
|
||||
Mutex arglck = 0;
|
||||
void argLock() { mutexLock(&arglck); }
|
||||
void argUnlock() { mutexUnlock(&arglck); }
|
||||
} copyArgs;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
FsSaveDataType type;
|
||||
uint64_t tid;
|
||||
AccountUid account;
|
||||
uint16_t index;
|
||||
} svCreateArgs;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const data::userTitleInfo *tinfo;
|
||||
uint64_t extSize;
|
||||
} svExtendArgs;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
std::string path;
|
||||
bool origin = false;
|
||||
unsigned dirCount = 0;
|
||||
unsigned fileCount = 0;
|
||||
uint64_t totalSize = 0;
|
||||
} dirCountArgs;
|
||||
}
|
||||
94
include/fs/ldn.h
Normal file
94
include/fs/ldn.h
Normal file
@@ -0,0 +1,94 @@
|
||||
#pragma once
|
||||
|
||||
#include <switch.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <lz4.h>
|
||||
#include <lz4frame.h>
|
||||
#include <lz4frame_static.h>
|
||||
#include <mutex>
|
||||
|
||||
#include "type.h"
|
||||
|
||||
#define SAVE_DATA_SERVER_PORT 25789
|
||||
#define SOCKET_BUFFER_SIZE 4096
|
||||
#define LENGTH_OF_LISTEN_QUEUE 20
|
||||
|
||||
enum LDN_COMMUNICATE_TYPE {
|
||||
UPDATE_FILE,
|
||||
UPDATE_ABORT,
|
||||
UPDATE_OK,
|
||||
UPDATE_DONE,
|
||||
};
|
||||
|
||||
struct LZ4_readFile_s {
|
||||
LZ4F_dctx *dctxPtr;
|
||||
int socket;
|
||||
LZ4_byte *srcBuf;
|
||||
size_t srcBufNext;
|
||||
size_t srcBufSize;
|
||||
size_t srcBufMaxSize;
|
||||
};
|
||||
|
||||
struct LZ4_writeFile_s {
|
||||
LZ4F_cctx *cctxPtr;
|
||||
int socket;
|
||||
LZ4_byte *dstBuf;
|
||||
size_t maxWriteSize;
|
||||
size_t dstBufMaxSize;
|
||||
LZ4F_errorCode_t errCode;
|
||||
};
|
||||
|
||||
namespace LDN {
|
||||
typedef struct {
|
||||
// point to server's socket fd;
|
||||
int serverFD;
|
||||
// point to communicate socket fd, send meta data
|
||||
int commFD;
|
||||
// for client to bind with server, communicate create file socket fd
|
||||
struct sockaddr_in serverAddr;
|
||||
} LDNCommunicate;
|
||||
|
||||
typedef struct {
|
||||
LDNCommunicate *comm;
|
||||
unsigned int filesize = 0, writeLimit = 0;
|
||||
std::string fullPath;
|
||||
std::mutex bufferLock;
|
||||
std::condition_variable cond;
|
||||
std::vector<uint8_t> sharedBuffer;
|
||||
bool bufferIsFull = false;
|
||||
std::string dst, dev;
|
||||
LZ4_writeFile_s* lz4fWrite;
|
||||
} LDNfcopyArgs;
|
||||
|
||||
typedef struct {
|
||||
u32 type;
|
||||
size_t fsz;
|
||||
} commMeta;
|
||||
|
||||
void destroyLDN();
|
||||
|
||||
LDN::LDNCommunicate *createCommunicate(void);
|
||||
|
||||
void destroyCommunicate(LDNCommunicate *comm);
|
||||
|
||||
Result createLDNServer(LDNCommunicate *comm);
|
||||
|
||||
Result createLDNClient(LDNCommunicate *comm);
|
||||
|
||||
int bindClient(int serverFD);
|
||||
|
||||
int bindServer(sockaddr_in *serverAddr);
|
||||
|
||||
bool waitForOK(int socketfd);
|
||||
bool waitForDONE(int socketfd);
|
||||
void sendOK(int socket_fd);
|
||||
void sendDONE(int socket_fd);
|
||||
void sendAbort(int socket_fd);
|
||||
void reciveMeta(commMeta *meta, int socketfd);
|
||||
void sendMeta(commMeta *meta, int socketfd);
|
||||
void copySaveFileToRemote(const std::string &local, threadInfo *t);
|
||||
void copyRemoteSaveFile(threadInfo *t);
|
||||
};
|
||||
18
include/fs/zip.h
Normal file
18
include/fs/zip.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <minizip/zip.h>
|
||||
#include <minizip/unzip.h>
|
||||
|
||||
#include "type.h"
|
||||
|
||||
namespace fs
|
||||
{
|
||||
//threadInfo is optional and only used when threaded versions are used
|
||||
void copyDirToZip(const std::string& src, zipFile dst, bool trimPath, int trimPlaces, threadInfo *t);
|
||||
void copyDirToZipThreaded(const std::string& src, zipFile dst, bool trimPath, int trimPlaces);
|
||||
void copyZipToDir(unzFile src, const std::string& dst, const std::string& dev, threadInfo *t);
|
||||
void copyZipToDirThreaded(unzFile src, const std::string& dst, const std::string& dev);
|
||||
uint64_t getZipTotalSize(unzFile unz);
|
||||
bool zipNotEmpty(unzFile unz);
|
||||
}
|
||||
Reference in New Issue
Block a user