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 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
|
/* gpathbuf.c: A mutable path builder
*
* SPDX-FileCopyrightText: 2023 Emmanuele Bassi
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include "gpathbuf.h"
#include "garray.h"
#include "gfileutils.h"
#include "ghash.h"
#include "gmessages.h"
#include "gstrfuncs.h"
/**
* GPathBuf:
*
* `GPathBuf` is a helper type that allows you to easily build paths from
* individual elements, using the platform specific conventions for path
* separators.
*
* ```c
* g_auto (GPathBuf) path;
*
* g_path_buf_init (&path);
*
* g_path_buf_push (&path, "usr");
* g_path_buf_push (&path, "bin");
* g_path_buf_push (&path, "echo");
*
* g_autofree char *echo = g_path_buf_to_path (&path);
* g_assert_cmpstr (echo, ==, "/usr/bin/echo");
* ```
*
* You can also load a full path and then operate on its components:
*
* ```c
* g_auto (GPathBuf) path;
*
* g_path_buf_init_from_path (&path, "/usr/bin/echo");
*
* g_path_buf_pop (&path);
* g_path_buf_push (&path, "sh");
*
* g_autofree char *sh = g_path_buf_to_path (&path);
* g_assert_cmpstr (sh, ==, "/usr/bin/sh");
* ```
*
* Since: 2.76
*/
typedef struct {
/* (nullable) (owned) (element-type filename) */
GPtrArray *path;
/* (nullable) (owned) */
char *extension;
gpointer padding[6];
} RealPathBuf;
G_STATIC_ASSERT (sizeof (GPathBuf) == sizeof (RealPathBuf));
#define PATH_BUF(b) ((RealPathBuf *) (b))
/**
* g_path_buf_init:
* @buf: a path buffer
*
* Initializes a `GPathBuf` instance.
*
* Returns: (transfer none): the initialized path builder
*
* Since: 2.76
*/
GPathBuf *
g_path_buf_init (GPathBuf *buf)
{
RealPathBuf *rbuf = PATH_BUF (buf);
rbuf->path = NULL;
rbuf->extension = NULL;
return buf;
}
/**
* g_path_buf_init_from_path:
* @buf: a path buffer
* @path: (type filename) (nullable): a file system path
*
* Initializes a `GPathBuf` instance with the given path.
*
* Returns: (transfer none): the initialized path builder
*
* Since: 2.76
*/
GPathBuf *
g_path_buf_init_from_path (GPathBuf *buf,
const char *path)
{
g_return_val_if_fail (buf != NULL, NULL);
g_return_val_if_fail (path == NULL || *path != '\0', NULL);
g_path_buf_init (buf);
if (path == NULL)
return buf;
else
return g_path_buf_push (buf, path);
}
/**
* g_path_buf_clear:
* @buf: a path buffer
*
* Clears the contents of the path buffer.
*
* This function should be use to free the resources in a stack-allocated
* `GPathBuf` initialized using g_path_buf_init() or
* g_path_buf_init_from_path().
*
* Since: 2.76
*/
void
g_path_buf_clear (GPathBuf *buf)
{
RealPathBuf *rbuf = PATH_BUF (buf);
g_return_if_fail (buf != NULL);
g_clear_pointer (&rbuf->path, g_ptr_array_unref);
g_clear_pointer (&rbuf->extension, g_free);
}
/**
* g_path_buf_clear_to_path:
* @buf: a path buffer
*
* Clears the contents of the path buffer and returns the built path.
*
* This function returns `NULL` if the `GPathBuf` is empty.
*
* See also: g_path_buf_to_path()
*
* Returns: (transfer full) (nullable) (type filename): the built path
*
* Since: 2.76
*/
char *
g_path_buf_clear_to_path (GPathBuf *buf)
{
char *res;
g_return_val_if_fail (buf != NULL, NULL);
res = g_path_buf_to_path (buf);
g_path_buf_clear (buf);
return g_steal_pointer (&res);
}
/**
* g_path_buf_new:
*
* Allocates a new `GPathBuf`.
*
* Returns: (transfer full): the newly allocated path buffer
*
* Since: 2.76
*/
GPathBuf *
g_path_buf_new (void)
{
return g_path_buf_init (g_new (GPathBuf, 1));
}
/**
* g_path_buf_new_from_path:
* @path: (type filename) (nullable): the path used to initialize the buffer
*
* Allocates a new `GPathBuf` with the given @path.
*
* Returns: (transfer full): the newly allocated path buffer
*
* Since: 2.76
*/
GPathBuf *
g_path_buf_new_from_path (const char *path)
{
return g_path_buf_init_from_path (g_new (GPathBuf, 1), path);
}
/**
* g_path_buf_free:
* @buf: (transfer full) (not nullable): a path buffer
*
* Frees a `GPathBuf` allocated by g_path_buf_new().
*
* Since: 2.76
*/
void
g_path_buf_free (GPathBuf *buf)
{
g_return_if_fail (buf != NULL);
g_path_buf_clear (buf);
g_free (buf);
}
/**
* g_path_buf_free_to_path:
* @buf: (transfer full) (not nullable): a path buffer
*
* Frees a `GPathBuf` allocated by g_path_buf_new(), and
* returns the path inside the buffer.
*
* This function returns `NULL` if the `GPathBuf` is empty.
*
* See also: g_path_buf_to_path()
*
* Returns: (transfer full) (nullable) (type filename): the path
*
* Since: 2.76
*/
char *
g_path_buf_free_to_path (GPathBuf *buf)
{
char *res;
g_return_val_if_fail (buf != NULL, NULL);
res = g_path_buf_clear_to_path (buf);
g_path_buf_free (buf);
return g_steal_pointer (&res);
}
/**
* g_path_buf_copy:
* @buf: (not nullable): a path buffer
*
* Copies the contents of a path buffer into a new `GPathBuf`.
*
* Returns: (transfer full): the newly allocated path buffer
*
* Since: 2.76
*/
GPathBuf *
g_path_buf_copy (GPathBuf *buf)
{
RealPathBuf *rbuf = PATH_BUF (buf);
RealPathBuf *rcopy;
GPathBuf *copy;
g_return_val_if_fail (buf != NULL, NULL);
copy = g_path_buf_new ();
rcopy = PATH_BUF (copy);
if (rbuf->path != NULL)
{
rcopy->path = g_ptr_array_new_null_terminated (rbuf->path->len, g_free, TRUE);
for (guint i = 0; i < rbuf->path->len; i++)
{
const char *p = g_ptr_array_index (rbuf->path, i);
if (p != NULL)
g_ptr_array_add (rcopy->path, g_strdup (p));
}
}
rcopy->extension = g_strdup (rbuf->extension);
return copy;
}
/**
* g_path_buf_push:
* @buf: a path buffer
* @path: (type filename): a path
*
* Extends the given path buffer with @path.
*
* If @path is absolute, it replaces the current path.
*
* If @path contains a directory separator, the buffer is extended by
* as many elements the path provides.
*
* On Windows, both forward slashes and backslashes are treated as
* directory separators. On other platforms, %G_DIR_SEPARATOR_S is the
* only directory separator.
*
* |[<!-- language="C" -->
* GPathBuf buf, cmp;
*
* g_path_buf_init_from_path (&buf, "/tmp");
* g_path_buf_push (&buf, ".X11-unix/X0");
* g_path_buf_init_from_path (&cmp, "/tmp/.X11-unix/X0");
* g_assert_true (g_path_buf_equal (&buf, &cmp));
* g_path_buf_clear (&cmp);
*
* g_path_buf_push (&buf, "/etc/locale.conf");
* g_path_buf_init_from_path (&cmp, "/etc/locale.conf");
* g_assert_true (g_path_buf_equal (&buf, &cmp));
* g_path_buf_clear (&cmp);
*
* g_path_buf_clear (&buf);
* ]|
*
* Returns: (transfer none): the same pointer to @buf, for convenience
*
* Since: 2.76
*/
GPathBuf *
g_path_buf_push (GPathBuf *buf,
const char *path)
{
RealPathBuf *rbuf = PATH_BUF (buf);
g_return_val_if_fail (buf != NULL, NULL);
g_return_val_if_fail (path != NULL && *path != '\0', buf);
if (g_path_is_absolute (path))
{
#ifdef G_OS_WIN32
char **elements = g_strsplit_set (path, "\\/", -1);
#else
char **elements = g_strsplit (path, G_DIR_SEPARATOR_S, -1);
#endif
#ifdef G_OS_UNIX
/* strsplit() will add an empty element for the leading root,
* which will cause the path build to ignore it; to avoid it,
* we re-inject the root as the first element.
*
* The first string is empty, but it's still allocated, so we
* need to free it to avoid leaking it.
*/
g_free (elements[0]);
elements[0] = g_strdup ("/");
#endif
g_clear_pointer (&rbuf->path, g_ptr_array_unref);
rbuf->path = g_ptr_array_new_null_terminated (g_strv_length (elements), g_free, TRUE);
/* Skip empty elements caused by repeated separators */
for (guint i = 0; elements[i] != NULL; i++)
{
if (*elements[i] != '\0')
g_ptr_array_add (rbuf->path, g_steal_pointer (&elements[i]));
else
g_free (elements[i]);
}
g_free (elements);
}
else
{
char **elements = g_strsplit (path, G_DIR_SEPARATOR_S, -1);
if (rbuf->path == NULL)
rbuf->path = g_ptr_array_new_null_terminated (g_strv_length (elements), g_free, TRUE);
/* Skip empty elements caused by repeated separators */
for (guint i = 0; elements[i] != NULL; i++)
{
if (*elements[i] != '\0')
g_ptr_array_add (rbuf->path, g_steal_pointer (&elements[i]));
else
g_free (elements[i]);
}
g_free (elements);
}
return buf;
}
/**
* g_path_buf_pop:
* @buf: a path buffer
*
* Removes the last element of the path buffer.
*
* If there is only one element in the path buffer (for example, `/` on
* Unix-like operating systems or the drive on Windows systems), it will
* not be removed and %FALSE will be returned instead.
*
* |[<!-- language="C" -->
* GPathBuf buf, cmp;
*
* g_path_buf_init_from_path (&buf, "/bin/sh");
*
* g_path_buf_pop (&buf);
* g_path_buf_init_from_path (&cmp, "/bin");
* g_assert_true (g_path_buf_equal (&buf, &cmp));
* g_path_buf_clear (&cmp);
*
* g_path_buf_pop (&buf);
* g_path_buf_init_from_path (&cmp, "/");
* g_assert_true (g_path_buf_equal (&buf, &cmp));
* g_path_buf_clear (&cmp);
*
* g_path_buf_clear (&buf);
* ]|
*
* Returns: `TRUE` if the buffer was modified and `FALSE` otherwise
*
* Since: 2.76
*/
gboolean
g_path_buf_pop (GPathBuf *buf)
{
RealPathBuf *rbuf = PATH_BUF (buf);
g_return_val_if_fail (buf != NULL, FALSE);
g_return_val_if_fail (rbuf->path != NULL, FALSE);
/* Keep the first element of the buffer; it's either '/' or the drive */
if (rbuf->path->len > 1)
{
g_ptr_array_remove_index (rbuf->path, rbuf->path->len - 1);
return TRUE;
}
return FALSE;
}
/**
* g_path_buf_set_filename:
* @buf: a path buffer
* @file_name: (type filename) (not nullable): the file name in the path
*
* Sets the file name of the path.
*
* If the path buffer is empty, the filename is left unset and this
* function returns `FALSE`.
*
* If the path buffer only contains the root element (on Unix-like operating
* systems) or the drive (on Windows), this is the equivalent of pushing
* the new @file_name.
*
* If the path buffer contains a path, this is the equivalent of
* popping the path buffer and pushing @file_name, creating a
* sibling of the original path.
*
* |[<!-- language="C" -->
* GPathBuf buf, cmp;
*
* g_path_buf_init_from_path (&buf, "/");
*
* g_path_buf_set_filename (&buf, "bar");
* g_path_buf_init_from_path (&cmp, "/bar");
* g_assert_true (g_path_buf_equal (&buf, &cmp));
* g_path_buf_clear (&cmp);
*
* g_path_buf_set_filename (&buf, "baz.txt");
* g_path_buf_init_from_path (&cmp, "/baz.txt");
* g_assert_true (g_path_buf_equal (&buf, &cmp);
* g_path_buf_clear (&cmp);
*
* g_path_buf_clear (&buf);
* ]|
*
* Returns: `TRUE` if the file name was replaced, and `FALSE` otherwise
*
* Since: 2.76
*/
gboolean
g_path_buf_set_filename (GPathBuf *buf,
const char *file_name)
{
g_return_val_if_fail (buf != NULL, FALSE);
g_return_val_if_fail (file_name != NULL, FALSE);
if (PATH_BUF (buf)->path == NULL)
return FALSE;
g_path_buf_pop (buf);
g_path_buf_push (buf, file_name);
return TRUE;
}
/**
* g_path_buf_set_extension:
* @buf: a path buffer
* @extension: (type filename) (nullable): the file extension
*
* Adds an extension to the file name in the path buffer.
*
* If @extension is `NULL`, the extension will be unset.
*
* If the path buffer does not have a file name set, this function returns
* `FALSE` and leaves the path buffer unmodified.
*
* Returns: `TRUE` if the extension was replaced, and `FALSE` otherwise
*
* Since: 2.76
*/
gboolean
g_path_buf_set_extension (GPathBuf *buf,
const char *extension)
{
RealPathBuf *rbuf = PATH_BUF (buf);
g_return_val_if_fail (buf != NULL, FALSE);
if (rbuf->path != NULL)
return g_set_str (&rbuf->extension, extension);
else
return FALSE;
}
/**
* g_path_buf_to_path:
* @buf: a path buffer
*
* Retrieves the built path from the path buffer.
*
* On Windows, the result contains backslashes as directory separators,
* even if forward slashes were used in input.
*
* If the path buffer is empty, this function returns `NULL`.
*
* Returns: (transfer full) (type filename) (nullable): the path
*
* Since: 2.76
*/
char *
g_path_buf_to_path (GPathBuf *buf)
{
RealPathBuf *rbuf = PATH_BUF (buf);
char *path = NULL;
g_return_val_if_fail (buf != NULL, NULL);
if (rbuf->path != NULL)
path = g_build_filenamev ((char **) rbuf->path->pdata);
if (path != NULL && rbuf->extension != NULL)
{
char *tmp = g_strconcat (path, ".", rbuf->extension, NULL);
g_free (path);
path = g_steal_pointer (&tmp);
}
return path;
}
/**
* g_path_buf_equal:
* @v1: (not nullable): a path buffer to compare
* @v2: (not nullable): a path buffer to compare
*
* Compares two path buffers for equality and returns `TRUE`
* if they are equal.
*
* The paths inside the path buffers are not going to be normalized,
* so `X/Y/Z/A/..`, `X/./Y/Z` and `X/Y/Z` are not going to be considered
* equal.
*
* This function can be passed to g_hash_table_new() as the
* `key_equal_func` parameter.
*
* Returns: `TRUE` if the two path buffers are equal,
* and `FALSE` otherwise
*
* Since: 2.76
*/
gboolean
g_path_buf_equal (gconstpointer v1,
gconstpointer v2)
{
if (v1 == v2)
return TRUE;
/* We resolve the buffer into a path to normalize its contents;
* this won't resolve symbolic links or `.` and `..` components
*/
char *p1 = g_path_buf_to_path ((GPathBuf *) v1);
char *p2 = g_path_buf_to_path ((GPathBuf *) v2);
gboolean res = p1 != NULL && p2 != NULL
? g_str_equal (p1, p2)
: FALSE;
g_free (p1);
g_free (p2);
return res;
}
|