2017-02-26 16:00:41 +00:00
|
|
|
#include <string.h> // strcpy
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2017-12-29 19:08:49 +00:00
|
|
|
#include <errno.h>
|
2018-02-07 15:20:26 +00:00
|
|
|
#include <dirent.h>
|
2020-07-08 01:20:22 +00:00
|
|
|
#include <stdlib.h>
|
2017-02-26 16:00:41 +00:00
|
|
|
|
2018-02-07 15:20:26 +00:00
|
|
|
#include "nix-filemanager.h"
|
2017-02-26 16:00:41 +00:00
|
|
|
|
2018-02-07 15:20:26 +00:00
|
|
|
#define ROOTDIR "./disks/"
|
|
|
|
|
|
|
|
NixFileManager::NixFileManager()
|
2017-02-26 16:00:41 +00:00
|
|
|
{
|
|
|
|
numCached = 0;
|
|
|
|
}
|
|
|
|
|
2018-02-07 15:20:26 +00:00
|
|
|
NixFileManager::~NixFileManager()
|
2017-02-26 16:00:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-07 15:20:26 +00:00
|
|
|
int8_t NixFileManager::openFile(const char *name)
|
2017-02-26 16:00:41 +00:00
|
|
|
{
|
|
|
|
// See if there's a hole to re-use...
|
|
|
|
for (int i=0; i<numCached; i++) {
|
|
|
|
if (cachedNames[i][0] == '\0') {
|
|
|
|
strncpy(cachedNames[i], name, MAXPATH-1);
|
|
|
|
cachedNames[i][MAXPATH-1] = '\0'; // safety: ensure string terminator
|
|
|
|
fileSeekPositions[i] = 0;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for too many open files
|
|
|
|
if (numCached >= MAXFILES)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
|
|
// No, so we'll add it to the end
|
|
|
|
strncpy(cachedNames[numCached], name, MAXPATH-1);
|
|
|
|
cachedNames[numCached][MAXPATH-1] = '\0'; // safety: ensure string terminator
|
|
|
|
fileSeekPositions[numCached] = 0;
|
|
|
|
|
|
|
|
numCached++;
|
|
|
|
return numCached-1;
|
|
|
|
}
|
|
|
|
|
2018-02-07 15:20:26 +00:00
|
|
|
void NixFileManager::closeFile(int8_t fd)
|
2017-02-26 16:00:41 +00:00
|
|
|
{
|
|
|
|
// invalid fd provided?
|
|
|
|
if (fd < 0 || fd >= numCached)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// clear the name
|
|
|
|
cachedNames[fd][0] = '\0';
|
|
|
|
}
|
|
|
|
|
2018-02-07 15:20:26 +00:00
|
|
|
const char *NixFileManager::fileName(int8_t fd)
|
2017-02-26 16:00:41 +00:00
|
|
|
{
|
|
|
|
if (fd < 0 || fd >= numCached)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return cachedNames[fd];
|
|
|
|
}
|
|
|
|
|
2020-07-11 11:39:56 +00:00
|
|
|
// FIXME make these member vars instead of globals
|
|
|
|
static DIR *dirp = NULL;
|
|
|
|
|
|
|
|
void NixFileManager::closeDir()
|
|
|
|
{
|
|
|
|
if (dirp) {
|
|
|
|
closedir(dirp);
|
|
|
|
dirp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int16_t NixFileManager::readDir(const char *where, const char *suffix, char *outputFN, int16_t startIdx, uint16_t maxlen)
|
2017-02-26 16:00:41 +00:00
|
|
|
{
|
2020-07-11 11:39:56 +00:00
|
|
|
if (startIdx == 0 || !dirp) {
|
|
|
|
// This is an openDir() -- so reset state, open the directory, etc.
|
|
|
|
closeDir();
|
|
|
|
dirp = opendir(where);
|
|
|
|
if (!dirp)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startIdx == 0) {
|
|
|
|
if (strcmp(where, ROOTDIR)) {
|
|
|
|
// As long as we're not at the root, we start with "../"
|
2018-02-07 15:20:26 +00:00
|
|
|
strcpy(outputFN, "../");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
outputFN[0] = '\0';
|
|
|
|
|
2020-07-11 11:39:56 +00:00
|
|
|
struct dirent *dp;
|
2018-02-07 15:20:26 +00:00
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
|
|
|
if (dp->d_name[0] == '.') {
|
|
|
|
// Skip any dot files (and dot directories)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: skip any non-files and non-directories
|
|
|
|
|
|
|
|
if (suffix && !(dp->d_type & DT_DIR) && strlen(dp->d_name) >= 3) {
|
|
|
|
// It's a valid file. If it doesn't match any of our suffixes,
|
|
|
|
// then skip it.
|
|
|
|
char pat[40];
|
|
|
|
strncpy(pat, suffix, sizeof(pat)); // make a working copy of the suffixes
|
|
|
|
|
|
|
|
char *fsuff = &dp->d_name[strlen(dp->d_name)-3];
|
|
|
|
|
|
|
|
if (strstr(pat, ",")) {
|
|
|
|
// We have a list of suffixes. Check each of them.
|
|
|
|
|
|
|
|
bool matchesAny = false;
|
|
|
|
char *tok = strtok((char *)pat, ",");
|
|
|
|
while (tok) {
|
|
|
|
// FIXME: assumes 3 character suffixes
|
|
|
|
if (!strncasecmp(fsuff, tok, 3)) {
|
|
|
|
matchesAny = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tok = strtok(NULL, ",");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!matchesAny) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// One single suffix - check it
|
|
|
|
if (strcasecmp(fsuff, suffix)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If we get here, it's something we want to show.
|
2020-07-11 11:39:56 +00:00
|
|
|
strncpy(outputFN, dp->d_name, maxlen-1);
|
|
|
|
|
|
|
|
if (dp->d_type & DT_DIR) {
|
|
|
|
// suffix
|
|
|
|
strcat(outputFN, "/");
|
2018-02-07 15:20:26 +00:00
|
|
|
}
|
2020-07-11 11:39:56 +00:00
|
|
|
|
|
|
|
return startIdx;
|
2018-02-07 15:20:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exited the loop - all done.
|
2020-07-11 11:39:56 +00:00
|
|
|
// didn't find any more
|
|
|
|
closeDir();
|
|
|
|
return -1;
|
2017-02-26 16:00:41 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 15:20:26 +00:00
|
|
|
void NixFileManager::getRootPath(char *toWhere, int8_t maxLen)
|
|
|
|
{
|
|
|
|
strcpy(toWhere, ROOTDIR);
|
|
|
|
// strncpy(toWhere, ROOTDIR, maxLen);
|
|
|
|
}
|
2019-02-20 16:51:55 +00:00
|
|
|
|
2019-02-21 03:06:55 +00:00
|
|
|
bool NixFileManager::setSeekPosition(int8_t fd, uint32_t pos)
|
|
|
|
{
|
|
|
|
// This could be a whole lot simpler.
|
|
|
|
bool ret = false;
|
|
|
|
FILE *f = fopen(cachedNames[fd], "r");
|
|
|
|
if (f) {
|
|
|
|
fseeko(f, 0, SEEK_END);
|
|
|
|
fileSeekPositions[fd] = ftello(f);
|
|
|
|
|
|
|
|
if (pos < ftello(f)) {
|
|
|
|
fileSeekPositions[fd] = pos;
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-02-20 16:51:55 +00:00
|
|
|
void NixFileManager::seekToEnd(int8_t fd)
|
|
|
|
{
|
|
|
|
// This could just be a stat call...
|
|
|
|
FILE *f = fopen(cachedNames[fd], "r");
|
|
|
|
if (f) {
|
|
|
|
fseeko(f, 0, SEEK_END);
|
|
|
|
fileSeekPositions[fd] = ftello(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
2020-06-28 13:21:27 +00:00
|
|
|
|
|
|
|
int NixFileManager::write(int8_t fd, const void *buf, int nbyte)
|
|
|
|
{
|
2021-01-10 02:32:40 +00:00
|
|
|
if (fd < 0 || fd >= numCached) {
|
|
|
|
printf("invalid fd (out of range)\n");
|
2020-06-28 13:26:02 +00:00
|
|
|
return -1;
|
2021-01-10 02:32:40 +00:00
|
|
|
}
|
2020-06-28 13:26:02 +00:00
|
|
|
|
2021-01-10 02:32:40 +00:00
|
|
|
if (cachedNames[fd][0] == 0) {
|
|
|
|
printf("invalid fd (not opened)\n");
|
2020-06-28 13:26:02 +00:00
|
|
|
return -1;
|
2021-01-10 02:32:40 +00:00
|
|
|
}
|
2020-06-28 13:26:02 +00:00
|
|
|
|
|
|
|
uint32_t pos = fileSeekPositions[fd];
|
|
|
|
|
|
|
|
// open, seek, write, close.
|
2021-01-10 02:32:40 +00:00
|
|
|
ssize_t rv = 0;
|
2020-07-08 01:20:22 +00:00
|
|
|
int ffd = ::open(cachedNames[fd], O_WRONLY|O_CREAT, 0644);
|
2021-01-10 02:32:40 +00:00
|
|
|
if (ffd == -1) {
|
2020-06-28 13:26:02 +00:00
|
|
|
printf("Failed to open '%s' for writing: %d\n",
|
|
|
|
cachedNames[fd], errno);
|
2021-01-10 02:32:40 +00:00
|
|
|
close(ffd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (::lseek(ffd, pos, SEEK_SET) == -1) {
|
|
|
|
printf("failed to open and seek\n");
|
|
|
|
close(ffd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = ::write(ffd, buf, nbyte);
|
|
|
|
if (rv != nbyte) {
|
|
|
|
printf("error writing: %d; wanted to write %d got %ld\n", errno, nbyte, rv);
|
2020-06-28 13:21:27 +00:00
|
|
|
}
|
2021-01-10 02:32:40 +00:00
|
|
|
|
|
|
|
close(ffd);
|
|
|
|
|
2020-06-28 13:26:02 +00:00
|
|
|
fileSeekPositions[fd]+=nbyte;
|
2021-01-10 02:32:40 +00:00
|
|
|
return (int)rv;
|
2020-06-28 13:21:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int NixFileManager::read(int8_t fd, void *buf, int nbyte)
|
|
|
|
{
|
2020-07-08 01:20:22 +00:00
|
|
|
if (fd < 0 || fd >= numCached) {
|
|
|
|
printf("no fd when reading? fd=%d\n", fd);
|
2020-06-28 13:26:02 +00:00
|
|
|
return -1; // FIXME: error handling?
|
2020-07-08 01:20:22 +00:00
|
|
|
}
|
2020-06-28 13:26:02 +00:00
|
|
|
|
2020-07-08 01:20:22 +00:00
|
|
|
if (cachedNames[fd][0] == 0) {
|
2020-06-28 13:26:02 +00:00
|
|
|
return -1; // FIXME: error handling?
|
2020-07-08 01:20:22 +00:00
|
|
|
}
|
2020-06-28 13:26:02 +00:00
|
|
|
|
2020-07-08 01:20:22 +00:00
|
|
|
off_t pos = fileSeekPositions[fd];
|
2020-06-28 13:26:02 +00:00
|
|
|
|
|
|
|
// open, seek, read, close.
|
2020-07-08 01:20:22 +00:00
|
|
|
int ffd = ::open(cachedNames[fd], O_RDONLY);
|
|
|
|
if (ffd == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (::lseek(ffd, pos, SEEK_SET) != pos) {
|
2020-06-28 13:26:02 +00:00
|
|
|
close(ffd);
|
2020-07-08 01:20:22 +00:00
|
|
|
return -1;
|
2020-06-28 13:21:27 +00:00
|
|
|
}
|
2020-06-28 13:26:02 +00:00
|
|
|
|
2020-07-08 01:20:22 +00:00
|
|
|
ssize_t rv = ::read(ffd, buf, nbyte);
|
|
|
|
if (rv != nbyte) {
|
|
|
|
close(ffd);
|
|
|
|
return -1;
|
2020-06-28 13:26:02 +00:00
|
|
|
}
|
2020-07-08 01:20:22 +00:00
|
|
|
|
|
|
|
fileSeekPositions[fd]+=rv;
|
|
|
|
close(ffd);
|
2020-06-28 13:26:02 +00:00
|
|
|
|
2020-07-08 01:20:22 +00:00
|
|
|
return rv;
|
2020-06-28 13:21:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int NixFileManager::lseek(int8_t fd, int offset, int whence)
|
|
|
|
{
|
|
|
|
if (whence == SEEK_CUR && offset == 0) {
|
|
|
|
return fileSeekPositions[fd];
|
|
|
|
}
|
|
|
|
if (whence == SEEK_SET) {
|
|
|
|
if (!setSeekPosition(fd, offset))
|
|
|
|
return -1;
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
// Other cases not supported yet
|
|
|
|
return -1;
|
|
|
|
};
|