OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
StdFile.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Lots of file helpers */
19 
20 #include "C4Include.h"
21 #include "platform/StdFile.h"
22 #include "lib/StdBuf.h"
23 
24 #include <stdio.h>
25 #ifdef HAVE_IO_H
26 #include <io.h>
27 #endif
28 #ifdef HAVE_DIRECT_H
29 #include <direct.h>
30 #endif
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #ifdef _WIN32
36 #endif
37 #include <errno.h>
38 #include <stdlib.h>
39 #include <ctype.h>
40 #include <fcntl.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <zlib.h>
44 #include <string>
45 
46 /* Path & Filename */
47 #ifdef _WIN32
48 static const char *DirectorySeparators = "/\\";
49 #else
50 static const char *DirectorySeparators = "/";
51 #endif
52 
53 // Return pointer to position after last backslash.
54 
55 char *GetFilename(char *szPath)
56 {
57  if (!szPath) return nullptr;
58  char *pPos,*pFilename=szPath;
59  for (pPos=szPath; *pPos; pPos++) if (*pPos==DirectorySeparator || *pPos=='/') pFilename = pPos+1;
60  return pFilename;
61 }
62 const char *GetFilename(const char *szPath)
63 {
64  if (!szPath) return nullptr;
65  const char *pPos,*pFilename=szPath;
66  for (pPos=szPath; *pPos; pPos++) if (*pPos==DirectorySeparator || *pPos=='/') pFilename = pPos+1;
67  return pFilename;
68 }
69 
70 const char* GetFilenameOnly(const char *strFilename)
71 {
72  // Get filename to static buffer
73  static char strBuffer[_MAX_PATH + 1];
74  SCopy(GetFilename(strFilename), strBuffer);
75  // Truncate extension
76  RemoveExtension(strBuffer);
77  // Return buffer
78  return strBuffer;
79 }
80 
81 const char *GetC4Filename(const char *szPath)
82 {
83  // returns path to file starting at first .c4*-directory.
84  if (!szPath) return nullptr;
85  const char *pPos,*pFilename=szPath;
86  for (pPos=szPath; *pPos; pPos++)
87  {
88  if (*pPos==DirectorySeparator || *pPos=='/')
89  {
90  if (pPos >= szPath+4 && SEqual2NoCase(pPos-4, ".oc")) return pFilename;
91  pFilename = pPos+1;
92  }
93  }
94  return pFilename;
95 }
96 
97 int GetTrailingNumber(const char *strString)
98 {
99  // Default
100  int iNumber = 0;
101  // Start from end
102  const char *cpPos = strString + SLen(strString);
103  // Walk back while number
104  while ((cpPos > strString) && Inside(*(cpPos - 1), '0', '9')) cpPos--;
105  // Scan number
106  sscanf(cpPos, "%d", &iNumber);
107  // Return result
108  return iNumber;
109 }
110 
111 // Like GetFilename, but searches for a slash instead of a backslash
112 // (unix-style paths)
113 
114 char *GetFilenameWeb(char *szPath)
115 {
116  if (!szPath) return nullptr;
117  char *pPos, *pFilename=szPath;
118  for (pPos=szPath; *pPos; pPos++) if (*pPos == '/') pFilename = pPos+1;
119  return pFilename;
120 }
121 const char *GetFilenameWeb(const char *szPath)
122 {
123  if (!szPath) return nullptr;
124  const char *pPos, *pFilename=szPath;
125  for (pPos=szPath; *pPos; pPos++) if (*pPos == '/') pFilename = pPos+1;
126  return pFilename;
127 }
128 
129 // Return pointer to last file extension.
130 
131 char *GetExtension(char *szFilename)
132 {
133  int pos, end;
134  for (end=0; szFilename[end]; end++) {}
135  pos = end;
136  while ((pos > 0) && (szFilename[pos-1] != '.') && (szFilename[pos-1] != DirectorySeparator)) --pos;
137  if ((pos > 0) && szFilename[pos-1] == '.') return szFilename + pos;
138  return szFilename + end;
139 }
140 const char *GetExtension(const char *szFilename)
141 {
142  int pos, end;
143  for (end=0; szFilename[end]; end++) {}
144  pos = end;
145  while ((pos>0) && (szFilename[pos-1] != '.') && (szFilename[pos-1] != DirectorySeparator)) pos--;
146  if ((pos > 0) && szFilename[pos-1] == '.') return szFilename+pos;
147  return szFilename+end;
148 }
149 
150 
151 void RealPath(const char *szFilename, char *pFullFilename)
152 {
153 #ifdef _WIN32
154  wchar_t *wpath = _wfullpath(0, GetWideChar(szFilename), 0);
155  StdStrBuf path(wpath);
156  // I'm pretty sure pFullFilename will always have at least a size of _MAX_PATH, but ughh
157  // This should return a StdStrBuf
158  SCopy(path.getData(), pFullFilename, _MAX_PATH);
159  free(wpath);
160 #else
161  char *pSuffix = nullptr;
162  char szCopy[_MAX_PATH + 1];
163  for (;;)
164  {
165  // Try to convert to full filename. Note this might fail if the given file doesn't exist
166  if (realpath(szFilename, pFullFilename))
167  break;
168  // ... which is undesired behaviour here. Try to reduce the filename until it works.
169  if (!pSuffix)
170  {
171  SCopy(szFilename, szCopy, _MAX_PATH);
172  szFilename = szCopy;
173  pSuffix = szCopy + SLen(szCopy);
174  }
175  else
176  *pSuffix = '/';
177  while (pSuffix >= szCopy)
178  if (*--pSuffix == '/')
179  break;
180  if (pSuffix < szCopy)
181  {
182  // Give up: Just copy whatever we got
183  SCopy(szFilename, pFullFilename, _MAX_PATH);
184  return;
185  }
186  *pSuffix = 0;
187  }
188  // Append suffix
189  if (pSuffix)
190  {
191  *pSuffix = '/';
192  SAppend(pSuffix, pFullFilename, _MAX_PATH);
193  }
194 #endif
195 }
196 
197 // Copy (extended) parent path (without backslash) to target buffer.
198 
199 bool GetParentPath(const char *szFilename, char *szBuffer)
200 {
201  // Prepare filename
202  SCopy(szFilename,szBuffer,_MAX_PATH);
203  // Extend relative single filenames
204 #ifdef _WIN32
205  if (!SCharCount(DirectorySeparator,szFilename)) _fullpath(szBuffer,szFilename,_MAX_PATH);
206 #else
207  if (!SCharCount(DirectorySeparator,szFilename)) RealPath(szFilename,szBuffer);
208 #endif
209  // Truncate path
210  return TruncatePath(szBuffer);
211 }
212 
213 bool GetParentPath(const char *szFilename, StdStrBuf *outBuf)
214 {
215  char buf[_MAX_PATH+1]; *buf='\0';
216  if (!GetParentPath(szFilename, buf)) return false;
217  outBuf->Copy(buf);
218  return true;
219 }
220 
221 const char *GetRelativePathS(const char *strPath, const char *strRelativeTo)
222 {
223  // Specified path is relative to base path
224 #ifdef _WIN32
225  if (SEqual2NoCase(strPath, strRelativeTo))
226 #else
227  if (SEqual2(strPath, strRelativeTo))
228 #endif
229  {
230  // return relative section
231  return strPath + SLen(strRelativeTo) + ((strPath[SLen(strRelativeTo)] == DirectorySeparator) ? +1 : 0);
232  }
233  // Not relative: return full path
234  return strPath;
235 }
236 
237 bool IsGlobalPath(const char *szPath)
238 {
239 #ifdef _WIN32
240  // C:\...
241  if (*szPath && szPath[1] == ':') return true;
242 #endif
243  // /usr/bin, \Temp\, ...
244  if (*szPath == DirectorySeparator) return true;
245  return false;
246 }
247 
248 // Truncate string before last backslash.
249 
250 bool TruncatePath(char *szPath)
251 {
252  if (!szPath) return false;
253  int iBSPos;
254  iBSPos=SCharLastPos(DirectorySeparator,szPath);
255 #ifndef _WIN32
256  int iBSPos2;
257  iBSPos2=SCharLastPos('\\',szPath);
258  if (iBSPos2 > iBSPos) fprintf(stderr, "Warning: TruncatePath with a \\ (%s)\n", szPath);
259 #endif
260  if (iBSPos<0) return false;
261  szPath[iBSPos]=0;
262  return true;
263 }
264 
265 // Append terminating backslash if not present.
266 
267 void AppendBackslash(char *szFilename)
268 {
269  int i=SLen(szFilename);
270  if (i>0) if (szFilename[i-1]==DirectorySeparator) return;
271  SAppendChar(DirectorySeparator,szFilename);
272 }
273 
274 // Remove terminating backslash if present.
275 
276 void TruncateBackslash(char *szFilename)
277 {
278  int i=SLen(szFilename);
279  if (i>0) if (szFilename[i-1]==DirectorySeparator) szFilename[i-1]=0;
280 }
281 
282 // Append extension if no extension.
283 
284 void DefaultExtension(char *szFilename, const char *szExtension)
285 {
286  if (!(*GetExtension(szFilename)))
287  { SAppend(".",szFilename); SAppend(szExtension,szFilename); }
288 }
289 
290 void DefaultExtension(StdStrBuf *sFilename, const char *szExtension)
291 {
292  assert(sFilename);
293  if (!(*GetExtension(sFilename->getData())))
294  { sFilename->AppendChar('.'); sFilename->Append(szExtension); }
295 }
296 
297 // Append or overwrite extension.
298 
299 void EnforceExtension(char *szFilename, const char *szExtension)
300 {
301  char *ext = GetExtension(szFilename);
302  if (ext[0]) { SCopy(szExtension,ext); }
303  else { SAppend(".",szFilename); SAppend(szExtension,szFilename); }
304 }
305 
306 void EnforceExtension(StdStrBuf *sFilename, const char *szExtension)
307 {
308  assert(sFilename);
309  const char *ext = GetExtension(sFilename->getData());
310  if (ext[0]) { sFilename->ReplaceEnd(ext - sFilename->getData(), szExtension); }
311  else { sFilename->AppendChar('.'); sFilename->Append(szExtension); }
312 }
313 
314 // remove extension
315 
316 void RemoveExtension(char *szFilename)
317 {
318  char *ext = GetExtension(szFilename);
319  if (ext[0]) ext[-1]=0;
320 }
321 
322 void RemoveExtension(StdStrBuf *psFileName)
323 {
324  if (psFileName && *psFileName)
325  {
326  RemoveExtension(psFileName->getMData());
327  psFileName->SetLength(strlen(psFileName->getData()));
328  }
329 }
330 
331 // Enforce indexed extension until item does not exist.
332 
333 void MakeTempFilename(char *szFilename)
334 {
335  DefaultExtension(szFilename,"tmp");
336  char *fn_ext=GetExtension(szFilename);
337  int cnum=-1;
338  do
339  {
340  cnum++;
341  osprintf(fn_ext,"%03d",cnum);
342  }
343  while (FileExists(szFilename) && (cnum<999));
344 }
345 
346 void MakeTempFilename(StdStrBuf *sFilename)
347 {
348  assert(sFilename);
349  if (!sFilename->getLength()) sFilename->Copy("temp.tmp");
350  EnforceExtension(sFilename, "tmp");
351  char *fn_ext=GetExtension(sFilename->getMData());
352  int cnum=-1;
353  do
354  {
355  cnum++;
356  osprintf(fn_ext,"%03d",cnum);
357  }
358  while (FileExists(sFilename->getData()) && (cnum<999));
359 }
360 
361 bool WildcardListMatch(const char *szWildcardList, const char *szString)
362 {
363  // safety
364  if (!szString || !szWildcardList) return false;
365  // match any item in list
366  StdStrBuf sWildcard, sWildcardList(szWildcardList);
367  int32_t i=0;
368  while (sWildcardList.GetSection(i++, &sWildcard, '|'))
369  {
370  if (WildcardMatch(sWildcard.getData(), szString)) return true;
371  }
372  // none matched
373  return false;
374 }
375 
376 bool IsWildcardString(const char *szString)
377 {
378  // safety
379  if (!szString) return false;
380  // known wildcard characters: *?
381  return (SCharCount('?', szString)>0) || (SCharCount('*', szString)>0);
382 }
383 
384 bool WildcardMatch(const char *szWildcard, const char *szString)
385 {
386  // safety
387  if (!szString || !szWildcard) return false;
388  // match char-wise
389  const char *pWild = szWildcard, *pPos = szString;
390  const char *pLWild = nullptr, *pLPos = nullptr; // backtracking
391  while (*pWild || pLWild)
392  // string wildcard?
393  if (*pWild == '*')
394  { pLWild = ++pWild; pLPos = pPos; }
395  // nothing left to match?
396  else if (!*pPos)
397  break;
398  // equal or one-character-wildcard? proceed
399  else if (*pWild == '?' || tolower(*pWild) == tolower(*pPos))
400  { pWild++; pPos++; }
401  // backtrack possible?
402  else if (pLPos)
403  { pWild = pLWild; pPos = ++pLPos; }
404  // match failed
405  else
406  return false;
407  // match complete if both strings are fully matched
408  return !*pWild && !*pPos;
409 }
410 
411 // create a valid file name from some title
412 void MakeFilenameFromTitle(char *szTitle)
413 {
414  // copy all chars but those to be stripped
415  char *szFilename=szTitle, *szTitle2=szTitle;
416  while (*szTitle2)
417  {
418  bool fStrip;
419  if (IsWhiteSpace(*szTitle2))
420  fStrip = (szFilename==szTitle);
421  else if (static_cast<unsigned int>(*szTitle2) > 127)
422  fStrip = true;
423  else
424  fStrip = (SCharPos(*szTitle2, "!\"'%&/=?+*#:;<>\\.") >= 0);
425  if (!fStrip) *szFilename++ = *szTitle2;
426  ++szTitle2;
427  }
428  // truncate spaces from end
429  while (IsWhiteSpace(*--szFilename)) if (szFilename==szTitle) { --szFilename; break; }
430  // terminate
431  *++szFilename=0;
432  // no name? (only invalid chars)
433  if (!*szTitle) SCopy("unnamed", szTitle, 50);
434  // done
435 }
436 
437 /* Files */
438 
439 bool FileExists(const char *szFilename)
440 {
441 #ifdef _WIN32
442  return GetFileAttributes(GetWideChar(szFilename)) != INVALID_FILE_ATTRIBUTES;
443 #else
444  return (!access(szFilename,F_OK));
445 #endif
446 }
447 
448 size_t FileSize(const char *szFilename)
449 {
450 #if defined(_WIN32) || defined(_WIN64)
451  auto attributes = WIN32_FILE_ATTRIBUTE_DATA();
452  if (GetFileAttributesEx(GetWideChar(szFilename), GetFileExInfoStandard, &attributes) == 0)
453  return 0;
454 #ifdef _WIN64
455  return (static_cast<size_t>(attributes.nFileSizeHigh) << (sizeof(attributes.nFileSizeLow) * 8)) | attributes.nFileSizeLow;
456 #else
457  return attributes.nFileSizeLow;
458 #endif
459 #else
460  struct stat stStats;
461  if (stat(szFilename,&stStats)) return 0;
462  return stStats.st_size;
463 #endif
464 }
465 
466 // operates on a filedescriptor from open or fileno
467 size_t FileSize(int fdes)
468 {
469 #ifdef _WIN32
470  return _filelength(fdes);
471 #else
472  struct stat stStats;
473  if (fstat(fdes,&stStats)) return 0;
474  return stStats.st_size;
475 #endif
476 }
477 
478 int FileTime(const char *szFilename)
479 {
480 #ifdef _WIN32
481  auto attributes = WIN32_FILE_ATTRIBUTE_DATA();
482  if (GetFileAttributesEx(GetWideChar(szFilename), GetFileExInfoStandard, &attributes) == 0)
483  return 0;
484  int64_t ft = (static_cast<int64_t>(attributes.ftLastWriteTime.dwHighDateTime) << (sizeof(attributes.ftLastWriteTime.dwLowDateTime) * 8)) | attributes.ftLastWriteTime.dwLowDateTime;
485  ft -= 116444736000000000;
486  ft /= 10000000;
487  return ft;
488 #else
489  struct stat stStats;
490  if (stat(szFilename,&stStats)!=0) return 0;
491  return stStats.st_mtime;
492 #endif
493 }
494 
495 bool EraseFile(const char *szFilename)
496 {
497 #ifdef _WIN32
498  SetFileAttributesW(GetWideChar(szFilename), FILE_ATTRIBUTE_NORMAL);
499  if (DeleteFileW(GetWideChar(szFilename)) == 0)
500  {
501  switch (GetLastError())
502  {
503  case ERROR_PATH_NOT_FOUND:
504  case ERROR_FILE_NOT_FOUND:
505  // While deleting it didn't work, the file doesn't exist (anymore).
506  // Pretend everything is fine.
507  return true;
508  default:
509  // Some other error left us unable to delete the file.
510  return false;
511  }
512  }
513  return true;
514 #else
515  // either unlink or remove could be used. Well, stick to ANSI C where possible.
516  if (remove(szFilename))
517  {
518  if (errno == ENOENT)
519  {
520  // Hah, here the wrapper actually makes sense:
521  // The engine only cares about the file not being there after this call.
522  return true;
523  }
524  return false;
525  }
526  return true;
527 #endif
528 }
529 
530 #ifndef _WIN32
531 bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists)
532 {
533  int fds = open (szSource, O_RDONLY | O_CLOEXEC);
534  if (!fds) return false;
535  struct stat info; fstat(fds, &info);
536  int fdt = open (szTarget, O_CLOEXEC | O_WRONLY | O_CREAT | (FailIfExists? O_EXCL : O_TRUNC), info.st_mode);
537  if (!fdt)
538  {
539  close (fds);
540  return false;
541  }
542  char buffer[1024]; ssize_t l;
543  while ((l = read(fds, buffer, sizeof(buffer))) > 0)
544  if (write(fdt, buffer, l) < l)
545  {
546  l = -1;
547  break;
548  }
549  close (fds);
550  close (fdt);
551  // On error, return false
552  return l != -1;
553 }
554 
555 bool RenameFile(const char *szFilename, const char *szNewFilename)
556 {
557  if (rename(szFilename,szNewFilename) < 0)
558  {
559  if (errno != EXDEV) return false;
560  if (CopyFile(szFilename, szNewFilename, false))
561  {
562  return EraseFile(szFilename);
563  }
564  return false;
565  }
566  return true;
567 }
568 #else
569 
570 #undef CopyFile
571 bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists)
572 {
573  return !!CopyFileW(GetWideChar(szSource), GetWideChar(szTarget), FailIfExists);
574 }
575 
576 bool RenameFile(const char *szFilename, const char *szNewFilename)
577 {
578  return !!MoveFileExW(GetWideChar(szFilename), GetWideChar(szNewFilename), MOVEFILE_COPY_ALLOWED);
579 }
580 
581 #endif
582 
583 bool MakeOriginalFilename(char *szFilename)
584 {
585  // safety
586  if (!szFilename) return false;
587 #ifdef _WIN32
588  // root-directory?
589  if (Inside(SLen(szFilename), 2u, 3u)) if (szFilename[1]==':')
590  {
591  szFilename[2]='\\'; szFilename[3]=0;
592  if (GetDriveTypeW(GetWideChar(szFilename)) == DRIVE_NO_ROOT_DIR) return false;
593  return true;
594  }
595  struct _wfinddata_t fdt; intptr_t shnd;
596  if ((shnd=_wfindfirst(GetWideChar(szFilename),&fdt))<0) return false;
597  _findclose(shnd);
598  StdStrBuf name(fdt.name);
599  SCopy(GetFilename(name.getData()),GetFilename(szFilename),_MAX_FNAME);
600 #else
601  if (SCharPos('*', szFilename) != -1)
602  {
603  fputs ("Warning: MakeOriginalFilename with \"", stderr);
604  fputs (szFilename, stderr);
605  fputs ("\"!\n", stderr);
606  }
607 #endif
608  return true;
609 }
610 
611 /* Directories */
612 
613 const char *GetWorkingDirectory()
614 {
615 #ifdef _WIN32
616  static StdStrBuf buffer;
617  wchar_t *widebuf = 0;
618  DWORD widebufsz = GetCurrentDirectoryW(0, 0);
619  widebuf = new wchar_t[widebufsz];
620  if (GetCurrentDirectoryW(widebufsz, widebuf) == 0) {
621  delete[] widebuf;
622  return 0;
623  }
624  buffer.Take(StdStrBuf(widebuf));
625  delete[] widebuf;
626  return buffer.getData();
627 #else
628  static char buf[_MAX_PATH+1];
629  return getcwd(buf,_MAX_PATH);
630 #endif
631 }
632 
633 bool SetWorkingDirectory(const char *path)
634 {
635 #ifdef _WIN32
636  return SetCurrentDirectoryW(GetWideChar(path)) != 0;
637 #else
638  return (chdir(path)==0);
639 #endif
640 }
641 
642 bool CreatePath(const std::string &path)
643 {
644  assert(!path.empty());
645 #ifdef _WIN32
646  if (CreateDirectoryW(GetWideChar(path.c_str()), nullptr))
647  {
648  return true;
649  }
650  else
651  {
652  DWORD err = GetLastError();
653  switch (err)
654  {
655  case ERROR_PATH_NOT_FOUND:
656  break;
657  case ERROR_ALREADY_EXISTS:
658  return true;
659  default:
660  // Something major has happened: Log
661  {
662  wchar_t * str;
663  if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
664  nullptr, err, 0, (LPWSTR)&str, 0, nullptr))
665  {
666  LogF("CreateDirectory failed: %s", StdStrBuf(str).getData());
667  LocalFree(str);
668  }
669  return false;
670  }
671  }
672  }
673 #else
674  if (!mkdir(path.c_str(), S_IREAD | S_IWRITE | S_IEXEC))
675  return true;
676  switch (errno)
677  {
678  case ENOENT:
679  break;
680  case EEXIST:
681  // FIXME: Check whether the path is blocked by a non-directory
682  return true;
683  default:
684  return false;
685  }
686 #endif
687  // Recursively create parent path
688  std::string::size_type slash = path.find_last_of(DirectorySeparators);
689  if (slash == 0 || slash == std::string::npos)
690  return false;
691  return CreatePath(path.substr(0, slash)) && CreatePath(path);
692 }
693 
694 bool DirectoryExists(const char *szFilename)
695 {
696  // Ignore trailing slash or backslash, except when we are probing the
697  // root directory '/'.
698  char bufFilename[_MAX_PATH + 1];
699  if (szFilename && szFilename[0])
700  {
701  unsigned int len = SLen(szFilename);
702  if (len > 1 && ((szFilename[len - 1] == '\\') || (szFilename[len - 1] == '/')))
703  {
704  SCopy(szFilename, bufFilename, _MAX_PATH);
705  bufFilename[SLen(bufFilename) - 1] = 0;
706  szFilename = bufFilename;
707  }
708  }
709  // Check file attributes
710 #ifdef _WIN32
711  struct _wfinddata_t fdt; intptr_t shnd;
712  if ((shnd=_wfindfirst(GetWideChar(szFilename),&fdt))<0) return false;
713  _findclose(shnd);
714  if (fdt.attrib & _A_SUBDIR) return true;
715 #else
716  struct stat stStats;
717  if (stat(szFilename,&stStats)!=0) return 0;
718  return (S_ISDIR(stStats.st_mode));
719 #endif
720  return false;
721 }
722 
723 bool CopyDirectory(const char *szSource, const char *szTarget, bool fResetAttributes)
724 {
725  // Source check
726  if (!szSource || !szTarget) return false;
727  if (!DirectoryExists(szSource)) return false;
728  // Do not process system navigation directories
729  if (SEqual(GetFilename(szSource),".")
730  || SEqual(GetFilename(szSource),".."))
731  return true;
732  // Overwrite target
733  if (!EraseItem(szTarget)) return false;
734  // Create target directory
735  bool status=true;
736 #ifdef _WIN32
737  if (_wmkdir(GetWideChar(szTarget))!=0) return false;
738  // Copy contents to target directory
739  char contents[_MAX_PATH+1];
740  SCopy(szSource,contents); AppendBackslash(contents);
741  SAppend("*",contents);
742  _wfinddata_t fdt; intptr_t hfdt;
743  if ( (hfdt=_wfindfirst(GetWideChar(contents),&fdt)) > -1 )
744  {
745  do
746  {
747  char itemsource[_MAX_PATH+1],itemtarget[_MAX_PATH+1];
748  SCopy(szSource,itemsource); AppendBackslash(itemsource); SAppend(StdStrBuf(fdt.name).getData(),itemsource);
749  SCopy(szTarget,itemtarget); AppendBackslash(itemtarget); SAppend(StdStrBuf(fdt.name).getData(),itemtarget);
750  if (!CopyItem(itemsource,itemtarget, fResetAttributes)) status=false;
751  }
752  while (_wfindnext(hfdt,&fdt)==0);
753  _findclose(hfdt);
754  }
755 #else
756  if (mkdir(szTarget,0777)!=0) return false;
757  DIR * d = opendir(szSource);
758  dirent * ent;
759  char itemsource[_MAX_PATH+1],itemtarget[_MAX_PATH+1];
760  while ((ent = readdir(d)))
761  {
762  SCopy(szSource,itemsource); AppendBackslash(itemsource); SAppend(ent->d_name,itemsource);
763  SCopy(szTarget,itemtarget); AppendBackslash(itemtarget); SAppend(ent->d_name,itemtarget);
764  if (!CopyItem(itemsource,itemtarget, fResetAttributes)) status=false;
765  }
766  closedir(d);
767 #endif
768  return status;
769 }
770 
771 bool EraseDirectory(const char *szDirName)
772 {
773  // Do not process system navigation directories
774  if (SEqual(GetFilename(szDirName),".")
775  || SEqual(GetFilename(szDirName),".."))
776  return true;
777  char path[_MAX_PATH+1];
778 #ifdef _WIN32
779  // Get path to directory contents
780  SCopy(szDirName,path); SAppend("\\*.*",path);
781  // Erase subdirectories and files
782  ForEachFile(path,&EraseItem);
783 #else
784  DIR * d = opendir(szDirName);
785  dirent * ent;
786  while ((ent = readdir(d)))
787  {
788  SCopy(szDirName,path); AppendBackslash(path); SAppend(ent->d_name,path);
789  if (!EraseItem(path)) return false;
790  }
791  closedir(d);
792 #endif
793  // Check working directory
794  if (SEqual(szDirName,GetWorkingDirectory()))
795  {
796  // Will work only if szDirName is full path and correct case!
797  SCopy(GetWorkingDirectory(),path);
798  int lbacks = SCharLastPos(DirectorySeparator,path);
799  if (lbacks > -1)
800  {
801  path[lbacks]=0; SetWorkingDirectory(path);
802  }
803  }
804  // Remove directory
805 #ifdef _WIN32
806  return !!RemoveDirectoryW(GetWideChar(szDirName));
807 #else
808  return (rmdir(szDirName)==0 || errno == ENOENT);
809 #endif
810 }
811 
812 /* Items */
813 bool RenameItem(const char *szItemName, const char *szNewItemName)
814 {
815  // FIXME: What if the directory would have to be copied?
816  return RenameFile(szItemName,szNewItemName);
817 }
818 
819 bool EraseItem(const char *szItemName)
820 {
821  if (!EraseFile(szItemName)) return EraseDirectory(szItemName);
822  else return true;
823 }
824 
825 bool CreateItem(const char *szItemname)
826 {
827  // Overwrite any old item
828  EraseItem(szItemname);
829  // Create dummy item
830  FILE *fhnd;
831 #ifdef _WIN32
832  if (!(fhnd=_wfopen(GetWideChar(szItemname), L"wb"))) return false;
833 #else
834  if (!(fhnd=fopen(szItemname,"wb"))) return false;
835 #endif
836  fclose(fhnd);
837  // Success
838  return true;
839 }
840 
841 bool CopyItem(const char *szSource, const char *szTarget, bool fResetAttributes)
842 {
843  // Check for identical source and target
844  if (ItemIdentical(szSource,szTarget)) return true;
845  // Copy directory
846  if (DirectoryExists(szSource))
847  return CopyDirectory(szSource,szTarget,fResetAttributes);
848  // Copy file
849  if (!CopyFile(szSource,szTarget,false)) return false;
850  // Reset any attributes if desired
851 #ifdef _WIN32
852  if (fResetAttributes) if (!SetFileAttributesW(GetWideChar(szTarget), FILE_ATTRIBUTE_NORMAL)) return false;
853 #else
854  if (fResetAttributes) if (chmod(szTarget, S_IRWXU)) return false;
855 #endif
856  return true;
857 }
858 
859 bool MoveItem(const char *szSource, const char *szTarget)
860 {
861  if (ItemIdentical(szSource,szTarget)) return true;
862  return RenameFile(szSource, szTarget);
863 }
864 
865 bool ItemIdentical(const char *szFilename1, const char *szFilename2)
866 {
867  char szFullFile1[_MAX_PATH+1],szFullFile2[_MAX_PATH+1];
868  RealPath(szFilename1, szFullFile1); RealPath(szFilename2, szFullFile2);
869 #ifdef _WIN32
870  if (SEqualNoCase(szFullFile1,szFullFile2)) return true;
871 #else
872  if (SEqual(szFullFile1,szFullFile2)) return true;
873 #endif
874  return false;
875 }
876 
877 //------------------------- Multi File Processing --------------------------------------------------------------------------------------------------------
878 
880 {
882  DirectoryIterator::FileList files;
883  std::string directory;
884  int ref;
885 };
886 
888  : p(new DirectoryIteratorP), iter(p->files.end())
889 {}
891  : p(other.p), iter(other.iter)
892 {
893  ++p->ref;
894 }
895 
897 {
898  p = other.p; iter = other.iter;
899  ++p->ref;
900  return *this;
901 }
902 
904 {
905  if (--p->ref == 0)
906  delete p;
907 }
908 
910 {
911  // clear cache
912  if (p->ref > 1)
913  {
914  // Detach from shared memory
915  --p->ref;
916  p = new DirectoryIteratorP;
917  }
918  p->directory.clear();
919  p->files.clear();
920  iter = p->files.end();
921 }
922 
924 {
925  iter = p->files.begin();
926 }
927 
928 void DirectoryIterator::Reset (const char * dirname, bool force_reread)
929 {
930  if (p->directory == dirname && !force_reread)
931  {
932  // Skip reinitialisation and just reset the iterator
933  iter = p->files.begin();
934  return;
935  }
936  if (p->ref > 1)
937  {
938  // Detach from shared memory
939  --p->ref;
940  p = new DirectoryIteratorP;
941  }
942  p->files.clear();
943  iter = p->files.end();
944  Read(dirname);
945 }
946 
948  : p(new DirectoryIteratorP), iter(p->files.end())
949 {
950  Read(dirname);
951 }
952 
953 void DirectoryIterator::Read(const char *dirname)
954 {
955  assert(dirname && *dirname);
956  assert(p->files.empty());
957  std::string search_path(dirname);
958  search_path.push_back(DirectorySeparator);
959 #ifdef WIN32
960  auto file = WIN32_FIND_DATAW();
961  HANDLE fh = FindFirstFileW(GetWideChar((search_path + '*').c_str()), &file);
962  if (fh == INVALID_HANDLE_VALUE)
963  {
964  switch (GetLastError())
965  {
966  case ERROR_PATH_NOT_FOUND:
967  case ERROR_FILE_NOT_FOUND:
968  // This is okay, either the directory doesn't exist or there are no files
969  return;
970  default:
971  // Something else broke
972  Log("DirectoryIterator::Read(const char*): Unable to read file system");
973  return;
974  }
975  }
976  // Insert files into list
977  do
978  {
979  // ...unless they're . or ..
980  if (file.cFileName[0] == '.' && (file.cFileName[1] == '\0' || (file.cFileName[1] == '.' && file.cFileName[2] == '\0')))
981  continue;
982 
983  size_t size = (file.nFileSizeHigh * (size_t(MAXDWORD) + 1)) + file.nFileSizeLow;
984 
985  p->files.emplace_back(StdStrBuf(file.cFileName).getData(), size);
986  }
987  while (FindNextFileW(fh, &file));
988  FindClose(fh);
989 #else
990  DIR *fh = opendir(dirname);
991  if (fh == nullptr)
992  {
993  switch (errno)
994  {
995  case ENOENT:
996  case ENOTDIR:
997  // Okay, so there's no files here.
998  return;
999  default:
1000  // Something else broke
1001  LogF("DirectoryIterator::Read(\"%s\"): %s", dirname, strerror(errno));
1002  return;
1003  }
1004  }
1005  dirent *file;
1006  // Insert files into list
1007  while ((file = readdir(fh)) != nullptr)
1008  {
1009  // ...unless they're . or ..
1010  if (file->d_name[0] == '.' && (file->d_name[1] == '\0' || (file->d_name[1] == '.' && file->d_name[2] == '\0')))
1011  continue;
1012  p->files.emplace_back(file->d_name, 0);
1013  }
1014  closedir(fh);
1015 #endif
1016  // Sort list
1017  std::sort(p->files.begin(), p->files.end());
1018  for (FileList::iterator it = p->files.begin(); it != p->files.end(); ++it)
1019  it->first.insert(0, search_path); // prepend path to all file entries
1020  iter = p->files.begin();
1021  p->directory = dirname;
1022 }
1023 
1025 {
1026  if (iter != p->files.end())
1027  ++iter;
1028  return *this;
1029 }
1030 
1031 const char * DirectoryIterator::operator*() const
1032 {
1033  if (iter == p->files.end())
1034  return nullptr;
1035  return iter->first.c_str();
1036 }
1038 {
1039  DirectoryIterator tmp(*this);
1040  ++*this;
1041  return tmp;
1042 }
1043 
1045 {
1046 #ifdef _WIN32
1047  return iter->second;
1048 #else
1049  return FileSize(iter->first.c_str());
1050 #endif
1051 }
1052 
1053 int ForEachFile(const char *szDirName, bool (*fnCallback)(const char *))
1054 {
1055  if (!szDirName || !fnCallback)
1056  return 0;
1057  char szFilename[_MAX_PATH+1];
1058  SCopy(szDirName,szFilename);
1059  bool fHasWildcard = (SCharPos('*', szFilename)>=0);
1060  if (!fHasWildcard) // parameter without wildcard: Append "/*.*" or "\*.*"
1061  AppendBackslash(szFilename);
1062  int iFileCount = 0;
1063 #ifdef _WIN32
1064  struct _wfinddata_t fdt; intptr_t fdthnd;
1065  if (!fHasWildcard) // parameter without wildcard: Append "/*.*" or "\*.*"
1066  SAppend("*",szFilename,_MAX_PATH);
1067  if ((fdthnd = _wfindfirst (GetWideChar(szFilename), &fdt)) < 0)
1068  return 0;
1069  do
1070  {
1071  if (!wcscmp(fdt.name, L".") || !wcscmp(fdt.name, L"..")) continue;
1072  StdStrBuf name(fdt.name);
1073  SCopy(name.getData(),GetFilename(szFilename));
1074  if ((*fnCallback)(szFilename))
1075  iFileCount++;
1076  }
1077  while (_wfindnext(fdthnd,&fdt)==0);
1078  _findclose(fdthnd);
1079 #else
1080  if (fHasWildcard) fprintf(stderr, "Warning: ForEachFile with * (%s)\n", szDirName);
1081  DIR * d = opendir(szDirName);
1082  if (!d) return 0;
1083  dirent * ent;
1084  while ((ent = readdir(d)))
1085  {
1086  SCopy(ent->d_name,GetFilename(szFilename));
1087  if ((*fnCallback)(szFilename))
1088  iFileCount++;
1089  }
1090  closedir(d);
1091 #endif
1092  return iFileCount;
1093 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:55
const char * getData() const
Definition: StdBuf.h:450
int osprintf(char *str, const char *fmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: Standard.h:164
bool CreateItem(const char *szItemname)
Definition: StdFile.cpp:825
void RealPath(const char *szFilename, char *pFullFilename)
Definition: StdFile.cpp:151
bool GetSection(size_t idx, StdStrBuf *psOutSection, char cSeparator=';') const
Definition: StdBuf.cpp:376
void TruncateBackslash(char *szFilename)
Definition: StdFile.cpp:276
const char * operator*() const
Definition: StdFile.cpp:1031
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:122
void SAppendChar(char cChar, char *szStr)
Definition: Standard.cpp:235
bool CopyFile(const char *szSource, const char *szTarget, bool FailIfExists)
Definition: StdFile.cpp:531
bool IsGlobalPath(const char *szPath)
Definition: StdFile.cpp:237
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:227
bool CopyItem(const char *szSource, const char *szTarget, bool fResetAttributes)
Definition: StdFile.cpp:841
bool RenameItem(const char *szItemName, const char *szNewItemName)
Definition: StdFile.cpp:813
bool ItemIdentical(const char *szFilename1, const char *szFilename2)
Definition: StdFile.cpp:865
std::string directory
Definition: StdFile.cpp:883
bool CreatePath(const std::string &path)
Definition: StdFile.cpp:642
StdStrBuf::wchar_t_holder GetWideChar(const char *utf8, bool double_null_terminate=false)
bool EraseFile(const char *szFilename)
Definition: StdFile.cpp:495
const char * GetC4Filename(const char *szPath)
Definition: StdFile.cpp:81
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:177
bool IsWhiteSpace(char cChar)
Definition: Standard.h:76
bool IsWildcardString(const char *szString)
Definition: StdFile.cpp:376
#define _MAX_PATH
const char * GetFilenameOnly(const char *strFilename)
Definition: StdFile.cpp:70
size_t SLen(const char *sptr)
Definition: Standard.h:78
void AppendBackslash(char *szFilename)
Definition: StdFile.cpp:267
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:97
#define O_CLOEXEC
unsigned int SCharCount(char cTarget, const char *szInStr, const char *cpUntil)
Definition: Standard.cpp:290
void MakeFilenameFromTitle(char *szTitle)
Definition: StdFile.cpp:412
int GetTrailingNumber(const char *strString)
Definition: StdFile.cpp:97
bool GetParentPath(const char *szFilename, char *szBuffer)
Definition: StdFile.cpp:199
char * getMData()
Definition: StdBuf.h:451
void AppendChar(char cChar)
Definition: StdBuf.h:596
DirectoryIterator & operator=(const DirectoryIterator &)
Definition: StdFile.cpp:896
bool MakeOriginalFilename(char *szFilename)
Definition: StdFile.cpp:583
int SCharLastPos(char cTarget, const char *szInStr)
Definition: Standard.cpp:217
ptrdiff_t ssize_t
size_t GetFileSize() const
Definition: StdFile.cpp:1044
const char * GetWorkingDirectory()
Definition: StdFile.cpp:613
int FileTime(const char *szFilename)
Definition: StdFile.cpp:478
void Take(char *pnData)
Definition: StdBuf.h:465
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:527
bool EraseDirectory(const char *szDirName)
Definition: StdFile.cpp:771
void ReplaceEnd(size_t iPos, const char *szNewEnd)
Definition: StdBuf.cpp:358
bool CopyDirectory(const char *szSource, const char *szTarget, bool fResetAttributes)
Definition: StdFile.cpp:723
#define _MAX_FNAME
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:131
void DefaultExtension(char *szFilename, const char *szExtension)
Definition: StdFile.cpp:284
void RemoveExtension(char *szFilename)
Definition: StdFile.cpp:316
bool DirectoryExists(const char *szFilename)
Definition: StdFile.cpp:694
size_t FileSize(const char *szFilename)
Definition: StdFile.cpp:448
char * GetFilenameWeb(char *szPath)
Definition: StdFile.cpp:114
void EnforceExtension(char *szFilename, const char *szExtension)
Definition: StdFile.cpp:299
bool SetWorkingDirectory(const char *path)
Definition: StdFile.cpp:633
bool EraseItem(const char *szItemName)
Definition: StdFile.cpp:819
bool WildcardMatch(const char *szWildcard, const char *szString)
Definition: StdFile.cpp:384
bool FileExists(const char *szFilename)
Definition: StdFile.cpp:439
int ForEachFile(const char *szDirName, bool(*fnCallback)(const char *))
Definition: StdFile.cpp:1053
bool MoveItem(const char *szSource, const char *szTarget)
Definition: StdFile.cpp:859
bool Log(const char *szMessage)
Definition: C4Log.cpp:195
friend struct DirectoryIteratorP
Definition: StdFile.h:106
bool SEqual2(const char *szStr1, const char *szStr2)
Definition: Standard.cpp:168
bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:190
DirectoryIterator::FileList files
Definition: StdFile.cpp:882
size_t getLength() const
Definition: StdBuf.h:453
void SetLength(size_t iLength)
Definition: StdBuf.h:517
bool TruncatePath(char *szPath)
Definition: StdFile.cpp:250
int SCharPos(char cTarget, const char *szInStr, int iIndex)
Definition: Standard.cpp:203
void MakeTempFilename(char *szFilename)
Definition: StdFile.cpp:333
uint32_t DWORD
#define DirectorySeparator
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:253
bool WildcardListMatch(const char *szWildcardList, const char *szString)
Definition: StdFile.cpp:361
void Copy()
Definition: StdBuf.h:475
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:45
DirectoryIterator & operator++()
Definition: StdFile.cpp:1024
const char * GetRelativePathS(const char *strPath, const char *strRelativeTo)
Definition: StdFile.cpp:221
bool RenameFile(const char *szFilename, const char *szNewFilename)
Definition: StdFile.cpp:555