1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
//------------------------------------------------------------------------------
// GB_file.c: portable file I/O
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// These methods provide portable open/close/lock/unlock/mkdir functions, in
// support of the JIT. If the JIT is disabled at compile time, these functions
// do nothing.
// Windows references:
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/open-wopen
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/close
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fdopen-wfdopen
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/mkdir-wmkdir
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/lock-file
// https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/unlock-file
#include "GB.h"
#include "jitifyer/GB_file.h"
#ifndef NJIT
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#if GB_WINDOWS
// Windows
#include <io.h>
#include <direct.h>
#include <windows.h>
#define GB_OPEN _open
#define GB_CLOSE _close
#define GB_FDOPEN _fdopen
#define GB_MKDIR(path) _mkdir (path)
#define GB_READ_ONLY (_O_RDONLY)
#define GB_WRITE_ONLY (_O_WRONLY | _O_CREAT | _O_APPEND)
#define GB_READ_WRITE (_O_RDWR | _O_CREAT | _O_APPEND)
#define GB_MODE (_S_IREAD | _S_IWRITE)
#else
// POSIX
#include <unistd.h>
#include <dlfcn.h>
#define GB_OPEN open
#define GB_CLOSE close
#define GB_FDOPEN fdopen
#define GB_MKDIR(path) mkdir (path, S_IRWXU)
#define GB_READ_ONLY (O_RDONLY)
#define GB_WRITE_ONLY (O_WRONLY | O_CREAT | O_APPEND)
#define GB_READ_WRITE (O_RDWR | O_CREAT | O_APPEND)
#define GB_MODE (S_IRUSR | S_IWUSR)
#endif
#endif
//------------------------------------------------------------------------------
// GB_file_lock: lock a file for exclusive writing
//------------------------------------------------------------------------------
// Returns true if successful, false on error
static bool GB_file_lock (FILE *fp, int fd)
{
#ifdef NJIT
{
// JIT disabled
return (false) ;
}
#elif GB_WINDOWS
{
// lock file for Windows
_lock_file (fp) ;
return (true) ;
}
#else
{
// lock file for POSIX
struct flock lock ;
lock.l_type = F_WRLCK ;
lock.l_whence = SEEK_SET ;
lock.l_start = 0 ;
lock.l_len = 0 ;
lock.l_pid = 0 ;
return (fcntl (fd, F_SETLKW, &lock) == 0) ;
}
#endif
}
//------------------------------------------------------------------------------
// GB_file_unlock: unlock a file
//------------------------------------------------------------------------------
// Returns true if successful, false on error
static bool GB_file_unlock (FILE *fp, int fd)
{
#ifdef NJIT
{
// JIT disabled
return (false) ;
}
#elif GB_WINDOWS
{
// unlock file for Windows
_unlock_file (fp) ;
return (true) ;
}
#else
{
// unlock file for POSIX
struct flock lock ;
lock.l_type = F_UNLCK ;
lock.l_whence = SEEK_SET ;
lock.l_start = 0 ;
lock.l_len = 0 ;
lock.l_pid = 0 ;
return (fcntl (fd, F_SETLKW, &lock) == 0) ;
}
#endif
}
//------------------------------------------------------------------------------
// GB_file_open_and_lock: open and lock a file for exclusive write
//------------------------------------------------------------------------------
bool GB_file_open_and_lock // true if successful, false on error
(
// input
char *filename, // full path to file to open
// output
FILE **fp_handle, // file pointer of open file (NULL on error)
int *fd_handle // file descriptor of open file (-1 on error)
)
{
#ifdef NJIT
{
// JIT disabled
return (false) ;
}
#else
{
if (filename == NULL || fp_handle == NULL || fd_handle == NULL)
{
// failure: inputs invalid
return (false) ;
}
(*fp_handle) = NULL ;
(*fd_handle) = -1 ;
// open the file, creating it if it doesn't exist
int fd = GB_OPEN (filename, GB_READ_WRITE, GB_MODE) ;
if (fd == -1)
{
// failure: file does not exist or cannot be created
return (false) ;
}
// get the file pointer from the file descriptor
FILE *fp = GB_FDOPEN (fd, "w+") ;
if (fp == NULL)
{
// failure: cannot create file pointer from file descriptor
GB_CLOSE (fd) ;
return (false) ;
}
// lock the file
if (!GB_file_lock (fp, fd))
{
// failure: cannot lock the file
fclose (fp) ;
return (false) ;
}
// success: file exists, is open, and is locked for writing
(*fp_handle) = fp ;
(*fd_handle) = fd ;
return (true) ;
}
#endif
}
//------------------------------------------------------------------------------
// GB_file_unlock_and_close: unlock and close a file
//------------------------------------------------------------------------------
bool GB_file_unlock_and_close // true if successful, false on error
(
// input/output
FILE **fp_handle, // file pointer, set to NULL on output
int *fd_handle // file descriptor, set to -1 on output
)
{
#ifdef NJIT
{
// JIT disabled
return (false) ;
}
#else
{
if (fp_handle == NULL || fd_handle == NULL)
{
// failure: inputs invalid
return (false) ;
}
FILE *fp = (*fp_handle) ;
int fd = (*fd_handle) ;
(*fp_handle) = NULL ;
(*fd_handle) = -1 ;
if (fp == NULL || fd < 0)
{
// failure: inputs invalid
return (false) ;
}
// unlock the file
bool ok = GB_file_unlock (fp, fd) ;
// close the file
ok = ok && (fclose (fp) == 0) ;
// return result
return (ok) ;
}
#endif
}
//------------------------------------------------------------------------------
// GB_file_mkdir: create a directory
//------------------------------------------------------------------------------
// Create a directory, including all parent directories if they do not exist.
// Returns true if the directory already exists or if it was successfully
// created. Returns true if the JIT is disabled (the directory is not created
// but also not needed in that case). Returns false on error.
bool GB_file_mkdir (char *path)
{
if (path == NULL)
{
// invalid input
return (false) ;
}
#ifdef NJIT
{
// JIT disabled; do not create the directory but do not return an error
return (true) ;
}
#else
{
// create all the leading directories
int result = 0 ;
bool first = true ;
for (char *p = path ; *p ; p++)
{
// look for a file separator
if (*p == '/' || *p == '\\')
{
// found a file separator
if (!first)
{
// terminate the path at this file separator
char save = *p ;
*p = '\0' ;
// construct the directory at this path
result = GB_MKDIR (path) ;
// err = (result == -1) ? errno : 0 ;
// restore the path
*p = save ;
}
first = false ;
}
}
// create the final directory
result = GB_MKDIR (path) ;
int err = (result == -1) ? errno : 0 ;
return (err == 0 || err == EEXIST) ;
}
#endif
}
//------------------------------------------------------------------------------
// GB_file_dlopen: open a dynamic library
//------------------------------------------------------------------------------
void *GB_file_dlopen (char *library_name)
{
#ifdef NJIT
{
// JIT disabled
return (NULL) ;
}
#elif GB_WINDOWS
{
// open a Windows dll
HINSTANCE hdll = LoadLibrary (library_name) ;
return ((void *) hdll) ;
}
#else
{
// open a POSIX dynamic library
return (dlopen (library_name, RTLD_LAZY)) ;
}
#endif
}
//------------------------------------------------------------------------------
// GB_file_dlsym: get a function pointer from a dynamic library
//------------------------------------------------------------------------------
void *GB_file_dlsym (void *dl_handle, char *symbol)
{
#ifdef NJIT
{
// JIT disabled
return (NULL) ;
}
#elif GB_WINDOWS
{
// lookup a symbol in a Windows dll
void *p = (void *) GetProcAddress (dl_handle, symbol) ;
return ((void *) p) ;
}
#else
{
// lookup a symbol in a POSIX dynamic library
return (dlsym (dl_handle, symbol)) ;
}
#endif
}
//------------------------------------------------------------------------------
// GB_file_dlclose: close a dynamic library
//------------------------------------------------------------------------------
void GB_file_dlclose (void *dl_handle)
{
if (dl_handle != NULL)
{
#ifdef NJIT
{
// JIT disabled: do nothing
}
#elif GB_WINDOWS
{
// close a Windows dll
FreeLibrary (dl_handle) ;
}
#else
{
// close a POSIX dynamic library
dlclose (dl_handle) ;
}
#endif
}
}
|