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);
179 for (
char *p = s + strlen(s) - 1; p >= s; p--) {
196 memmove(p + 1, q, strlen(q) + 1);
200 memmove(s, t, strlen(t) + 1);
211 if (strchr(chars, *p)) {
213 buffer =
MALLOC(
char, 2 * strlen(s) + 1);
214 t = buffer + (p - s);
215 s = strcpy(buffer, s);
239 const char *se = s + strlen(s) - 1;
240 const char *pe = p + strlen(p) - 1;
242 if (*pe-- != *se-- || (se < s && pe >= p))
277 int64_t n = strtoll(s, &t, 10);
293 if (strcmp(*a, s) == 0)
303 return cString::sprintf(
"%s/%s", DirName && *DirName ? DirName :
".", FileName);
306 #define DECIMAL_POINT_C '.'
310 static lconv *loc = localeconv();
312 char buf[strlen(s) + 1];
316 *p = *loc->decimal_point;
331 static lconv *loc = localeconv();
333 snprintf(buf,
sizeof(buf), Format, d);
342 snprintf(buf,
sizeof(buf),
"%d", n);
349 if (stat(File1, &st) == 0) {
350 dev_t dev1 = st.st_dev;
351 if (stat(File2, &st) == 0)
352 return st.st_dev == dev1;
366 struct statfs statFs;
367 if (statfs(Directory, &statFs) == 0) {
368 double blocksPerMeg = 1024.0 * 1024.0 / statFs.f_bsize;
370 *UsedMB = int((statFs.f_blocks - statFs.f_bfree) / blocksPerMeg);
371 Free = int(statFs.f_bavail / blocksPerMeg);
381 if (stat(DirName, &ds) == 0) {
382 if (S_ISDIR(ds.st_mode)) {
383 if (access(DirName, R_OK | W_OK | X_OK) == 0)
386 esyslog(
"ERROR: can't access %s", DirName);
389 esyslog(
"ERROR: %s is not a directory", DirName);
396 bool MakeDirs(
const char *FileName,
bool IsDirectory)
399 char *s = strdup(FileName);
403 while ((p = strchr(p,
'/')) != NULL || IsDirectory) {
407 if (stat(s, &fs) != 0 || !S_ISDIR(fs.st_mode)) {
408 dsyslog(
"creating directory %s", s);
409 if (mkdir(s, ACCESSPERMS) == -1) {
427 if (stat(FileName, &st) == 0) {
428 if (S_ISDIR(st.st_mode)) {
432 while ((e = d.
Next()) != NULL) {
434 if (FollowSymlinks) {
436 if (lstat(buffer, &st2) == 0) {
437 if (S_ISLNK(st2.st_mode)) {
438 int size = st2.st_size + 1;
439 char *l =
MALLOC(
char, size);
440 int n = readlink(buffer, l, size - 1);
454 else if (errno != ENOENT) {
459 dsyslog(
"removing %s", *buffer);
460 if (
remove(buffer) < 0)
469 dsyslog(
"removing %s", FileName);
470 if (
remove(FileName) < 0) {
475 else if (errno != ENOENT) {
484 bool HasIgnoredFiles =
false;
489 while ((e = d.
Next()) != NULL) {
490 if (strcmp(e->d_name,
"lost+found")) {
493 if (stat(buffer, &st) == 0) {
494 if (S_ISDIR(st.st_mode)) {
498 else if (RemoveThis && IgnoreFiles &&
StrInArray(IgnoreFiles, e->d_name))
499 HasIgnoredFiles =
true;
509 if (RemoveThis && empty) {
510 if (HasIgnoredFiles) {
511 while (*IgnoreFiles) {
513 if (access(buffer, F_OK) == 0) {
514 dsyslog(
"removing %s", *buffer);
515 if (
remove(buffer) < 0) {
523 dsyslog(
"removing %s", DirName);
524 if (
remove(DirName) < 0) {
542 while (size >= 0 && (e = d.
Next()) != NULL) {
545 if (stat(buffer, &st) == 0) {
546 if (S_ISDIR(st.st_mode)) {
572 char *TargetName = canonicalize_file_name(FileName);
575 TargetName = strdup(FileName);
584 for (
int n = 0; n < 10; n++) {
590 if (access(buf, F_OK) != 0) {
592 gettimeofday(&tp1, NULL);
593 int f = open(buf, O_WRONLY | O_CREAT, DEFFILEMODE);
596 if (fdatasync(f) < 0)
600 gettimeofday(&tp2, NULL);
601 double seconds = (((
long long)tp2.tv_sec * 1000000 + tp2.tv_usec) - ((
long long)tp1.tv_sec * 1000000 + tp1.tv_usec)) / 1000000.0;
603 dsyslog(
"SpinUpDisk took %.2f seconds", seconds);
610 esyslog(
"ERROR: SpinUpDisk failed");
616 if (utime(FileName, NULL) == -1 && errno != ENOENT)
623 if (stat(FileName, &fs) == 0)
631 if (stat(FileName, &fs) == 0)
648 #if _POSIX_TIMERS > 0 && defined(_POSIX_MONOTONIC_CLOCK)
649 #define MIN_RESOLUTION 5 // ms
650 static bool initialized =
false;
651 static bool monotonic =
false;
655 if (clock_getres(CLOCK_MONOTONIC, &tp) == 0) {
656 long Resolution = tp.tv_nsec;
658 if (tp.tv_sec == 0 && tp.tv_nsec <= MIN_RESOLUTION * 1000000) {
659 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) {
660 dsyslog(
"cTimeMs: using monotonic clock (resolution is %ld ns)", Resolution);
664 esyslog(
"cTimeMs: clock_gettime(CLOCK_MONOTONIC) failed");
667 dsyslog(
"cTimeMs: not using monotonic clock - resolution is too bad (%ld s %ld ns)", tp.tv_sec, tp.tv_nsec);
670 esyslog(
"cTimeMs: clock_getres(CLOCK_MONOTONIC) failed");
674 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
675 return (uint64_t(tp.tv_sec)) * 1000 + tp.tv_nsec / 1000000;
676 esyslog(
"cTimeMs: clock_gettime(CLOCK_MONOTONIC) failed");
681 # warning Posix monotonic clock not available
684 if (gettimeofday(&t, NULL) == 0)
685 return (uint64_t(t.tv_sec)) * 1000 + t.tv_usec / 1000;
712 #define MT(s, m, v) ((*(s) & (m)) == (v)) // Mask Test
713 if (
MT(s, 0xE0, 0xC0) &&
MT(s + 1, 0xC0, 0x80))
715 if (
MT(s, 0xF0, 0xE0) &&
MT(s + 1, 0xC0, 0x80) &&
MT(s + 2, 0xC0, 0x80))
717 if (
MT(s, 0xF8, 0xF0) &&
MT(s + 1, 0xC0, 0x80) &&
MT(s + 2, 0xC0, 0x80) &&
MT(s + 3, 0xC0, 0x80))
729 case 2:
return ((*s & 0x1F) << 6) | (*(s + 1) & 0x3F);
730 case 3:
return ((*s & 0x0F) << 12) | ((*(s + 1) & 0x3F) << 6) | (*(s + 2) & 0x3F);
731 case 4:
return ((*s & 0x07) << 18) | ((*(s + 1) & 0x3F) << 12) | ((*(s + 2) & 0x3F) << 6) | (*(s + 3) & 0x3F);
746 *s++ = ((c >> 6) & 0x1F) | 0xC0;
747 *s = (c & 0x3F) | 0x80;
753 *s++ = ((c >> 12) & 0x0F) | 0xE0;
754 *s++ = ((c >> 6) & 0x3F) | 0x80;
755 *s = (c & 0x3F) | 0x80;
761 *s++ = ((c >> 18) & 0x07) | 0xF0;
762 *s++ = ((c >> 12) & 0x3F) | 0x80;
763 *s++ = ((c >> 6) & 0x3F) | 0x80;
764 *s = (c & 0x3F) | 0x80;
776 while (*s && Symbols--) {
818 while (*s && --Size > 0) {
820 *a++ = (
uchar)(*s++);
837 while (*a && NumChars < Size) {
838 if (Max >= 0 && NumSyms++ >= Max)
846 if (NumChars + sl <= Size) {
871 cd = iconv_open(ToCode, FromCode);
879 if (
cd != (iconv_t)-1)
887 if (!strcasestr(CharacterTable,
"UTF-8")) {
890 for (
int i = 0; i < 128; i++)
894 const char *s = csc.
Convert(buf);
908 if (
cd != (iconv_t)-1 && From && *From) {
909 char *FromPtr = (
char *)From;
910 size_t FromLength = strlen(From);
913 int NewLength =
max(
length, FromLength * 2);
914 if (
char *NewBuffer = (
char *)realloc(
result, NewLength)) {
919 esyslog(
"ERROR: out of memory");
928 char *Converted = ToPtr;
929 while (FromLength > 0) {
930 if (iconv(
cd, &FromPtr, &FromLength, &ToPtr, &ToLength) ==
size_t(-1)) {
931 if (errno == E2BIG || errno == EILSEQ && ToLength < 1) {
935 size_t d = ToPtr -
result;
937 int NewLength =
length + r;
938 if (
char *NewBuffer = (
char *)realloc(result, NewLength)) {
940 Converted = result = NewBuffer;
943 esyslog(
"ERROR: out of memory");
949 if (errno == EILSEQ) {
956 else if (errno != E2BIG)
970 s = TakePointer ? (
char *)S : S ? strdup(S) : NULL;
975 s = String.
s ? strdup(String.
s) : NULL;
988 s = String.
s ? strdup(String.
s) : NULL;
997 s = String ? strdup(String) : NULL;
1006 if (Index >= 0 && Index < l)
1016 if (!fmt || vasprintf(&buffer, fmt, ap) < 0) {
1017 esyslog(
"error in vasprintf('%s', ...)", fmt);
1018 buffer = strdup(
"???");
1027 if (!fmt || vasprintf(&buffer, fmt, ap) < 0) {
1028 esyslog(
"error in vasprintf('%s', ...)", fmt);
1029 buffer = strdup(
"???");
1037 WeekDay = WeekDay == 0 ? 6 : WeekDay - 1;
1038 if (0 <= WeekDay && WeekDay <= 6) {
1040 const char *day =
tr(
"MonTueWedThuFriSatSun");
1052 return WeekDayName(localtime_r(&t, &tm_r)->tm_wday);
1057 WeekDay = WeekDay == 0 ? 6 : WeekDay - 1;
1059 case 0:
return tr(
"Monday");
1060 case 1:
return tr(
"Tuesday");
1061 case 2:
return tr(
"Wednesday");
1062 case 3:
return tr(
"Thursday");
1063 case 4:
return tr(
"Friday");
1064 case 5:
return tr(
"Saturday");
1065 case 6:
return tr(
"Sunday");
1066 default:
return "???";
1082 tm *tm = localtime_r(&t, &tm_r);
1083 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);
1090 if (ctime_r(&t, buffer)) {
1091 buffer[strlen(buffer) - 1] = 0;
1101 tm *tm = localtime_r(&t, &tm_r);
1104 strftime(p,
sizeof(buf) - (p - buf),
"%d.%m.%Y", tm);
1112 tm *tm = localtime_r(&t, &tm_r);
1113 strftime(buf,
sizeof(buf),
"%d.%m.%y", tm);
1121 strftime(buf,
sizeof(buf),
"%R", localtime_r(&t, &tm_r));
1127 #define JPEGCOMPRESSMEM 500000
1147 int Used = jcd->
size;
1149 if (
uchar *NewBuffer = (
uchar *)realloc(jcd->
mem, NewSize)) {
1150 jcd->
size = NewSize;
1151 jcd->
mem = NewBuffer;
1154 esyslog(
"ERROR: out of memory");
1158 cinfo->dest->next_output_byte = jcd->
mem + Used;
1159 cinfo->dest->free_in_buffer = jcd->
size - Used;
1170 int Used = cinfo->dest->next_output_byte - jcd->
mem;
1171 if (Used < jcd->size) {
1174 jcd->
mem = NewBuffer;
1177 esyslog(
"ERROR: out of memory");
1186 else if (Quality > 100)
1189 jpeg_destination_mgr jdm;
1195 struct jpeg_compress_struct cinfo;
1196 struct jpeg_error_mgr jerr;
1197 cinfo.err = jpeg_std_error(&jerr);
1198 jpeg_create_compress(&cinfo);
1201 cinfo.client_data = &jcd;
1202 cinfo.image_width = Width;
1203 cinfo.image_height = Height;
1204 cinfo.input_components = 3;
1205 cinfo.in_color_space = JCS_RGB;
1207 jpeg_set_defaults(&cinfo);
1208 jpeg_set_quality(&cinfo, Quality,
true);
1209 jpeg_start_compress(&cinfo,
true);
1212 JSAMPROW rp[Height];
1213 for (
int k = 0; k < Height; k++)
1214 rp[k] = &Mem[rs * k];
1215 jpeg_write_scanlines(&cinfo, rp, Height);
1216 jpeg_finish_compress(&cinfo);
1217 jpeg_destroy_compress(&cinfo);
1225 const char *
cBase64Encoder::b64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1248 c |= (
data[
i] >> 4) & 0x0F;
1251 c = (
data[
i] << 2) & 0x3F;
1253 c |= (
data[
i] >> 6) & 0x03;
1352 Add(FileHandle, Out);
1357 if (FileHandle >= 0) {
1359 if (
pfd[i].fd == FileHandle &&
pfd[i].events == (Out ? POLLOUT : POLLIN))
1369 esyslog(
"ERROR: too many file handles in cPoller");
1401 if (strcmp(
result->d_name,
".") && strcmp(
result->d_name,
".."))
1417 for (
int i = 0; i <
Size(); i++) {
1418 if (!strcmp(s,
At(i)))
1426 for (
int i = 0; i <
Size(); i++)
1436 Load(Directory, DirsOnly);
1446 while ((e = d.
Next()) != NULL) {
1449 if (stat(
AddDirectory(Directory, e->d_name), &ds) == 0) {
1450 if (!S_ISDIR(ds.st_mode))
1454 Append(strdup(e->d_name));
1483 return Open(open(FileName, Flags, Mode));
1484 esyslog(
"ERROR: attempt to re-open %s", FileName);
1494 if (
f < FD_SETSIZE) {
1500 esyslog(
"ERROR: file descriptor %d already in files[]", f);
1504 esyslog(
"ERROR: file descriptor %d is larger than FD_SETSIZE (%d)",
f, FD_SETSIZE);
1508 esyslog(
"ERROR: attempt to re-open file descriptor %d", FileDes);
1531 for (
int i = 0; i <
maxFiles; i++) {
1535 if (0 <= FileDes && FileDes < FD_SETSIZE && !
files[FileDes])
1536 FD_SET(FileDes, &set);
1539 struct timeval timeout;
1540 timeout.tv_sec = TimeoutMs / 1000;
1541 timeout.tv_usec = (TimeoutMs % 1000) * 1000;
1542 return select(FD_SETSIZE, &set, NULL, NULL, &timeout) > 0 && (FileDes < 0 || FD_ISSET(FileDes, &set));
1548 struct timeval timeout;
1550 FD_SET(FileDes, &set);
1551 if (TimeoutMs >= 0) {
1552 if (TimeoutMs < 100)
1554 timeout.tv_sec = TimeoutMs / 1000;
1555 timeout.tv_usec = (TimeoutMs % 1000) * 1000;
1557 return select(FD_SETSIZE, &set, NULL, NULL, (TimeoutMs >= 0) ? &timeout : NULL) > 0 && FD_ISSET(FileDes, &set);
1563 struct timeval timeout;
1565 FD_SET(FileDes, &set);
1566 if (TimeoutMs < 100)
1569 timeout.tv_usec = TimeoutMs * 1000;
1570 return select(FD_SETSIZE, NULL, &set, NULL, &timeout) > 0 && FD_ISSET(FileDes, &set);
1607 if (ferror(
f) != 0) {
1613 if (fclose(
f) < 0) {
1632 #define WRITE_BUFFER KILOBYTE(800)
1647 fd = open(FileName, Flags, Mode);
1657 posix_fadvise(
fd, 0, 0, POSIX_FADV_RANDOM);
1668 posix_fadvise(
fd, 0, 0, POSIX_FADV_DONTNEED);
1672 return close(OldFd);
1683 #define FADVGRAN KILOBYTE(4) // AKA fadvise-chunk-size; PAGE_SIZE or getpagesize(2) would also work.
1684 #define READCHUNK MEGABYTE(8)
1694 return posix_fadvise(
fd, Offset - (
FADVGRAN - 1), Len + (
FADVGRAN - 1) * 2, POSIX_FADV_DONTNEED);
1699 if (Whence == SEEK_SET && Offset ==
curpos)
1701 curpos = lseek(
fd, Offset, Whence);
1719 if (bytesRead > 0) {
1726 if (jumped >= 0 && jumped <= (off_t)
readahead) {
1731 posix_fadvise(
fd,
curpos, readahead, POSIX_FADV_WILLNEED);
1735 if (readahead < Size * 32) {
1736 readahead = Size * 32;
1768 if (bytesWritten > 0) {
1809 return bytesWritten;
1817 if (File->
Open(FileName, Flags, Mode) < 0) {
1826 #define LOCKFILENAME ".lock-vdr"
1827 #define LOCKFILESTALETIME 600 // seconds before considering a lock file "stale"
1846 time_t Timeout = time(NULL) + WaitSeconds;
1848 f = open(
fileName, O_WRONLY | O_CREAT | O_EXCL, DEFFILEMODE);
1850 if (errno == EEXIST) {
1862 else if (errno != ENOENT) {
1874 }
while (
f < 0 && time(NULL) < Timeout);
1902 Object->
prev =
this;
1908 Object->
next =
this;
1963 if (Before && Before !=
objects) {
1996 if (From && To && From != To) {
2034 while (
object && Index-- > 0)
2035 object =
object->
Next();
2052 while (
object && i < n) {
2054 object =
object->
Next();
2058 for (i = 0; i < n; i++) {
2081 unsigned int hash =
hashfn(Id);
2092 if (hob->object == Object) {
2102 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
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 void SetSystemCharacterTable(const char *CharacterTable)
static tThreadId ThreadId(void)
cPoller(int FileHandle=-1, bool Out=false)
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)