type.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2002 Juli Mallett. All rights reserved.
  3. *
  4. * This software was written by Juli Mallett <jmallett@FreeBSD.org> for the
  5. * FreeBSD project. Redistribution and use in source and binary forms, with
  6. * or without modification, are permitted provided that the following
  7. * conditions are met:
  8. *
  9. * 1. Redistribution of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistribution in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <u.h>
  28. #include <libc.h>
  29. #include <ufs/libufsdat.h>
  30. #include <ufs/fs.h>
  31. #include <ufs/libufs.h>
  32. /* Internally, track the 'name' value, it's ours. */
  33. #define MINE_NAME 0x01
  34. /* Track if its fd points to a writable device. */
  35. #define MINE_WRITE 0x02
  36. int
  37. ufs_disk_close(Uufsd *disk)
  38. {
  39. libufserror(disk, nil);
  40. close(disk->d_fd);
  41. if (disk->d_inoblock != nil) {
  42. free(disk->d_inoblock);
  43. disk->d_inoblock = nil;
  44. }
  45. if (disk->d_mine & MINE_NAME) {
  46. free((char *)(uintptr_t)disk->d_name);
  47. disk->d_name = nil;
  48. }
  49. if (disk->d_sbcsum != nil) {
  50. free(disk->d_sbcsum);
  51. disk->d_sbcsum = nil;
  52. }
  53. return (0);
  54. }
  55. int
  56. ufs_disk_create(Uufsd *disk)
  57. {
  58. disk->d_fd = create(disk->d_name, ORDWR, 0664);
  59. if (disk->d_fd < 0) {
  60. libufserror(disk, "failed to create file");
  61. return (-1);
  62. }
  63. disk->d_mine |= MINE_WRITE;
  64. return (0);
  65. }
  66. int
  67. ufs_disk_write(Uufsd *disk)
  68. {
  69. libufserror(disk, nil);
  70. if (disk->d_mine & MINE_WRITE)
  71. return (0);
  72. close(disk->d_fd);
  73. disk->d_fd = open(disk->d_name, ORDWR);
  74. if (disk->d_fd < 0) {
  75. libufserror(disk, "failed to open file for writing");
  76. return (-1);
  77. }
  78. disk->d_mine |= MINE_WRITE;
  79. return (0);
  80. }