aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "ReadWriteUtils" |
| 19 | #include <utils/Log.h> |
| 20 | |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 21 | #include <ReadWriteUtils.h> |
| 22 | #include <sys/mman.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <unistd.h> |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 29 | #include <utils/String8.h> |
| 30 | |
| 31 | using namespace android; |
| 32 | |
Chih-Hung Hsieh | 92c6b82 | 2016-05-17 15:20:14 -0700 | [diff] [blame] | 33 | #define FAILURE (-1) |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 34 | |
| 35 | String8 ReadWriteUtils::readBytes(const String8& filePath) { |
| 36 | FILE* file = NULL; |
| 37 | file = fopen(filePath.string(), "r"); |
| 38 | |
| 39 | String8 string(""); |
| 40 | if (NULL != file) { |
| 41 | int fd = fileno(file); |
| 42 | struct stat sb; |
| 43 | |
| 44 | if (fstat(fd, &sb) == 0 && sb.st_size > 0) { |
Gloria Wang | a2cd44c | 2010-11-19 15:19:36 -0800 | [diff] [blame] | 45 | off64_t length = sb.st_size; |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 46 | char* bytes = new char[length]; |
| 47 | if (length == read(fd, (void*) bytes, length)) { |
| 48 | string.append(bytes, length); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 49 | } |
Hung Nguyen | 0bf4384 | 2012-06-05 13:19:53 +0200 | [diff] [blame] | 50 | delete[] bytes; |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 51 | } |
| 52 | fclose(file); |
| 53 | } |
| 54 | return string; |
| 55 | } |
| 56 | |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 57 | int ReadWriteUtils::readBytes(const String8& filePath, char** buffer) { |
| 58 | FILE* file = NULL; |
| 59 | file = fopen(filePath.string(), "r"); |
Gloria Wang | a2cd44c | 2010-11-19 15:19:36 -0800 | [diff] [blame] | 60 | off64_t length = 0; |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 61 | |
| 62 | if (NULL != file) { |
| 63 | int fd = fileno(file); |
| 64 | struct stat sb; |
| 65 | |
| 66 | if (fstat(fd, &sb) == 0 && sb.st_size > 0) { |
| 67 | length = sb.st_size; |
| 68 | *buffer = new char[length]; |
| 69 | if (length != read(fd, (void*) *buffer, length)) { |
| 70 | length = FAILURE; |
| 71 | } |
| 72 | } |
| 73 | fclose(file); |
| 74 | } |
| 75 | return length; |
| 76 | } |
| 77 | |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 78 | void ReadWriteUtils::writeToFile(const String8& filePath, const String8& data) { |
| 79 | FILE* file = NULL; |
| 80 | file = fopen(filePath.string(), "w+"); |
| 81 | |
| 82 | if (NULL != file) { |
| 83 | int fd = fileno(file); |
| 84 | |
| 85 | int size = data.size(); |
| 86 | if (FAILURE != ftruncate(fd, size)) { |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 87 | if (size != write(fd, data.string(), size)) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 88 | ALOGE("Failed to write the data to: %s", filePath.string()); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | fclose(file); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void ReadWriteUtils::appendToFile(const String8& filePath, const String8& data) { |
| 96 | FILE* file = NULL; |
| 97 | file = fopen(filePath.string(), "a+"); |
| 98 | |
| 99 | if (NULL != file) { |
| 100 | int fd = fileno(file); |
| 101 | |
Takeshi Aimi | 2272ee2 | 2010-09-20 23:40:41 +0900 | [diff] [blame] | 102 | int size = data.size(); |
| 103 | if (size != write(fd, data.string(), size)) { |
Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 104 | ALOGE("Failed to write the data to: %s", filePath.string()); |
aimitakeshi | 27ed8ad | 2010-07-29 10:12:27 +0900 | [diff] [blame] | 105 | } |
| 106 | fclose(file); |
| 107 | } |
| 108 | } |
| 109 | |