2000-02-19 13:36:48 +01:00
|
|
|
/*
|
|
|
|
* tools.c: Various tools
|
|
|
|
*
|
2000-04-24 09:46:05 +02:00
|
|
|
* See the main source file 'vdr.c' for copyright information and
|
2000-02-19 13:36:48 +01:00
|
|
|
* how to reach the author.
|
|
|
|
*
|
2012-12-03 09:34:12 +01:00
|
|
|
* $Id: tools.c 2.27 2012/12/03 09:31:32 kls Exp $
|
2000-02-19 13:36:48 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tools.h"
|
2000-07-23 15:01:31 +02:00
|
|
|
#include <ctype.h>
|
2000-04-15 17:38:11 +02:00
|
|
|
#include <dirent.h>
|
2000-03-11 11:22:37 +01:00
|
|
|
#include <errno.h>
|
2005-12-29 11:24:02 +01:00
|
|
|
extern "C" {
|
|
|
|
#ifdef boolean
|
|
|
|
#define HAVE_BOOLEAN
|
|
|
|
#endif
|
|
|
|
#include <jpeglib.h>
|
|
|
|
#undef boolean
|
|
|
|
}
|
2001-08-11 11:15:41 +02:00
|
|
|
#include <stdlib.h>
|
2000-02-19 13:36:48 +01:00
|
|
|
#include <sys/time.h>
|
2002-03-31 20:51:06 +02:00
|
|
|
#include <sys/vfs.h>
|
2001-08-26 15:52:17 +02:00
|
|
|
#include <time.h>
|
2000-04-15 17:38:11 +02:00
|
|
|
#include <unistd.h>
|
2005-09-25 13:49:31 +02:00
|
|
|
#include <utime.h>
|
2001-08-25 13:27:26 +02:00
|
|
|
#include "i18n.h"
|
2006-01-15 16:42:37 +01:00
|
|
|
#include "thread.h"
|
2000-02-19 13:36:48 +01:00
|
|
|
|
2000-04-15 17:38:11 +02:00
|
|
|
int SysLogLevel = 3;
|
|
|
|
|
2006-01-15 16:42:37 +01:00
|
|
|
#define MAXSYSLOGBUF 256
|
|
|
|
|
|
|
|
void syslog_with_tid(int priority, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char fmt[MAXSYSLOGBUF];
|
|
|
|
snprintf(fmt, sizeof(fmt), "[%d] %s", cThread::ThreadId(), format);
|
|
|
|
va_start(ap, format);
|
|
|
|
vsyslog(priority, fmt, ap);
|
2006-04-17 12:19:31 +02:00
|
|
|
va_end(ap);
|
2006-01-15 16:42:37 +01:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:54:37 +01:00
|
|
|
int BCD2INT(int x)
|
|
|
|
{
|
|
|
|
return ((1000000 * BCDCHARTOINT((x >> 24) & 0xFF)) +
|
|
|
|
(10000 * BCDCHARTOINT((x >> 16) & 0xFF)) +
|
|
|
|
(100 * BCDCHARTOINT((x >> 8) & 0xFF)) +
|
|
|
|
BCDCHARTOINT( x & 0xFF));
|
|
|
|
}
|
|
|
|
|
2001-08-12 15:22:48 +02:00
|
|
|
ssize_t safe_read(int filedes, void *buffer, size_t size)
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
ssize_t p = read(filedes, buffer, size);
|
|
|
|
if (p < 0 && errno == EINTR) {
|
2002-05-13 16:35:49 +02:00
|
|
|
dsyslog("EINTR while reading from file handle %d - retrying", filedes);
|
2001-08-12 15:22:48 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t safe_write(int filedes, const void *buffer, size_t size)
|
|
|
|
{
|
2002-03-23 15:48:08 +01:00
|
|
|
ssize_t p = 0;
|
|
|
|
ssize_t written = size;
|
2002-03-22 15:18:58 +01:00
|
|
|
const unsigned char *ptr = (const unsigned char *)buffer;
|
|
|
|
while (size > 0) {
|
|
|
|
p = write(filedes, ptr, size);
|
|
|
|
if (p < 0) {
|
|
|
|
if (errno == EINTR) {
|
2002-05-13 16:35:49 +02:00
|
|
|
dsyslog("EINTR while writing to file handle %d - retrying", filedes);
|
2002-03-22 15:18:58 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ptr += p;
|
|
|
|
size -= p;
|
|
|
|
}
|
2002-03-23 15:48:08 +01:00
|
|
|
return p < 0 ? p : written;
|
2001-08-12 15:22:48 +02:00
|
|
|
}
|
|
|
|
|
2000-04-15 17:38:11 +02:00
|
|
|
void writechar(int filedes, char c)
|
|
|
|
{
|
2001-08-12 15:22:48 +02:00
|
|
|
safe_write(filedes, &c, sizeof(c));
|
2000-04-15 17:38:11 +02:00
|
|
|
}
|
|
|
|
|
2005-01-16 12:02:39 +01:00
|
|
|
int WriteAllOrNothing(int fd, const uchar *Data, int Length, int TimeoutMs, int RetryMs)
|
|
|
|
{
|
|
|
|
int written = 0;
|
|
|
|
while (Length > 0) {
|
|
|
|
int w = write(fd, Data + written, Length);
|
|
|
|
if (w > 0) {
|
|
|
|
Length -= w;
|
|
|
|
written += w;
|
|
|
|
}
|
|
|
|
else if (written > 0 && !FATALERRNO) {
|
|
|
|
// we've started writing, so we must finish it!
|
|
|
|
cTimeMs t;
|
|
|
|
cPoller Poller(fd, true);
|
|
|
|
Poller.Poll(RetryMs);
|
|
|
|
if (TimeoutMs > 0 && (TimeoutMs -= t.Elapsed()) <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
// nothing written yet (or fatal error), so we can just return the error code:
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2001-09-22 13:07:43 +02:00
|
|
|
char *strcpyrealloc(char *dest, const char *src)
|
|
|
|
{
|
|
|
|
if (src) {
|
|
|
|
int l = max(dest ? strlen(dest) : 0, strlen(src)) + 1; // don't let the block get smaller!
|
|
|
|
dest = (char *)realloc(dest, l);
|
|
|
|
if (dest)
|
|
|
|
strcpy(dest, src);
|
|
|
|
else
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: out of memory");
|
2001-09-22 13:07:43 +02:00
|
|
|
}
|
2001-10-05 20:40:53 +02:00
|
|
|
else {
|
2002-08-11 13:32:23 +02:00
|
|
|
free(dest);
|
2001-10-05 20:40:53 +02:00
|
|
|
dest = NULL;
|
|
|
|
}
|
2001-09-22 13:07:43 +02:00
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2000-09-09 14:57:43 +02:00
|
|
|
char *strn0cpy(char *dest, const char *src, size_t n)
|
|
|
|
{
|
|
|
|
char *s = dest;
|
|
|
|
for ( ; --n && (*dest = *src) != 0; dest++, src++) ;
|
|
|
|
*dest = 0;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2000-07-16 15:02:33 +02:00
|
|
|
char *strreplace(char *s, char c1, char c2)
|
|
|
|
{
|
2006-01-20 14:02:17 +01:00
|
|
|
if (s) {
|
|
|
|
char *p = s;
|
|
|
|
while (*p) {
|
|
|
|
if (*p == c1)
|
|
|
|
*p = c2;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
2000-07-16 15:02:33 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2002-02-03 15:55:04 +01:00
|
|
|
char *strreplace(char *s, const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
char *p = strstr(s, s1);
|
|
|
|
if (p) {
|
|
|
|
int of = p - s;
|
|
|
|
int l = strlen(s);
|
|
|
|
int l1 = strlen(s1);
|
|
|
|
int l2 = strlen(s2);
|
2011-02-25 15:25:42 +01:00
|
|
|
if (l2 > l1) {
|
|
|
|
if (char *NewBuffer = (char *)realloc(s, l + l2 - l1 + 1))
|
|
|
|
s = NewBuffer;
|
|
|
|
else {
|
|
|
|
esyslog("ERROR: out of memory");
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
2007-07-20 14:25:46 +02:00
|
|
|
char *sof = s + of;
|
2002-02-03 15:55:04 +01:00
|
|
|
if (l2 != l1)
|
2007-07-20 14:25:46 +02:00
|
|
|
memmove(sof + l2, sof + l1, l - of - l1 + 1);
|
|
|
|
strncpy(sof, s2, l2);
|
2002-02-03 15:55:04 +01:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2000-11-11 16:38:41 +01:00
|
|
|
char *stripspace(char *s)
|
|
|
|
{
|
|
|
|
if (s && *s) {
|
|
|
|
for (char *p = s + strlen(s) - 1; p >= s; p--) {
|
|
|
|
if (!isspace(*p))
|
|
|
|
break;
|
|
|
|
*p = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2001-08-17 13:19:10 +02:00
|
|
|
char *compactspace(char *s)
|
|
|
|
{
|
|
|
|
if (s && *s) {
|
|
|
|
char *t = stripspace(skipspace(s));
|
|
|
|
char *p = t;
|
|
|
|
while (p && *p) {
|
|
|
|
char *q = skipspace(p);
|
|
|
|
if (q - p > 1)
|
|
|
|
memmove(p + 1, q, strlen(q) + 1);
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (t != s)
|
|
|
|
memmove(s, t, strlen(t) + 1);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2004-12-26 12:45:22 +01:00
|
|
|
cString strescape(const char *s, const char *chars)
|
|
|
|
{
|
|
|
|
char *buffer;
|
|
|
|
const char *p = s;
|
|
|
|
char *t = NULL;
|
|
|
|
while (*p) {
|
|
|
|
if (strchr(chars, *p)) {
|
|
|
|
if (!t) {
|
|
|
|
buffer = MALLOC(char, 2 * strlen(s) + 1);
|
|
|
|
t = buffer + (p - s);
|
|
|
|
s = strcpy(buffer, s);
|
|
|
|
}
|
|
|
|
*t++ = '\\';
|
|
|
|
}
|
|
|
|
if (t)
|
|
|
|
*t++ = *p;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (t)
|
|
|
|
*t = 0;
|
2005-02-05 10:12:14 +01:00
|
|
|
return cString(s, t != NULL);
|
2004-12-26 12:45:22 +01:00
|
|
|
}
|
|
|
|
|
2001-08-17 13:19:10 +02:00
|
|
|
bool startswith(const char *s, const char *p)
|
|
|
|
{
|
|
|
|
while (*p) {
|
|
|
|
if (*p++ != *s++)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-01-20 16:47:09 +01:00
|
|
|
bool endswith(const char *s, const char *p)
|
|
|
|
{
|
|
|
|
const char *se = s + strlen(s) - 1;
|
|
|
|
const char *pe = p + strlen(p) - 1;
|
|
|
|
while (pe >= p) {
|
|
|
|
if (*pe-- != *se-- || (se < s && pe >= p))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2000-10-29 13:17:22 +01:00
|
|
|
bool isempty(const char *s)
|
|
|
|
{
|
|
|
|
return !(s && *skipspace(s));
|
2000-07-23 15:01:31 +02:00
|
|
|
}
|
|
|
|
|
2002-10-19 15:33:37 +02:00
|
|
|
int numdigits(int n)
|
|
|
|
{
|
2007-07-20 14:25:46 +02:00
|
|
|
int res = 1;
|
|
|
|
while (n >= 10) {
|
|
|
|
n /= 10;
|
|
|
|
res++;
|
|
|
|
}
|
|
|
|
return res;
|
2002-10-19 15:33:37 +02:00
|
|
|
}
|
|
|
|
|
2000-07-23 15:01:31 +02:00
|
|
|
bool isnumber(const char *s)
|
|
|
|
{
|
2011-07-31 13:23:31 +02:00
|
|
|
if (!s || !*s)
|
2002-11-24 15:56:24 +01:00
|
|
|
return false;
|
2007-07-20 14:25:46 +02:00
|
|
|
do {
|
|
|
|
if (!isdigit(*s))
|
|
|
|
return false;
|
|
|
|
} while (*++s);
|
2000-07-23 15:01:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-15 12:45:40 +02:00
|
|
|
int64_t StrToNum(const char *s)
|
|
|
|
{
|
|
|
|
char *t = NULL;
|
|
|
|
int64_t n = strtoll(s, &t, 10);
|
|
|
|
if (t) {
|
|
|
|
switch (*t) {
|
|
|
|
case 'T': n *= 1024;
|
|
|
|
case 'G': n *= 1024;
|
|
|
|
case 'M': n *= 1024;
|
|
|
|
case 'K': n *= 1024;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2012-09-30 13:05:14 +02:00
|
|
|
bool StrInArray(const char *a[], const char *s)
|
|
|
|
{
|
|
|
|
if (a) {
|
|
|
|
while (*a) {
|
|
|
|
if (strcmp(*a, s) == 0)
|
|
|
|
return true;
|
|
|
|
a++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-12-26 12:45:22 +01:00
|
|
|
cString AddDirectory(const char *DirName, const char *FileName)
|
|
|
|
{
|
2008-02-15 14:57:48 +01:00
|
|
|
return cString::sprintf("%s/%s", DirName && *DirName ? DirName : ".", FileName);
|
2004-12-26 12:45:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cString itoa(int n)
|
|
|
|
{
|
|
|
|
char buf[16];
|
|
|
|
snprintf(buf, sizeof(buf), "%d", n);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2008-02-16 13:38:22 +01:00
|
|
|
bool EntriesOnSameFileSystem(const char *File1, const char *File2)
|
|
|
|
{
|
2009-11-06 15:23:52 +01:00
|
|
|
struct stat st;
|
|
|
|
if (stat(File1, &st) == 0) {
|
|
|
|
dev_t dev1 = st.st_dev;
|
|
|
|
if (stat(File2, &st) == 0)
|
|
|
|
return st.st_dev == dev1;
|
2008-02-16 13:38:22 +01:00
|
|
|
else
|
|
|
|
LOG_ERROR_STR(File2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LOG_ERROR_STR(File1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-01-27 13:11:23 +01:00
|
|
|
int FreeDiskSpaceMB(const char *Directory, int *UsedMB)
|
2000-07-29 15:21:42 +02:00
|
|
|
{
|
2002-01-27 13:11:23 +01:00
|
|
|
if (UsedMB)
|
|
|
|
*UsedMB = 0;
|
|
|
|
int Free = 0;
|
2002-03-31 20:51:06 +02:00
|
|
|
struct statfs statFs;
|
|
|
|
if (statfs(Directory, &statFs) == 0) {
|
2002-05-01 16:22:41 +02:00
|
|
|
double blocksPerMeg = 1024.0 * 1024.0 / statFs.f_bsize;
|
2002-03-31 20:51:06 +02:00
|
|
|
if (UsedMB)
|
2002-05-01 16:22:41 +02:00
|
|
|
*UsedMB = int((statFs.f_blocks - statFs.f_bfree) / blocksPerMeg);
|
|
|
|
Free = int(statFs.f_bavail / blocksPerMeg);
|
2000-07-29 15:21:42 +02:00
|
|
|
}
|
|
|
|
else
|
2002-03-31 20:51:06 +02:00
|
|
|
LOG_ERROR_STR(Directory);
|
2000-07-29 15:21:42 +02:00
|
|
|
return Free;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DirectoryOk(const char *DirName, bool LogErrors)
|
2000-07-28 13:44:31 +02:00
|
|
|
{
|
|
|
|
struct stat ds;
|
|
|
|
if (stat(DirName, &ds) == 0) {
|
|
|
|
if (S_ISDIR(ds.st_mode)) {
|
|
|
|
if (access(DirName, R_OK | W_OK | X_OK) == 0)
|
|
|
|
return true;
|
2000-07-29 15:21:42 +02:00
|
|
|
else if (LogErrors)
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: can't access %s", DirName);
|
2000-07-28 13:44:31 +02:00
|
|
|
}
|
2000-07-29 15:21:42 +02:00
|
|
|
else if (LogErrors)
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: %s is not a directory", DirName);
|
2000-07-28 13:44:31 +02:00
|
|
|
}
|
2000-07-29 15:21:42 +02:00
|
|
|
else if (LogErrors)
|
2000-07-28 13:44:31 +02:00
|
|
|
LOG_ERROR_STR(DirName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2000-04-15 17:38:11 +02:00
|
|
|
bool MakeDirs(const char *FileName, bool IsDirectory)
|
2000-03-11 11:22:37 +01:00
|
|
|
{
|
|
|
|
bool result = true;
|
|
|
|
char *s = strdup(FileName);
|
|
|
|
char *p = s;
|
|
|
|
if (*p == '/')
|
|
|
|
p++;
|
2000-04-15 17:38:11 +02:00
|
|
|
while ((p = strchr(p, '/')) != NULL || IsDirectory) {
|
|
|
|
if (p)
|
|
|
|
*p = 0;
|
2000-03-11 11:22:37 +01:00
|
|
|
struct stat fs;
|
|
|
|
if (stat(s, &fs) != 0 || !S_ISDIR(fs.st_mode)) {
|
2002-05-13 16:35:49 +02:00
|
|
|
dsyslog("creating directory %s", s);
|
2005-08-06 09:56:08 +02:00
|
|
|
if (mkdir(s, ACCESSPERMS) == -1) {
|
2000-12-08 16:23:32 +01:00
|
|
|
LOG_ERROR_STR(s);
|
2000-03-11 11:22:37 +01:00
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-04-15 17:38:11 +02:00
|
|
|
if (p)
|
|
|
|
*p++ = '/';
|
|
|
|
else
|
|
|
|
break;
|
2000-03-11 11:22:37 +01:00
|
|
|
}
|
2002-08-11 13:32:23 +02:00
|
|
|
free(s);
|
2000-03-11 11:22:37 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-07-29 15:21:42 +02:00
|
|
|
bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks)
|
2000-04-15 17:38:11 +02:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (stat(FileName, &st) == 0) {
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2004-12-19 16:33:34 +01:00
|
|
|
cReadDir d(FileName);
|
|
|
|
if (d.Ok()) {
|
2000-04-15 17:38:11 +02:00
|
|
|
struct dirent *e;
|
2004-12-19 16:33:34 +01:00
|
|
|
while ((e = d.Next()) != NULL) {
|
2012-02-17 14:02:13 +01:00
|
|
|
cString buffer = AddDirectory(FileName, e->d_name);
|
|
|
|
if (FollowSymlinks) {
|
|
|
|
struct stat st2;
|
|
|
|
if (lstat(buffer, &st2) == 0) {
|
|
|
|
if (S_ISLNK(st2.st_mode)) {
|
|
|
|
int size = st2.st_size + 1;
|
|
|
|
char *l = MALLOC(char, size);
|
|
|
|
int n = readlink(buffer, l, size - 1);
|
|
|
|
if (n < 0) {
|
|
|
|
if (errno != EINVAL)
|
|
|
|
LOG_ERROR_STR(*buffer);
|
2010-08-29 15:03:08 +02:00
|
|
|
}
|
2012-02-17 14:02:13 +01:00
|
|
|
else {
|
|
|
|
l[n] = 0;
|
|
|
|
dsyslog("removing %s", l);
|
|
|
|
if (remove(l) < 0)
|
|
|
|
LOG_ERROR_STR(l);
|
|
|
|
}
|
|
|
|
free(l);
|
2000-07-29 15:21:42 +02:00
|
|
|
}
|
|
|
|
}
|
2012-02-17 14:02:13 +01:00
|
|
|
else if (errno != ENOENT) {
|
|
|
|
LOG_ERROR_STR(FileName);
|
|
|
|
return false;
|
|
|
|
}
|
2000-04-15 17:38:11 +02:00
|
|
|
}
|
2012-02-17 14:02:13 +01:00
|
|
|
dsyslog("removing %s", *buffer);
|
|
|
|
if (remove(buffer) < 0)
|
|
|
|
LOG_ERROR_STR(*buffer);
|
2000-04-15 17:38:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2000-07-29 15:21:42 +02:00
|
|
|
LOG_ERROR_STR(FileName);
|
2000-04-15 17:38:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2002-05-13 16:35:49 +02:00
|
|
|
dsyslog("removing %s", FileName);
|
2001-02-04 12:36:32 +01:00
|
|
|
if (remove(FileName) < 0) {
|
|
|
|
LOG_ERROR_STR(FileName);
|
|
|
|
return false;
|
|
|
|
}
|
2000-04-15 17:38:11 +02:00
|
|
|
}
|
2000-12-28 12:57:16 +01:00
|
|
|
else if (errno != ENOENT) {
|
2000-07-29 15:21:42 +02:00
|
|
|
LOG_ERROR_STR(FileName);
|
2000-12-28 12:57:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2000-04-15 17:38:11 +02:00
|
|
|
}
|
|
|
|
|
2012-09-30 13:05:14 +02:00
|
|
|
bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis, const char *IgnoreFiles[])
|
2001-02-11 14:53:44 +01:00
|
|
|
{
|
2012-09-30 13:05:14 +02:00
|
|
|
bool HasIgnoredFiles = false;
|
2004-12-19 16:33:34 +01:00
|
|
|
cReadDir d(DirName);
|
|
|
|
if (d.Ok()) {
|
2001-02-11 14:53:44 +01:00
|
|
|
bool empty = true;
|
|
|
|
struct dirent *e;
|
2004-12-19 16:33:34 +01:00
|
|
|
while ((e = d.Next()) != NULL) {
|
2012-02-17 14:02:13 +01:00
|
|
|
if (strcmp(e->d_name, "lost+found")) {
|
2008-02-15 14:57:48 +01:00
|
|
|
cString buffer = AddDirectory(DirName, e->d_name);
|
2001-02-11 14:53:44 +01:00
|
|
|
struct stat st;
|
|
|
|
if (stat(buffer, &st) == 0) {
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2012-09-30 13:05:14 +02:00
|
|
|
if (!RemoveEmptyDirectories(buffer, true, IgnoreFiles))
|
2001-02-11 14:53:44 +01:00
|
|
|
empty = false;
|
|
|
|
}
|
2012-09-30 13:05:14 +02:00
|
|
|
else if (RemoveThis && IgnoreFiles && StrInArray(IgnoreFiles, e->d_name))
|
|
|
|
HasIgnoredFiles = true;
|
2001-02-11 14:53:44 +01:00
|
|
|
else
|
|
|
|
empty = false;
|
|
|
|
}
|
|
|
|
else {
|
2008-02-15 14:57:48 +01:00
|
|
|
LOG_ERROR_STR(*buffer);
|
2006-08-12 13:32:06 +02:00
|
|
|
empty = false;
|
2001-02-11 14:53:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (RemoveThis && empty) {
|
2012-09-30 13:05:14 +02:00
|
|
|
if (HasIgnoredFiles) {
|
|
|
|
while (*IgnoreFiles) {
|
|
|
|
cString buffer = AddDirectory(DirName, *IgnoreFiles);
|
|
|
|
if (access(buffer, F_OK) == 0) {
|
|
|
|
dsyslog("removing %s", *buffer);
|
|
|
|
if (remove(buffer) < 0) {
|
|
|
|
LOG_ERROR_STR(*buffer);
|
|
|
|
return false;
|
2012-08-21 10:42:01 +02:00
|
|
|
}
|
|
|
|
}
|
2012-09-30 13:05:14 +02:00
|
|
|
IgnoreFiles++;
|
|
|
|
}
|
2012-08-21 10:42:01 +02:00
|
|
|
}
|
2002-05-13 16:35:49 +02:00
|
|
|
dsyslog("removing %s", DirName);
|
2001-02-11 14:53:44 +01:00
|
|
|
if (remove(DirName) < 0) {
|
|
|
|
LOG_ERROR_STR(DirName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LOG_ERROR_STR(DirName);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-12-18 10:41:26 +01:00
|
|
|
int DirSizeMB(const char *DirName)
|
|
|
|
{
|
|
|
|
cReadDir d(DirName);
|
|
|
|
if (d.Ok()) {
|
|
|
|
int size = 0;
|
|
|
|
struct dirent *e;
|
|
|
|
while (size >= 0 && (e = d.Next()) != NULL) {
|
2012-02-17 14:02:13 +01:00
|
|
|
cString buffer = AddDirectory(DirName, e->d_name);
|
|
|
|
struct stat st;
|
|
|
|
if (stat(buffer, &st) == 0) {
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
int n = DirSizeMB(buffer);
|
|
|
|
if (n >= 0)
|
|
|
|
size += n;
|
2005-12-18 10:41:26 +01:00
|
|
|
else
|
2012-02-17 14:02:13 +01:00
|
|
|
size = -1;
|
2005-12-18 10:41:26 +01:00
|
|
|
}
|
2012-02-17 14:02:13 +01:00
|
|
|
else
|
|
|
|
size += st.st_size / MEGABYTE(1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG_ERROR_STR(*buffer);
|
|
|
|
size = -1;
|
2005-12-18 10:41:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LOG_ERROR_STR(DirName);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-01-13 15:36:31 +01:00
|
|
|
char *ReadLink(const char *FileName)
|
|
|
|
{
|
2006-06-17 09:48:50 +02:00
|
|
|
if (!FileName)
|
|
|
|
return NULL;
|
|
|
|
char *TargetName = canonicalize_file_name(FileName);
|
|
|
|
if (!TargetName) {
|
|
|
|
if (errno == ENOENT) // file doesn't exist
|
|
|
|
TargetName = strdup(FileName);
|
2002-02-02 13:44:24 +01:00
|
|
|
else // some other error occurred
|
2001-01-13 15:36:31 +01:00
|
|
|
LOG_ERROR_STR(FileName);
|
|
|
|
}
|
2006-06-17 09:48:50 +02:00
|
|
|
return TargetName;
|
2001-01-13 15:36:31 +01:00
|
|
|
}
|
|
|
|
|
2001-08-11 11:15:41 +02:00
|
|
|
bool SpinUpDisk(const char *FileName)
|
|
|
|
{
|
|
|
|
for (int n = 0; n < 10; n++) {
|
2008-02-15 14:57:48 +01:00
|
|
|
cString buf;
|
2001-08-11 11:15:41 +02:00
|
|
|
if (DirectoryOk(FileName))
|
2008-02-15 14:57:48 +01:00
|
|
|
buf = cString::sprintf("%s/vdr-%06d", *FileName ? FileName : ".", n);
|
2001-08-11 11:15:41 +02:00
|
|
|
else
|
2008-02-15 14:57:48 +01:00
|
|
|
buf = cString::sprintf("%s.vdr-%06d", FileName, n);
|
2001-08-11 11:15:41 +02:00
|
|
|
if (access(buf, F_OK) != 0) { // the file does not exist
|
|
|
|
timeval tp1, tp2;
|
|
|
|
gettimeofday(&tp1, NULL);
|
2005-08-06 09:56:08 +02:00
|
|
|
int f = open(buf, O_WRONLY | O_CREAT, DEFFILEMODE);
|
2001-08-11 11:15:41 +02:00
|
|
|
// O_SYNC doesn't work on all file systems
|
|
|
|
if (f >= 0) {
|
2005-02-19 13:51:44 +01:00
|
|
|
if (fdatasync(f) < 0)
|
2008-02-15 14:57:48 +01:00
|
|
|
LOG_ERROR_STR(*buf);
|
2001-08-11 11:15:41 +02:00
|
|
|
close(f);
|
|
|
|
remove(buf);
|
|
|
|
gettimeofday(&tp2, NULL);
|
|
|
|
double seconds = (((long long)tp2.tv_sec * 1000000 + tp2.tv_usec) - ((long long)tp1.tv_sec * 1000000 + tp1.tv_usec)) / 1000000.0;
|
|
|
|
if (seconds > 0.5)
|
2005-11-04 14:22:04 +01:00
|
|
|
dsyslog("SpinUpDisk took %.2f seconds", seconds);
|
2001-08-11 11:15:41 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
2008-02-15 14:57:48 +01:00
|
|
|
LOG_ERROR_STR(*buf);
|
2001-08-11 11:15:41 +02:00
|
|
|
}
|
|
|
|
}
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: SpinUpDisk failed");
|
2001-08-11 11:15:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-09-25 13:49:31 +02:00
|
|
|
void TouchFile(const char *FileName)
|
|
|
|
{
|
|
|
|
if (utime(FileName, NULL) == -1 && errno != ENOENT)
|
|
|
|
LOG_ERROR_STR(FileName);
|
|
|
|
}
|
|
|
|
|
2004-06-13 20:26:51 +02:00
|
|
|
time_t LastModifiedTime(const char *FileName)
|
|
|
|
{
|
|
|
|
struct stat fs;
|
|
|
|
if (stat(FileName, &fs) == 0)
|
|
|
|
return fs.st_mtime;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-19 11:50:20 +01:00
|
|
|
off_t FileSize(const char *FileName)
|
|
|
|
{
|
|
|
|
struct stat fs;
|
|
|
|
if (stat(FileName, &fs) == 0)
|
|
|
|
return fs.st_size;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-12-19 18:08:09 +01:00
|
|
|
// --- cTimeMs ---------------------------------------------------------------
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
cTimeMs::cTimeMs(int Ms)
|
2004-12-19 18:08:09 +01:00
|
|
|
{
|
2011-08-15 14:13:42 +02:00
|
|
|
if (Ms >= 0)
|
|
|
|
Set(Ms);
|
|
|
|
else
|
|
|
|
begin = 0;
|
2004-12-19 18:08:09 +01:00
|
|
|
}
|
|
|
|
|
2006-12-02 11:30:19 +01:00
|
|
|
uint64_t cTimeMs::Now(void)
|
2005-01-04 11:06:45 +01:00
|
|
|
{
|
2007-12-27 15:58:18 +01:00
|
|
|
#if _POSIX_TIMERS > 0 && defined(_POSIX_MONOTONIC_CLOCK)
|
|
|
|
#define MIN_RESOLUTION 5 // ms
|
|
|
|
static bool initialized = false;
|
|
|
|
static bool monotonic = false;
|
|
|
|
struct timespec tp;
|
|
|
|
if (!initialized) {
|
|
|
|
// check if monotonic timer is available and provides enough accurate resolution:
|
|
|
|
if (clock_getres(CLOCK_MONOTONIC, &tp) == 0) {
|
|
|
|
long Resolution = tp.tv_nsec;
|
|
|
|
// require a minimum resolution:
|
|
|
|
if (tp.tv_sec == 0 && tp.tv_nsec <= MIN_RESOLUTION * 1000000) {
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
|
|
|
|
dsyslog("cTimeMs: using monotonic clock (resolution is %ld ns)", Resolution);
|
|
|
|
monotonic = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("cTimeMs: clock_gettime(CLOCK_MONOTONIC) failed");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dsyslog("cTimeMs: not using monotonic clock - resolution is too bad (%ld s %ld ns)", tp.tv_sec, tp.tv_nsec);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("cTimeMs: clock_getres(CLOCK_MONOTONIC) failed");
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
if (monotonic) {
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
|
|
|
|
return (uint64_t(tp.tv_sec)) * 1000 + tp.tv_nsec / 1000000;
|
|
|
|
esyslog("cTimeMs: clock_gettime(CLOCK_MONOTONIC) failed");
|
|
|
|
monotonic = false;
|
|
|
|
// fall back to gettimeofday()
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# warning Posix monotonic clock not available
|
|
|
|
#endif
|
2005-01-04 11:06:45 +01:00
|
|
|
struct timeval t;
|
|
|
|
if (gettimeofday(&t, NULL) == 0)
|
2006-12-02 11:30:19 +01:00
|
|
|
return (uint64_t(t.tv_sec)) * 1000 + t.tv_usec / 1000;
|
2005-01-04 11:06:45 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-19 18:08:09 +01:00
|
|
|
void cTimeMs::Set(int Ms)
|
|
|
|
{
|
|
|
|
begin = Now() + Ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cTimeMs::TimedOut(void)
|
|
|
|
{
|
|
|
|
return Now() >= begin;
|
|
|
|
}
|
|
|
|
|
2006-12-02 11:30:19 +01:00
|
|
|
uint64_t cTimeMs::Elapsed(void)
|
2004-12-19 18:08:09 +01:00
|
|
|
{
|
|
|
|
return Now() - begin;
|
|
|
|
}
|
|
|
|
|
2007-06-10 13:02:43 +02:00
|
|
|
// --- UTF-8 support ---------------------------------------------------------
|
|
|
|
|
|
|
|
static uint SystemToUtf8[128] = { 0 };
|
|
|
|
|
|
|
|
int Utf8CharLen(const char *s)
|
|
|
|
{
|
|
|
|
if (cCharSetConv::SystemCharacterTable())
|
|
|
|
return 1;
|
|
|
|
#define MT(s, m, v) ((*(s) & (m)) == (v)) // Mask Test
|
|
|
|
if (MT(s, 0xE0, 0xC0) && MT(s + 1, 0xC0, 0x80))
|
|
|
|
return 2;
|
|
|
|
if (MT(s, 0xF0, 0xE0) && MT(s + 1, 0xC0, 0x80) && MT(s + 2, 0xC0, 0x80))
|
|
|
|
return 3;
|
|
|
|
if (MT(s, 0xF8, 0xF0) && MT(s + 1, 0xC0, 0x80) && MT(s + 2, 0xC0, 0x80) && MT(s + 3, 0xC0, 0x80))
|
|
|
|
return 4;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint Utf8CharGet(const char *s, int Length)
|
|
|
|
{
|
|
|
|
if (cCharSetConv::SystemCharacterTable())
|
|
|
|
return (uchar)*s < 128 ? *s : SystemToUtf8[(uchar)*s - 128];
|
|
|
|
if (!Length)
|
|
|
|
Length = Utf8CharLen(s);
|
|
|
|
switch (Length) {
|
2008-02-29 13:23:02 +01:00
|
|
|
case 2: return ((*s & 0x1F) << 6) | (*(s + 1) & 0x3F);
|
|
|
|
case 3: return ((*s & 0x0F) << 12) | ((*(s + 1) & 0x3F) << 6) | (*(s + 2) & 0x3F);
|
|
|
|
case 4: return ((*s & 0x07) << 18) | ((*(s + 1) & 0x3F) << 12) | ((*(s + 2) & 0x3F) << 6) | (*(s + 3) & 0x3F);
|
2009-12-06 12:57:45 +01:00
|
|
|
default: ;
|
2007-06-10 13:02:43 +02:00
|
|
|
}
|
|
|
|
return *s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Utf8CharSet(uint c, char *s)
|
|
|
|
{
|
|
|
|
if (c < 0x80 || cCharSetConv::SystemCharacterTable()) {
|
|
|
|
if (s)
|
|
|
|
*s = c;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (c < 0x800) {
|
|
|
|
if (s) {
|
|
|
|
*s++ = ((c >> 6) & 0x1F) | 0xC0;
|
|
|
|
*s = (c & 0x3F) | 0x80;
|
|
|
|
}
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (c < 0x10000) {
|
|
|
|
if (s) {
|
|
|
|
*s++ = ((c >> 12) & 0x0F) | 0xE0;
|
|
|
|
*s++ = ((c >> 6) & 0x3F) | 0x80;
|
|
|
|
*s = (c & 0x3F) | 0x80;
|
|
|
|
}
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
if (c < 0x110000) {
|
|
|
|
if (s) {
|
|
|
|
*s++ = ((c >> 18) & 0x07) | 0xF0;
|
|
|
|
*s++ = ((c >> 12) & 0x3F) | 0x80;
|
|
|
|
*s++ = ((c >> 6) & 0x3F) | 0x80;
|
|
|
|
*s = (c & 0x3F) | 0x80;
|
|
|
|
}
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
return 0; // can't convert to UTF-8
|
|
|
|
}
|
|
|
|
|
|
|
|
int Utf8SymChars(const char *s, int Symbols)
|
|
|
|
{
|
|
|
|
if (cCharSetConv::SystemCharacterTable())
|
|
|
|
return Symbols;
|
|
|
|
int n = 0;
|
|
|
|
while (*s && Symbols--) {
|
|
|
|
int sl = Utf8CharLen(s);
|
|
|
|
s += sl;
|
|
|
|
n += sl;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-06-16 09:05:22 +02:00
|
|
|
int Utf8StrLen(const char *s)
|
2007-06-15 12:24:06 +02:00
|
|
|
{
|
|
|
|
if (cCharSetConv::SystemCharacterTable())
|
|
|
|
return strlen(s);
|
|
|
|
int n = 0;
|
|
|
|
while (*s) {
|
|
|
|
s += Utf8CharLen(s);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-06-16 10:41:21 +02:00
|
|
|
char *Utf8Strn0Cpy(char *Dest, const char *Src, int n)
|
|
|
|
{
|
|
|
|
if (cCharSetConv::SystemCharacterTable())
|
|
|
|
return strn0cpy(Dest, Src, n);
|
|
|
|
char *d = Dest;
|
|
|
|
while (*Src) {
|
|
|
|
int sl = Utf8CharLen(Src);
|
|
|
|
n -= sl;
|
|
|
|
if (n > 0) {
|
|
|
|
while (sl--)
|
|
|
|
*d++ = *Src++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*d = 0;
|
|
|
|
return Dest;
|
|
|
|
}
|
|
|
|
|
2007-06-10 13:02:43 +02:00
|
|
|
int Utf8ToArray(const char *s, uint *a, int Size)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
while (*s && --Size > 0) {
|
|
|
|
if (cCharSetConv::SystemCharacterTable())
|
2007-07-20 13:56:38 +02:00
|
|
|
*a++ = (uchar)(*s++);
|
2007-06-10 13:02:43 +02:00
|
|
|
else {
|
|
|
|
int sl = Utf8CharLen(s);
|
|
|
|
*a++ = Utf8CharGet(s, sl);
|
|
|
|
s += sl;
|
|
|
|
}
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (Size > 0)
|
|
|
|
*a = 0;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Utf8FromArray(const uint *a, char *s, int Size, int Max)
|
|
|
|
{
|
|
|
|
int NumChars = 0;
|
|
|
|
int NumSyms = 0;
|
|
|
|
while (*a && NumChars < Size) {
|
|
|
|
if (Max >= 0 && NumSyms++ >= Max)
|
|
|
|
break;
|
|
|
|
if (cCharSetConv::SystemCharacterTable()) {
|
|
|
|
*s++ = *a++;
|
|
|
|
NumChars++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int sl = Utf8CharSet(*a);
|
|
|
|
if (NumChars + sl <= Size) {
|
|
|
|
Utf8CharSet(*a, s);
|
|
|
|
a++;
|
|
|
|
s += sl;
|
|
|
|
NumChars += sl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NumChars < Size)
|
|
|
|
*s = 0;
|
|
|
|
return NumChars;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- cCharSetConv ----------------------------------------------------------
|
|
|
|
|
|
|
|
char *cCharSetConv::systemCharacterTable = NULL;
|
|
|
|
|
|
|
|
cCharSetConv::cCharSetConv(const char *FromCode, const char *ToCode)
|
|
|
|
{
|
|
|
|
if (!FromCode)
|
2009-12-23 15:25:05 +01:00
|
|
|
FromCode = systemCharacterTable ? systemCharacterTable : "UTF-8";
|
2007-06-10 13:02:43 +02:00
|
|
|
if (!ToCode)
|
|
|
|
ToCode = "UTF-8";
|
2009-12-23 15:25:05 +01:00
|
|
|
cd = iconv_open(ToCode, FromCode);
|
2007-06-10 13:02:43 +02:00
|
|
|
result = NULL;
|
|
|
|
length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cCharSetConv::~cCharSetConv()
|
|
|
|
{
|
|
|
|
free(result);
|
2012-12-03 09:34:12 +01:00
|
|
|
if (cd != (iconv_t)-1)
|
|
|
|
iconv_close(cd);
|
2007-06-10 13:02:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cCharSetConv::SetSystemCharacterTable(const char *CharacterTable)
|
|
|
|
{
|
|
|
|
free(systemCharacterTable);
|
|
|
|
systemCharacterTable = NULL;
|
2007-06-15 13:25:35 +02:00
|
|
|
if (!strcasestr(CharacterTable, "UTF-8")) {
|
2007-06-10 13:02:43 +02:00
|
|
|
// Set up a map for the character values 128...255:
|
|
|
|
char buf[129];
|
|
|
|
for (int i = 0; i < 128; i++)
|
|
|
|
buf[i] = i + 128;
|
2007-07-20 12:31:38 +02:00
|
|
|
buf[128] = 0;
|
2007-06-10 13:02:43 +02:00
|
|
|
cCharSetConv csc(CharacterTable);
|
|
|
|
const char *s = csc.Convert(buf);
|
|
|
|
int i = 0;
|
|
|
|
while (*s) {
|
|
|
|
int sl = Utf8CharLen(s);
|
|
|
|
SystemToUtf8[i] = Utf8CharGet(s, sl);
|
|
|
|
s += sl;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
systemCharacterTable = strdup(CharacterTable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *cCharSetConv::Convert(const char *From, char *To, size_t ToLength)
|
|
|
|
{
|
2007-06-15 13:01:18 +02:00
|
|
|
if (cd != (iconv_t)-1 && From && *From) {
|
2007-06-10 13:02:43 +02:00
|
|
|
char *FromPtr = (char *)From;
|
|
|
|
size_t FromLength = strlen(From);
|
|
|
|
char *ToPtr = To;
|
|
|
|
if (!ToPtr) {
|
2011-02-25 15:25:42 +01:00
|
|
|
int NewLength = max(length, FromLength * 2); // some reserve to avoid later reallocations
|
|
|
|
if (char *NewBuffer = (char *)realloc(result, NewLength)) {
|
|
|
|
length = NewLength;
|
|
|
|
result = NewBuffer;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
esyslog("ERROR: out of memory");
|
|
|
|
return From;
|
|
|
|
}
|
2007-06-10 13:02:43 +02:00
|
|
|
ToPtr = result;
|
|
|
|
ToLength = length;
|
|
|
|
}
|
|
|
|
else if (!ToLength)
|
|
|
|
return From; // can't convert into a zero sized buffer
|
|
|
|
ToLength--; // save space for terminating 0
|
|
|
|
char *Converted = ToPtr;
|
|
|
|
while (FromLength > 0) {
|
|
|
|
if (iconv(cd, &FromPtr, &FromLength, &ToPtr, &ToLength) == size_t(-1)) {
|
|
|
|
if (errno == E2BIG || errno == EILSEQ && ToLength < 1) {
|
|
|
|
if (To)
|
|
|
|
break; // caller provided a fixed size buffer, but it was too small
|
|
|
|
// The result buffer is too small, so increase it:
|
|
|
|
size_t d = ToPtr - result;
|
|
|
|
size_t r = length / 2;
|
2011-02-25 15:25:42 +01:00
|
|
|
int NewLength = length + r;
|
|
|
|
if (char *NewBuffer = (char *)realloc(result, NewLength)) {
|
|
|
|
length = NewLength;
|
|
|
|
Converted = result = NewBuffer;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
esyslog("ERROR: out of memory");
|
|
|
|
return From;
|
|
|
|
}
|
2007-06-10 13:02:43 +02:00
|
|
|
ToLength += r;
|
|
|
|
ToPtr = result + d;
|
|
|
|
}
|
|
|
|
if (errno == EILSEQ) {
|
|
|
|
// A character can't be converted, so mark it with '?' and proceed:
|
|
|
|
FromPtr++;
|
|
|
|
FromLength--;
|
|
|
|
*ToPtr++ = '?';
|
|
|
|
ToLength--;
|
|
|
|
}
|
|
|
|
else if (errno != E2BIG)
|
|
|
|
return From; // unknown error, return original string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*ToPtr = 0;
|
|
|
|
return Converted;
|
|
|
|
}
|
|
|
|
return From;
|
|
|
|
}
|
|
|
|
|
2004-12-26 12:45:22 +01:00
|
|
|
// --- cString ---------------------------------------------------------------
|
2004-12-19 16:33:34 +01:00
|
|
|
|
2005-02-05 10:12:14 +01:00
|
|
|
cString::cString(const char *S, bool TakePointer)
|
2004-12-19 16:33:34 +01:00
|
|
|
{
|
2005-02-05 10:12:14 +01:00
|
|
|
s = TakePointer ? (char *)S : S ? strdup(S) : NULL;
|
2004-12-19 16:33:34 +01:00
|
|
|
}
|
|
|
|
|
2005-11-26 14:16:02 +01:00
|
|
|
cString::cString(const cString &String)
|
|
|
|
{
|
|
|
|
s = String.s ? strdup(String.s) : NULL;
|
|
|
|
}
|
|
|
|
|
2004-12-26 12:45:22 +01:00
|
|
|
cString::~cString()
|
2004-12-19 16:33:34 +01:00
|
|
|
{
|
2004-12-26 12:45:22 +01:00
|
|
|
free(s);
|
2004-12-19 16:33:34 +01:00
|
|
|
}
|
|
|
|
|
2004-12-26 12:45:22 +01:00
|
|
|
cString &cString::operator=(const cString &String)
|
2004-12-19 16:33:34 +01:00
|
|
|
{
|
2005-11-26 14:16:02 +01:00
|
|
|
if (this == &String)
|
|
|
|
return *this;
|
2005-11-04 14:27:51 +01:00
|
|
|
free(s);
|
2004-12-26 12:45:22 +01:00
|
|
|
s = String.s ? strdup(String.s) : NULL;
|
|
|
|
return *this;
|
2004-12-19 16:33:34 +01:00
|
|
|
}
|
|
|
|
|
2010-10-24 13:08:55 +02:00
|
|
|
cString &cString::operator=(const char *String)
|
|
|
|
{
|
2011-05-15 11:46:30 +02:00
|
|
|
if (s == String)
|
|
|
|
return *this;
|
2010-10-24 13:08:55 +02:00
|
|
|
free(s);
|
|
|
|
s = String ? strdup(String) : NULL;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-01-13 11:26:30 +01:00
|
|
|
cString &cString::Truncate(int Index)
|
|
|
|
{
|
|
|
|
int l = strlen(s);
|
|
|
|
if (Index < 0)
|
|
|
|
Index = l + Index;
|
|
|
|
if (Index >= 0 && Index < l)
|
|
|
|
s[Index] = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2005-08-27 16:42:28 +02:00
|
|
|
cString cString::sprintf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
char *buffer;
|
2008-02-15 14:57:48 +01:00
|
|
|
if (!fmt || vasprintf(&buffer, fmt, ap) < 0) {
|
|
|
|
esyslog("error in vasprintf('%s', ...)", fmt);
|
|
|
|
buffer = strdup("???");
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
return cString(buffer, true);
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:23:56 +02:00
|
|
|
cString cString::vsprintf(const char *fmt, va_list &ap)
|
2008-02-15 14:57:48 +01:00
|
|
|
{
|
|
|
|
char *buffer;
|
|
|
|
if (!fmt || vasprintf(&buffer, fmt, ap) < 0) {
|
|
|
|
esyslog("error in vasprintf('%s', ...)", fmt);
|
|
|
|
buffer = strdup("???");
|
|
|
|
}
|
2005-08-27 16:42:28 +02:00
|
|
|
return cString(buffer, true);
|
|
|
|
}
|
|
|
|
|
2004-12-26 12:45:22 +01:00
|
|
|
cString WeekDayName(int WeekDay)
|
2002-02-17 13:05:05 +01:00
|
|
|
{
|
2007-06-10 13:02:43 +02:00
|
|
|
char buffer[16];
|
2006-04-21 15:15:18 +02:00
|
|
|
WeekDay = WeekDay == 0 ? 6 : WeekDay - 1; // we start with Monday==0!
|
2002-02-17 13:05:05 +01:00
|
|
|
if (0 <= WeekDay && WeekDay <= 6) {
|
2007-08-11 12:39:06 +02:00
|
|
|
// TRANSLATORS: abbreviated weekdays, beginning with monday (must all be 3 letters!)
|
2002-02-17 13:05:05 +01:00
|
|
|
const char *day = tr("MonTueWedThuFriSatSun");
|
2007-06-10 13:02:43 +02:00
|
|
|
day += Utf8SymChars(day, WeekDay * 3);
|
|
|
|
strn0cpy(buffer, day, min(Utf8SymChars(day, 3) + 1, int(sizeof(buffer))));
|
2004-12-26 12:45:22 +01:00
|
|
|
return buffer;
|
2002-02-17 13:05:05 +01:00
|
|
|
}
|
|
|
|
else
|
2004-12-26 12:45:22 +01:00
|
|
|
return "???";
|
2002-02-17 13:05:05 +01:00
|
|
|
}
|
|
|
|
|
2004-12-26 12:45:22 +01:00
|
|
|
cString WeekDayName(time_t t)
|
|
|
|
{
|
|
|
|
struct tm tm_r;
|
|
|
|
return WeekDayName(localtime_r(&t, &tm_r)->tm_wday);
|
|
|
|
}
|
2004-05-22 13:23:22 +02:00
|
|
|
|
2007-06-23 13:40:04 +02:00
|
|
|
cString WeekDayNameFull(int WeekDay)
|
|
|
|
{
|
|
|
|
WeekDay = WeekDay == 0 ? 6 : WeekDay - 1; // we start with Monday==0!
|
|
|
|
switch (WeekDay) {
|
|
|
|
case 0: return tr("Monday");
|
|
|
|
case 1: return tr("Tuesday");
|
|
|
|
case 2: return tr("Wednesday");
|
|
|
|
case 3: return tr("Thursday");
|
|
|
|
case 4: return tr("Friday");
|
|
|
|
case 5: return tr("Saturday");
|
|
|
|
case 6: return tr("Sunday");
|
2009-12-06 12:57:45 +01:00
|
|
|
default: return "???";
|
2007-06-23 13:40:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cString WeekDayNameFull(time_t t)
|
|
|
|
{
|
|
|
|
struct tm tm_r;
|
|
|
|
return WeekDayNameFull(localtime_r(&t, &tm_r)->tm_wday);
|
|
|
|
}
|
|
|
|
|
2004-12-26 12:45:22 +01:00
|
|
|
cString DayDateTime(time_t t)
|
2001-08-25 13:27:26 +02:00
|
|
|
{
|
2004-12-26 12:45:22 +01:00
|
|
|
char buffer[32];
|
2001-08-25 13:27:26 +02:00
|
|
|
if (t == 0)
|
|
|
|
time(&t);
|
2001-10-19 13:22:24 +02:00
|
|
|
struct tm tm_r;
|
|
|
|
tm *tm = localtime_r(&t, &tm_r);
|
2007-11-03 15:36:41 +01:00
|
|
|
snprintf(buffer, sizeof(buffer), "%s %02d.%02d. %02d:%02d", *WeekDayName(tm->tm_wday), tm->tm_mday, tm->tm_mon + 1, tm->tm_hour, tm->tm_min);
|
2004-12-26 12:45:22 +01:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
cString TimeToString(time_t t)
|
|
|
|
{
|
|
|
|
char buffer[32];
|
|
|
|
if (ctime_r(&t, buffer)) {
|
|
|
|
buffer[strlen(buffer) - 1] = 0; // strip trailing newline
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
return "???";
|
2004-12-19 16:33:34 +01:00
|
|
|
}
|
|
|
|
|
2005-05-16 14:45:11 +02:00
|
|
|
cString DateString(time_t t)
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
struct tm tm_r;
|
|
|
|
tm *tm = localtime_r(&t, &tm_r);
|
|
|
|
char *p = stpcpy(buf, WeekDayName(tm->tm_wday));
|
|
|
|
*p++ = ' ';
|
|
|
|
strftime(p, sizeof(buf) - (p - buf), "%d.%m.%Y", tm);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2012-05-12 14:20:41 +02:00
|
|
|
cString ShortDateString(time_t t)
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
struct tm tm_r;
|
|
|
|
tm *tm = localtime_r(&t, &tm_r);
|
|
|
|
strftime(buf, sizeof(buf), "%d.%m.%y", tm);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2005-05-16 14:45:11 +02:00
|
|
|
cString TimeString(time_t t)
|
|
|
|
{
|
|
|
|
char buf[25];
|
|
|
|
struct tm tm_r;
|
|
|
|
strftime(buf, sizeof(buf), "%R", localtime_r(&t, &tm_r));
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2005-12-29 11:24:02 +01:00
|
|
|
// --- RgbToJpeg -------------------------------------------------------------
|
|
|
|
|
|
|
|
#define JPEGCOMPRESSMEM 500000
|
|
|
|
|
|
|
|
struct tJpegCompressData {
|
|
|
|
int size;
|
|
|
|
uchar *mem;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void JpegCompressInitDestination(j_compress_ptr cinfo)
|
|
|
|
{
|
|
|
|
tJpegCompressData *jcd = (tJpegCompressData *)cinfo->client_data;
|
|
|
|
if (jcd) {
|
|
|
|
cinfo->dest->free_in_buffer = jcd->size = JPEGCOMPRESSMEM;
|
|
|
|
cinfo->dest->next_output_byte = jcd->mem = MALLOC(uchar, jcd->size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static boolean JpegCompressEmptyOutputBuffer(j_compress_ptr cinfo)
|
|
|
|
{
|
|
|
|
tJpegCompressData *jcd = (tJpegCompressData *)cinfo->client_data;
|
|
|
|
if (jcd) {
|
|
|
|
int Used = jcd->size;
|
2011-02-25 15:25:42 +01:00
|
|
|
int NewSize = jcd->size + JPEGCOMPRESSMEM;
|
|
|
|
if (uchar *NewBuffer = (uchar *)realloc(jcd->mem, NewSize)) {
|
|
|
|
jcd->size = NewSize;
|
|
|
|
jcd->mem = NewBuffer;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
esyslog("ERROR: out of memory");
|
|
|
|
return false;
|
|
|
|
}
|
2005-12-29 11:24:02 +01:00
|
|
|
if (jcd->mem) {
|
|
|
|
cinfo->dest->next_output_byte = jcd->mem + Used;
|
|
|
|
cinfo->dest->free_in_buffer = jcd->size - Used;
|
2011-02-25 15:25:42 +01:00
|
|
|
return true;
|
2005-12-29 11:24:02 +01:00
|
|
|
}
|
|
|
|
}
|
2011-02-25 15:25:42 +01:00
|
|
|
return false;
|
2005-12-29 11:24:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void JpegCompressTermDestination(j_compress_ptr cinfo)
|
|
|
|
{
|
|
|
|
tJpegCompressData *jcd = (tJpegCompressData *)cinfo->client_data;
|
|
|
|
if (jcd) {
|
|
|
|
int Used = cinfo->dest->next_output_byte - jcd->mem;
|
|
|
|
if (Used < jcd->size) {
|
2011-02-25 15:25:42 +01:00
|
|
|
if (uchar *NewBuffer = (uchar *)realloc(jcd->mem, Used)) {
|
|
|
|
jcd->size = Used;
|
|
|
|
jcd->mem = NewBuffer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("ERROR: out of memory");
|
2005-12-29 11:24:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uchar *RgbToJpeg(uchar *Mem, int Width, int Height, int &Size, int Quality)
|
|
|
|
{
|
|
|
|
if (Quality < 0)
|
|
|
|
Quality = 0;
|
|
|
|
else if (Quality > 100)
|
|
|
|
Quality = 100;
|
2006-01-08 11:44:37 +01:00
|
|
|
|
2005-12-29 11:24:02 +01:00
|
|
|
jpeg_destination_mgr jdm;
|
|
|
|
|
|
|
|
jdm.init_destination = JpegCompressInitDestination;
|
|
|
|
jdm.empty_output_buffer = JpegCompressEmptyOutputBuffer;
|
|
|
|
jdm.term_destination = JpegCompressTermDestination;
|
|
|
|
|
|
|
|
struct jpeg_compress_struct cinfo;
|
|
|
|
struct jpeg_error_mgr jerr;
|
|
|
|
cinfo.err = jpeg_std_error(&jerr);
|
|
|
|
jpeg_create_compress(&cinfo);
|
|
|
|
cinfo.dest = &jdm;
|
|
|
|
tJpegCompressData jcd;
|
|
|
|
cinfo.client_data = &jcd;
|
|
|
|
cinfo.image_width = Width;
|
|
|
|
cinfo.image_height = Height;
|
|
|
|
cinfo.input_components = 3;
|
|
|
|
cinfo.in_color_space = JCS_RGB;
|
|
|
|
|
|
|
|
jpeg_set_defaults(&cinfo);
|
|
|
|
jpeg_set_quality(&cinfo, Quality, true);
|
|
|
|
jpeg_start_compress(&cinfo, true);
|
|
|
|
|
|
|
|
int rs = Width * 3;
|
|
|
|
JSAMPROW rp[Height];
|
|
|
|
for (int k = 0; k < Height; k++)
|
|
|
|
rp[k] = &Mem[rs * k];
|
|
|
|
jpeg_write_scanlines(&cinfo, rp, Height);
|
|
|
|
jpeg_finish_compress(&cinfo);
|
|
|
|
jpeg_destroy_compress(&cinfo);
|
|
|
|
|
|
|
|
Size = jcd.size;
|
|
|
|
return jcd.mem;
|
|
|
|
}
|
|
|
|
|
2005-12-29 16:02:37 +01:00
|
|
|
// --- cBase64Encoder --------------------------------------------------------
|
|
|
|
|
|
|
|
const char *cBase64Encoder::b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
|
|
|
|
cBase64Encoder::cBase64Encoder(const uchar *Data, int Length, int MaxResult)
|
|
|
|
{
|
|
|
|
data = Data;
|
|
|
|
length = Length;
|
|
|
|
maxResult = MaxResult;
|
|
|
|
i = 0;
|
|
|
|
result = MALLOC(char, maxResult + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
cBase64Encoder::~cBase64Encoder()
|
|
|
|
{
|
|
|
|
free(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *cBase64Encoder::NextLine(void)
|
|
|
|
{
|
|
|
|
int r = 0;
|
|
|
|
while (i < length && r < maxResult - 3) {
|
|
|
|
result[r++] = b64[(data[i] >> 2) & 0x3F];
|
2008-03-05 17:25:44 +01:00
|
|
|
uchar c = (data[i] << 4) & 0x3F;
|
2005-12-29 16:02:37 +01:00
|
|
|
if (++i < length)
|
|
|
|
c |= (data[i] >> 4) & 0x0F;
|
|
|
|
result[r++] = b64[c];
|
|
|
|
if (i < length) {
|
|
|
|
c = (data[i] << 2) & 0x3F;
|
|
|
|
if (++i < length)
|
|
|
|
c |= (data[i] >> 6) & 0x03;
|
|
|
|
result[r++] = b64[c];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i++;
|
|
|
|
result[r++] = '=';
|
|
|
|
}
|
|
|
|
if (i < length) {
|
|
|
|
c = data[i] & 0x3F;
|
|
|
|
result[r++] = b64[c];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result[r++] = '=';
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (r > 0) {
|
|
|
|
result[r] = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-18 11:36:38 +02:00
|
|
|
// --- cBitStream ------------------------------------------------------------
|
|
|
|
|
|
|
|
int cBitStream::GetBit(void)
|
|
|
|
{
|
|
|
|
if (index >= length)
|
|
|
|
return 1;
|
|
|
|
int r = (data[index >> 3] >> (7 - (index & 7))) & 1;
|
|
|
|
++index;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t cBitStream::GetBits(int n)
|
|
|
|
{
|
|
|
|
uint32_t r = 0;
|
|
|
|
while (n--)
|
|
|
|
r |= GetBit() << n;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cBitStream::ByteAlign(void)
|
|
|
|
{
|
|
|
|
int n = index % 8;
|
|
|
|
if (n > 0)
|
|
|
|
SkipBits(8 - n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cBitStream::WordAlign(void)
|
|
|
|
{
|
|
|
|
int n = index % 16;
|
|
|
|
if (n > 0)
|
|
|
|
SkipBits(16 - n);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cBitStream::SetLength(int Length)
|
|
|
|
{
|
|
|
|
if (Length > length)
|
|
|
|
return false;
|
|
|
|
length = Length;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-12-19 16:33:34 +01:00
|
|
|
// --- cReadLine -------------------------------------------------------------
|
|
|
|
|
2005-11-04 17:18:33 +01:00
|
|
|
cReadLine::cReadLine(void)
|
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cReadLine::~cReadLine()
|
|
|
|
{
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
2004-12-19 16:33:34 +01:00
|
|
|
char *cReadLine::Read(FILE *f)
|
|
|
|
{
|
2005-11-04 17:18:33 +01:00
|
|
|
int n = getline(&buffer, &size, f);
|
|
|
|
if (n > 0) {
|
|
|
|
n--;
|
2006-03-19 12:28:16 +01:00
|
|
|
if (buffer[n] == '\n') {
|
2005-11-04 17:18:33 +01:00
|
|
|
buffer[n] = 0;
|
2006-03-19 12:28:16 +01:00
|
|
|
if (n > 0) {
|
|
|
|
n--;
|
|
|
|
if (buffer[n] == '\r')
|
|
|
|
buffer[n] = 0;
|
|
|
|
}
|
|
|
|
}
|
2004-12-19 16:33:34 +01:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
return NULL;
|
2001-08-25 13:27:26 +02:00
|
|
|
}
|
|
|
|
|
2002-08-16 09:22:29 +02:00
|
|
|
// --- cPoller ---------------------------------------------------------------
|
|
|
|
|
|
|
|
cPoller::cPoller(int FileHandle, bool Out)
|
|
|
|
{
|
|
|
|
numFileHandles = 0;
|
|
|
|
Add(FileHandle, Out);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cPoller::Add(int FileHandle, bool Out)
|
|
|
|
{
|
|
|
|
if (FileHandle >= 0) {
|
|
|
|
for (int i = 0; i < numFileHandles; i++) {
|
2004-11-21 14:37:36 +01:00
|
|
|
if (pfd[i].fd == FileHandle && pfd[i].events == (Out ? POLLOUT : POLLIN))
|
2002-08-16 09:22:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (numFileHandles < MaxPollFiles) {
|
|
|
|
pfd[numFileHandles].fd = FileHandle;
|
|
|
|
pfd[numFileHandles].events = Out ? POLLOUT : POLLIN;
|
2004-11-21 14:37:36 +01:00
|
|
|
pfd[numFileHandles].revents = 0;
|
2002-08-16 09:22:29 +02:00
|
|
|
numFileHandles++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
esyslog("ERROR: too many file handles in cPoller");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cPoller::Poll(int TimeoutMs)
|
|
|
|
{
|
|
|
|
if (numFileHandles) {
|
|
|
|
if (poll(pfd, numFileHandles, TimeoutMs) != 0)
|
|
|
|
return true; // returns true even in case of an error, to let the caller
|
|
|
|
// access the file and thus see the error code
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-12-19 16:33:34 +01:00
|
|
|
// --- cReadDir --------------------------------------------------------------
|
|
|
|
|
|
|
|
cReadDir::cReadDir(const char *Directory)
|
|
|
|
{
|
|
|
|
directory = opendir(Directory);
|
|
|
|
}
|
|
|
|
|
|
|
|
cReadDir::~cReadDir()
|
|
|
|
{
|
|
|
|
if (directory)
|
|
|
|
closedir(directory);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dirent *cReadDir::Next(void)
|
|
|
|
{
|
2012-02-17 14:02:13 +01:00
|
|
|
if (directory) {
|
|
|
|
while (readdir_r(directory, &u.d, &result) == 0 && result) {
|
|
|
|
if (strcmp(result->d_name, ".") && strcmp(result->d_name, ".."))
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2004-12-19 16:33:34 +01:00
|
|
|
}
|
|
|
|
|
2007-06-17 11:12:46 +02:00
|
|
|
// --- cStringList -----------------------------------------------------------
|
2007-06-10 13:02:43 +02:00
|
|
|
|
2007-06-17 11:12:46 +02:00
|
|
|
cStringList::~cStringList()
|
2007-06-10 13:02:43 +02:00
|
|
|
{
|
2007-06-17 11:12:46 +02:00
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
int cStringList::Find(const char *s) const
|
|
|
|
{
|
|
|
|
for (int i = 0; i < Size(); i++) {
|
|
|
|
if (!strcmp(s, At(i)))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
2007-06-10 13:02:43 +02:00
|
|
|
}
|
|
|
|
|
2007-06-17 11:12:46 +02:00
|
|
|
void cStringList::Clear(void)
|
2007-06-10 13:02:43 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < Size(); i++)
|
|
|
|
free(At(i));
|
2008-01-01 15:10:07 +01:00
|
|
|
cVector<char *>::Clear();
|
2007-06-10 13:02:43 +02:00
|
|
|
}
|
|
|
|
|
2007-06-17 11:12:46 +02:00
|
|
|
// --- cFileNameList ---------------------------------------------------------
|
|
|
|
|
|
|
|
// TODO better GetFileNames(const char *Directory, cStringList *List)?
|
2007-08-11 12:39:06 +02:00
|
|
|
cFileNameList::cFileNameList(const char *Directory, bool DirsOnly)
|
2007-06-17 11:12:46 +02:00
|
|
|
{
|
2007-08-11 12:39:06 +02:00
|
|
|
Load(Directory, DirsOnly);
|
2007-06-17 11:12:46 +02:00
|
|
|
}
|
|
|
|
|
2007-08-11 12:39:06 +02:00
|
|
|
bool cFileNameList::Load(const char *Directory, bool DirsOnly)
|
2007-06-10 13:02:43 +02:00
|
|
|
{
|
2007-06-17 11:12:46 +02:00
|
|
|
Clear();
|
2007-06-10 13:02:43 +02:00
|
|
|
if (Directory) {
|
|
|
|
cReadDir d(Directory);
|
|
|
|
struct dirent *e;
|
|
|
|
if (d.Ok()) {
|
|
|
|
while ((e = d.Next()) != NULL) {
|
2012-02-17 14:02:13 +01:00
|
|
|
if (DirsOnly) {
|
|
|
|
struct stat ds;
|
|
|
|
if (stat(AddDirectory(Directory, e->d_name), &ds) == 0) {
|
|
|
|
if (!S_ISDIR(ds.st_mode))
|
|
|
|
continue;
|
2007-08-11 12:39:06 +02:00
|
|
|
}
|
|
|
|
}
|
2012-02-17 14:02:13 +01:00
|
|
|
Append(strdup(e->d_name));
|
2007-06-10 13:02:43 +02:00
|
|
|
}
|
2007-06-17 11:12:46 +02:00
|
|
|
Sort();
|
2007-06-10 13:02:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LOG_ERROR_STR(Directory);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2000-09-17 08:23:46 +02:00
|
|
|
// --- cFile -----------------------------------------------------------------
|
|
|
|
|
|
|
|
bool cFile::files[FD_SETSIZE] = { false };
|
|
|
|
int cFile::maxFiles = 0;
|
|
|
|
|
|
|
|
cFile::cFile(void)
|
|
|
|
{
|
|
|
|
f = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cFile::~cFile()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cFile::Open(const char *FileName, int Flags, mode_t Mode)
|
|
|
|
{
|
|
|
|
if (!IsOpen())
|
|
|
|
return Open(open(FileName, Flags, Mode));
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: attempt to re-open %s", FileName);
|
2000-09-17 08:23:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cFile::Open(int FileDes)
|
|
|
|
{
|
|
|
|
if (FileDes >= 0) {
|
|
|
|
if (!IsOpen()) {
|
|
|
|
f = FileDes;
|
|
|
|
if (f >= 0) {
|
|
|
|
if (f < FD_SETSIZE) {
|
|
|
|
if (f >= maxFiles)
|
|
|
|
maxFiles = f + 1;
|
|
|
|
if (!files[f])
|
|
|
|
files[f] = true;
|
|
|
|
else
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: file descriptor %d already in files[]", f);
|
2000-09-17 08:23:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: file descriptor %d is larger than FD_SETSIZE (%d)", f, FD_SETSIZE);
|
2000-09-17 08:23:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: attempt to re-open file descriptor %d", FileDes);
|
2000-09-17 08:23:46 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cFile::Close(void)
|
|
|
|
{
|
|
|
|
if (f >= 0) {
|
|
|
|
close(f);
|
|
|
|
files[f] = false;
|
|
|
|
f = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cFile::Ready(bool Wait)
|
|
|
|
{
|
|
|
|
return f >= 0 && AnyFileReady(f, Wait ? 1000 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cFile::AnyFileReady(int FileDes, int TimeoutMs)
|
|
|
|
{
|
|
|
|
fd_set set;
|
|
|
|
FD_ZERO(&set);
|
|
|
|
for (int i = 0; i < maxFiles; i++) {
|
|
|
|
if (files[i])
|
|
|
|
FD_SET(i, &set);
|
|
|
|
}
|
|
|
|
if (0 <= FileDes && FileDes < FD_SETSIZE && !files[FileDes])
|
|
|
|
FD_SET(FileDes, &set); // in case we come in with an arbitrary descriptor
|
2000-09-17 15:36:33 +02:00
|
|
|
if (TimeoutMs == 0)
|
|
|
|
TimeoutMs = 10; // load gets too heavy with 0
|
2000-09-17 08:23:46 +02:00
|
|
|
struct timeval timeout;
|
|
|
|
timeout.tv_sec = TimeoutMs / 1000;
|
|
|
|
timeout.tv_usec = (TimeoutMs % 1000) * 1000;
|
|
|
|
return select(FD_SETSIZE, &set, NULL, NULL, &timeout) > 0 && (FileDes < 0 || FD_ISSET(FileDes, &set));
|
|
|
|
}
|
|
|
|
|
2000-10-08 09:25:20 +02:00
|
|
|
bool cFile::FileReady(int FileDes, int TimeoutMs)
|
|
|
|
{
|
|
|
|
fd_set set;
|
|
|
|
struct timeval timeout;
|
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(FileDes, &set);
|
2003-04-06 15:43:41 +02:00
|
|
|
if (TimeoutMs >= 0) {
|
|
|
|
if (TimeoutMs < 100)
|
|
|
|
TimeoutMs = 100;
|
|
|
|
timeout.tv_sec = TimeoutMs / 1000;
|
|
|
|
timeout.tv_usec = (TimeoutMs % 1000) * 1000;
|
|
|
|
}
|
|
|
|
return select(FD_SETSIZE, &set, NULL, NULL, (TimeoutMs >= 0) ? &timeout : NULL) > 0 && FD_ISSET(FileDes, &set);
|
2000-10-08 09:25:20 +02:00
|
|
|
}
|
|
|
|
|
2001-06-02 10:47:40 +02:00
|
|
|
bool cFile::FileReadyForWriting(int FileDes, int TimeoutMs)
|
|
|
|
{
|
|
|
|
fd_set set;
|
|
|
|
struct timeval timeout;
|
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(FileDes, &set);
|
|
|
|
if (TimeoutMs < 100)
|
|
|
|
TimeoutMs = 100;
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = TimeoutMs * 1000;
|
|
|
|
return select(FD_SETSIZE, NULL, &set, NULL, &timeout) > 0 && FD_ISSET(FileDes, &set);
|
|
|
|
}
|
|
|
|
|
2001-01-13 15:36:31 +01:00
|
|
|
// --- cSafeFile -------------------------------------------------------------
|
|
|
|
|
|
|
|
cSafeFile::cSafeFile(const char *FileName)
|
|
|
|
{
|
|
|
|
f = NULL;
|
|
|
|
fileName = ReadLink(FileName);
|
2002-08-11 13:32:23 +02:00
|
|
|
tempName = fileName ? MALLOC(char, strlen(fileName) + 5) : NULL;
|
2001-01-13 15:36:31 +01:00
|
|
|
if (tempName)
|
|
|
|
strcat(strcpy(tempName, fileName), ".$$$");
|
|
|
|
}
|
|
|
|
|
|
|
|
cSafeFile::~cSafeFile()
|
|
|
|
{
|
|
|
|
if (f)
|
|
|
|
fclose(f);
|
|
|
|
unlink(tempName);
|
2002-08-11 13:32:23 +02:00
|
|
|
free(fileName);
|
|
|
|
free(tempName);
|
2001-01-13 15:36:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cSafeFile::Open(void)
|
|
|
|
{
|
|
|
|
if (!f && fileName && tempName) {
|
|
|
|
f = fopen(tempName, "w");
|
|
|
|
if (!f)
|
|
|
|
LOG_ERROR_STR(tempName);
|
|
|
|
}
|
|
|
|
return f != NULL;
|
|
|
|
}
|
|
|
|
|
2001-09-16 08:57:58 +02:00
|
|
|
bool cSafeFile::Close(void)
|
2001-01-13 15:36:31 +01:00
|
|
|
{
|
2001-09-16 08:57:58 +02:00
|
|
|
bool result = true;
|
2001-01-13 15:36:31 +01:00
|
|
|
if (f) {
|
2001-09-16 08:57:58 +02:00
|
|
|
if (ferror(f) != 0) {
|
|
|
|
LOG_ERROR_STR(tempName);
|
|
|
|
result = false;
|
|
|
|
}
|
2010-02-28 13:36:26 +01:00
|
|
|
fflush(f);
|
|
|
|
fsync(fileno(f));
|
2001-09-16 08:57:58 +02:00
|
|
|
if (fclose(f) < 0) {
|
|
|
|
LOG_ERROR_STR(tempName);
|
|
|
|
result = false;
|
|
|
|
}
|
2001-01-13 15:36:31 +01:00
|
|
|
f = NULL;
|
2001-09-16 08:57:58 +02:00
|
|
|
if (result && rename(tempName, fileName) < 0) {
|
|
|
|
LOG_ERROR_STR(fileName);
|
|
|
|
result = false;
|
|
|
|
}
|
2001-01-13 15:36:31 +01:00
|
|
|
}
|
2001-09-16 08:57:58 +02:00
|
|
|
else
|
|
|
|
result = false;
|
|
|
|
return result;
|
2001-01-13 15:36:31 +01:00
|
|
|
}
|
|
|
|
|
2005-10-31 13:14:26 +01:00
|
|
|
// --- cUnbufferedFile -------------------------------------------------------
|
|
|
|
|
2006-02-04 14:12:17 +01:00
|
|
|
#define USE_FADVISE
|
2006-01-05 11:32:32 +01:00
|
|
|
|
2006-02-04 14:12:17 +01:00
|
|
|
#define WRITE_BUFFER KILOBYTE(800)
|
2005-10-31 13:14:26 +01:00
|
|
|
|
|
|
|
cUnbufferedFile::cUnbufferedFile(void)
|
|
|
|
{
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cUnbufferedFile::~cUnbufferedFile()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
int cUnbufferedFile::Open(const char *FileName, int Flags, mode_t Mode)
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
fd = open(FileName, Flags, Mode);
|
2006-02-04 14:12:17 +01:00
|
|
|
curpos = 0;
|
|
|
|
#ifdef USE_FADVISE
|
|
|
|
begin = lastpos = ahead = 0;
|
|
|
|
cachedstart = 0;
|
|
|
|
cachedend = 0;
|
|
|
|
readahead = KILOBYTE(128);
|
2005-10-31 13:14:26 +01:00
|
|
|
written = 0;
|
2006-02-04 14:12:17 +01:00
|
|
|
totwritten = 0;
|
|
|
|
if (fd >= 0)
|
|
|
|
posix_fadvise(fd, 0, 0, POSIX_FADV_RANDOM); // we could use POSIX_FADV_SEQUENTIAL, but we do our own readahead, disabling the kernel one.
|
|
|
|
#endif
|
2005-10-31 13:14:26 +01:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cUnbufferedFile::Close(void)
|
|
|
|
{
|
|
|
|
if (fd >= 0) {
|
2009-05-31 11:47:05 +02:00
|
|
|
#ifdef USE_FADVISE
|
2006-02-04 14:12:17 +01:00
|
|
|
if (totwritten) // if we wrote anything make sure the data has hit the disk before
|
|
|
|
fdatasync(fd); // calling fadvise, as this is our last chance to un-cache it.
|
|
|
|
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
|
2006-01-05 11:32:32 +01:00
|
|
|
#endif
|
2009-05-31 11:47:05 +02:00
|
|
|
int OldFd = fd;
|
|
|
|
fd = -1;
|
|
|
|
return close(OldFd);
|
|
|
|
}
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
2005-10-31 13:14:26 +01:00
|
|
|
}
|
|
|
|
|
2006-02-04 14:12:17 +01:00
|
|
|
// When replaying and going e.g. FF->PLAY the position jumps back 2..8M
|
|
|
|
// hence we do not want to drop recently accessed data at once.
|
|
|
|
// We try to handle the common cases such as PLAY->FF->PLAY, small
|
|
|
|
// jumps, moving editing marks etc.
|
|
|
|
|
|
|
|
#define FADVGRAN KILOBYTE(4) // AKA fadvise-chunk-size; PAGE_SIZE or getpagesize(2) would also work.
|
|
|
|
#define READCHUNK MEGABYTE(8)
|
|
|
|
|
|
|
|
void cUnbufferedFile::SetReadAhead(size_t ra)
|
|
|
|
{
|
|
|
|
readahead = ra;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cUnbufferedFile::FadviseDrop(off_t Offset, off_t Len)
|
|
|
|
{
|
|
|
|
// rounding up the window to make sure that not PAGE_SIZE-aligned data gets freed.
|
|
|
|
return posix_fadvise(fd, Offset - (FADVGRAN - 1), Len + (FADVGRAN - 1) * 2, POSIX_FADV_DONTNEED);
|
|
|
|
}
|
|
|
|
|
2005-10-31 13:14:26 +01:00
|
|
|
off_t cUnbufferedFile::Seek(off_t Offset, int Whence)
|
|
|
|
{
|
2006-02-04 14:12:17 +01:00
|
|
|
if (Whence == SEEK_SET && Offset == curpos)
|
|
|
|
return curpos;
|
|
|
|
curpos = lseek(fd, Offset, Whence);
|
|
|
|
return curpos;
|
2005-10-31 13:14:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t cUnbufferedFile::Read(void *Data, size_t Size)
|
|
|
|
{
|
|
|
|
if (fd >= 0) {
|
2006-01-05 11:32:32 +01:00
|
|
|
#ifdef USE_FADVISE
|
2006-02-04 14:12:17 +01:00
|
|
|
off_t jumped = curpos-lastpos; // nonzero means we're not at the last offset
|
|
|
|
if ((cachedstart < cachedend) && (curpos < cachedstart || curpos > cachedend)) {
|
2006-02-05 11:06:47 +01:00
|
|
|
// current position is outside the cached window -- invalidate it.
|
2006-02-04 14:12:17 +01:00
|
|
|
FadviseDrop(cachedstart, cachedend-cachedstart);
|
|
|
|
cachedstart = curpos;
|
|
|
|
cachedend = curpos;
|
|
|
|
}
|
|
|
|
cachedstart = min(cachedstart, curpos);
|
2006-01-05 11:32:32 +01:00
|
|
|
#endif
|
2005-10-31 13:14:26 +01:00
|
|
|
ssize_t bytesRead = safe_read(fd, Data, Size);
|
|
|
|
if (bytesRead > 0) {
|
2006-02-04 14:12:17 +01:00
|
|
|
curpos += bytesRead;
|
2011-03-20 15:25:06 +01:00
|
|
|
#ifdef USE_FADVISE
|
2006-02-04 14:12:17 +01:00
|
|
|
cachedend = max(cachedend, curpos);
|
|
|
|
|
|
|
|
// Read ahead:
|
|
|
|
// no jump? (allow small forward jump still inside readahead window).
|
|
|
|
if (jumped >= 0 && jumped <= (off_t)readahead) {
|
|
|
|
// Trigger the readahead IO, but only if we've used at least
|
|
|
|
// 1/2 of the previously requested area. This avoids calling
|
|
|
|
// fadvise() after every read() call.
|
2006-02-05 11:06:47 +01:00
|
|
|
if (ahead - curpos < (off_t)(readahead / 2)) {
|
2006-02-04 14:12:17 +01:00
|
|
|
posix_fadvise(fd, curpos, readahead, POSIX_FADV_WILLNEED);
|
|
|
|
ahead = curpos + readahead;
|
|
|
|
cachedend = max(cachedend, ahead);
|
|
|
|
}
|
|
|
|
if (readahead < Size * 32) { // automagically tune readahead size.
|
|
|
|
readahead = Size * 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2006-02-05 11:06:47 +01:00
|
|
|
ahead = curpos; // jumped -> we really don't want any readahead, otherwise e.g. fast-rewind gets in trouble.
|
2011-03-20 15:25:06 +01:00
|
|
|
#endif
|
2005-10-31 13:14:26 +01:00
|
|
|
}
|
2011-03-20 15:25:06 +01:00
|
|
|
#ifdef USE_FADVISE
|
2006-02-04 14:12:17 +01:00
|
|
|
if (cachedstart < cachedend) {
|
|
|
|
if (curpos - cachedstart > READCHUNK * 2) {
|
2006-02-05 11:06:47 +01:00
|
|
|
// current position has moved forward enough, shrink tail window.
|
2006-02-04 14:12:17 +01:00
|
|
|
FadviseDrop(cachedstart, curpos - READCHUNK - cachedstart);
|
|
|
|
cachedstart = curpos - READCHUNK;
|
|
|
|
}
|
|
|
|
else if (cachedend > ahead && cachedend - curpos > READCHUNK * 2) {
|
2006-02-05 11:06:47 +01:00
|
|
|
// current position has moved back enough, shrink head window.
|
2006-05-26 10:13:48 +02:00
|
|
|
FadviseDrop(curpos + READCHUNK, cachedend - (curpos + READCHUNK));
|
2006-02-04 14:12:17 +01:00
|
|
|
cachedend = curpos + READCHUNK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastpos = curpos;
|
2006-01-05 11:32:32 +01:00
|
|
|
#endif
|
2005-10-31 13:14:26 +01:00
|
|
|
return bytesRead;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t cUnbufferedFile::Write(const void *Data, size_t Size)
|
|
|
|
{
|
|
|
|
if (fd >=0) {
|
|
|
|
ssize_t bytesWritten = safe_write(fd, Data, Size);
|
2006-01-05 11:32:32 +01:00
|
|
|
#ifdef USE_FADVISE
|
2006-02-04 14:12:17 +01:00
|
|
|
if (bytesWritten > 0) {
|
|
|
|
begin = min(begin, curpos);
|
|
|
|
curpos += bytesWritten;
|
2005-10-31 13:14:26 +01:00
|
|
|
written += bytesWritten;
|
2006-02-04 14:12:17 +01:00
|
|
|
lastpos = max(lastpos, curpos);
|
2005-10-31 13:14:26 +01:00
|
|
|
if (written > WRITE_BUFFER) {
|
2006-02-04 14:12:17 +01:00
|
|
|
if (lastpos > begin) {
|
2006-02-05 11:06:47 +01:00
|
|
|
// Now do three things:
|
|
|
|
// 1) Start writeback of begin..lastpos range
|
|
|
|
// 2) Drop the already written range (by the previous fadvise call)
|
|
|
|
// 3) Handle nonpagealigned data.
|
|
|
|
// This is why we double the WRITE_BUFFER; the first time around the
|
|
|
|
// last (partial) page might be skipped, writeback will start only after
|
|
|
|
// second call; the third call will still include this page and finally
|
|
|
|
// drop it from cache.
|
2009-01-06 14:41:11 +01:00
|
|
|
off_t headdrop = min(begin, off_t(WRITE_BUFFER * 2));
|
2006-02-04 14:12:17 +01:00
|
|
|
posix_fadvise(fd, begin - headdrop, lastpos - begin + headdrop, POSIX_FADV_DONTNEED);
|
|
|
|
}
|
2006-02-05 11:06:47 +01:00
|
|
|
begin = lastpos = curpos;
|
2006-02-04 14:12:17 +01:00
|
|
|
totwritten += written;
|
2005-10-31 13:14:26 +01:00
|
|
|
written = 0;
|
2006-02-04 14:12:17 +01:00
|
|
|
// The above fadvise() works when writing slowly (recording), but could
|
2006-02-05 11:06:47 +01:00
|
|
|
// leave cached data around when writing at a high rate, e.g. when cutting,
|
|
|
|
// because by the time we try to flush the cached pages (above) the data
|
|
|
|
// can still be dirty - we are faster than the disk I/O.
|
|
|
|
// So we do another round of flushing, just like above, but at larger
|
|
|
|
// intervals -- this should catch any pages that couldn't be released
|
|
|
|
// earlier.
|
|
|
|
if (totwritten > MEGABYTE(32)) {
|
|
|
|
// It seems in some setups, fadvise() does not trigger any I/O and
|
|
|
|
// a fdatasync() call would be required do all the work (reiserfs with some
|
|
|
|
// kind of write gathering enabled), but the syncs cause (io) load..
|
|
|
|
// Uncomment the next line if you think you need them.
|
2006-02-04 14:12:17 +01:00
|
|
|
//fdatasync(fd);
|
2009-01-16 14:31:26 +01:00
|
|
|
off_t headdrop = min(off_t(curpos - totwritten), off_t(totwritten * 2));
|
2006-02-04 14:12:17 +01:00
|
|
|
posix_fadvise(fd, curpos - totwritten - headdrop, totwritten + headdrop, POSIX_FADV_DONTNEED);
|
|
|
|
totwritten = 0;
|
|
|
|
}
|
2005-10-31 13:14:26 +01:00
|
|
|
}
|
|
|
|
}
|
2006-01-05 11:32:32 +01:00
|
|
|
#endif
|
2005-10-31 13:14:26 +01:00
|
|
|
return bytesWritten;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cUnbufferedFile *cUnbufferedFile::Create(const char *FileName, int Flags, mode_t Mode)
|
|
|
|
{
|
|
|
|
cUnbufferedFile *File = new cUnbufferedFile;
|
|
|
|
if (File->Open(FileName, Flags, Mode) < 0) {
|
|
|
|
delete File;
|
|
|
|
File = NULL;
|
|
|
|
}
|
|
|
|
return File;
|
|
|
|
}
|
|
|
|
|
2001-09-30 10:38:06 +02:00
|
|
|
// --- cLockFile -------------------------------------------------------------
|
|
|
|
|
|
|
|
#define LOCKFILENAME ".lock-vdr"
|
|
|
|
#define LOCKFILESTALETIME 600 // seconds before considering a lock file "stale"
|
|
|
|
|
|
|
|
cLockFile::cLockFile(const char *Directory)
|
|
|
|
{
|
|
|
|
fileName = NULL;
|
|
|
|
f = -1;
|
|
|
|
if (DirectoryOk(Directory))
|
2008-02-15 14:57:48 +01:00
|
|
|
fileName = strdup(AddDirectory(Directory, LOCKFILENAME));
|
2001-09-30 10:38:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cLockFile::~cLockFile()
|
|
|
|
{
|
|
|
|
Unlock();
|
2002-08-11 13:32:23 +02:00
|
|
|
free(fileName);
|
2001-09-30 10:38:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cLockFile::Lock(int WaitSeconds)
|
|
|
|
{
|
|
|
|
if (f < 0 && fileName) {
|
|
|
|
time_t Timeout = time(NULL) + WaitSeconds;
|
|
|
|
do {
|
2005-08-06 09:56:08 +02:00
|
|
|
f = open(fileName, O_WRONLY | O_CREAT | O_EXCL, DEFFILEMODE);
|
2001-09-30 10:38:06 +02:00
|
|
|
if (f < 0) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
struct stat fs;
|
|
|
|
if (stat(fileName, &fs) == 0) {
|
2002-11-24 20:18:55 +01:00
|
|
|
if (abs(time(NULL) - fs.st_mtime) > LOCKFILESTALETIME) {
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: removing stale lock file '%s'", fileName);
|
2001-09-30 10:38:06 +02:00
|
|
|
if (remove(fileName) < 0) {
|
|
|
|
LOG_ERROR_STR(fileName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (errno != ENOENT) {
|
|
|
|
LOG_ERROR_STR(fileName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG_ERROR_STR(fileName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (WaitSeconds)
|
2011-12-04 14:53:41 +01:00
|
|
|
cCondWait::SleepMs(1000);
|
2001-09-30 10:38:06 +02:00
|
|
|
}
|
|
|
|
} while (f < 0 && time(NULL) < Timeout);
|
|
|
|
}
|
|
|
|
return f >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cLockFile::Unlock(void)
|
|
|
|
{
|
|
|
|
if (f >= 0) {
|
|
|
|
close(f);
|
|
|
|
remove(fileName);
|
|
|
|
f = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-19 13:36:48 +01:00
|
|
|
// --- cListObject -----------------------------------------------------------
|
|
|
|
|
|
|
|
cListObject::cListObject(void)
|
|
|
|
{
|
|
|
|
prev = next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cListObject::~cListObject()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void cListObject::Append(cListObject *Object)
|
|
|
|
{
|
|
|
|
next = Object;
|
|
|
|
Object->prev = this;
|
|
|
|
}
|
|
|
|
|
2002-05-12 14:46:46 +02:00
|
|
|
void cListObject::Insert(cListObject *Object)
|
|
|
|
{
|
|
|
|
prev = Object;
|
|
|
|
Object->next = this;
|
|
|
|
}
|
|
|
|
|
2000-02-19 13:36:48 +01:00
|
|
|
void cListObject::Unlink(void)
|
|
|
|
{
|
|
|
|
if (next)
|
|
|
|
next->prev = prev;
|
|
|
|
if (prev)
|
|
|
|
prev->next = next;
|
2000-03-11 11:22:37 +01:00
|
|
|
next = prev = NULL;
|
2000-02-19 13:36:48 +01:00
|
|
|
}
|
|
|
|
|
2005-03-20 15:15:42 +01:00
|
|
|
int cListObject::Index(void) const
|
2000-02-19 13:36:48 +01:00
|
|
|
{
|
|
|
|
cListObject *p = prev;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (p) {
|
|
|
|
i++;
|
|
|
|
p = p->prev;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- cListBase -------------------------------------------------------------
|
|
|
|
|
|
|
|
cListBase::cListBase(void)
|
2006-01-08 11:44:37 +01:00
|
|
|
{
|
2000-02-19 13:36:48 +01:00
|
|
|
objects = lastObject = NULL;
|
2005-05-26 11:41:33 +02:00
|
|
|
count = 0;
|
2000-02-19 13:36:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cListBase::~cListBase()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
2002-05-12 14:46:46 +02:00
|
|
|
void cListBase::Add(cListObject *Object, cListObject *After)
|
2006-01-08 11:44:37 +01:00
|
|
|
{
|
2002-05-12 14:46:46 +02:00
|
|
|
if (After && After != lastObject) {
|
|
|
|
After->Next()->Insert(Object);
|
|
|
|
After->Append(Object);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (lastObject)
|
|
|
|
lastObject->Append(Object);
|
|
|
|
else
|
|
|
|
objects = Object;
|
|
|
|
lastObject = Object;
|
|
|
|
}
|
2005-05-26 11:41:33 +02:00
|
|
|
count++;
|
2002-05-12 14:46:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cListBase::Ins(cListObject *Object, cListObject *Before)
|
2006-01-08 11:44:37 +01:00
|
|
|
{
|
2002-05-12 14:46:46 +02:00
|
|
|
if (Before && Before != objects) {
|
|
|
|
Before->Prev()->Append(Object);
|
|
|
|
Before->Insert(Object);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (objects)
|
|
|
|
objects->Insert(Object);
|
|
|
|
else
|
|
|
|
lastObject = Object;
|
2002-05-13 17:56:17 +02:00
|
|
|
objects = Object;
|
2002-05-12 14:46:46 +02:00
|
|
|
}
|
2005-05-26 11:41:33 +02:00
|
|
|
count++;
|
2000-02-19 13:36:48 +01:00
|
|
|
}
|
|
|
|
|
2002-05-19 15:50:11 +02:00
|
|
|
void cListBase::Del(cListObject *Object, bool DeleteObject)
|
2000-02-19 13:36:48 +01:00
|
|
|
{
|
|
|
|
if (Object == objects)
|
|
|
|
objects = Object->Next();
|
|
|
|
if (Object == lastObject)
|
|
|
|
lastObject = Object->Prev();
|
|
|
|
Object->Unlink();
|
2002-05-19 15:50:11 +02:00
|
|
|
if (DeleteObject)
|
|
|
|
delete Object;
|
2005-05-26 11:41:33 +02:00
|
|
|
count--;
|
2000-02-19 13:36:48 +01:00
|
|
|
}
|
|
|
|
|
2000-03-11 11:22:37 +01:00
|
|
|
void cListBase::Move(int From, int To)
|
|
|
|
{
|
|
|
|
Move(Get(From), Get(To));
|
|
|
|
}
|
|
|
|
|
|
|
|
void cListBase::Move(cListObject *From, cListObject *To)
|
|
|
|
{
|
2012-01-11 11:23:38 +01:00
|
|
|
if (From && To && From != To) {
|
2000-03-11 11:22:37 +01:00
|
|
|
if (From->Index() < To->Index())
|
|
|
|
To = To->Next();
|
|
|
|
if (From == objects)
|
|
|
|
objects = From->Next();
|
|
|
|
if (From == lastObject)
|
|
|
|
lastObject = From->Prev();
|
|
|
|
From->Unlink();
|
|
|
|
if (To) {
|
|
|
|
if (To->Prev())
|
|
|
|
To->Prev()->Append(From);
|
|
|
|
From->Append(To);
|
|
|
|
}
|
2002-02-05 18:28:14 +01:00
|
|
|
else {
|
2000-03-11 11:22:37 +01:00
|
|
|
lastObject->Append(From);
|
2002-02-05 18:28:14 +01:00
|
|
|
lastObject = From;
|
|
|
|
}
|
2000-03-11 11:22:37 +01:00
|
|
|
if (!From->Prev())
|
|
|
|
objects = From;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-19 13:36:48 +01:00
|
|
|
void cListBase::Clear(void)
|
|
|
|
{
|
|
|
|
while (objects) {
|
|
|
|
cListObject *object = objects->Next();
|
|
|
|
delete objects;
|
|
|
|
objects = object;
|
|
|
|
}
|
|
|
|
objects = lastObject = NULL;
|
2005-05-26 11:41:33 +02:00
|
|
|
count = 0;
|
2000-02-19 13:36:48 +01:00
|
|
|
}
|
|
|
|
|
2000-10-29 13:17:22 +01:00
|
|
|
cListObject *cListBase::Get(int Index) const
|
2000-02-19 13:36:48 +01:00
|
|
|
{
|
2000-09-09 14:57:43 +02:00
|
|
|
if (Index < 0)
|
|
|
|
return NULL;
|
2000-02-19 13:36:48 +01:00
|
|
|
cListObject *object = objects;
|
|
|
|
while (object && Index-- > 0)
|
|
|
|
object = object->Next();
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2004-11-01 10:40:38 +01:00
|
|
|
static int CompareListObjects(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const cListObject *la = *(const cListObject **)a;
|
|
|
|
const cListObject *lb = *(const cListObject **)b;
|
|
|
|
return la->Compare(*lb);
|
|
|
|
}
|
|
|
|
|
2001-08-26 14:17:20 +02:00
|
|
|
void cListBase::Sort(void)
|
|
|
|
{
|
2004-11-01 10:40:38 +01:00
|
|
|
int n = Count();
|
|
|
|
cListObject *a[n];
|
|
|
|
cListObject *object = objects;
|
|
|
|
int i = 0;
|
|
|
|
while (object && i < n) {
|
|
|
|
a[i++] = object;
|
|
|
|
object = object->Next();
|
|
|
|
}
|
|
|
|
qsort(a, n, sizeof(cListObject *), CompareListObjects);
|
|
|
|
objects = lastObject = NULL;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
a[i]->Unlink();
|
2005-05-26 11:41:33 +02:00
|
|
|
count--;
|
2004-11-01 10:40:38 +01:00
|
|
|
Add(a[i]);
|
|
|
|
}
|
2001-08-26 14:17:20 +02:00
|
|
|
}
|
|
|
|
|
2005-05-28 13:17:20 +02:00
|
|
|
// --- cHashBase -------------------------------------------------------------
|
|
|
|
|
|
|
|
cHashBase::cHashBase(int Size)
|
|
|
|
{
|
|
|
|
size = Size;
|
|
|
|
hashTable = (cList<cHashObject>**)calloc(size, sizeof(cList<cHashObject>*));
|
|
|
|
}
|
|
|
|
|
|
|
|
cHashBase::~cHashBase(void)
|
|
|
|
{
|
2005-09-11 13:23:49 +02:00
|
|
|
Clear();
|
2005-05-28 13:17:20 +02:00
|
|
|
free(hashTable);
|
|
|
|
}
|
|
|
|
|
2005-05-29 10:24:54 +02:00
|
|
|
void cHashBase::Add(cListObject *Object, unsigned int Id)
|
2005-05-28 13:17:20 +02:00
|
|
|
{
|
2005-05-29 10:24:54 +02:00
|
|
|
unsigned int hash = hashfn(Id);
|
2005-05-28 13:17:20 +02:00
|
|
|
if (!hashTable[hash])
|
|
|
|
hashTable[hash] = new cList<cHashObject>;
|
|
|
|
hashTable[hash]->Add(new cHashObject(Object, Id));
|
|
|
|
}
|
|
|
|
|
2005-05-29 10:24:54 +02:00
|
|
|
void cHashBase::Del(cListObject *Object, unsigned int Id)
|
2005-05-28 13:17:20 +02:00
|
|
|
{
|
|
|
|
cList<cHashObject> *list = hashTable[hashfn(Id)];
|
|
|
|
if (list) {
|
|
|
|
for (cHashObject *hob = list->First(); hob; hob = list->Next(hob)) {
|
|
|
|
if (hob->object == Object) {
|
|
|
|
list->Del(hob);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-11 13:23:49 +02:00
|
|
|
void cHashBase::Clear(void)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
delete hashTable[i];
|
|
|
|
hashTable[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-29 10:24:54 +02:00
|
|
|
cListObject *cHashBase::Get(unsigned int Id) const
|
2005-05-28 13:17:20 +02:00
|
|
|
{
|
|
|
|
cList<cHashObject> *list = hashTable[hashfn(Id)];
|
|
|
|
if (list) {
|
|
|
|
for (cHashObject *hob = list->First(); hob; hob = list->Next(hob)) {
|
|
|
|
if (hob->id == Id)
|
|
|
|
return hob->object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-05-29 10:24:54 +02:00
|
|
|
cList<cHashObject> *cHashBase::GetList(unsigned int Id) const
|
2005-05-28 13:17:20 +02:00
|
|
|
{
|
|
|
|
return hashTable[hashfn(Id)];
|
|
|
|
}
|