33 #define MAXSYSLOGBUF 256
41 vsyslog(priority, fmt, ap);
53 ssize_t
safe_read(
int filedes,
void *buffer,
size_t size)
56 ssize_t p = read(filedes, buffer, size);
57 if (p < 0 && errno == EINTR) {
58 dsyslog(
"EINTR while reading from file handle %d - retrying", filedes);
65 ssize_t
safe_write(
int filedes,
const void *buffer,
size_t size)
68 ssize_t written = size;
69 const unsigned char *ptr = (
const unsigned char *)buffer;
71 p = write(filedes, ptr, size);
74 dsyslog(
"EINTR while writing to file handle %d - retrying", filedes);
82 return p < 0 ? p : written;
94 int w = write(fd, Data + written, Length);
103 Poller.
Poll(RetryMs);
104 if (TimeoutMs > 0 && (TimeoutMs -= t.
Elapsed()) <= 0)
117 int l =
max(dest ? strlen(dest) : 0, strlen(src)) + 1;
118 dest = (
char *)realloc(dest, l);
122 esyslog(
"ERROR: out of memory");
131 char *
strn0cpy(
char *dest,
const char *src,
size_t n)
134 for ( ; --n && (*dest = *src) != 0; dest++, src++) ;
154 char *p = strstr(s, s1);
161 if (
char *NewBuffer = (
char *)realloc(s, l + l2 - l1 + 1))
164 esyslog(
"ERROR: out of memory");
170 memmove(sof + l2, sof + l1, l - of - l1 + 1);
171 strncpy(sof, s2, l2);
176 const char *
strchrn(
const char *s,
char c,
size_t n)
182 if (*s == c && --n == 0)
204 for (
char *p = s + strlen(s) - 1; p >= s; p--) {
221 memmove(p + 1, q, strlen(q) + 1);
225 memmove(s, t, strlen(t) + 1);
241 else if (t != s && n == 0) {
260 if (strchr(chars, *p)) {
262 buffer =
MALLOC(
char, 2 * strlen(s) + 1);
263 t = buffer + (p - s);
264 s = strcpy(buffer, s);
288 const char *se = s + strlen(s) - 1;
289 const char *pe = p + strlen(p) - 1;
291 if (*pe-- != *se-- || (se < s && pe >= p))
326 int64_t n = strtoll(s, &t, 10);
342 if (strcmp(*a, s) == 0)
352 return cString::sprintf(
"%s/%s", DirName && *DirName ? DirName :
".", FileName);
355 #define DECIMAL_POINT_C '.'
359 static lconv *loc = localeconv();
361 char buf[strlen(s) + 1];
365 *p = *loc->decimal_point;
380 static lconv *loc = localeconv();
382 snprintf(buf,
sizeof(buf), Format, d);
391 snprintf(buf,
sizeof(buf),
"%d", n);
398 if (stat(File1, &st) == 0) {
399 dev_t dev1 = st.st_dev;
400 if (stat(File2, &st) == 0)
401 return st.st_dev == dev1;
415 struct statfs statFs;
416 if (statfs(Directory, &statFs) == 0) {
417 double blocksPerMeg = 1024.0 * 1024.0 / statFs.f_bsize;
419 *UsedMB = int((statFs.f_blocks - statFs.f_bfree) / blocksPerMeg);
420 Free = int(statFs.f_bavail / blocksPerMeg);
430 if (stat(DirName, &ds) == 0) {
431 if (S_ISDIR(ds.st_mode)) {
432 if (access(DirName, R_OK | W_OK | X_OK) == 0)
435 esyslog(
"ERROR: can't access %s", DirName);
438 esyslog(
"ERROR: %s is not a directory", DirName);
445 bool MakeDirs(
const char *FileName,
bool IsDirectory)
448 char *s = strdup(FileName);
452 while ((p = strchr(p,
'/')) != NULL || IsDirectory) {
456 if (stat(s, &fs) != 0 || !S_ISDIR(fs.st_mode)) {
457 dsyslog(
"creating directory %s", s);
458 if (mkdir(s, ACCESSPERMS) == -1) {
476 if (stat(FileName, &st) == 0) {
477 if (S_ISDIR(st.st_mode)) {
481 while ((e = d.
Next()) != NULL) {
483 if (FollowSymlinks) {
485 if (lstat(buffer, &st2) == 0) {
486 if (S_ISLNK(st2.st_mode)) {
487 int size = st2.st_size + 1;
488 char *l =
MALLOC(
char, size);
489 int n = readlink(buffer, l, size - 1);
503 else if (errno != ENOENT) {
508 dsyslog(
"removing %s", *buffer);
509 if (
remove(buffer) < 0)
518 dsyslog(
"removing %s", FileName);
519 if (
remove(FileName) < 0) {
524 else if (errno != ENOENT) {
533 bool HasIgnoredFiles =
false;
538 while ((e = d.
Next()) != NULL) {
539 if (strcmp(e->d_name,
"lost+found")) {
542 if (stat(buffer, &st) == 0) {
543 if (S_ISDIR(st.st_mode)) {
547 else if (RemoveThis && IgnoreFiles &&
StrInArray(IgnoreFiles, e->d_name))
548 HasIgnoredFiles =
true;
558 if (RemoveThis && empty) {
559 if (HasIgnoredFiles) {
560 while (*IgnoreFiles) {
562 if (access(buffer, F_OK) == 0) {
563 dsyslog(
"removing %s", *buffer);
564 if (
remove(buffer) < 0) {
572 dsyslog(
"removing %s", DirName);
573 if (
remove(DirName) < 0) {
591 while (size >= 0 && (e = d.
Next()) != NULL) {
594 if (stat(buffer, &st) == 0) {
595 if (S_ISDIR(st.st_mode)) {
612 else if (errno != ENOENT)
621 char *TargetName = canonicalize_file_name(FileName);
624 TargetName = strdup(FileName);
633 for (
int n = 0; n < 10; n++) {
639 if (access(buf, F_OK) != 0) {
641 gettimeofday(&tp1, NULL);
642 int f = open(buf, O_WRONLY | O_CREAT, DEFFILEMODE);
645 if (fdatasync(f) < 0)
649 gettimeofday(&tp2, NULL);
650 double seconds = (((
long long)tp2.tv_sec * 1000000 + tp2.tv_usec) - ((
long long)tp1.tv_sec * 1000000 + tp1.tv_usec)) / 1000000.0;
652 dsyslog(
"SpinUpDisk took %.2f seconds", seconds);
659 esyslog(
"ERROR: SpinUpDisk failed");
665 if (utime(FileName, NULL) == -1 && errno != ENOENT)
672 if (stat(FileName, &fs) == 0)
680 if (stat(FileName, &fs) == 0)
697 #if _POSIX_TIMERS > 0 && defined(_POSIX_MONOTONIC_CLOCK)
698 #define MIN_RESOLUTION 5 // ms
699 static bool initialized =
false;
700 static bool monotonic =
false;
704 if (clock_getres(CLOCK_MONOTONIC, &tp) == 0) {
705 long Resolution = tp.tv_nsec;
707 if (tp.tv_sec == 0 && tp.tv_nsec <= MIN_RESOLUTION * 1000000) {
708 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
709 dsyslog(
"cTimeMs: using monotonic clock (resolution is %ld ns)", Resolution);
713 esyslog(
"cTimeMs: clock_gettime(CLOCK_MONOTONIC) failed");
716 dsyslog(
"cTimeMs: not using monotonic clock - resolution is too bad (%ld s %ld ns)", tp.tv_sec, tp.tv_nsec);
719 esyslog(
"cTimeMs: clock_getres(CLOCK_MONOTONIC) failed");
723 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
724 return (uint64_t(tp.tv_sec)) * 1000 + tp.tv_nsec / 1000000;
725 esyslog(
"cTimeMs: clock_gettime(CLOCK_MONOTONIC) failed");
730 # warning Posix monotonic clock not available
733 if (gettimeofday(&t, NULL) == 0)
734 return (uint64_t(t.tv_sec)) * 1000 + t.tv_usec / 1000;
761 #define MT(s, m, v) ((*(s) & (m)) == (v)) // Mask Test
762 if (
MT(s, 0xE0, 0xC0) &&
MT(s + 1, 0xC0, 0x80))
764 if (
MT(s, 0xF0, 0xE0) &&
MT(s + 1, 0xC0, 0x80) &&
MT(s + 2, 0xC0, 0x80))
766 if (
MT(s, 0xF8, 0xF0) &&
MT(s + 1, 0xC0, 0x80) &&
MT(s + 2, 0xC0, 0x80) &&
MT(s + 3, 0xC0, 0x80))
778 case 2:
return ((*s & 0x1F) << 6) | (*(s + 1) & 0x3F);
779 case 3:
return ((*s & 0x0F) << 12) | ((*(s + 1) & 0x3F) << 6) | (*(s + 2) & 0x3F);
780 case 4:
return ((*s & 0x07) << 18) | ((*(s + 1) & 0x3F) << 12) | ((*(s + 2) & 0x3F) << 6) | (*(s + 3) & 0x3F);
795 *s++ = ((c >> 6) & 0x1F) | 0xC0;
796 *s = (c & 0x3F) | 0x80;
802 *s++ = ((c >> 12) & 0x0F) | 0xE0;
803 *s++ = ((c >> 6) & 0x3F) | 0x80;
804 *s = (c & 0x3F) | 0x80;
810 *s++ = ((c >> 18) & 0x07) | 0xF0;
811 *s++ = ((c >> 12) & 0x3F) | 0x80;
812 *s++ = ((c >> 6) & 0x3F) | 0x80;
813 *s = (c & 0x3F) | 0x80;
825 while (*s && Symbols--) {
867 while (*s && --Size > 0) {
869 *a++ = (
uchar)(*s++);
886 while (*a && NumChars < Size) {
887 if (Max >= 0 && NumSyms++ >= Max)
895 if (NumChars + sl <= Size) {
920 cd = iconv_open(ToCode, FromCode);
928 if (
cd != (iconv_t)-1)
936 if (!strcasestr(CharacterTable,
"UTF-8")) {
939 for (
int i = 0; i < 128; i++)
943 const char *s = csc.
Convert(buf);
957 if (
cd != (iconv_t)-1 && From && *From) {
958 char *FromPtr = (
char *)From;
959 size_t FromLength = strlen(From);
962 int NewLength =
max(
length, FromLength * 2);
963 if (
char *NewBuffer = (
char *)realloc(
result, NewLength)) {
968 esyslog(
"ERROR: out of memory");
977 char *Converted = ToPtr;
978 while (FromLength > 0) {
979 if (iconv(
cd, &FromPtr, &FromLength, &ToPtr, &ToLength) ==
size_t(-1)) {
980 if (errno == E2BIG || errno == EILSEQ && ToLength < 1) {
984 size_t d = ToPtr -
result;
986 int NewLength =
length + r;
987 if (
char *NewBuffer = (
char *)realloc(result, NewLength)) {
989 Converted = result = NewBuffer;
992 esyslog(
"ERROR: out of memory");
998 if (errno == EILSEQ) {
1005 else if (errno != E2BIG)
1019 s = TakePointer ? (
char *)S : S ? strdup(S) : NULL;
1038 s = String.
s ? strdup(String.
s) : NULL;
1048 if (
this == &String)
1051 s = String.
s ? strdup(String.
s) : NULL;
1060 s = String ? strdup(String) : NULL;
1069 if (Index >= 0 && Index < l)
1085 if (!fmt || vasprintf(&buffer, fmt, ap) < 0) {
1086 esyslog(
"error in vasprintf('%s', ...)", fmt);
1087 buffer = strdup(
"???");
1096 if (!fmt || vasprintf(&buffer, fmt, ap) < 0) {
1097 esyslog(
"error in vasprintf('%s', ...)", fmt);
1098 buffer = strdup(
"???");
1106 WeekDay = WeekDay == 0 ? 6 : WeekDay - 1;
1107 if (0 <= WeekDay && WeekDay <= 6) {
1109 const char *day =
tr(
"MonTueWedThuFriSatSun");
1121 return WeekDayName(localtime_r(&t, &tm_r)->tm_wday);
1126 WeekDay = WeekDay == 0 ? 6 : WeekDay - 1;
1128 case 0:
return tr(
"Monday");
1129 case 1:
return tr(
"Tuesday");
1130 case 2:
return tr(
"Wednesday");
1131 case 3:
return tr(
"Thursday");
1132 case 4:
return tr(
"Friday");
1133 case 5:
return tr(
"Saturday");
1134 case 6:
return tr(
"Sunday");
1135 default:
return "???";
1151 tm *tm = localtime_r(&t, &tm_r);
1152 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);
1159 if (ctime_r(&t, buffer)) {
1160 buffer[strlen(buffer) - 1] = 0;
1170 tm *tm = localtime_r(&t, &tm_r);
1173 strftime(p,
sizeof(buf) - (p - buf),
"%d.%m.%Y", tm);
1181 tm *tm = localtime_r(&t, &tm_r);
1182 strftime(buf,
sizeof(buf),
"%d.%m.%y", tm);
1190 strftime(buf,
sizeof(buf),
"%R", localtime_r(&t, &tm_r));
1196 #define JPEGCOMPRESSMEM 500000
1216 int Used = jcd->
size;
1218 if (
uchar *NewBuffer = (
uchar *)realloc(jcd->
mem, NewSize)) {
1219 jcd->
size = NewSize;
1220 jcd->
mem = NewBuffer;
1223 esyslog(
"ERROR: out of memory");
1227 cinfo->dest->next_output_byte = jcd->
mem + Used;
1228 cinfo->dest->free_in_buffer = jcd->
size - Used;
1239 int Used = cinfo->dest->next_output_byte - jcd->
mem;
1240 if (Used < jcd->size) {
1243 jcd->
mem = NewBuffer;
1246 esyslog(
"ERROR: out of memory");
1255 else if (Quality > 100)
1258 jpeg_destination_mgr jdm;
1264 struct jpeg_compress_struct cinfo;
1265 struct jpeg_error_mgr jerr;
1266 cinfo.err = jpeg_std_error(&jerr);
1267 jpeg_create_compress(&cinfo);
1270 cinfo.client_data = &jcd;
1271 cinfo.image_width = Width;
1272 cinfo.image_height = Height;
1273 cinfo.input_components = 3;
1274 cinfo.in_color_space = JCS_RGB;
1276 jpeg_set_defaults(&cinfo);
1277 jpeg_set_quality(&cinfo, Quality,
true);
1278 jpeg_start_compress(&cinfo,
true);
1281 JSAMPROW rp[Height];
1282 for (
int k = 0; k < Height; k++)
1283 rp[k] = &Mem[rs * k];
1284 jpeg_write_scanlines(&cinfo, rp, Height);
1285 jpeg_finish_compress(&cinfo);
1286 jpeg_destroy_compress(&cinfo);
1294 const char *
cBase64Encoder::b64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1317 c |= (
data[
i] >> 4) & 0x0F;
1320 c = (
data[
i] << 2) & 0x3F;
1322 c |= (
data[
i] >> 6) & 0x03;
1421 Add(FileHandle, Out);
1426 if (FileHandle >= 0) {
1428 if (
pfd[i].fd == FileHandle &&
pfd[i].events == (Out ? POLLOUT : POLLIN))
1438 esyslog(
"ERROR: too many file handles in cPoller");
1470 if (strcmp(
result->d_name,
".") && strcmp(
result->d_name,
".."))
1486 for (
int i = 0; i <
Size(); i++) {
1487 if (!strcmp(s,
At(i)))
1495 for (
int i = 0; i <
Size(); i++)
1505 Load(Directory, DirsOnly);
1515 while ((e = d.
Next()) != NULL) {
1518 if (stat(
AddDirectory(Directory, e->d_name), &ds) == 0) {
1519 if (!S_ISDIR(ds.st_mode))
1523 Append(strdup(e->d_name));
1552 return Open(open(FileName, Flags, Mode));
1553 esyslog(
"ERROR: attempt to re-open %s", FileName);
1563 if (
f < FD_SETSIZE) {
1569 esyslog(
"ERROR: file descriptor %d already in files[]", f);
1573 esyslog(
"ERROR: file descriptor %d is larger than FD_SETSIZE (%d)",
f, FD_SETSIZE);
1577 esyslog(
"ERROR: attempt to re-open file descriptor %d", FileDes);
1600 for (
int i = 0; i <
maxFiles; i++) {
1604 if (0 <= FileDes && FileDes < FD_SETSIZE && !
files[FileDes])
1605 FD_SET(FileDes, &set);
1608 struct timeval timeout;
1609 timeout.tv_sec = TimeoutMs / 1000;
1610 timeout.tv_usec = (TimeoutMs % 1000) * 1000;
1611 return select(FD_SETSIZE, &set, NULL, NULL, &timeout) > 0 && (FileDes < 0 || FD_ISSET(FileDes, &set));
1617 struct timeval timeout;
1619 FD_SET(FileDes, &set);
1620 if (TimeoutMs >= 0) {
1621 if (TimeoutMs < 100)
1623 timeout.tv_sec = TimeoutMs / 1000;
1624 timeout.tv_usec = (TimeoutMs % 1000) * 1000;
1626 return select(FD_SETSIZE, &set, NULL, NULL, (TimeoutMs >= 0) ? &timeout : NULL) > 0 && FD_ISSET(FileDes, &set);
1632 struct timeval timeout;
1634 FD_SET(FileDes, &set);
1635 if (TimeoutMs < 100)
1638 timeout.tv_usec = TimeoutMs * 1000;
1639 return select(FD_SETSIZE, NULL, &set, NULL, &timeout) > 0 && FD_ISSET(FileDes, &set);
1676 if (ferror(
f) != 0) {
1682 if (fclose(
f) < 0) {
1701 #define WRITE_BUFFER KILOBYTE(800)
1716 fd = open(FileName, Flags, Mode);
1726 posix_fadvise(
fd, 0, 0, POSIX_FADV_RANDOM);
1737 posix_fadvise(
fd, 0, 0, POSIX_FADV_DONTNEED);
1741 return close(OldFd);
1752 #define FADVGRAN KILOBYTE(4) // AKA fadvise-chunk-size; PAGE_SIZE or getpagesize(2) would also work.
1753 #define READCHUNK MEGABYTE(8)
1763 return posix_fadvise(
fd, Offset - (
FADVGRAN - 1), Len + (
FADVGRAN - 1) * 2, POSIX_FADV_DONTNEED);
1768 if (Whence == SEEK_SET && Offset ==
curpos)
1770 curpos = lseek(
fd, Offset, Whence);
1788 if (bytesRead > 0) {
1795 if (jumped >= 0 && jumped <= (off_t)
readahead) {
1800 posix_fadvise(
fd,
curpos, readahead, POSIX_FADV_WILLNEED);
1804 if (readahead < Size * 32) {
1805 readahead = Size * 32;
1837 if (bytesWritten > 0) {
1878 return bytesWritten;
1886 if (File->
Open(FileName, Flags, Mode) < 0) {
1895 #define LOCKFILENAME ".lock-vdr"
1896 #define LOCKFILESTALETIME 600 // seconds before considering a lock file "stale"
1915 time_t Timeout = time(NULL) + WaitSeconds;
1917 f = open(
fileName, O_WRONLY | O_CREAT | O_EXCL, DEFFILEMODE);
1919 if (errno == EEXIST) {
1931 else if (errno != ENOENT) {
1943 }
while (
f < 0 && time(NULL) < Timeout);
1971 Object->
prev =
this;
1977 Object->
next =
this;
2032 if (Before && Before !=
objects) {
2065 if (From && To && From != To) {
2103 while (
object && Index-- > 0)
2104 object =
object->
Next();
2121 while (
object && i < n) {
2123 object =
object->
Next();
2127 for (i = 0; i < n; i++) {
2150 unsigned int hash =
hashfn(Id);
2161 if (hob->object == Object) {
2171 for (
int i = 0; i <
size; i++) {
int Find(const char *s) const
struct dirent * Next(void)
void Append(cListObject *Object)
ssize_t Write(const void *Data, size_t Size)
cString(const char *S=NULL, bool TakePointer=false)
bool Ready(bool Wait=true)
cListObject * Get(unsigned int Id) const
cList< cHashObject > * GetList(unsigned int Id) const
void Add(cListObject *Object, cListObject *After=NULL)
cTimeMs(int Ms=0)
Creates a timer with ms resolution and an initial timeout of Ms.
static char * systemCharacterTable
ssize_t Read(void *Data, size_t Size)
static const char * SystemCharacterTable(void)
static cString sprintf(const char *fmt,...) __attribute__((format(printf
off_t Seek(off_t Offset, int Whence)
static bool FileReady(int FileDes, int TimeoutMs=1000)
virtual void Append(char *Data)
void Del(cListObject *Object, unsigned int Id)
bool Add(int FileHandle, bool Out)
cString & Truncate(int Index)
Truncate the string at the given Index (if Index is < 0 it is counted from the end of the string)...
cReadDir(const char *Directory)
cUnbufferedFile is used for large files that are mainly written or read in a streaming manner...
const char * Convert(const char *From, char *To=NULL, size_t ToLength=0)
Converts the given Text from FromCode to ToCode (as set in the constructor).
cCharSetConv(const char *FromCode=NULL, const char *ToCode=NULL)
Sets up a character set converter to convert from FromCode to ToCode.
bool Poll(int TimeoutMs=0)
int Open(const char *FileName, int Flags, mode_t Mode=DEFFILEMODE)
cList< cHashObject > ** hashTable
static cString static cString vsprintf(const char *fmt, va_list &ap)
cBase64Encoder(const uchar *Data, int Length, int MaxResult=64)
Sets up a new base 64 encoder for the given Data, with the given Length.
virtual int Compare(const cListObject &ListObject) const
Must return 0 if this object is equal to ListObject, a positive value if it is "greater", and a negative value if it is "smaller".
T * Next(const T *object) const
cListObject * Next(void) const
bool TimedOut(void) const
static void SleepMs(int TimeoutMs)
Creates a cCondWait object and uses it to sleep for TimeoutMs milliseconds, immediately giving up the...
void Ins(cListObject *Object, cListObject *Before=NULL)
cFileNameList(const char *Directory=NULL, bool DirsOnly=false)
bool Open(const char *FileName, int Flags, mode_t Mode=DEFFILEMODE)
const char * NextLine(void)
Returns the next line of encoded data (terminated by '\0'), or NULL if there is no more encoded data...
static int Utf8CharLen(const char *s)
static void SetSystemCharacterTable(const char *CharacterTable)
static tThreadId ThreadId(void)
cPoller(int FileHandle=-1, bool Out=false)
cString & CompactChars(char c)
Compact any sequence of characters 'c' to a single character, and strip all of them from the beginnin...
unsigned int hashfn(unsigned int Id) const
bool Lock(int WaitSeconds=0)
bool Load(const char *Directory, bool DirsOnly=false)
void SetReadAhead(size_t ra)
cListObject * Prev(void) const
void Del(cListObject *Object, bool DeleteObject=true)
cListObject * Get(int Index) const
cLockFile(const char *Directory)
static bool FileReadyForWriting(int FileDes, int TimeoutMs=1000)
virtual void Move(int From, int To)
cSafeFile(const char *FileName)
void Sort(bool IgnoreCase=false)
void Add(cListObject *Object, unsigned int Id)
static uint64_t Now(void)
bool SetLength(int Length)
static cUnbufferedFile * Create(const char *FileName, int Flags, mode_t Mode=DEFFILEMODE)
cString & operator=(const cString &String)
char *& At(int Index) const
static bool AnyFileReady(int FileDes=-1, int TimeoutMs=1000)
int FadviseDrop(off_t Offset, off_t Len)
void Insert(cListObject *Object)
uint64_t Elapsed(void) const