Changeset 50985c34 in mainline
- Timestamp:
- 2012-06-01T00:04:30Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 11b9ad7
- Parents:
- 5c5c346a
- Location:
- uspace/app/bithenge
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bithenge/file.c
r5c5c346a r50985c34 40 40 #include <fcntl.h> 41 41 #include <macros.h> 42 #include <stdio.h> 42 43 #include <stdlib.h> 43 44 #include <sys/stat.h> … … 49 50 int fd; 50 51 aoff64_t size; // needed by file_read() 52 bool needs_close; 51 53 } file_blob_t; 52 54 … … 95 97 }; 96 98 99 static int new_file_blob(bithenge_blob_t **out, int fd, bool needs_close) 100 { 101 assert(out); 102 103 struct stat stat; 104 int rc = fstat(fd, &stat); 105 if (rc != EOK) { 106 if (needs_close) 107 close(fd); 108 return rc; 109 } 110 111 // Create blob 112 file_blob_t *blob = malloc(sizeof(*blob)); 113 if (!blob) { 114 if (needs_close) 115 close(fd); 116 return ENOMEM; 117 } 118 rc = bithenge_new_random_access_blob(blob_from_file(blob), 119 &file_ops); 120 if (rc != EOK) { 121 free(blob); 122 if (needs_close) 123 close(fd); 124 return rc; 125 } 126 blob->fd = fd; 127 blob->size = stat.size; 128 blob->needs_close = needs_close; 129 *out = blob_from_file(blob); 130 131 return EOK; 132 } 133 97 134 /** Create a blob for a file. The blob must be freed with @a 98 135 * bithenge_blob_t::bithenge_blob_destroy after it is used. … … 102 139 int bithenge_new_file_blob(bithenge_blob_t **out, const char *filename) 103 140 { 104 assert(out);105 141 assert(filename); 106 142 107 int fd; 108 fd = open(filename, O_RDONLY); 143 int fd = open(filename, O_RDONLY); 109 144 if (fd < 0) 110 145 return fd; 111 146 112 struct stat stat; 113 int rc = fstat(fd, &stat); 114 if (rc != EOK) { 115 close(fd); 116 return rc; 117 } 147 return new_file_blob(out, fd, true); 148 } 118 149 119 // Create blob 120 file_blob_t *blob = malloc(sizeof(*blob)); 121 if (!blob) { 122 close(fd); 123 return ENOMEM; 124 } 125 rc = bithenge_new_random_access_blob(blob_from_file(blob), 126 &file_ops); 127 if (rc != EOK) { 128 free(blob); 129 close(fd); 130 return rc; 131 } 132 blob->fd = fd; 133 blob->size = stat.size; 134 *out = blob_from_file(blob); 150 /** Create a blob for a file descriptor. The blob must be freed with @a 151 * bithenge_blob_t::bithenge_blob_destroy after it is used. 152 * @param[out] out Stores the created blob. 153 * @param fd The file descriptor. 154 * @return EOK on success or an error code from errno.h. */ 155 int bithenge_new_file_blob_from_fd(bithenge_blob_t **out, int fd) 156 { 157 return new_file_blob(out, fd, false); 158 } 135 159 136 return EOK; 160 /** Create a blob for a file pointer. The blob must be freed with @a 161 * bithenge_blob_t::bithenge_blob_destroy after it is used. 162 * @param[out] out Stores the created blob. 163 * @param file The file pointer. 164 * @return EOK on success or an error code from errno.h. */ 165 int bithenge_new_file_blob_from_file(bithenge_blob_t **out, FILE *file) 166 { 167 int fd = fileno(file); 168 if (fd < 0) 169 return errno; 170 return new_file_blob(out, fd, false); 137 171 } 138 172 -
uspace/app/bithenge/file.h
r5c5c346a r50985c34 38 38 #define BITHENGE_FILE_H_ 39 39 40 #include <stdio.h> 40 41 #include "blob.h" 41 42 42 int bithenge_new_file_blob(bithenge_blob_t **, const char *filename); 43 int bithenge_new_file_blob(bithenge_blob_t **, const char *); 44 int bithenge_new_file_blob_from_fd(bithenge_blob_t **, int); 45 int bithenge_new_file_blob_from_file(bithenge_blob_t **, FILE *); 43 46 44 47 #endif -
uspace/app/bithenge/test.c
r5c5c346a r50985c34 89 89 bithenge_blob_destroy(blob); 90 90 91 bithenge_new_file_blob_from_fd(&blob, 0); 92 printf("Data from fd:0: "); 93 print_blob(blob); 94 bithenge_blob_destroy(blob); 95 91 96 return 0; 92 97 }
Note:
See TracChangeset
for help on using the changeset viewer.