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.
|
|
|
|
*
|
2003-04-06 15:43:41 +02:00
|
|
|
* $Id: tools.c 1.77 2003/04/06 15:31:45 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>
|
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>
|
2001-08-25 13:27:26 +02:00
|
|
|
#include "i18n.h"
|
2000-02-19 13:36:48 +01:00
|
|
|
|
2000-04-15 17:38:11 +02:00
|
|
|
int SysLogLevel = 3;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2000-03-11 11:22:37 +01:00
|
|
|
char *readline(FILE *f)
|
|
|
|
{
|
2001-09-14 14:35:40 +02:00
|
|
|
static char buffer[MAXPARSEBUFFER];
|
2000-03-11 11:22:37 +01:00
|
|
|
if (fgets(buffer, sizeof(buffer), f) > 0) {
|
|
|
|
int l = strlen(buffer) - 1;
|
|
|
|
if (l >= 0 && buffer[l] == '\n')
|
|
|
|
buffer[l] = 0;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
char *p = s;
|
|
|
|
|
2001-02-11 11:29:22 +01:00
|
|
|
while (p && *p) {
|
2000-07-16 15:02:33 +02:00
|
|
|
if (*p == c1)
|
|
|
|
*p = c2;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
if (l2 > l1)
|
|
|
|
s = (char *)realloc(s, strlen(s) + l2 - l1 + 1);
|
|
|
|
if (l2 != l1)
|
|
|
|
memmove(s + of + l2, s + of + l1, l - of - l1 + 1);
|
|
|
|
strncpy(s + of, s2, l2);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2000-10-29 13:17:22 +01:00
|
|
|
char *skipspace(const char *s)
|
2000-07-23 15:01:31 +02:00
|
|
|
{
|
|
|
|
while (*s && isspace(*s))
|
|
|
|
s++;
|
2000-10-29 13:17:22 +01:00
|
|
|
return (char *)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;
|
|
|
|
}
|
|
|
|
|
2002-01-26 12:04:32 +01:00
|
|
|
const char *strescape(const char *s, const char *chars)
|
|
|
|
{
|
|
|
|
static char *buffer = NULL;
|
|
|
|
const char *p = s;
|
|
|
|
char *t = NULL;
|
|
|
|
while (*p) {
|
|
|
|
if (strchr(chars, *p)) {
|
|
|
|
if (!t) {
|
|
|
|
buffer = (char *)realloc(buffer, 2 * strlen(s) + 1);
|
|
|
|
t = buffer + (p - s);
|
|
|
|
s = strcpy(buffer, s);
|
|
|
|
}
|
|
|
|
*t++ = '\\';
|
|
|
|
}
|
|
|
|
if (t)
|
|
|
|
*t++ = *p;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (t)
|
|
|
|
*t = 0;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
char buf[16];
|
|
|
|
snprintf(buf, sizeof(buf), "%d", n);
|
|
|
|
return strlen(buf);
|
|
|
|
}
|
|
|
|
|
2000-02-19 13:36:48 +01:00
|
|
|
int time_ms(void)
|
|
|
|
{
|
2000-04-15 17:38:11 +02:00
|
|
|
static time_t t0 = 0;
|
2000-02-19 13:36:48 +01:00
|
|
|
struct timeval t;
|
2000-04-15 17:38:11 +02:00
|
|
|
if (gettimeofday(&t, NULL) == 0) {
|
|
|
|
if (t0 == 0)
|
|
|
|
t0 = t.tv_sec; // this avoids an overflow (we only work with deltas)
|
|
|
|
return (t.tv_sec - t0) * 1000 + t.tv_usec / 1000;
|
|
|
|
}
|
2000-02-19 13:36:48 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-04-15 17:38:11 +02:00
|
|
|
void delay_ms(int ms)
|
|
|
|
{
|
|
|
|
int t0 = time_ms();
|
|
|
|
while (time_ms() - t0 < ms)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2000-07-23 15:01:31 +02:00
|
|
|
bool isnumber(const char *s)
|
|
|
|
{
|
2002-11-24 15:56:24 +01:00
|
|
|
if (!*s)
|
|
|
|
return false;
|
2000-07-23 15:01:31 +02:00
|
|
|
while (*s) {
|
|
|
|
if (!isdigit(*s))
|
|
|
|
return false;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-11-10 15:50:21 +01:00
|
|
|
const char *itoa(int n)
|
|
|
|
{
|
|
|
|
static char buf[16];
|
|
|
|
snprintf(buf, sizeof(buf), "%d", n);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2000-09-15 15:09:15 +02:00
|
|
|
const char *AddDirectory(const char *DirName, const char *FileName)
|
|
|
|
{
|
|
|
|
static char *buf = NULL;
|
2002-08-11 13:32:23 +02:00
|
|
|
free(buf);
|
2001-08-11 14:51:28 +02:00
|
|
|
asprintf(&buf, "%s/%s", DirName && *DirName ? DirName : ".", FileName);
|
2000-09-15 15:09:15 +02:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
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);
|
2000-03-11 11:22:37 +01:00
|
|
|
if (mkdir(s, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -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)) {
|
|
|
|
DIR *d = opendir(FileName);
|
|
|
|
if (d) {
|
|
|
|
struct dirent *e;
|
|
|
|
while ((e = readdir(d)) != NULL) {
|
|
|
|
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) {
|
|
|
|
char *buffer;
|
|
|
|
asprintf(&buffer, "%s/%s", FileName, e->d_name);
|
2000-07-29 15:21:42 +02:00
|
|
|
if (FollowSymlinks) {
|
|
|
|
int size = strlen(buffer) * 2; // should be large enough
|
2002-08-11 13:32:23 +02:00
|
|
|
char *l = MALLOC(char, size);
|
2000-07-29 15:21:42 +02:00
|
|
|
int n = readlink(buffer, l, size);
|
|
|
|
if (n < 0) {
|
|
|
|
if (errno != EINVAL)
|
|
|
|
LOG_ERROR_STR(buffer);
|
|
|
|
}
|
|
|
|
else if (n < size) {
|
|
|
|
l[n] = 0;
|
2002-05-13 16:35:49 +02:00
|
|
|
dsyslog("removing %s", l);
|
2000-07-29 15:21:42 +02:00
|
|
|
if (remove(l) < 0)
|
|
|
|
LOG_ERROR_STR(l);
|
|
|
|
}
|
|
|
|
else
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: symlink name length (%d) exceeded anticipated buffer size (%d)", n, size);
|
2002-08-11 13:32:23 +02:00
|
|
|
free(l);
|
2000-07-29 15:21:42 +02:00
|
|
|
}
|
2002-05-13 16:35:49 +02:00
|
|
|
dsyslog("removing %s", buffer);
|
2000-04-15 17:38:11 +02:00
|
|
|
if (remove(buffer) < 0)
|
2000-07-29 15:21:42 +02:00
|
|
|
LOG_ERROR_STR(buffer);
|
2002-08-11 13:32:23 +02:00
|
|
|
free(buffer);
|
2000-04-15 17:38:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2001-02-11 14:53:44 +01:00
|
|
|
bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis)
|
|
|
|
{
|
|
|
|
DIR *d = opendir(DirName);
|
|
|
|
if (d) {
|
|
|
|
bool empty = true;
|
|
|
|
struct dirent *e;
|
|
|
|
while ((e = readdir(d)) != NULL) {
|
|
|
|
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..") && strcmp(e->d_name, "lost+found")) {
|
|
|
|
char *buffer;
|
|
|
|
asprintf(&buffer, "%s/%s", DirName, e->d_name);
|
|
|
|
struct stat st;
|
|
|
|
if (stat(buffer, &st) == 0) {
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
if (!RemoveEmptyDirectories(buffer, true))
|
|
|
|
empty = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
empty = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG_ERROR_STR(buffer);
|
2002-08-11 13:32:23 +02:00
|
|
|
free(buffer);
|
2001-02-11 14:53:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
2002-08-11 13:32:23 +02:00
|
|
|
free(buffer);
|
2001-02-11 14:53:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
if (RemoveThis && empty) {
|
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;
|
|
|
|
}
|
|
|
|
|
2001-01-13 15:36:31 +01:00
|
|
|
char *ReadLink(const char *FileName)
|
|
|
|
{
|
2002-08-04 14:57:29 +02:00
|
|
|
char RealName[PATH_MAX];
|
2001-01-13 15:36:31 +01:00
|
|
|
const char *TargetName = NULL;
|
|
|
|
int n = readlink(FileName, RealName, sizeof(RealName) - 1);
|
|
|
|
if (n < 0) {
|
|
|
|
if (errno == ENOENT || errno == EINVAL) // file doesn't exist or is not a symlink
|
|
|
|
TargetName = 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);
|
|
|
|
}
|
|
|
|
else if (n < int(sizeof(RealName))) { // got it!
|
|
|
|
RealName[n] = 0;
|
|
|
|
TargetName = RealName;
|
|
|
|
}
|
|
|
|
else
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: symlink's target name too long: %s", FileName);
|
2001-01-13 15:36:31 +01:00
|
|
|
return TargetName ? strdup(TargetName) : NULL;
|
|
|
|
}
|
|
|
|
|
2001-08-11 11:15:41 +02:00
|
|
|
bool SpinUpDisk(const char *FileName)
|
|
|
|
{
|
|
|
|
static char *buf = NULL;
|
|
|
|
for (int n = 0; n < 10; n++) {
|
2002-08-11 13:32:23 +02:00
|
|
|
free(buf);
|
2001-08-11 11:15:41 +02:00
|
|
|
if (DirectoryOk(FileName))
|
|
|
|
asprintf(&buf, "%s/vdr-%06d", *FileName ? FileName : ".", n);
|
|
|
|
else
|
|
|
|
asprintf(&buf, "%s.vdr-%06d", FileName, n);
|
|
|
|
if (access(buf, F_OK) != 0) { // the file does not exist
|
|
|
|
timeval tp1, tp2;
|
|
|
|
gettimeofday(&tp1, NULL);
|
|
|
|
int f = open(buf, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
|
|
|
// O_SYNC doesn't work on all file systems
|
|
|
|
if (f >= 0) {
|
|
|
|
close(f);
|
|
|
|
system("sync");
|
|
|
|
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)
|
2002-05-13 16:35:49 +02:00
|
|
|
dsyslog("SpinUpDisk took %.2f seconds\n", seconds);
|
2001-08-11 11:15:41 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LOG_ERROR_STR(buf);
|
|
|
|
}
|
|
|
|
}
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: SpinUpDisk failed");
|
2001-08-11 11:15:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-02-17 13:05:05 +01:00
|
|
|
const char *WeekDayName(int WeekDay)
|
|
|
|
{
|
|
|
|
static char buffer[4];
|
|
|
|
WeekDay = WeekDay == 0 ? 6 : WeekDay - 1; // we start with monday==0!
|
|
|
|
if (0 <= WeekDay && WeekDay <= 6) {
|
|
|
|
const char *day = tr("MonTueWedThuFriSatSun");
|
|
|
|
day += WeekDay * 3;
|
|
|
|
strncpy(buffer, day, 3);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
|
2001-08-25 13:27:26 +02:00
|
|
|
const char *DayDateTime(time_t t)
|
|
|
|
{
|
|
|
|
static char buffer[32];
|
|
|
|
if (t == 0)
|
|
|
|
time(&t);
|
2001-10-19 13:22:24 +02:00
|
|
|
struct tm tm_r;
|
|
|
|
tm *tm = localtime_r(&t, &tm_r);
|
2002-02-17 13:05:05 +01:00
|
|
|
snprintf(buffer, sizeof(buffer), "%s %2d.%02d %02d:%02d", WeekDayName(tm->tm_wday), tm->tm_mday, tm->tm_mon + 1, tm->tm_hour, tm->tm_min);
|
2001-08-25 13:27:26 +02:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
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++) {
|
|
|
|
if (pfd[i].fd == FileHandle)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (numFileHandles < MaxPollFiles) {
|
|
|
|
pfd[numFileHandles].fd = FileHandle;
|
|
|
|
pfd[numFileHandles].events = Out ? POLLOUT : POLLIN;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
asprintf(&fileName, "%s/%s", Directory, LOCKFILENAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
f = open(fileName, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
|
|
|
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)
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
|
|
int cListObject::Index(void)
|
|
|
|
{
|
|
|
|
cListObject *p = prev;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (p) {
|
|
|
|
i++;
|
|
|
|
p = p->prev;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- cListBase -------------------------------------------------------------
|
|
|
|
|
|
|
|
cListBase::cListBase(void)
|
|
|
|
{
|
|
|
|
objects = lastObject = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cListBase::~cListBase()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
2002-05-12 14:46:46 +02:00
|
|
|
void cListBase::Add(cListObject *Object, cListObject *After)
|
2000-02-19 13:36:48 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cListBase::Ins(cListObject *Object, cListObject *Before)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
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;
|
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)
|
|
|
|
{
|
|
|
|
if (From && To) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2000-10-29 13:17:22 +01:00
|
|
|
int cListBase::Count(void) const
|
2000-02-19 13:36:48 +01:00
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
cListObject *object = objects;
|
|
|
|
|
|
|
|
while (object) {
|
|
|
|
n++;
|
|
|
|
object = object->Next();
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2001-08-26 14:17:20 +02:00
|
|
|
void cListBase::Sort(void)
|
|
|
|
{
|
|
|
|
bool swapped;
|
|
|
|
do {
|
|
|
|
swapped = false;
|
|
|
|
cListObject *object = objects;
|
|
|
|
while (object) {
|
|
|
|
if (object->Next() && *object->Next() < *object) {
|
|
|
|
Move(object->Next(), object);
|
|
|
|
swapped = true;
|
|
|
|
}
|
|
|
|
object = object->Next();
|
|
|
|
}
|
|
|
|
} while (swapped);
|
|
|
|
}
|
|
|
|
|