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