sandboxfs.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2012, Google Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  17. * MA 02111-1307 USA
  18. */
  19. #include <common.h>
  20. #include <fs.h>
  21. #include <os.h>
  22. int sandbox_fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
  23. {
  24. return 0;
  25. }
  26. long sandbox_fs_read_at(const char *filename, unsigned long pos,
  27. void *buffer, unsigned long maxsize)
  28. {
  29. ssize_t size;
  30. int fd, ret;
  31. fd = os_open(filename, OS_O_RDONLY);
  32. if (fd < 0)
  33. return fd;
  34. ret = os_lseek(fd, pos, OS_SEEK_SET);
  35. if (ret == -1) {
  36. os_close(fd);
  37. return ret;
  38. }
  39. if (!maxsize)
  40. maxsize = os_get_filesize(filename);
  41. size = os_read(fd, buffer, maxsize);
  42. os_close(fd);
  43. return size;
  44. }
  45. long sandbox_fs_write_at(const char *filename, unsigned long pos,
  46. void *buffer, unsigned long towrite)
  47. {
  48. ssize_t size;
  49. int fd, ret;
  50. fd = os_open(filename, OS_O_RDWR | OS_O_CREAT);
  51. if (fd < 0)
  52. return fd;
  53. ret = os_lseek(fd, pos, OS_SEEK_SET);
  54. if (ret == -1) {
  55. os_close(fd);
  56. return ret;
  57. }
  58. size = os_write(fd, buffer, towrite);
  59. os_close(fd);
  60. return size;
  61. }
  62. int sandbox_fs_ls(const char *dirname)
  63. {
  64. struct os_dirent_node *head, *node;
  65. int ret;
  66. ret = os_dirent_ls(dirname, &head);
  67. if (ret)
  68. return ret;
  69. for (node = head; node; node = node->next) {
  70. printf("%s %10lu %s\n", os_dirent_get_typename(node->type),
  71. node->size, node->name);
  72. }
  73. return 0;
  74. }
  75. void sandbox_fs_close(void)
  76. {
  77. }
  78. int fs_read_sandbox(const char *filename, void *buf, int offset, int len)
  79. {
  80. int len_read;
  81. len_read = sandbox_fs_read_at(filename, offset, buf, len);
  82. if (len_read == -1) {
  83. printf("** Unable to read file %s **\n", filename);
  84. return -1;
  85. }
  86. return len_read;
  87. }
  88. int fs_write_sandbox(const char *filename, void *buf, int offset, int len)
  89. {
  90. int len_written;
  91. len_written = sandbox_fs_write_at(filename, offset, buf, len);
  92. if (len_written == -1) {
  93. printf("** Unable to write file %s **\n", filename);
  94. return -1;
  95. }
  96. return len_written;
  97. }