summaryrefslogtreecommitdiff
path: root/tools.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools.c')
-rw-r--r--tools.c78
1 files changed, 48 insertions, 30 deletions
diff --git a/tools.c b/tools.c
index 98d2501..64f4464 100644
--- a/tools.c
+++ b/tools.c
@@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
- * $Id: tools.c 1.140 2008/01/13 11:26:30 kls Exp $
+ * $Id: tools.c 1.143 2008/02/16 13:38:22 kls Exp $
*/
#include "tools.h"
@@ -267,9 +267,7 @@ bool isnumber(const char *s)
cString AddDirectory(const char *DirName, const char *FileName)
{
- char *buf;
- asprintf(&buf, "%s/%s", DirName && *DirName ? DirName : ".", FileName);
- return cString(buf, true);
+ return cString::sprintf("%s/%s", DirName && *DirName ? DirName : ".", FileName);
}
cString itoa(int n)
@@ -279,6 +277,21 @@ cString itoa(int n)
return buf;
}
+bool EntriesOnSameFileSystem(const char *File1, const char *File2)
+{
+ struct statfs statFs;
+ if (statfs(File1, &statFs) == 0) {
+ fsid_t fsid1 = statFs.f_fsid;
+ if (statfs(File2, &statFs) == 0)
+ return memcmp(&statFs.f_fsid, &fsid1, sizeof(fsid1)) == 0;
+ else
+ LOG_ERROR_STR(File2);
+ }
+ else
+ LOG_ERROR_STR(File1);
+ return false;
+}
+
int FreeDiskSpaceMB(const char *Directory, int *UsedMB)
{
if (UsedMB)
@@ -352,15 +365,14 @@ bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks)
struct dirent *e;
while ((e = d.Next()) != NULL) {
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) {
- char *buffer;
- asprintf(&buffer, "%s/%s", FileName, e->d_name);
+ cString buffer = AddDirectory(FileName, e->d_name);
if (FollowSymlinks) {
int size = strlen(buffer) * 2; // should be large enough
char *l = MALLOC(char, size);
int n = readlink(buffer, l, size);
if (n < 0) {
if (errno != EINVAL)
- LOG_ERROR_STR(buffer);
+ LOG_ERROR_STR(*buffer);
}
else if (n < size) {
l[n] = 0;
@@ -372,10 +384,9 @@ bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks)
esyslog("ERROR: symlink name length (%d) exceeded anticipated buffer size (%d)", n, size);
free(l);
}
- dsyslog("removing %s", buffer);
+ dsyslog("removing %s", *buffer);
if (remove(buffer) < 0)
- LOG_ERROR_STR(buffer);
- free(buffer);
+ LOG_ERROR_STR(*buffer);
}
}
}
@@ -405,8 +416,7 @@ bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis)
struct dirent *e;
while ((e = d.Next()) != 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);
+ cString buffer = AddDirectory(DirName, e->d_name);
struct stat st;
if (stat(buffer, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
@@ -417,10 +427,9 @@ bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis)
empty = false;
}
else {
- LOG_ERROR_STR(buffer);
+ LOG_ERROR_STR(*buffer);
empty = false;
}
- free(buffer);
}
}
if (RemoveThis && empty) {
@@ -445,8 +454,7 @@ int DirSizeMB(const char *DirName)
struct dirent *e;
while (size >= 0 && (e = d.Next()) != NULL) {
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) {
- char *buffer;
- asprintf(&buffer, "%s/%s", DirName, e->d_name);
+ cString buffer = AddDirectory(DirName, e->d_name);
struct stat st;
if (stat(buffer, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
@@ -460,10 +468,9 @@ int DirSizeMB(const char *DirName)
size += st.st_size / MEGABYTE(1);
}
else {
- LOG_ERROR_STR(buffer);
+ LOG_ERROR_STR(*buffer);
size = -1;
}
- free(buffer);
}
}
return size;
@@ -489,13 +496,12 @@ char *ReadLink(const char *FileName)
bool SpinUpDisk(const char *FileName)
{
- char *buf = NULL;
for (int n = 0; n < 10; n++) {
- free(buf);
+ cString buf;
if (DirectoryOk(FileName))
- asprintf(&buf, "%s/vdr-%06d", *FileName ? FileName : ".", n);
+ buf = cString::sprintf("%s/vdr-%06d", *FileName ? FileName : ".", n);
else
- asprintf(&buf, "%s.vdr-%06d", FileName, n);
+ buf = cString::sprintf("%s.vdr-%06d", FileName, n);
if (access(buf, F_OK) != 0) { // the file does not exist
timeval tp1, tp2;
gettimeofday(&tp1, NULL);
@@ -503,21 +509,19 @@ bool SpinUpDisk(const char *FileName)
// O_SYNC doesn't work on all file systems
if (f >= 0) {
if (fdatasync(f) < 0)
- LOG_ERROR_STR(buf);
+ LOG_ERROR_STR(*buf);
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)
dsyslog("SpinUpDisk took %.2f seconds", seconds);
- free(buf);
return true;
}
else
- LOG_ERROR_STR(buf);
+ LOG_ERROR_STR(*buf);
}
}
- free(buf);
esyslog("ERROR: SpinUpDisk failed");
return false;
}
@@ -888,7 +892,21 @@ cString cString::sprintf(const char *fmt, ...)
va_list ap;
va_start(ap, fmt);
char *buffer;
- vasprintf(&buffer, fmt, ap);
+ if (!fmt || vasprintf(&buffer, fmt, ap) < 0) {
+ esyslog("error in vasprintf('%s', ...)", fmt);
+ buffer = strdup("???");
+ }
+ va_end(ap);
+ return cString(buffer, true);
+}
+
+cString cString::sprintf(const char *fmt, va_list &ap)
+{
+ char *buffer;
+ if (!fmt || vasprintf(&buffer, fmt, ap) < 0) {
+ esyslog("error in vasprintf('%s', ...)", fmt);
+ buffer = strdup("???");
+ }
return cString(buffer, true);
}
@@ -1241,8 +1259,8 @@ bool cFileNameList::Load(const char *Directory, bool DirsOnly)
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) {
if (DirsOnly) {
struct stat ds;
- if (stat(e->d_name, &ds) == 0) {
- if (S_ISDIR(ds.st_mode))
+ if (stat(AddDirectory(Directory, e->d_name), &ds) == 0) {
+ if (!S_ISDIR(ds.st_mode))
continue;
}
}
@@ -1622,7 +1640,7 @@ cLockFile::cLockFile(const char *Directory)
fileName = NULL;
f = -1;
if (DirectoryOk(Directory))
- asprintf(&fileName, "%s/%s", Directory, LOCKFILENAME);
+ fileName = strdup(AddDirectory(Directory, LOCKFILENAME));
}
cLockFile::~cLockFile()