Raymond Toy pushed to branch issue-373-handle-temp-files at cmucl / cmucl
Commits:
-
71b5e795
by Raymond Toy at 2025-11-11T18:37:08-08:00
4 changed files:
Changes:
| ... | ... | @@ -624,11 +624,7 @@ |
| 624 | 624 | (error "Unable to find path to temporary directory"))
|
| 625 | 625 | |
| 626 | 626 | (unwind-protect
|
| 627 | - (let* ((string (unix::%file->name (cast path c-call:c-string)))
|
|
| 628 | - (len (length string)))
|
|
| 629 | - (if (char= #\/ (aref string (1- len)))
|
|
| 630 | - string
|
|
| 631 | - (concatenate 'string string "/")))
|
|
| 627 | + (unix::%file->name (cast path c-call:c-string))
|
|
| 632 | 628 | (unless (alien:null-alien path)
|
| 633 | 629 | (alien:free-alien path)))))
|
| 634 | 630 | |
| ... | ... | @@ -674,6 +670,7 @@ |
| 674 | 670 | (when (pathnamep ,filename)
|
| 675 | 671 | (delete-file ,filename))))))
|
| 676 | 672 | |
| 673 | +#+nil
|
|
| 677 | 674 | (defun recursive-delete-directory (dir)
|
| 678 | 675 | _N"Recursively delete the directory DIR. All files and subdirectories of
|
| 679 | 676 | DIR are removed. DIR must be a pathname to a directory. Any NAME
|
| ... | ... | @@ -690,6 +687,25 @@ |
| 690 | 687 | (unix:unix-rmdir (namestring dir))
|
| 691 | 688 | (values))
|
| 692 | 689 | |
| 690 | +(defun delete-directory (dirname &key recursive)
|
|
| 691 | + _N"Delete the directory Dirname. If the Recursive is non-NIL,
|
|
| 692 | + recursively delete the directory Dirname including all files and
|
|
| 693 | + subdirectories. Dirname must be a pathname to a directory. Any NAME
|
|
| 694 | + or TYPE components in Dirname are ignored."
|
|
| 695 | + (declare (type pathname dirname))
|
|
| 696 | + (when recusive
|
|
| 697 | + ;; Find all the files or directories in DIRNAME.
|
|
| 698 | + (dolist (path (directory (merge-pathnames "*.*" dirname)))
|
|
| 699 | + ;; If the path is a directory, recursively delete the directory.
|
|
| 700 | + ;; Otherwise delete the file. We do not follow any symlinks.
|
|
| 701 | + (if (eq (unix:unix-file-kind (namestring path)) :directory)
|
|
| 702 | + (delete-directory path :recursive t)
|
|
| 703 | + (delete-file path))))
|
|
| 704 | + ;; Finally delete the directory.
|
|
| 705 | + (unix:unix-rmdir (namestring dir))
|
|
| 706 | + (values))
|
|
| 707 | + |
|
| 708 | + |
|
| 693 | 709 | ;;; WITH-TEMPORARY-DIRECTORY -- Public
|
| 694 | 710 | (defmacro with-temporary-directory ((dirname &key directory (prefix "cmucl-temp-dir-"))
|
| 695 | 711 | &parse-body (forms decls))
|
| ... | ... | @@ -717,4 +733,4 @@ |
| 717 | 733 | ;; If a temp directory was created, remove it and all its
|
| 718 | 734 | ;; contents. Is there a better way?
|
| 719 | 735 | (when ,dirname
|
| 720 | - (recursive-delete-directory ,dirname)))))) |
|
| 736 | + (delete-directory ,dirname :recursive t)))))) |
| ... | ... | @@ -6014,6 +6014,14 @@ msgid "" |
| 6014 | 6014 | " or TYPE components in DIR are ignored."
|
| 6015 | 6015 | msgstr ""
|
| 6016 | 6016 | |
| 6017 | +#: src/code/extensions.lisp
|
|
| 6018 | +msgid ""
|
|
| 6019 | +"Delete the directory Dirname. If the Recursive is non-NIL,\n"
|
|
| 6020 | +" recursively delete the directory Dirname including all files and\n"
|
|
| 6021 | +" subdirectories. Dirname must be a pathname to a directory. Any NAME\n"
|
|
| 6022 | +" or TYPE components in Dirname are ignored."
|
|
| 6023 | +msgstr ""
|
|
| 6024 | + |
|
| 6017 | 6025 | #: src/code/extensions.lisp
|
| 6018 | 6026 | msgid ""
|
| 6019 | 6027 | "Return a namestring to a temporary directory. If Directory is not\n"
|
| ... | ... | @@ -588,6 +588,8 @@ os_temporary_directory(void) |
| 588 | 588 | * macosx has a secure per-user temporary directory.
|
| 589 | 589 | * Don't cache the result as this is only called once.
|
| 590 | 590 | */
|
| 591 | + int len;
|
|
| 592 | + char *result;
|
|
| 591 | 593 | char path[PATH_MAX];
|
| 592 | 594 | |
| 593 | 595 | int path_size = confstr(_CS_DARWIN_USER_TEMP_DIR, path, PATH_MAX);
|
| ... | ... | @@ -595,5 +597,18 @@ os_temporary_directory(void) |
| 595 | 597 | strlcpy(path, "/tmp", sizeof(path));
|
| 596 | 598 | }
|
| 597 | 599 | |
| 598 | - return strdup(path);
|
|
| 600 | + /* Append a slash if needed */
|
|
| 601 | + len = strlen(path);
|
|
| 602 | + result = malloc(len + 1);
|
|
| 603 | +
|
|
| 604 | + /* If malloc fails, just return NULL. */
|
|
| 605 | + if (result) {
|
|
| 606 | + strcpy(result, path);
|
|
| 607 | + |
|
| 608 | + if (path[len] != '/') {
|
|
| 609 | + strcat(result, "/");
|
|
| 610 | + }
|
|
| 611 | + }
|
|
| 612 | +
|
|
| 613 | + return result;
|
|
| 599 | 614 | } |
| ... | ... | @@ -645,10 +645,25 @@ os_temporary_directory(void) |
| 645 | 645 | * Otherwise, just assume "/tmp" will work.
|
| 646 | 646 | */
|
| 647 | 647 | char *tmp_path = getenv("TMP");
|
| 648 | + char *result;
|
|
| 649 | + int len;
|
|
| 648 | 650 | |
| 649 | 651 | if (tmp_path == NULL) {
|
| 650 | 652 | tmp_path = "/tmp";
|
| 651 | 653 | }
|
| 652 | 654 | |
| 653 | - return strdup(tmp_path);
|
|
| 655 | + /* Append a slash if needed */
|
|
| 656 | + len = strlen(tmp_path);
|
|
| 657 | + result = malloc(len + 1);
|
|
| 658 | + |
|
| 659 | + /* If malloc fails, just return NULL. */
|
|
| 660 | + if (result) {
|
|
| 661 | + strcpy(result, tmp_path);
|
|
| 662 | + |
|
| 663 | + if (tmp_path[len] != '/') {
|
|
| 664 | + strcat(result, "/");
|
|
| 665 | + }
|
|
| 666 | + }
|
|
| 667 | +
|
|
| 668 | + return result;
|
|
| 654 | 669 | } |