Nuitka
The Python compiler
Loading...
Searching...
No Matches
filesystem_paths.h
1// Copyright 2026, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3#ifndef __NUITKA_FILESYSTEM_PATH_OPS_H__
4#define __NUITKA_FILESYSTEM_PATH_OPS_H__
5
6#include "nuitka/safe_string_ops.h"
7
8// Have a type for filename type different on Linux and Win32.
9#if defined(_WIN32)
10#define _NUITKA_NATIVE_WCHAR_ARGV 1
11#define filename_char_t wchar_t
12#define FILENAME_EMPTY_STR L""
13#define FILENAME_SEP_STR L"\\"
14#define FILENAME_SEP_CHAR L'\\'
15#define FILENAME_FORMAT_STR "%ls"
16#define copyStringSafeFilename copyStringSafeW
17#define appendStringSafeFilename appendWStringSafeW
18#define appendCharSafeFilename appendWCharSafeW
19#define FILENAME_TMP_STR L".tmp"
20#define FILENAME_AWAY_STR L".away"
21#define expandTemplatePathFilename expandTemplatePathW
22#define strlenFilename wcslen
23#define strcmpFilename wcscmp
24#define strncmpFilename wcsncmp
25#define findFilenameSubstring wcsstr
26#define strdupFilename wcsdup
27#define scanFilename swscanf
28#define Nuitka_String_FromFilename(filename) NuitkaUnicode_FromWideChar(filename, -1)
29#else
30#define _NUITKA_NATIVE_WCHAR_ARGV 0
31#define filename_char_t char
32#define FILENAME_EMPTY_STR ""
33#define FILENAME_SEP_STR "/"
34#define FILENAME_SEP_CHAR '/'
35#define FILENAME_FORMAT_STR "%s"
36#define copyStringSafeFilename copyStringSafe
37#define appendStringSafeFilename appendStringSafe
38#define appendCharSafeFilename appendCharSafe
39#define FILENAME_TMP_STR ".tmp"
40#define FILENAME_AWAY_STR ".away"
41#define expandTemplatePathFilename expandTemplatePath
42#define strlenFilename strlen
43#define strcmpFilename strcmp
44#define strncmpFilename strncmp
45#define findFilenameSubstring strstr
46#define strdupFilename strdup
47#define scanFilename sscanf
48#define Nuitka_String_FromFilename Nuitka_String_FromString
49#endif
50
51#if defined(_WIN32)
52#include <windows.h>
53#endif
54
55#include <stdbool.h>
56#include <stdint.h>
57#include <stdio.h>
58#include <wchar.h>
59
60// Have a type for file type different on Linux and Win32.
61#if defined(_WIN32)
62#define FILE_HANDLE HANDLE
63#define FILE_HANDLE_NULL INVALID_HANDLE_VALUE
64#else
65#define FILE_HANDLE FILE *
66#define FILE_HANDLE_NULL NULL
67#endif
68
69// Defined by Python headers, for onefile we do it ourselves.
70#ifndef MAXPATHLEN
71#define MAXPATHLEN 4096
72#endif
73
74// Get path of the running binary.
75extern filename_char_t const *getBinaryPath(void);
76
77// Get the DLL directory, set the DLL filename if necessary..
78extern void setDllFilename(filename_char_t const *filename);
79extern filename_char_t const *getDllDirectory(void);
80
81extern FILE_HANDLE openFileForReading(filename_char_t const *filename);
82extern FILE_HANDLE createFileForWriting(filename_char_t const *filename);
83extern int64_t getFileSize(FILE_HANDLE file_handle);
84extern bool readFileChunk(FILE_HANDLE file_handle, void *buffer, size_t size);
85extern bool writeFileChunk(FILE_HANDLE file_handle, void const *buffer, size_t size);
86extern bool closeFile(FILE_HANDLE target_file);
87extern error_code_t getLastErrorCode(void);
88
89extern bool isExecutableFile(filename_char_t const *filename);
90extern int getFileMode(filename_char_t const *filename);
91extern bool copyFile(filename_char_t const *source, filename_char_t const *dest, int mode);
92extern bool deleteFile(filename_char_t const *filename);
93extern bool renameFile(filename_char_t const *source, filename_char_t const *dest);
94
95extern uint32_t getFileCRC32(filename_char_t const *filename);
96
97// Expand symbolic paths, containing {TEMP}, {PID} without overflowing.
98extern bool expandTemplatePathW(wchar_t *target, wchar_t const *source, size_t buffer_size);
99extern bool expandTemplatePath(char *target, char const *source, size_t buffer_size);
100
101// Strip the last part of a filename, giving the directory name.
102extern filename_char_t *stripBaseFilename(filename_char_t const *filename);
103
104// Normalize a given path, removing duplicate separators and ".." and "." usages
105// with separators.
106extern void normalizePath(filename_char_t *filename);
107
108#endif
109
110// Part of "Nuitka", an optimizing Python compiler that is compatible and
111// integrates with CPython, but also works on its own.
112//
113// Licensed under the GNU Affero General Public License, Version 3 (the "License");
114// you may not use this file except in compliance with the License.
115// You may obtain a copy of the License at
116//
117// http://www.gnu.org/licenses/agpl.txt
118//
119// Unless required by applicable law or agreed to in writing, software
120// distributed under the License is distributed on an "AS IS" BASIS,
121// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122// See the License for the specific language governing permissions and
123// limitations under the License.