1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
/*
* fileio.h
*
* Created on: 25.07.2010
* Author: ed
*/
#ifndef FILEIO_H_
#define FILEIO_H_
#include "meta.h"
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <cstdio>
#include <unistd.h>
#ifdef HAVE_LINUX_SENDFILE
#include <sys/sendfile.h>
#endif
#ifndef O_BINARY
#define O_BINARY 0 // ignore on Unix
#endif
int falloc_helper(int fd, off_t start, off_t len);
ssize_t sendfile_generic(int out_fd, int in_fd, off_t *offset, size_t count);
class Cstat : public stat
{
bool bResult;
public:
inline Cstat(cmstring &s) { bResult = !::stat(s.c_str(), static_cast<struct stat*>(this)); }
inline operator bool() const { return bResult; }
};
bool FileCopy_generic(cmstring &from, cmstring &to);
// in fact, pipe&splice&splice method works about 10% but only without considering other IO costs
// with them, the benefit is neglible
//#if defined(HAVE_LINUX_SPLICE) && defined(HAVE_PREAD)
//bool FileCopy(cmstring &from, cmstring &to);
//#else
#define FileCopy(x,y) FileCopy_generic(x,y)
//#endif
bool LinkOrCopy(const mstring &from, const mstring &to);
void set_nb(int fd);
void set_block(int fd);
#define forceclose(fd) { while(0 != ::close(fd)) { if(errno != EINTR) break; }; fd=-1; }
#define checkforceclose(fd) if(fd>=0){ while(0 != ::close(fd)) { if(errno != EINTR) break; }; fd=-1; }
inline void checkForceFclose(FILE* &fh)
{
if (fh)
{
int fd = fileno(fh);
if (0 != ::fclose(fh) && errno != EBADF)
{
forceclose(fd);
}
fh = NULL;
}
}
struct FILE_RAII
{
FILE *p;
inline FILE_RAII() : p(NULL) {}
inline void close() { checkForceFclose(p); };
inline ~FILE_RAII() { close(); }
private:
FILE_RAII(const FILE_RAII&);
FILE_RAII operator=(const FILE_RAII&);
};
void mkbasedir(const mstring & path);
#endif /* FILEIO_H_ */
|