Fix warnings in file_unix.cpp (#383)

Long overdue fixes to the warnings in file_unix.cpp. Also, move the POSIX-exclusive fcntl.h include from file.h to file_unix.cpp and remove MSVC warning suppression related to exception specifications, which were also removed as they have been deprecated since C++11.
This commit is contained in:
Daniel Kamil Kozar 2020-12-21 00:22:24 +01:00 committed by GitHub
parent c1a1328560
commit 4f21320a68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 38 deletions

View File

@ -1,16 +1,8 @@
#ifndef LIBMEDIATION_FILE_H #ifndef LIBMEDIATION_FILE_H
#define LIBMEDIATION_FILE_H #define LIBMEDIATION_FILE_H
#include <fcntl.h>
#include <stdexcept>
#include "../types/types.h" #include "../types/types.h"
#ifdef _WIN32
#pragma warning(disable : 4290)
#endif
class AbstractStream class AbstractStream
{ {
public: public:
@ -58,8 +50,7 @@ class File : public AbstractOutputStream
In the win32 implementation, this is the dwFlagsAndAttributes parameter to the CreateFile function, In the win32 implementation, this is the dwFlagsAndAttributes parameter to the CreateFile function,
In the unix implementation, this is the second parameter to the open function. In the unix implementation, this is the second parameter to the open function.
*/ */
File(const char* fName, unsigned int oflag, File(const char* fName, unsigned int oflag, unsigned int systemDependentFlags = 0);
unsigned int systemDependentFlags = 0) /* throw ( std::runtime_error ) */;
~File() override; ~File() override;
//! Open the file //! Open the file
@ -138,9 +129,6 @@ class File : public AbstractOutputStream
private: private:
void* m_impl; void* m_impl;
friend class MemoryMappedFile;
std::string m_name; std::string m_name;
mutable uint64_t m_pos; mutable uint64_t m_pos;
}; };

View File

@ -5,6 +5,7 @@
***********************************************************************/ ***********************************************************************/
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -19,7 +20,12 @@
#define O_LARGEFILE 0 #define O_LARGEFILE 0
#endif #endif
void makeUnixOpenFlags(unsigned int oflag, int* const unixOflag) namespace
{
int to_fd(void* impl) { return static_cast<int>(reinterpret_cast<std::intptr_t>(impl)); }
void* from_fd(int fd) { return reinterpret_cast<void*>(static_cast<std::intptr_t>(fd)); }
int makeUnixOpenFlags(unsigned int oflag)
{ {
int sysFlags = 0; int sysFlags = 0;
if (oflag & File::ofRead) if (oflag & File::ofRead)
@ -44,23 +50,23 @@ void makeUnixOpenFlags(unsigned int oflag, int* const unixOflag)
} }
if (oflag & File::ofCreateNew) if (oflag & File::ofCreateNew)
sysFlags |= O_CREAT | O_EXCL; sysFlags |= O_CREAT | O_EXCL;
*unixOflag = sysFlags; return sysFlags;
} }
} // namespace
File::File() : m_impl((void*)0xffffffff), m_name(""), m_pos(0) {} File::File() : m_impl(from_fd(-1)), m_pos(0) {}
File::File(const char* fName, unsigned int oflag, unsigned int systemDependentFlags) /* throw ( std::runtime_error ) */ File::File(const char* fName, unsigned int oflag, unsigned int systemDependentFlags) : m_name(fName), m_pos(0)
: m_name(fName), m_pos(0)
{ {
int sysFlags = 0; int sysFlags = makeUnixOpenFlags(oflag);
makeUnixOpenFlags(oflag, &sysFlags); auto fd = ::open(fName, sysFlags | O_LARGEFILE | systemDependentFlags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
m_impl = (void*)::open(fName, sysFlags | O_LARGEFILE | systemDependentFlags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (fd == -1)
if ((long)m_impl == -1)
{ {
std::ostringstream ss; std::ostringstream ss;
ss << "Error opening file " << fName << ": " << strerror(errno) << "(" << errno << ")"; ss << "Error opening file " << fName << ": " << strerror(errno) << "(" << errno << ")";
throw std::runtime_error(ss.str()); throw std::runtime_error(ss.str());
} }
m_impl = from_fd(fd);
} }
File::~File() File::~File()
@ -76,19 +82,18 @@ bool File::open(const char* fName, unsigned int oflag, unsigned int systemDepend
if (isOpen()) if (isOpen())
close(); close();
int sysFlags = 0; int sysFlags = makeUnixOpenFlags(oflag);
makeUnixOpenFlags(oflag, &sysFlags);
createDir(extractFileDir(fName), true); createDir(extractFileDir(fName), true);
m_impl = (void*)::open(fName, sysFlags | O_LARGEFILE | systemDependentFlags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); auto fd = ::open(fName, sysFlags | O_LARGEFILE | systemDependentFlags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
m_impl = from_fd(fd);
return (long)m_impl != -1; return fd != -1;
} }
bool File::close() bool File::close()
{ {
if (::close((long)m_impl) == 0) if (::close(to_fd(m_impl)) == 0)
{ {
m_impl = (void*)0xffffffff; m_impl = from_fd(-1);
return true; return true;
} }
@ -100,7 +105,7 @@ int File::read(void* buffer, uint32_t count) const
if (!isOpen()) if (!isOpen())
return -1; return -1;
m_pos += count; m_pos += count;
return ::read((long)m_impl, buffer, count); return ::read(to_fd(m_impl), buffer, count);
} }
int File::write(const void* buffer, uint32_t count) int File::write(const void* buffer, uint32_t count)
@ -108,10 +113,10 @@ int File::write(const void* buffer, uint32_t count)
if (!isOpen()) if (!isOpen())
return -1; return -1;
m_pos += count; m_pos += count;
return ::write((long)m_impl, buffer, count); return ::write(to_fd(m_impl), buffer, count);
} }
bool File::isOpen() const { return m_impl != (void*)0xffffffff; } bool File::isOpen() const { return to_fd(m_impl) != -1; }
bool File::size(uint64_t* const fileSize) const bool File::size(uint64_t* const fileSize) const
{ {
@ -119,7 +124,7 @@ bool File::size(uint64_t* const fileSize) const
struct stat64 buf; struct stat64 buf;
if (isOpen() && (fstat64((long)m_impl, &buf) == 0)) if (isOpen() && (fstat64(to_fd(m_impl), &buf) == 0))
{ {
*fileSize = buf.st_size; *fileSize = buf.st_size;
res = true; res = true;
@ -131,7 +136,7 @@ bool File::size(uint64_t* const fileSize) const
uint64_t File::seek(int64_t offset, SeekMethod whence) uint64_t File::seek(int64_t offset, SeekMethod whence)
{ {
if (!isOpen()) if (!isOpen())
return (uint64_t)-1; return UINT64_C(-1);
int sWhence = 0; int sWhence = 0;
switch (whence) switch (whence)
@ -148,18 +153,18 @@ uint64_t File::seek(int64_t offset, SeekMethod whence)
} }
m_pos = offset; m_pos = offset;
#if defined(__APPLE__) && defined(__MACH__) #if defined(__APPLE__) && defined(__MACH__)
return lseek((long)m_impl, offset, sWhence); return lseek(to_fd(m_impl), offset, sWhence);
#else #else
return lseek64((long)m_impl, offset, sWhence); return lseek64(to_fd(m_impl), offset, sWhence);
#endif #endif
} }
bool File::truncate(uint64_t newFileSize) bool File::truncate(uint64_t newFileSize)
{ {
#if defined(__APPLE__) && defined(__MACH__) #if defined(__APPLE__) && defined(__MACH__)
return ftruncate((long)m_impl, newFileSize) == 0; return ftruncate(to_fd(m_impl), newFileSize) == 0;
#else #else
return ftruncate64((long)m_impl, newFileSize) == 0; return ftruncate64(to_fd(m_impl), newFileSize) == 0;
#endif #endif
} }