33 #include <sys/types.h>
52 name =
new char[strlen(n)+1];
87 name =
new char[strlen(n)+1];
154 char sctime[256] = {0};
156 localtime_r(&ttmp,&tst);
157 asctime_r(&tst,sctime);
158 sctime[strlen(sctime)-1] = 0;
161 char sitime[256] = {0};
163 localtime_r(&ttmp,&tst);
164 asctime_r(&tst,sitime);
165 sitime[strlen(sitime)-1] = 0;
168 "//------------------------------------"
169 "------------------------------//\n"
171 "// File Header dump \n"
175 "// last changed on: %s (%d sec) \n"
176 "// index changed on: %s (%d sec) \n"
178 "// unreachable: %d \n"
179 "// first ofs: %d \n"
181 "//------------------------------------"
182 "------------------------------//\n",
194 name =
new char[strlen(n)+1];
203 valid =
Init(n, openmode, createmode, hashtab);
213 name =
new char[strlen(f.name)+1];
249 name =
new char[strlen(n)+1];
266 bool leaveopen = (openmode &
kPFEopen);
270 if (
stat(name, &st) == -1) {
271 if (errno == ENOENT) {
273 if (
Open(1,0,0,createmode) > 0) {
320 if (wasopen) *wasopen = 0;
323 char *fnam = (
char *)nam;
330 if (!nam && fFd > -1) {
334 if (
fcntl(fFd, F_GETFL, &omode) != -1) {
335 if (!(omode | O_WRONLY))
339 if (wasopen) *wasopen = 1;
346 if (
stat(fnam, &st) == -1) {
347 if (errno != ENOENT) {
363 char *pn = strstr(fnam,
"XXXXXX");
364 if (pn && (pn == (fnam + strlen(fnam) - 6))) {
365 if (opt > 0 && newfile) {
379 mode |= (O_TRUNC | O_RDWR) ;
402 fd =
open(fnam, mode, createmode);
409 int lockmode = (opt > 0) ? (F_WRLCK | F_RDLCK) : F_RDLCK;
412 while (lck && rc == -1) {
414 memset(&flck, 0,
sizeof(flck));
415 flck.l_type = lockmode;
416 flck.l_whence = SEEK_SET;
417 if ((rc =
fcntl(fd, F_SETLK, &flck)) == 0)
419 struct timespec lftp, rqtp = {1, 0};
420 while (nanosleep(&rqtp, &lftp) < 0 && errno == EINTR) {
421 rqtp.tv_sec = lftp.tv_sec;
422 rqtp.tv_nsec = lftp.tv_nsec;
426 if (errno == EACCES || errno == EAGAIN) {
430 memset(&flck, 0,
sizeof(flck));
431 flck.l_type = lockmode;
432 flck.l_whence = SEEK_SET;
433 if (
fcntl(fd,F_GETLK,&flck) != -1)
464 memset(&flck, 0,
sizeof(flck));
465 flck.l_type = F_UNLCK;
466 flck.l_whence = SEEK_SET;
467 if (
fcntl(fd, F_SETLK, &flck) == -1) {
510 if (
Open(1, &wasopen) < 0)
517 if (!wasopen)
Close();
533 char *bout =
new char[ltot];
562 if (bout)
delete[] bout;
564 (
const char *)&lp, (
const char *)<ot);
572 if (
lseek(fFd, 0, SEEK_SET) == -1) {
573 return Err(
kPFErrSeek,
"WriteHeader",
"SEEK_SET",(
const char *)&fFd);
578 while ((nw =
write(fFd, bout, ltot)) < 0 && errno == EINTR)
602 if (
Open(1, &wasopen) < 0)
610 if (ReadHeader(header) < 0) {
611 if (!wasopen)
Close();
614 if ((ofs =
lseek(fFd, 0, SEEK_CUR)) == -1) {
615 if (!wasopen)
Close();
616 return Err(
kPFErrSeek,
"WriteEntry",
"SEEK_CUR",(
const char *)&fFd);
622 if ((nw = WriteEnt(ofs, ent)) < 0) {
623 if (!wasopen)
Close();
631 if (WriteInd(indofs, ind) < 0) {
632 if (!wasopen)
Close();
638 header.
ctime = time(0);
640 if (WriteHeader(header) < 0) {
641 if (!wasopen)
Close();
644 if (!wasopen)
Close();
653 while (!found && nr > 0 && indofs > 0) {
654 nr = ReadInd(indofs, ind);
674 if ((nw = WriteEnt(ind.
entofs, ent)) < 0) {
675 if (!wasopen)
Close();
681 if ((entofs =
lseek(fFd, 0, SEEK_END)) == -1) {
682 if (!wasopen)
Close();
684 "SEEK_END",(
const char *)&fFd);
686 if ((nw = WriteEnt(entofs, ent)) < 0) {
687 if (!wasopen)
Close();
692 if (
lseek(fFd, wrtofs, SEEK_SET) == -1) {
693 if (!wasopen)
Close();
695 "SEEK_SET",(
const char *)&fFd);
698 while (
write(fFd, &status,
sizeof(
short)) < 0 &&
699 errno == EINTR) errno = 0;
701 if (Reset(wrtofs +
sizeof(
short), ind.
entsiz -
sizeof(
short)) < 0) {
702 if (!wasopen)
Close();
708 if (!wasopen)
Close();
710 "SEEK_SET",(
const char *)&fFd);
713 errno == EINTR) errno = 0;
716 if (
lseek(fFd, wrtofs, SEEK_SET) == -1) {
717 if (!wasopen)
Close();
719 "SEEK_SET",(
const char *)&fFd);
722 errno == EINTR) errno = 0;
724 errno == EINTR) errno = 0;
729 if (!wasopen)
Close();
731 "SEEK_SET",(
const char *)&fFd);
734 errno == EINTR) errno = 0;
737 header.
ctime = (ct > 0) ? ct : time(0);
739 if (!wasopen)
Close();
741 "SEEK_SET",(
const char *)&fFd);
744 errno == EINTR) errno = 0;
745 if (!wasopen)
Close();
751 if ((ofs =
lseek(fFd, 0, SEEK_END)) == -1) {
752 if (!wasopen)
Close();
754 "SEEK_END",(
const char *)&fFd);
756 if ((nw = WriteEnt(ofs, ent)) < 0) {
757 if (!wasopen)
Close();
763 if (WriteInd(ofs+nw, newind) < 0) {
764 if (!wasopen)
Close();
771 if (
lseek(fFd, wrtofs, SEEK_SET) == -1) {
772 if (!wasopen)
Close();
774 "SEEK_SET",(
const char *)&fFd);
777 errno == EINTR) errno = 0;
781 header.
ctime = time(0);
783 if (WriteHeader(header) < 0) {
784 if (!wasopen)
Close();
789 if (!wasopen)
Close();
796 int step,
bool reset)
815 if (ReadHeader(header) < 0) {
821 if (fHashTable && header.
itime > fHTutime) {
823 if (UpdateHashTable() < 0) {
836 if (ReadInd(*refofs, ind) < 0) {
847 if (ReadInd(indofs, ind) < 0) {
852 if (strlen(ind.
name) == strlen(tag)) {
853 if (!strncmp(ind.
name,tag,strlen(tag))) {
870 if (ReadEnt(ind.
entofs, ent) < 0) {
876 if (reset && ent.
cnt != 0) {
890 if (WriteEnt(ind.
entofs, ent) < 0) {
926 if (
Open(1 &wasopen) < 0)
931 if (ReadHeader(header) < 0) {
932 if (!wasopen)
Close();
937 if (fHashTable && header.
itime > fHTutime) {
939 if (UpdateHashTable() < 0) {
940 if (!wasopen)
Close();
950 kXR_int32 refofs = reftmp ? *reftmp : -1;
953 if (ReadInd(refofs, ind) < 0) {
954 if (!wasopen)
Close();
964 if (ReadInd(indofs, ind) < 0) {
965 if (!wasopen)
Close();
969 if (strlen(ind.
name) == strlen(tag)) {
970 if (!strncmp(ind.
name,tag,strlen(tag))) {
981 if (!found && opt == 1) {
992 if (ReadInd(iofs, ind) < 0) {
993 if (!wasopen)
Close();
1000 if (match > nmmax && ind.
entofs > 0) {
1012 if (ReadInd(refofs, ind) < 0) {
1013 if (!wasopen)
Close();
1026 if ((nr = ReadEnt(ind.
entofs, ent)) < 0) {
1027 if (!wasopen)
Close();
1036 if (!wasopen)
Close();
1052 if (
Open(1, &wasopen) < 0)
1059 if (ReadInd(ofs, ind) < 0) {
1060 if (!wasopen)
Close();
1065 if ((nr = ReadEnt(ind.
entofs, ent)) < 0) {
1066 if (!wasopen)
Close();
1074 if (!wasopen)
Close();
1087 if (!tag || !strlen(tag))
1096 if (ReadHeader(header) < 0) {
1102 if (fHashTable && header.
itime > fHTutime) {
1104 if (UpdateHashTable() < 0) {
1116 indofs = indtmp ? *indtmp : indofs;
1119 if (ReadInd(indofs, ind) < 0) {
1128 while (indofs > 0) {
1130 if (ReadInd(indofs, ind) < 0) {
1135 if (strlen(ind.
name) == strlen(tag)) {
1136 if (!strncmp(ind.
name,tag,strlen(tag))) {
1153 "SEEK_SET",(
const char *)&fFd);
1155 while (
write(fFd, &status,
sizeof(
short)) < 0 &&
1156 errno == EINTR) errno = 0;
1158 if (Reset(ind.
entofs +
sizeof(
short), ind.
entsiz -
sizeof(
short)) < 0) {
1164 if (WriteInd(indofs, ind) < 0) {
1176 if (WriteHeader(header) < 0) {
1209 if (ReadHeader(header) < 0) {
1215 if (header.
itime > fHTutime) {
1217 if (UpdateHashTable() < 0) {
1225 if (ReadInd(ofs, ind) < 0) {
1235 "SEEK_SET",(
const char *)&fFd);
1237 while (
write(fFd, &status,
sizeof(
short)) < 0 &&
1238 errno == EINTR) errno = 0;
1240 if (Reset(ind.
entofs +
sizeof(
short), ind.
entsiz -
sizeof(
short)) < 0) {
1246 if (WriteInd(ofs, ind) < 0) {
1258 if (WriteHeader(header) < 0) {
1275 if (
lseek(fFd, ofs, SEEK_SET) == -1)
1277 "SEEK_SET",(
const char *)&fFd);
1283 while (
write(fFd, &c, 1) < 0 && errno == EINTR)
1302 if (
lseek(fFd, ofs, SEEK_SET) == -1)
1304 "SEEK_SET",(
const char *)&fFd);
1312 char *bout =
new char[ltot];
1320 memcpy(bout+lp,&lnam,
sizeof(
kXR_int32));
1332 memcpy(bout+lp,ind.
name,lnam);
1336 if (bout)
delete[] bout;
1338 (
const char *)&lp, (
const char *)<ot);
1343 while ((nw =
write(fFd, bout, ltot)) < 0 && errno == EINTR)
1359 if (
lseek(fFd, ofs, SEEK_SET) == -1)
1361 "SEEK_SET",(
const char *)&fFd);
1369 char *bout =
new char[ltot];
1376 memcpy(bout+lp,&ent.
status,
sizeof(
short));
1377 lp +=
sizeof(short);
1379 memcpy(bout+lp,&ent.
cnt,
sizeof(
short));
1380 lp +=
sizeof(short);
1418 if (bout)
delete[] bout;
1420 (
const char *)&lp, (
const char *)<ot);
1425 while ((nw =
write(fFd, bout, ltot)) < 0 && errno == EINTR)
1442 if (
lseek(fFd, 0, SEEK_SET) == -1)
1444 "SEEK_SET",(
const char *)&fFd);
1493 if (
lseek(fFd, ofs, SEEK_SET) == -1)
1495 "SEEK_SET",(
const char *)&fFd);
1524 ind.
name =
new char[lnam+1];
1526 if ((nr =
read(fFd,ind.
name,lnam)) != lnam)
1548 if (
lseek(fFd, ofs, SEEK_SET) == -1)
1550 "SEEK_SET",(
const char *)&fFd);
1556 if ((nr =
read(fFd,&ent.
status,
sizeof(
short))) !=
sizeof(
short))
1560 if ((nr =
read(fFd,&ent.
cnt,
sizeof(
short))) !=
sizeof(
short))
1634 if (ReadHeader(hdr) < 0) {
1641 char sctime[256] = {0};
1642 time_t ttmp = hdr.
ctime;
1643 localtime_r(&ttmp,&tst);
1644 asctime_r(&tst,sctime);
1645 sctime[strlen(sctime)-1] = 0;
1646 char sitime[256] = {0};
1648 localtime_r(&ttmp,&tst);
1649 asctime_r(&tst,sitime);
1650 sitime[strlen(sitime)-1] = 0;
1653 FILE *out = oout ? (FILE *)oout : stdout;
1655 fprintf(out,
"//-----------------------------------------------------"
1656 "--------------------//\n");
1657 fprintf(out,
"//\n");
1658 fprintf(out,
"// File: %s\n",name);
1659 fprintf(out,
"// ID: %s\n",hdr.
fileID);
1660 fprintf(out,
"// Version: %d\n",hdr.
version);
1661 fprintf(out,
"// Last change : %s (%d sec)\n",sctime,hdr.
ctime);
1662 fprintf(out,
"// Index change: %s (%d sec)\n",sitime,hdr.
itime);
1663 fprintf(out,
"//\n");
1664 fprintf(out,
"// Number of Entries: %d\n",hdr.
entries);
1665 fprintf(out,
"// Bytes unreachable: %d\n",hdr.
jnksiz);
1666 fprintf(out,
"//\n");
1678 fprintf(out,
"// Special entries (%d):\n",ns);
1684 if (ReadInd(sofs[i], ind) < 0) {
1692 if (ReadEnt(ind.
entofs, ent) < 0) {
1699 char buf[2048] = {0};
1701 sprintf(buf,
"// #%d mod:%s",i+1,smt);
1703 sprintf(buf,
" name:%s",ind.
name);
1705 fprintf(out,
"%s\n",buffer.c_str());
1708 sprintf(buf,
"// buf");
1733 sprintf(buf,
":%d:%d:%d:%d",
1737 buffer +=
" (protected)";
1739 fprintf(out,
"%s\n",buf);
1742 fprintf(out,
"//\n");
1748 fprintf(out,
"// Normal entries (%d):\n",hdr.
entries-ns);
1756 if (ReadInd(nxtofs, ind) < 0) {
1764 if (ReadEnt(ind.
entofs, ent) < 0) {
1774 "// #:%d st:%d cn:%d buf:%d,%d,%d,%d mod:%s name:%s\n",
1783 fprintf(out,
"//\n");
1785 fprintf(out,
"//-----------------------------------------------------"
1786 "--------------------//\n");
1806 if (header.
jnksiz <= 0) {
1807 DEBUG(
"nothing to trim - return ");
1812 char *nbak = (
char *)fbak;
1815 nbak =
new char[strlen(name)+5];
1818 sprintf(nbak,
"%s.bak",name);
1819 DEBUG(
"backup file: "<<nbak);
1823 if (
rename(name,nbak) == -1)
1827 int fdnew =
Open(1);
1832 int fdbck =
Open(1,0,nbak);
1840 if (ReadHeader(header) < 0) {
1847 if (WriteHeader(header) < 0) {
1855 "SEEK_CUR",(
const char *)&fdnew);
1870 if (ReadInd(nxtofs,ind) < 0) {
1881 if (ReadEnt(ind.
entofs,ent) < 0) {
1890 if (WriteEnt(wrofs,ent) < 0) {
1896 if ((wrofs =
lseek(fdnew, 0, SEEK_CUR)) == -1) {
1899 "SEEK_CUR",(
const char *)&fdnew);
1910 if (WriteInd(lastofs,indlast) < 0) {
1925 if (WriteInd(wrofs,ind) < 0) {
1931 if ((wrofs =
lseek(fdnew, 0, SEEK_CUR)) == -1) {
1934 "SEEK_CUR",(
const char *)&fdnew);
1949 if (WriteHeader(header) < 0) {
1961 kXR_int32 XrdSutPFile::UpdateHashTable(
bool force)
1974 if (ReadHeader(header) < 0)
1978 if (!force && header.
itime < fHTutime)
1983 fHashTable->
Purge();
1995 while (nxtofs > 0) {
1996 if (ReadInd(nxtofs, ind) < 0)
2001 fHashTable->
Add(ind.
name,key);
2021 EPNAME(
"PFile::RemoveEntries");
2027 DEBUG(
"found "<<nm<<
" entries for tag '"<<tag<<
"'");
2030 int *ofs =
new int[nm];
2037 for (; i < nm ; i++) {
2039 DEBUG(
"entry for tag '"<<tag<<
"' removed from file");
2041 DEBUG(
"entry for tag '"<<tag<<
"' not found in file");
2045 DEBUG(
"no entry for tag '"<<tag<<
"' found in file: "<<
Name());
2073 if (
Open(1,&wasopen) < 0)
2078 if (ReadHeader(header) < 0) {
2079 if (!wasopen)
Close();
2095 if (ReadInd(indofs, ind) < 0) {
2096 if (!wasopen)
Close();
2103 if (!strncmp(ind.
name,tag,strlen(tag)))
2105 }
else if (opt == 1) {
2107 }
else if (opt == 2) {
2112 if (match > 0 && ind.
entofs > 0) {
2128 if (!wasopen)
Close();
2145 if (
Open(1,&wasopen) < 0)
2150 if (ReadHeader(header) < 0) {
2151 if (!wasopen)
Close();
2164 if (ReadInd(indofs, ind) < 0) {
2165 if (!wasopen)
Close();
2174 if (ReadEnt(ind.
entofs, ent) < 0) {
2175 if (!wasopen)
Close();
2197 if (!wasopen)
Close();
2204 const char *em1,
const char *em2)
2211 int fd = 0, lp = 0, lt = 0;
2221 "XrdSutPFile::%s: bad input arguments",loc);
2225 "XrdSutPFile::%s: file already open"
2226 " in incompatible mode",loc);
2230 "XrdSutPFile::%s: file %s does not exists",
2235 "XrdSutPFile::%s: error renaming file %s to %s"
2236 " (%s)",loc,em1,em2,errbuf);
2240 "XrdSutPFile::%s: cannot file %s (%s)",
2245 "XrdSutPFile::%s: cannot open file %s (%s)",
2250 "XrdSutPFile::%s: file is not open", loc);
2255 "XrdSutPFile::%s: cannot lock file descriptor %d (%s)",
2261 "XrdSutPFile::%s: cannot unlock file descriptor %d (%s)",
2267 "XrdSutPFile::%s: file %s is locked by process %d",
2273 "XrdSutPFile::%s: lseek %s error on descriptor %d (%s)",
2279 "XrdSutPFile::%s: read error on descriptor %d (%s)",
2284 "XrdSutPFile::%s: out of memory (%s)",
2291 "XrdSutPFile::%s: length mismatch: %d (expected: %d)",
2296 "XrdSutPFile::%s: bad option: %s", loc,em1);
2299 DEBUG(
"unknown error code: "<<code);
off_t lseek(int fildes, off_t offset, int whence)
int stat(const char *path, struct stat *buf)
int open(const char *path, int oflag,...)
int fcntl(int fd, int cmd,...)
int rename(const char *oldpath, const char *newpath)
ssize_t write(int fildes, const void *buf, size_t nbyte)
ssize_t read(int fildes, void *buf, size_t nbyte)
int XrdSutTimeString(int t, char *st, int opt)
const char * XrdSysE2T(int errcode)
T * Add(const char *KeyVal, T *KeyData, const int LifeTime=0, XrdOucHash_Options opt=Hash_default)
T * Find(const char *KeyVal, time_t *KeyTime=0)
const char * c_str() const
void assign(const char *s, int j, int k=-1)
int matches(const char *s, char wch=' *')
void SetName(const char *n=0)
XrdSutPFEntInd & operator=(const XrdSutPFEntInd ei)
XrdSutPFEntInd(const char *n=0, kXR_int32 no=0, kXR_int32 eo=0, kXR_int32 es=0)
void SetName(const char *n=0)
kXR_int32 UpdateCount(const char *nm, int *cnt=0, int step=1, bool reset=0)
kXR_int32 SearchSpecialEntries(kXR_int32 *ofs=0, kXR_int32 nofs=1)
kXR_int32 Browse(void *out=0)
kXR_int32 Trim(const char *fbak=0)
kXR_int32 WriteEntry(XrdSutPFEntry ent)
kXR_int32 RemoveEntries(const char *name, char opt)
const char * Name() const
kXR_int32 RetrieveHeader(XrdSutPFHeader &hd)
kXR_int32 SearchEntries(const char *name, char opt, kXR_int32 *ofs=0, kXR_int32 nofs=1)
kXR_int32 Close(kXR_int32 d=-1)
kXR_int32 ReadEntry(const char *name, XrdSutPFEntry &ent, int opt=0)
XrdSutPFile(const char *n, kXR_int32 openmode=kPFEcreate, kXR_int32 createmode=0600, bool hashtab=1)
bool Init(const char *n, kXR_int32 openmode=kPFEcreate, kXR_int32 createmode=0600, bool hashtab=1)
kXR_int32 UpdateHeader(XrdSutPFHeader hd)
kXR_int32 RemoveEntry(const char *name)
kXR_int32 Open(kXR_int32 opt, bool *wasopen=0, const char *nam=0, kXR_int32 createmode=0600)