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