fileutl.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: fileutl.cc,v 1.5 1999/10/24 06:53:12 jgg Exp $
  4. /* ######################################################################
  5. File Utilities
  6. CopyFile - Buffered copy of a single file
  7. GetLock - dpkg compatible lock file manipulation (fcntl)
  8. This source is placed in the Public Domain, do with it what you will
  9. It was originally written by Jason Gunthorpe.
  10. ##################################################################### */
  11. /*}}}*/
  12. // Include Files /*{{{*/
  13. #ifdef __GNUG__
  14. #pragma implementation "dsync/fileutl.h"
  15. #endif
  16. #include <dsync/fileutl.h>
  17. #include <dsync/error.h>
  18. #include <unistd.h>
  19. #include <sys/stat.h>
  20. #include <sys/fcntl.h>
  21. #include <sys/types.h>
  22. #include <sys/time.h>
  23. #include <signal.h>
  24. #include <wait.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <iostream>
  28. using namespace std;
  29. /*}}}*/
  30. // CopyFile - Buffered copy of a file /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* The caller is expected to set things so that failure causes erasure */
  33. bool CopyFile(FileFd &From,FileFd &To)
  34. {
  35. if (From.IsOpen() == false || To.IsOpen() == false)
  36. return false;
  37. // Buffered copy between fds
  38. unsigned char *Buf = new unsigned char[64000];
  39. unsigned long Size = From.Size();
  40. while (Size != 0)
  41. {
  42. unsigned long ToRead = Size;
  43. if (Size > 64000)
  44. ToRead = 64000;
  45. if (From.Read(Buf,ToRead) == false ||
  46. To.Write(Buf,ToRead) == false)
  47. {
  48. delete [] Buf;
  49. return false;
  50. }
  51. Size -= ToRead;
  52. }
  53. delete [] Buf;
  54. return true;
  55. }
  56. /*}}}*/
  57. // GetLock - Gets a lock file /*{{{*/
  58. // ---------------------------------------------------------------------
  59. /* This will create an empty file of the given name and lock it. Once this
  60. is done all other calls to GetLock in any other process will fail with
  61. -1. The return result is the fd of the file, the call should call
  62. close at some time. */
  63. int GetLock(string File,bool Errors)
  64. {
  65. int FD = open(File.c_str(),O_RDWR | O_CREAT | O_TRUNC,0640);
  66. if (FD < 0)
  67. {
  68. if (Errors == true)
  69. _error->Errno("open","Could not open lock file %s",File.c_str());
  70. return -1;
  71. }
  72. // Aquire a write lock
  73. struct flock fl;
  74. fl.l_type = F_WRLCK;
  75. fl.l_whence = SEEK_SET;
  76. fl.l_start = 0;
  77. fl.l_len = 0;
  78. if (fcntl(FD,F_SETLK,&fl) == -1)
  79. {
  80. if (errno == ENOLCK)
  81. {
  82. _error->Warning("Not using locking for nfs mounted lock file %s",File.c_str());
  83. return true;
  84. }
  85. if (Errors == true)
  86. _error->Errno("open","Could not get lock %s",File.c_str());
  87. close(FD);
  88. return -1;
  89. }
  90. return FD;
  91. }
  92. /*}}}*/
  93. // FileExists - Check if a file exists /*{{{*/
  94. // ---------------------------------------------------------------------
  95. /* */
  96. bool FileExists(string File)
  97. {
  98. struct stat Buf;
  99. if (stat(File.c_str(),&Buf) != 0)
  100. return false;
  101. return true;
  102. }
  103. /*}}}*/
  104. // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/
  105. // ---------------------------------------------------------------------
  106. /* We return / on failure. */
  107. string SafeGetCWD()
  108. {
  109. // Stash the current dir.
  110. char S[300];
  111. S[0] = 0;
  112. if (getcwd(S,sizeof(S)-2) == 0)
  113. return "/";
  114. unsigned int Len = strlen(S);
  115. S[Len] = '/';
  116. S[Len+1] = 0;
  117. return S;
  118. }
  119. /*}}}*/
  120. // flNotDir - Strip the directory from the filename /*{{{*/
  121. // ---------------------------------------------------------------------
  122. /* */
  123. string flNotDir(string File)
  124. {
  125. string::size_type Res = File.rfind('/');
  126. if (Res == string::npos)
  127. return File;
  128. Res++;
  129. return string(File,Res,Res - File.length());
  130. }
  131. /*}}}*/
  132. // flNotFile - Strip the file from the directory name /*{{{*/
  133. // ---------------------------------------------------------------------
  134. /* */
  135. string flNotFile(string File)
  136. {
  137. string::size_type Res = File.rfind('/');
  138. if (Res == string::npos)
  139. return File;
  140. Res++;
  141. return string(File,0,Res);
  142. }
  143. /*}}}*/
  144. // flNoLink - If file is a symlink then deref it /*{{{*/
  145. // ---------------------------------------------------------------------
  146. /* If the name is not a link then the returned path is the input. */
  147. string flNoLink(string File)
  148. {
  149. struct stat St;
  150. if (lstat(File.c_str(),&St) != 0 || S_ISLNK(St.st_mode) == 0)
  151. return File;
  152. if (stat(File.c_str(),&St) != 0)
  153. return File;
  154. /* Loop resolving the link. There is no need to limit the number of
  155. loops because the stat call above ensures that the symlink is not
  156. circular */
  157. char Buffer[1024];
  158. string NFile = File;
  159. while (1)
  160. {
  161. // Read the link
  162. int Res;
  163. if ((Res = readlink(NFile.c_str(),Buffer,sizeof(Buffer))) <= 0 ||
  164. (unsigned)Res >= sizeof(Buffer))
  165. return File;
  166. // Append or replace the previous path
  167. Buffer[Res] = 0;
  168. if (Buffer[0] == '/')
  169. NFile = Buffer;
  170. else
  171. NFile = flNotFile(NFile) + Buffer;
  172. // See if we are done
  173. if (lstat(NFile.c_str(),&St) != 0)
  174. return File;
  175. if (S_ISLNK(St.st_mode) == 0)
  176. return NFile;
  177. }
  178. }
  179. /*}}}*/
  180. // SetCloseExec - Set the close on exec flag /*{{{*/
  181. // ---------------------------------------------------------------------
  182. /* */
  183. void SetCloseExec(int Fd,bool Close)
  184. {
  185. if (fcntl(Fd,F_SETFD,(Close == false)?0:FD_CLOEXEC) != 0)
  186. {
  187. cerr << "FATAL -> Could not set close on exec " << strerror(errno) << endl;
  188. exit(100);
  189. }
  190. }
  191. /*}}}*/
  192. // SetNonBlock - Set the nonblocking flag /*{{{*/
  193. // ---------------------------------------------------------------------
  194. /* */
  195. void SetNonBlock(int Fd,bool Block)
  196. {
  197. int Flags = fcntl(Fd,F_GETFL) & (~O_NONBLOCK);
  198. if (fcntl(Fd,F_SETFL,Flags | ((Block == false)?0:O_NONBLOCK)) != 0)
  199. {
  200. cerr << "FATAL -> Could not set non-blocking flag " << strerror(errno) << endl;
  201. exit(100);
  202. }
  203. }
  204. /*}}}*/
  205. // WaitFd - Wait for a FD to become readable /*{{{*/
  206. // ---------------------------------------------------------------------
  207. /* This waits for a FD to become readable using select. It is usefull for
  208. applications making use of non-blocking sockets. The timeout is
  209. in seconds. */
  210. bool WaitFd(int Fd,bool write,unsigned long timeout)
  211. {
  212. fd_set Set;
  213. struct timeval tv;
  214. FD_ZERO(&Set);
  215. FD_SET(Fd,&Set);
  216. tv.tv_sec = timeout;
  217. tv.tv_usec = 0;
  218. if (write == true)
  219. {
  220. int Res;
  221. do
  222. {
  223. Res = select(Fd+1,0,&Set,0,(timeout != 0?&tv:0));
  224. }
  225. while (Res < 0 && errno == EINTR);
  226. if (Res <= 0)
  227. return false;
  228. }
  229. else
  230. {
  231. int Res;
  232. do
  233. {
  234. Res = select(Fd+1,&Set,0,0,(timeout != 0?&tv:0));
  235. }
  236. while (Res < 0 && errno == EINTR);
  237. if (Res <= 0)
  238. return false;
  239. }
  240. return true;
  241. }
  242. /*}}}*/
  243. // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/
  244. // ---------------------------------------------------------------------
  245. /* This is used if you want to cleanse the environment for the forked
  246. child, it fixes up the important signals and nukes all of the fds,
  247. otherwise acts like normal fork. */
  248. int ExecFork()
  249. {
  250. // Fork off the process
  251. pid_t Process = fork();
  252. if (Process < 0)
  253. {
  254. cerr << "FATAL -> Failed to fork." << endl;
  255. exit(100);
  256. }
  257. // Spawn the subprocess
  258. if (Process == 0)
  259. {
  260. // Setup the signals
  261. signal(SIGPIPE,SIG_DFL);
  262. signal(SIGQUIT,SIG_DFL);
  263. signal(SIGINT,SIG_DFL);
  264. signal(SIGWINCH,SIG_DFL);
  265. signal(SIGCONT,SIG_DFL);
  266. signal(SIGTSTP,SIG_DFL);
  267. // Close all of our FDs - just in case
  268. for (int K = 3; K != 40; K++)
  269. fcntl(K,F_SETFD,FD_CLOEXEC);
  270. }
  271. return Process;
  272. }
  273. /*}}}*/
  274. // ExecWait - Fancy waitpid /*{{{*/
  275. // ---------------------------------------------------------------------
  276. /* Waits for the given sub process. If Reap is set the no errors are
  277. generated. Otherwise a failed subprocess will generate a proper descriptive
  278. message */
  279. bool ExecWait(int Pid,const char *Name,bool Reap)
  280. {
  281. if (Pid <= 1)
  282. return true;
  283. // Wait and collect the error code
  284. int Status;
  285. while (waitpid(Pid,&Status,0) != Pid)
  286. {
  287. if (errno == EINTR)
  288. continue;
  289. if (Reap == true)
  290. return false;
  291. return _error->Error("Waited, for %s but it wasn't there",Name);
  292. }
  293. // Check for an error code.
  294. if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
  295. {
  296. if (Reap == true)
  297. return false;
  298. if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV)
  299. return _error->Error("Sub-process %s recieved a segmentation fault.",Name);
  300. if (WIFEXITED(Status) != 0)
  301. return _error->Error("Sub-process %s returned an error code (%u)",Name,WEXITSTATUS(Status));
  302. return _error->Error("Sub-process %s exited unexpectedly",Name);
  303. }
  304. return true;
  305. }
  306. /*}}}*/
  307. // FileFd::Open - Open a file /*{{{*/
  308. // ---------------------------------------------------------------------
  309. /* The most commonly used open mode combinations are given with Mode */
  310. bool FileFd::Open(string FileName,OpenMode Mode, unsigned long Perms)
  311. {
  312. Close();
  313. Flags = AutoClose;
  314. switch (Mode)
  315. {
  316. case ReadOnly:
  317. iFd = open(FileName.c_str(),O_RDONLY);
  318. break;
  319. case WriteEmpty:
  320. {
  321. struct stat Buf;
  322. if (stat(FileName.c_str(),&Buf) == 0 && S_ISLNK(Buf.st_mode))
  323. unlink(FileName.c_str());
  324. iFd = open(FileName.c_str(),O_RDWR | O_CREAT | O_TRUNC,Perms);
  325. break;
  326. }
  327. case WriteExists:
  328. iFd = open(FileName.c_str(),O_RDWR);
  329. break;
  330. case WriteAny:
  331. iFd = open(FileName.c_str(),O_RDWR | O_CREAT,Perms);
  332. break;
  333. }
  334. if (iFd < 0)
  335. return _error->Errno("open","Could not open file %s",FileName.c_str());
  336. this->FileName = FileName;
  337. SetCloseExec(iFd,true);
  338. return true;
  339. }
  340. /*}}}*/
  341. // FileFd::~File - Closes the file /*{{{*/
  342. // ---------------------------------------------------------------------
  343. /* If the proper modes are selected then we close the Fd and possibly
  344. unlink the file on error. */
  345. FileFd::~FileFd()
  346. {
  347. Close();
  348. }
  349. /*}}}*/
  350. // FileFd::Read - Read a bit of the file /*{{{*/
  351. // ---------------------------------------------------------------------
  352. /* We are carefull to handle interruption by a signal while reading
  353. gracefully. */
  354. bool FileFd::Read(void *To,unsigned long Size,bool AllowEof)
  355. {
  356. int Res;
  357. errno = 0;
  358. do
  359. {
  360. Res = read(iFd,To,Size);
  361. if (Res < 0 && errno == EINTR)
  362. continue;
  363. if (Res < 0)
  364. {
  365. Flags |= Fail;
  366. return _error->Errno("read","Read error");
  367. }
  368. To = (char *)To + Res;
  369. Size -= Res;
  370. }
  371. while (Res > 0 && Size > 0);
  372. if (Size == 0)
  373. return true;
  374. // Eof handling
  375. if (AllowEof == true)
  376. {
  377. Flags |= HitEof;
  378. return true;
  379. }
  380. Flags |= Fail;
  381. return _error->Error("read, still have %u to read but none left",Size);
  382. }
  383. /*}}}*/
  384. // FileFd::Write - Write to the file /*{{{*/
  385. // ---------------------------------------------------------------------
  386. /* */
  387. bool FileFd::Write(const void *From,unsigned long Size)
  388. {
  389. int Res;
  390. errno = 0;
  391. do
  392. {
  393. Res = write(iFd,From,Size);
  394. if (Res < 0 && errno == EINTR)
  395. continue;
  396. if (Res < 0)
  397. {
  398. Flags |= Fail;
  399. return _error->Errno("write","Write error");
  400. }
  401. From = (char *)From + Res;
  402. Size -= Res;
  403. }
  404. while (Res > 0 && Size > 0);
  405. if (Size == 0)
  406. return true;
  407. Flags |= Fail;
  408. return _error->Error("write, still have %u to write but couldn't",Size);
  409. }
  410. /*}}}*/
  411. // FileFd::Seek - Seek in the file /*{{{*/
  412. // ---------------------------------------------------------------------
  413. /* */
  414. bool FileFd::Seek(unsigned long To)
  415. {
  416. if (lseek(iFd,To,SEEK_SET) != (signed)To)
  417. {
  418. Flags |= Fail;
  419. return _error->Error("Unable to seek to %u",To);
  420. }
  421. return true;
  422. }
  423. /*}}}*/
  424. // FileFd::Skip - Seek in the file /*{{{*/
  425. // ---------------------------------------------------------------------
  426. /* */
  427. bool FileFd::Skip(unsigned long Over)
  428. {
  429. if (lseek(iFd,Over,SEEK_CUR) < 0)
  430. {
  431. Flags |= Fail;
  432. return _error->Error("Unable to seek ahead %u",Over);
  433. }
  434. return true;
  435. }
  436. /*}}}*/
  437. // FileFd::Truncate - Truncate the file /*{{{*/
  438. // ---------------------------------------------------------------------
  439. /* */
  440. bool FileFd::Truncate(unsigned long To)
  441. {
  442. if (ftruncate(iFd,To) != 0)
  443. {
  444. Flags |= Fail;
  445. return _error->Error("Unable to truncate to %u",To);
  446. }
  447. return true;
  448. }
  449. /*}}}*/
  450. // FileFd::Tell - Current seek position /*{{{*/
  451. // ---------------------------------------------------------------------
  452. /* */
  453. unsigned long FileFd::Tell()
  454. {
  455. off_t Res = lseek(iFd,0,SEEK_CUR);
  456. if (Res == (off_t)-1)
  457. _error->Errno("lseek","Failed to determine the current file position");
  458. return Res;
  459. }
  460. /*}}}*/
  461. // FileFd::Size - Return the size of the file /*{{{*/
  462. // ---------------------------------------------------------------------
  463. /* */
  464. unsigned long FileFd::Size()
  465. {
  466. struct stat Buf;
  467. if (fstat(iFd,&Buf) != 0)
  468. return _error->Errno("fstat","Unable to determine the file size");
  469. return Buf.st_size;
  470. }
  471. /*}}}*/
  472. // FileFd::Close - Close the file if the close flag is set /*{{{*/
  473. // ---------------------------------------------------------------------
  474. /* */
  475. bool FileFd::Close()
  476. {
  477. bool Res = true;
  478. if ((Flags & AutoClose) == AutoClose)
  479. if (iFd >= 0 && close(iFd) != 0)
  480. Res &= _error->Errno("close","Problem closing the file");
  481. iFd = -1;
  482. if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail &&
  483. FileName.empty() == false)
  484. if (unlink(FileName.c_str()) != 0)
  485. Res &= _error->Warning("unlnk","Problem unlinking the file");
  486. return Res;
  487. }
  488. /*}}}*/