18#ifndef MAGICKCORE_UTILITY_PRIVATE_H
19#define MAGICKCORE_UTILITY_PRIVATE_H
21#include "MagickCore/memory_.h"
22#include "MagickCore/nt-base.h"
23#include "MagickCore/nt-base-private.h"
24#if defined(MAGICKCORE_HAVE_UTIME_H)
28#if defined(__cplusplus) || defined(c_plusplus)
32extern MagickPrivate
char
33 **GetPathComponents(
const char *,
size_t *),
34 **ListFiles(
const char *,
const char *,
size_t *);
36extern MagickPrivate MagickBooleanType
37 GetExecutionPath(
char *,
const size_t),
38 ShredFile(
const char *);
40extern MagickPrivate ssize_t
41 GetMagickPageSize(
void);
43extern MagickPrivate
void
44 ChopPathComponents(
char *,
const size_t),
45 ExpandFilename(
char *);
47static inline int MagickReadDirectory(DIR *directory,
struct dirent *entry,
48 struct dirent **result)
52 *result=readdir(directory);
60#if defined(MAGICKCORE_WINDOWS_SUPPORT)
61static inline wchar_t *create_wchar_path(
const char *utf8)
69 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
70 if ((count > MAX_PATH) && (strncmp(utf8,
"\\\\?\\",4) != 0) &&
71 (NTLongPathsEnabled() == MagickFalse))
74 buffer[MagickPathExtent];
80 (void) FormatLocaleString(buffer,MagickPathExtent,
"\\\\?\\%s",utf8);
82 longPath=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count,
84 if (longPath == (
wchar_t *) NULL)
85 return((
wchar_t *) NULL);
86 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
88 count=(int) GetShortPathNameW(longPath,shortPath,MAX_PATH);
89 longPath=(
wchar_t *) RelinquishMagickMemory(longPath);
90 if ((count < 5) || (count >= MAX_PATH))
91 return((
wchar_t *) NULL);
92 wide=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count-3,
sizeof(*wide));
93 wcscpy(wide,shortPath+4);
96 wide=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count,
sizeof(*wide));
97 if ((wide != (
wchar_t *) NULL) &&
98 (MultiByteToWideChar(CP_UTF8,0,utf8,-1,wide,count) == 0))
99 wide=(
wchar_t *) RelinquishMagickMemory(wide);
103static inline wchar_t *create_wchar_mode(
const char *mode)
111 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
112 wide=(
wchar_t *) AcquireQuantumMemory((
size_t) count+1,
114 if (wide == (
wchar_t *) NULL)
115 return((
wchar_t *) NULL);
116 if (MultiByteToWideChar(CP_UTF8,0,mode,-1,wide,count) == 0)
118 wide=(
wchar_t *) RelinquishMagickMemory(wide);
119 return((
wchar_t *) NULL);
123 wide[count-1] = L
'N';
128static inline int access_utf8(
const char *path,
int mode)
130 if (path == (
const char *) NULL)
132#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
133 return(access(path,mode));
141 path_wide=create_wchar_path(path);
142 if (path_wide == (
wchar_t *) NULL)
144 status=_waccess(path_wide,mode);
145 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
150#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
151#define close_utf8 _close
153#define close_utf8 close
156static inline FILE *fopen_utf8(
const char *path,
const char *mode)
158#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
159 return(fopen(path,mode));
168 path_wide=create_wchar_path(path);
169 if (path_wide == (
wchar_t *) NULL)
170 return((FILE *) NULL);
171 mode_wide=create_wchar_mode(mode);
172 if (mode_wide == (
wchar_t *) NULL)
174 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
175 return((FILE *) NULL);
177 file=_wfopen(path_wide,mode_wide);
178 mode_wide=(
wchar_t *) RelinquishMagickMemory(mode_wide);
179 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
184static inline MagickBooleanType is_symlink_utf8(
const char *path)
186#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
187#if defined(MAGICKCORE_POSIX_SUPPORT)
191 if (lstat(path,&status) == -1)
193 return(S_ISLNK(status.st_mode) != 0 ? MagickTrue : MagickFalse);
198 return(NTIsSymlinkWide(path));
202static inline void getcwd_utf8(
char *path,
size_t extent)
204#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
208 directory=getcwd(path,extent);
212 wide_path[MagickPathExtent];
214 (void) _wgetcwd(wide_path,MagickPathExtent-1);
215 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
219#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
224static inline int open_utf8(
const char *path,
int flags,mode_t mode)
226#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
227 return(open(path,flags,mode));
235 path_wide=create_wchar_path(path);
236 if (path_wide == (
wchar_t *) NULL)
239 status=_wopen(path_wide,flags | O_NOINHERIT,mode);
240 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
245static inline FILE *popen_utf8(
const char *command,
const char *type)
247#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
248 return(popen(command,type));
261 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
264 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
267 command_wide=(
wchar_t *) AcquireQuantumMemory((
size_t) length,
268 sizeof(*command_wide));
269 if (command_wide == (
wchar_t *) NULL)
271 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
273 file=_wpopen(command_wide,type_wide);
274 command_wide=(
wchar_t *) RelinquishMagickMemory(command_wide);
279static inline char *realpath_utf8(
const char *path)
281#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
282#if defined(MAGICKCORE_HAVE_REALPATH)
283 return(realpath(path,(
char *) NULL));
285 return(AcquireString(path));
310 if (path == (
const char *) NULL)
311 return((
char *) NULL);
312 length=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
314 return((
char *) NULL);
315 wide_path=(
wchar_t *) AcquireQuantumMemory(length,
sizeof(
wchar_t));
316 if (wide_path == (
wchar_t *) NULL)
317 return((
char *) NULL);
318 MultiByteToWideChar(CP_UTF8,0,path,-1,wide_path,length);
322 full_path_length=GetFullPathNameW(wide_path,0,NULL,NULL);
323 if (full_path_length == 0)
325 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
326 return((
char *) NULL);
328 full_path=(
wchar_t *) AcquireQuantumMemory(full_path_length,
sizeof(
wchar_t));
329 if (full_path == (
wchar_t *) NULL)
331 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
332 return((
char *) NULL);
334 GetFullPathNameW(wide_path,full_path_length,full_path,NULL);
335 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
339 file_handle=CreateFileW(full_path,GENERIC_READ,FILE_SHARE_READ |
340 FILE_SHARE_WRITE | FILE_SHARE_DELETE,NULL,OPEN_EXISTING,
341 FILE_FLAG_BACKUP_SEMANTICS,NULL);
342 if (file_handle != INVALID_HANDLE_VALUE)
347 final_path_length=GetFinalPathNameByHandleW(file_handle,NULL,0,
348 FILE_NAME_NORMALIZED);
349 if (final_path_length == 0)
351 CloseHandle(file_handle);
352 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
353 return((
char *) NULL);
355 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
356 full_path=(
wchar_t *) AcquireQuantumMemory(final_path_length,
358 if (full_path == (
wchar_t *) NULL)
360 CloseHandle(file_handle);
361 return((
char *) NULL);
363 GetFinalPathNameByHandleW(file_handle,full_path,final_path_length,
364 FILE_NAME_NORMALIZED);
365 CloseHandle(file_handle);
370 clean_path=full_path;
371 if (wcsncmp(full_path,L
"\\\\?\\",4) == 0)
372 clean_path=full_path+4;
376 utf8_length=WideCharToMultiByte(CP_UTF8,0,clean_path,-1,NULL,0,NULL,NULL);
377 if (utf8_length <= 0)
379 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
382 real_path=(
char *) AcquireQuantumMemory(utf8_length,
sizeof(
char));
383 if (real_path == (
char *) NULL)
385 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
388 WideCharToMultiByte(CP_UTF8,0,clean_path,-1,real_path,utf8_length,NULL,NULL);
389 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
394static inline int remove_utf8(
const char *path)
396#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
397 return(unlink(path));
405 path_wide=create_wchar_path(path);
406 if (path_wide == (
wchar_t *) NULL)
408 status=_wremove(path_wide);
409 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
414static inline int rename_utf8(
const char *source,
const char *destination)
416#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
417 return(rename(source,destination));
426 source_wide=create_wchar_path(source);
427 if (source_wide == (
wchar_t *) NULL)
429 destination_wide=create_wchar_path(destination);
430 if (destination_wide == (
wchar_t *) NULL)
432 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
435 status=_wrename(source_wide,destination_wide);
436 destination_wide=(
wchar_t *) RelinquishMagickMemory(destination_wide);
437 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
442static inline int set_file_timestamp(
const char *path,
struct stat *attributes)
447#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
448#if defined(MAGICKCORE_HAVE_UTIMENSAT)
449#if defined(__APPLE__) || defined(__NetBSD__)
450#define st_atim st_atimespec
451#define st_ctim st_ctimespec
452#define st_mtim st_mtimespec
458 timestamp[0].tv_sec=attributes->st_atim.tv_sec;
459 timestamp[0].tv_nsec=attributes->st_atim.tv_nsec;
460 timestamp[1].tv_sec=attributes->st_mtim.tv_sec;
461 timestamp[1].tv_nsec=attributes->st_mtim.tv_nsec;
462 status=utimensat(AT_FDCWD,path,timestamp,0);
467 timestamp.actime=attributes->st_atime;
468 timestamp.modtime=attributes->st_mtime;
469 status=utime(path,×tamp);
479 path_wide=create_wchar_path(path);
480 if (path_wide == (WCHAR *) NULL)
482 handle=CreateFileW(path_wide,FILE_WRITE_ATTRIBUTES,FILE_SHARE_WRITE |
483 FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
484 if (handle != (HANDLE) NULL)
494 date_time.QuadPart=(ULONGLONG) (attributes->st_ctime*10000000LL)+
495 116444736000000000LL;
496 creation_time.dwLowDateTime=date_time.LowPart;
497 creation_time.dwHighDateTime=date_time.HighPart;
498 date_time.QuadPart=(ULONGLONG) (attributes->st_atime*10000000LL)+
499 116444736000000000LL;
500 last_access_time.dwLowDateTime=date_time.LowPart;
501 last_access_time.dwHighDateTime=date_time.HighPart;
502 date_time.QuadPart=(ULONGLONG) (attributes->st_mtime*10000000LL)+
503 116444736000000000LL;
504 last_write_time.dwLowDateTime=date_time.LowPart;
505 last_write_time.dwHighDateTime=date_time.HighPart;
506 status=SetFileTime(handle,&creation_time,&last_access_time,&last_write_time);
510 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
515static inline int stat_utf8(
const char *path,
struct stat *attributes)
517#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
518 return(stat(path,attributes));
526 path_wide=create_wchar_path(path);
527 if (path_wide == (WCHAR *) NULL)
529 status=_wstati64(path_wide,attributes);
530 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
535#if defined(__cplusplus) || defined(c_plusplus)