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