| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- #include "filefunctions.h"
- #include "debugging.h"
- #include "exif.h"
- #include <string>
- #include <algorithm>
- #include <cstdio>
- #include <iostream>
- #include <sstream>
- #include <iomanip>
- #include <locale>
- using namespace filefunctions;
- std::string filefunctions::filefunctions_wd;
- COMMAND_FLAGS filefunctions::operator|(filefunctions::COMMAND_FLAGS a, filefunctions::COMMAND_FLAGS b)
- {
- return COMMAND_FLAGS(int(a)|int(b));
- }
- #ifdef __linux__
- // Linux methods
- Ej implementerat
- #elif _WIN32
- // Windows methods
- #include <windows.h>
- time_t filetime_to_unixtime(FILETIME const &t)
- {
- //FILETIME t;
- //FileTimeToLocalFileTime(&utc_t, &t);
- LARGE_INTEGER big_time;
- time_t unix_time;
- big_time.HighPart = t.dwHighDateTime;
- big_time.LowPart = t.dwLowDateTime;
- unix_time = big_time.QuadPart/10000000-11644473600;
- unix_time += 3600; // UTC+1
- DEBUG("__FILETIME__ " << big_time.QuadPart);
- return unix_time;
- }
- tm* get_exif_time(std::string path, long file_size)
- {
- FILE *filen = fopen(path.c_str(), "rb");
- if (filen == nullptr)
- {
- DEBUG("__GET_EXIF_TIME__ Could not find file: " << path);
- return nullptr;
- }
- DEBUG("__GET_EXIF_TIME__ Opened file: " << path);
- /*
- fseek(filen, 0, SEEK_END);
- unsigned long file_size = ftell(filen);
- rewind(filen);
- unsigned char *buf = new unsigned char[file_size];
- DEBUG("__GET_EXIF_TIME__ File size: " << file_size);
- if (fread(buf, 1, file_size, filen) != file_size)
- {
- DEBUG("__GET_EXIF_TIME__ Could not read file");
- delete[] buf;
- fclose(filen);
- return nullptr;
- }
- fclose(filen);*/
- DEBUG("__GET_EXIF_TIME__ File size: " << file_size);
- unsigned int shortened = file_size > 0x10000 ? 0x10000 : file_size;
- DEBUG("__GET_EXIF_TIME__ Shortened size: " << shortened);
- unsigned char *buf = new unsigned char[shortened];
- if (fread(buf, 1, shortened, filen) != shortened)
- {
- delete[] buf;
- fclose(filen);
- ASSERT("__GET_EXIF_TIME__ Could not read file");
- return nullptr;
- }
- fclose(filen);
- easyexif::EXIFInfo exif;
- if(exif.parseFrom(buf, shortened))
- {
- delete[] buf;
- ASSERT("__GET_EXIF_TIME__ Could not parse file");
- return nullptr;
- }
- delete[] buf;
- DEBUG("__GET_EXIF_TIME__ File parsed!");
- if(exif.DateTimeOriginal.size()==0)
- {
- DEBUG("__GET_EXIF_TIME__ No original datetime found!");
- delete[] buf;
- return nullptr;
- }
- tm *result = new tm;
- //tm *result = (tm*)malloc(sizeof(tm));
- sscanf(exif.DateTimeOriginal.c_str(), "%d:%d:%d %d:%d:%d", &result->tm_year, &result->tm_mon, &result->tm_mday, &result->tm_hour, &result->tm_min, &result->tm_sec);
- DEBUG("__GET_EXIF_TIME__ DateTime: " << exif.DateTimeOriginal.c_str());
- DEBUG("__GET_EXIF_TIME__ year: " << result->tm_year);
- DEBUG("__GET_EXIF_TIME__ month: " << result->tm_mon);
- DEBUG("__GET_EXIF_TIME__ date: " << result->tm_mday);
- DEBUG("__GET_EXIF_TIME__ hour: " << result->tm_hour);
- DEBUG("__GET_EXIF_TIME__ minute: " << result->tm_min);
- DEBUG("__GET_EXIF_TIME__ second: " << result->tm_sec);
- result->tm_year -= 1900;
- result->tm_mon -= 1;
- tm dont_touch_my_time = *result;
- time_t unix_time = mktime(result);
- *result = dont_touch_my_time;
- DEBUG("__GET_EXIF_TIME__ Time: " << unix_time);
- // Sort of a sanity check
- if(unix_time == -1)
- {
- delete result;
- return nullptr;
- }//*/
- return result;
- }
- STATUS_CODE filefunctions::cd(std::string const &path_arg, COMMAND_FLAGS flags)
- {
- DWORD attr;
- std::string path;
- if(flags&CF_RELATIVE)
- {
- path = filefunctions_wd;
- if(path.length() == 0)
- {
- int path_len = GetCurrentDirectory(0, nullptr);
- LPTSTR temp = new TCHAR[path_len+1];
- if(!GetCurrentDirectory(path_len+1, temp))
- return SC_INVALID_PATH;
- path = std::string(temp);
- DEBUG("__CD__ GetCurrentDirectory() = " << temp);
- delete[] temp;
- }
- if(path_arg.length())
- path += "\\" + path_arg;
- }
- else
- {
- path = path_arg;
- }
- attr = GetFileAttributes(path.c_str());
- DEBUG("__CD__ Path: " << path);
- DEBUG("__CD__ Attr: " << std::hex << attr);
- if(attr != INVALID_FILE_ATTRIBUTES && attr&FILE_ATTRIBUTE_DIRECTORY)
- {
- filefunctions_wd = path;
- return SC_SUCCESS;
- }
- return SC_INVALID_PATH;
- }
- STATUS_CODE filefunctions::mv(std::string const &src_arg, std::string const &dest_arg, COMMAND_FLAGS flags)
- {
- std::string src, dest;
- DWORD attr;
- if(flags & CF_RELATIVE)
- {
- src = filefunctions_wd;
- dest = filefunctions_wd;
- if(src.length())
- src += "\\" + src_arg;
- if(dest.length())
- dest += "\\" + dest_arg;
- }
- else
- {
- src = src_arg;
- dest = dest_arg;
- }
- DEBUG("__MV__ Source: " << src);
- DEBUG("__MV__ Destination: " << dest);
- attr = GetFileAttributes(src.c_str());
- if (attr == INVALID_FILE_ATTRIBUTES)
- {
- DEBUG("__MV__ Source does not exist");
- return SC_DOES_NOT_EXIST;
- }
- if(mkdir(dest.substr(0,dest.find_last_of("/\\"))))
- {
- DEBUG("__MV__ Could not determine destination directory");
- return SC_INVALID_DESTINATION;
- }
- if(MoveFile(src.c_str(), dest.c_str()))
- {
- DEBUG("__MV__ Moved file\n\n");
- return SC_SUCCESS;
- }
- if(GetLastError() == ERROR_ALREADY_EXISTS)
- {
- DEBUG("__MV__ Target already exists");
- return SC_ALREADY_EXISTS;
- }
- return SC_OTHER_FAILIURE;
- }
- STATUS_CODE filefunctions::mkdir(std::string const &arg_path, COMMAND_FLAGS flags)
- {
- std::string path;
- if(flags & CF_RELATIVE)
- {
- path = filefunctions_wd;
- if(arg_path.length())
- path += "\\" + arg_path;
- }
- else
- {
- path = arg_path;
- }
- DEBUG("__MKDIR__ Path: " << path);
- DWORD attr = GetFileAttributes(path.c_str());
- if(attr != INVALID_FILE_ATTRIBUTES && attr&FILE_ATTRIBUTE_DIRECTORY)
- {
- DEBUG("__MKDIR__ SUCCESS Attr: " << attr);
- return SC_SUCCESS;
- }
- else if(attr != INVALID_FILE_ATTRIBUTES)
- {
- DEBUG("__MKDIR__ FAILED Attr: " << attr);
- return SC_IS_NOT_DIRECTORY;
- }
- size_t parent_separator = path.find_last_of("/\\");
- if(parent_separator == std::string::npos)
- {
- DEBUG("__MKDIR__ Invalid path");
- return SC_INVALID_PATH;
- }
- std::string parent = path.substr(0,parent_separator);
- DEBUG("__MKDIR__ Parent: " << parent);
- if(mkdir(parent) == SC_SUCCESS)
- {
- if(CreateDirectory(path.c_str(), nullptr))
- {
- DEBUG("__MKDIR__ Created directory: " << path);
- return SC_SUCCESS;
- }
- else
- {
- DEBUG("__MKDIR__ CreateDirectory failed");
- return SC_OTHER_FAILIURE;
- }
- }
- else
- {
- DEBUG("__MKDIR__ mkdir(" << parent << ") failed");
- return SC_OTHER_FAILIURE;
- }
- }
- //STATUS_CODE filefunctions::rm(std::string const &path, COMMAND_FLAGS flags = CF_NONE);
- std::string filefunctions::pwd(void)
- {
- return filefunctions_wd;
- }
- STATUS_CODE filefunctions::ls(std::string path_arg, std::queue<File_Struct> &content, COMMAND_FLAGS flags)
- {
- std::string path;
- if(flags & CF_RELATIVE)
- {
- path = filefunctions_wd;
- if(path_arg.length())
- path += path_arg;
- }
- else
- {
- path = path_arg;
- }
- DEBUG("__LS__ ls(" << path << ")");
- DWORD attr = GetFileAttributes(path.c_str());
- if(attr != INVALID_FILE_ATTRIBUTES && !(attr&FILE_ATTRIBUTE_DIRECTORY))
- return SC_IS_NOT_DIRECTORY;
- std::string path_joker = path + "\\*";
- WIN32_FIND_DATA file_data;
- HANDLE status = FindFirstFile(path_joker.c_str(), &file_data);
- if(status == INVALID_HANDLE_VALUE)
- return SC_OTHER_FAILIURE;
- do
- {
- File_Struct temp;
- temp.modified = nullptr;
- temp.taken = nullptr;
- temp.file_ending = "";
- temp.path = path + "\\" + file_data.cFileName;
- if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- {
- DEBUG("__LS__ Found directory!");
- DEBUG("__LS__ Directory: \"" << temp.path << "\"");
- if(flags&CF_RECURSIVE)
- {
- if(strcmp(file_data.cFileName, ".")==0 || strcmp(file_data.cFileName, "..")==0)
- {
- DEBUG("__LS__ Ignoring directory . or ..");
- }
- else
- {
- INFO("__LS__ Found directory. Going in!\n");
- ls(temp.path, content, CF_RECURSIVE);
- }
- }
- else
- {
- DEBUG("__LS__ Ignoring directory, not recursive");
- }
- }
- else
- {
- DEBUG("__LS__ Found file!");
- INFO("__LS__ File: \"" << temp.path << "\"");
- time_t unix_time = filetime_to_unixtime(file_data.ftLastWriteTime);
- DEBUG("__LS__ Modified time: " << std::dec << unix_time);
- if(unix_time < 800000000 || unix_time > 3500000000)
- {
- delete temp.modified;
- temp.modified = nullptr;
- }
- else
- {
- temp.modified = new tm;
- memcpy(temp.modified, gmtime(&unix_time), sizeof(tm));
- }
- temp.file_ending = temp.path.substr(temp.path.find_last_of('.')+1);
- std::transform(temp.file_ending.begin(), temp.file_ending.end(), temp.file_ending.begin(), ::tolower);
- DEBUG("__LS__ File ending: " << temp.file_ending);
- if(temp.file_ending == "jpeg" || temp.file_ending == "jpg")
- {
- temp.file_ending = "jpg";
- DEBUG("__LS__ Found JPEG-file, decoding EXIF");
- LARGE_INTEGER dryg;
- dryg.HighPart = file_data.nFileSizeHigh;
- dryg.LowPart = file_data.nFileSizeLow;
- temp.taken = get_exif_time(temp.path, dryg.QuadPart);
- if(temp.taken)
- {
- DEBUG("__LS__ year: " << temp.taken->tm_year);
- DEBUG("__LS__ month: " << temp.taken->tm_mon);
- DEBUG("__LS__ date: " << temp.taken->tm_mday);
- DEBUG("__LS__ hour: " << temp.taken->tm_hour);
- DEBUG("__LS__ minute: " << temp.taken->tm_min);
- DEBUG("__LS__ second: " << temp.taken->tm_sec);
- }
- }
- content.push(temp);
- }
- DEBUG("__LS__ Checking for other files\n");
- } while (FindNextFile(status, &file_data) != 0);
- DEBUG("__LS__ Finnished ls()\n\n");
- return SC_SUCCESS;
- }
- #else
- // Fail?
- Hård fail som fan!
- #endif // __linux__
|