code format
This commit is contained in:
@@ -1,25 +1,26 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
File
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
PIP - Platform Independent Primitives
|
||||
File
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "piincludes_p.h"
|
||||
#include "pifile.h"
|
||||
|
||||
#include "pidir.h"
|
||||
#include "piincludes_p.h"
|
||||
#include "piiostream.h"
|
||||
#include "pitime_win.h"
|
||||
#ifdef WINDOWS
|
||||
@@ -36,38 +37,38 @@
|
||||
# define S_IFCHR 0x10
|
||||
# define S_IFSOCK 0x20
|
||||
#else
|
||||
# include <fcntl.h>
|
||||
# include <sys/stat.h>
|
||||
# include <sys/time.h>
|
||||
# include <fcntl.h>
|
||||
# include <utime.h>
|
||||
#endif
|
||||
#define S_IFHDN 0x40
|
||||
#define S_IFHDN 0x40
|
||||
#if defined(QNX) || defined(ANDROID) || defined(FREERTOS)
|
||||
# define _fopen_call_ fopen
|
||||
# define _fseek_call_ fseek
|
||||
# define _ftell_call_ ftell
|
||||
# define _stat_struct_ struct stat
|
||||
# define _stat_call_ stat
|
||||
# define _stat_link_ lstat
|
||||
# define _fopen_call_ fopen
|
||||
# define _fseek_call_ fseek
|
||||
# define _ftell_call_ ftell
|
||||
# define _stat_struct_ struct stat
|
||||
# define _stat_call_ stat
|
||||
# define _stat_link_ lstat
|
||||
#else
|
||||
# if defined(MAC_OS)
|
||||
# define _fopen_call_ fopen
|
||||
# define _fseek_call_ fseek
|
||||
# define _ftell_call_ ftell
|
||||
# define _fopen_call_ fopen
|
||||
# define _fseek_call_ fseek
|
||||
# define _ftell_call_ ftell
|
||||
# else
|
||||
# ifdef CC_GCC
|
||||
# define _fopen_call_ fopen64
|
||||
# define _fseek_call_ fseeko64
|
||||
# define _ftell_call_ ftello64
|
||||
# else
|
||||
# define _fopen_call_ fopen
|
||||
# define _fseek_call_ fseek
|
||||
# define _ftell_call_ ftell
|
||||
# endif
|
||||
# ifdef CC_GCC
|
||||
# define _fopen_call_ fopen64
|
||||
# define _fseek_call_ fseeko64
|
||||
# define _ftell_call_ ftello64
|
||||
# else
|
||||
# define _fopen_call_ fopen
|
||||
# define _fseek_call_ fseek
|
||||
# define _ftell_call_ ftell
|
||||
# endif
|
||||
# endif
|
||||
# define _stat_struct_ struct stat64
|
||||
# define _stat_call_ stat64
|
||||
# define _stat_link_ lstat64
|
||||
# define _stat_struct_ struct stat64
|
||||
# define _stat_call_ stat64
|
||||
# define _stat_link_ lstat64
|
||||
#endif
|
||||
|
||||
|
||||
@@ -152,10 +153,7 @@ PIString PIFile::FileInfo::dir() const {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
PIFile::PIFile(): PIIODevice() {
|
||||
}
|
||||
PIFile::PIFile(): PIIODevice() {}
|
||||
|
||||
|
||||
PIFile::PIFile(const PIString & path, PIIODevice::DeviceMode mode): PIIODevice(path, mode) {
|
||||
@@ -196,7 +194,7 @@ bool PIFile::openDevice() {
|
||||
}
|
||||
}
|
||||
PRIVATE->fd = _fopen_call_(p.data(), strType(mode_).data());
|
||||
//piCout << "fopen " << path() << ": " << strType(mode_).data() << PRIVATE->fd;
|
||||
// piCout << "fopen " << path() << ": " << strType(mode_).data() << PRIVATE->fd;
|
||||
bool opened = (PRIVATE->fd != 0);
|
||||
if (opened) {
|
||||
fdi = fileno(PRIVATE->fd);
|
||||
@@ -210,19 +208,19 @@ bool PIFile::openDevice() {
|
||||
_fseek_call_(PRIVATE->fd, 0, SEEK_SET);
|
||||
clearerr(PRIVATE->fd);
|
||||
}
|
||||
//piCout << "open file" << PRIVATE->fd << opened_;
|
||||
// piCout << "open file" << PRIVATE->fd << opened_;
|
||||
return opened;
|
||||
}
|
||||
|
||||
|
||||
bool PIFile::closeDevice() {
|
||||
//piCout << "close file" << PRIVATE->fd << opened_;
|
||||
// piCout << "close file" << PRIVATE->fd << opened_;
|
||||
if (isClosed() || PRIVATE->fd == 0) return true;
|
||||
bool cs = (fclose(PRIVATE->fd) == 0);
|
||||
if (cs) PRIVATE->fd = 0;
|
||||
fdi = -1;
|
||||
fdi = -1;
|
||||
_size = -1;
|
||||
//piCout << "closed file" << PRIVATE->fd << opened_;
|
||||
// piCout << "closed file" << PRIVATE->fd << opened_;
|
||||
return cs;
|
||||
}
|
||||
|
||||
@@ -232,9 +230,9 @@ llong PIFile::readAll(void * data) {
|
||||
seekToBegin();
|
||||
if (s < 0) {
|
||||
while (!isEnd())
|
||||
read(&(((char*)data)[++i]), 1);
|
||||
read(&(((char *)data)[++i]), 1);
|
||||
} else
|
||||
read((char * )data, s);
|
||||
read((char *)data, s);
|
||||
seek(cp);
|
||||
return s;
|
||||
}
|
||||
@@ -264,9 +262,11 @@ PIByteArray PIFile::readAll(bool forceRead) {
|
||||
llong PIFile::size() const {
|
||||
if (isClosed()) return -1;
|
||||
llong s, cp = pos();
|
||||
_fseek_call_(PRIVATE->fd, 0, SEEK_END); clearerr(PRIVATE->fd);
|
||||
_fseek_call_(PRIVATE->fd, 0, SEEK_END);
|
||||
clearerr(PRIVATE->fd);
|
||||
s = pos();
|
||||
_fseek_call_(PRIVATE->fd, cp, SEEK_SET); clearerr(PRIVATE->fd);
|
||||
_fseek_call_(PRIVATE->fd, cp, SEEK_SET);
|
||||
clearerr(PRIVATE->fd);
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -289,10 +289,9 @@ void PIFile::resize(llong new_size, uchar fill_) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool PIFile::isExists(const PIString & path) {
|
||||
FILE * f = _fopen_call_(PIString(path).data(), "r");
|
||||
bool ok = (f != 0);
|
||||
bool ok = (f != 0);
|
||||
if (ok) fclose(f);
|
||||
return ok;
|
||||
}
|
||||
@@ -304,7 +303,7 @@ bool PIFile::remove(const PIString & path) {
|
||||
return RemoveDirectoryA(path.data()) > 0;
|
||||
else
|
||||
#endif
|
||||
return ::remove(path.data()) == 0;
|
||||
return ::remove(path.data()) == 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -378,7 +377,7 @@ void PIFile::seekToLine(llong line) {
|
||||
if (isClosed()) return;
|
||||
seekToBegin();
|
||||
PIIOTextStream ts(this);
|
||||
piForTimes (line) ts.readLine();
|
||||
piForTimes(line) ts.readLine();
|
||||
clearerr(PRIVATE->fd);
|
||||
}
|
||||
|
||||
@@ -410,8 +409,7 @@ bool PIFile::isEnd() const {
|
||||
if (isClosed()) return true;
|
||||
bool ret = (feof(PRIVATE->fd) || ferror(PRIVATE->fd));
|
||||
if (!ret && (_size >= 0)) {
|
||||
if (pos() > _size)
|
||||
ret = true;
|
||||
if (pos() > _size) ret = true;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -455,7 +453,7 @@ void PIFile::clear() {
|
||||
PRIVATE->fd = fopen(path().data(), "w");
|
||||
if (PRIVATE->fd != 0) fclose(PRIVATE->fd);
|
||||
PRIVATE->fd = 0;
|
||||
opened_ = false;
|
||||
opened_ = false;
|
||||
open();
|
||||
}
|
||||
|
||||
@@ -466,14 +464,12 @@ void PIFile::remove() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
PIFile::FileInfo PIFile::fileInfo(const PIString & path) {
|
||||
FileInfo ret;
|
||||
if (path.isEmpty()) return ret;
|
||||
ret.path = path.replacedAll("\\", PIDir::separator);
|
||||
ret.path = path.replacedAll("\\", PIDir::separator);
|
||||
PIString n = ret.name();
|
||||
//piCout << "open" << path;
|
||||
// piCout << "open" << path;
|
||||
#ifdef WINDOWS
|
||||
DWORD attr = GetFileAttributesA((LPCSTR)(path.data()));
|
||||
if (attr == 0xFFFFFFFF) return ret;
|
||||
@@ -490,51 +486,53 @@ PIFile::FileInfo PIFile::fileInfo(const PIString & path) {
|
||||
LARGE_INTEGER filesize;
|
||||
filesize.LowPart = filesize.HighPart = 0;
|
||||
if (fi.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ret.flags |= FileInfo::Hidden;
|
||||
if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ret.flags |= FileInfo::Dir;
|
||||
if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
||||
ret.flags |= FileInfo::Dir;
|
||||
else {
|
||||
ret.flags |= FileInfo::File;
|
||||
filesize.LowPart = fi.nFileSizeLow;
|
||||
filesize.LowPart = fi.nFileSizeLow;
|
||||
filesize.HighPart = fi.nFileSizeHigh;
|
||||
}
|
||||
PIString ext = ret.extension();
|
||||
ret.perm_user = FileInfo::Permissions(true, (attr & FILE_ATTRIBUTE_READONLY) != FILE_ATTRIBUTE_READONLY, ext == "bat" || ext == "exe");
|
||||
ret.perm_user =
|
||||
FileInfo::Permissions(true, (attr & FILE_ATTRIBUTE_READONLY) != FILE_ATTRIBUTE_READONLY, ext == "bat" || ext == "exe");
|
||||
ret.perm_group = ret.perm_other = ret.perm_user;
|
||||
ret.size = filesize.QuadPart;
|
||||
ret.time_access = FILETIME2PIDateTime(fi.ftLastAccessTime);
|
||||
ret.time_modification = FILETIME2PIDateTime(fi.ftLastWriteTime);
|
||||
ret.size = filesize.QuadPart;
|
||||
ret.time_access = FILETIME2PIDateTime(fi.ftLastAccessTime);
|
||||
ret.time_modification = FILETIME2PIDateTime(fi.ftLastWriteTime);
|
||||
}
|
||||
CloseHandle(hFile);
|
||||
#else
|
||||
_stat_struct_ fs;
|
||||
memset(&fs, 0, sizeof(fs));
|
||||
_stat_call_(path.data(), &fs);
|
||||
int mode = fs.st_mode;
|
||||
ret.size = fs.st_size;
|
||||
ret.id_user = fs.st_uid;
|
||||
ret.id_group = fs.st_gid;
|
||||
#ifdef ANDROID
|
||||
ret.time_access = PIDateTime::fromSystemTime(PISystemTime(fs.st_atime, fs.st_atime_nsec));
|
||||
int mode = fs.st_mode;
|
||||
ret.size = fs.st_size;
|
||||
ret.id_user = fs.st_uid;
|
||||
ret.id_group = fs.st_gid;
|
||||
# ifdef ANDROID
|
||||
ret.time_access = PIDateTime::fromSystemTime(PISystemTime(fs.st_atime, fs.st_atime_nsec));
|
||||
ret.time_modification = PIDateTime::fromSystemTime(PISystemTime(fs.st_mtime, fs.st_mtime_nsec));
|
||||
#else
|
||||
# if defined(QNX) || defined(FREERTOS)
|
||||
ret.time_access = PIDateTime::fromSecondSinceEpoch(fs.st_atime);
|
||||
ret.time_modification = PIDateTime::fromSecondSinceEpoch(fs.st_mtime);
|
||||
# else
|
||||
# ifdef MAC_OS
|
||||
# define ATIME st_atimespec
|
||||
# define MTIME st_ctimespec
|
||||
# else
|
||||
# define ATIME st_atim
|
||||
# define MTIME st_mtim
|
||||
# endif
|
||||
ret.time_access = PIDateTime::fromSystemTime(PISystemTime(fs.ATIME.tv_sec, fs.ATIME.tv_nsec));
|
||||
# if defined(QNX) || defined(FREERTOS)
|
||||
ret.time_access = PIDateTime::fromSecondSinceEpoch(fs.st_atime);
|
||||
ret.time_modification = PIDateTime::fromSecondSinceEpoch(fs.st_mtime);
|
||||
# else
|
||||
# ifdef MAC_OS
|
||||
# define ATIME st_atimespec
|
||||
# define MTIME st_ctimespec
|
||||
# else
|
||||
# define ATIME st_atim
|
||||
# define MTIME st_mtim
|
||||
# endif
|
||||
ret.time_access = PIDateTime::fromSystemTime(PISystemTime(fs.ATIME.tv_sec, fs.ATIME.tv_nsec));
|
||||
ret.time_modification = PIDateTime::fromSystemTime(PISystemTime(fs.MTIME.tv_sec, fs.MTIME.tv_nsec));
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
#ifndef MICRO_PIP
|
||||
ret.perm_user = FileInfo::Permissions((mode & S_IRUSR) == S_IRUSR, (mode & S_IWUSR) == S_IWUSR, (mode & S_IXUSR) == S_IXUSR);
|
||||
ret.perm_group = FileInfo::Permissions((mode & S_IRGRP) == S_IRGRP, (mode & S_IWGRP) == S_IWGRP, (mode & S_IXGRP) == S_IXGRP);
|
||||
ret.perm_other = FileInfo::Permissions((mode & S_IROTH) == S_IROTH, (mode & S_IWOTH) == S_IWOTH, (mode & S_IXOTH) == S_IXOTH);
|
||||
# ifndef MICRO_PIP
|
||||
ret.perm_user = FileInfo::Permissions((mode & S_IRUSR) == S_IRUSR, (mode & S_IWUSR) == S_IWUSR, (mode & S_IXUSR) == S_IXUSR);
|
||||
ret.perm_group = FileInfo::Permissions((mode & S_IRGRP) == S_IRGRP, (mode & S_IWGRP) == S_IWGRP, (mode & S_IXGRP) == S_IXGRP);
|
||||
ret.perm_other = FileInfo::Permissions((mode & S_IROTH) == S_IROTH, (mode & S_IWOTH) == S_IWOTH, (mode & S_IXOTH) == S_IXOTH);
|
||||
memset(&fs, 0, sizeof(fs));
|
||||
_stat_link_(path.data(), &fs);
|
||||
mode &= ~S_IFLNK;
|
||||
@@ -544,7 +542,7 @@ PIFile::FileInfo PIFile::fileInfo(const PIString & path) {
|
||||
if ((mode & S_IFREG) == S_IFREG) ret.flags |= FileInfo::File;
|
||||
if ((mode & S_IFLNK) == S_IFLNK) ret.flags |= FileInfo::SymbolicLink;
|
||||
if ((mode & S_IFHDN) == S_IFHDN) ret.flags |= FileInfo::Hidden;
|
||||
#endif
|
||||
# endif
|
||||
#endif
|
||||
if (n == ".") ret.flags = FileInfo::Dir | FileInfo::Dot;
|
||||
if (n == "..") ret.flags = FileInfo::Dir | FileInfo::DotDot;
|
||||
@@ -567,7 +565,13 @@ bool PIFile::applyFileInfo(const PIString & path, const PIFile::FileInfo & info)
|
||||
}
|
||||
HANDLE hFile = 0;
|
||||
if ((attr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) {
|
||||
hFile = CreateFileA(path.data(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
|
||||
hFile = CreateFileA(path.data(),
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
0,
|
||||
OPEN_EXISTING,
|
||||
FILE_FLAG_BACKUP_SEMANTICS,
|
||||
0);
|
||||
} else {
|
||||
hFile = CreateFileA(path.data(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
|
||||
}
|
||||
@@ -580,15 +584,15 @@ bool PIFile::applyFileInfo(const PIString & path, const PIFile::FileInfo & info)
|
||||
CloseHandle(hFile);
|
||||
#else
|
||||
int mode(0);
|
||||
if (info.perm_user.read) mode |= S_IRUSR;
|
||||
if (info.perm_user.write) mode |= S_IWUSR;
|
||||
if (info.perm_user.exec) mode |= S_IXUSR;
|
||||
if (info.perm_group.read) mode |= S_IRGRP;
|
||||
if (info.perm_user.read) mode |= S_IRUSR;
|
||||
if (info.perm_user.write) mode |= S_IWUSR;
|
||||
if (info.perm_user.exec) mode |= S_IXUSR;
|
||||
if (info.perm_group.read) mode |= S_IRGRP;
|
||||
if (info.perm_group.write) mode |= S_IWGRP;
|
||||
if (info.perm_group.exec) mode |= S_IXGRP;
|
||||
if (info.perm_other.read) mode |= S_IROTH;
|
||||
if (info.perm_group.exec) mode |= S_IXGRP;
|
||||
if (info.perm_other.read) mode |= S_IROTH;
|
||||
if (info.perm_other.write) mode |= S_IWOTH;
|
||||
if (info.perm_other.exec) mode |= S_IXOTH;
|
||||
if (info.perm_other.exec) mode |= S_IXOTH;
|
||||
if (chmod(fp.data(), mode) != 0) {
|
||||
piCout << "[PIFile] applyFileInfo: \"chmod\" error:" << errorString();
|
||||
}
|
||||
@@ -597,11 +601,11 @@ bool PIFile::applyFileInfo(const PIString & path, const PIFile::FileInfo & info)
|
||||
}
|
||||
struct timeval tm[2];
|
||||
PISystemTime st = info.time_access.toSystemTime();
|
||||
tm[0].tv_sec = st.seconds;
|
||||
tm[0].tv_usec = st.nanoseconds / 1000;
|
||||
st = info.time_modification.toSystemTime();
|
||||
tm[1].tv_sec = st.seconds;
|
||||
tm[1].tv_usec = st.nanoseconds / 1000;
|
||||
tm[0].tv_sec = st.seconds;
|
||||
tm[0].tv_usec = st.nanoseconds / 1000;
|
||||
st = info.time_modification.toSystemTime();
|
||||
tm[1].tv_sec = st.seconds;
|
||||
tm[1].tv_usec = st.nanoseconds / 1000;
|
||||
if (utimes(fp.data(), tm) != 0) {
|
||||
piCout << "[PIFile] applyFileInfo: \"utimes\" error:" << errorString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user