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
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "ReadWriteUtils"
19 #include <utils/Log.h>
20
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>
29 #include <utils/String8.h>
30
31 using namespace android;
32
33 #define FAILURE -1
34
readBytes(const String8 & filePath)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) {
45 off64_t length = sb.st_size;
46 char* bytes = new char[length];
47 if (length == read(fd, (void*) bytes, length)) {
48 string.append(bytes, length);
49 }
50 delete[] bytes;
51 }
52 fclose(file);
53 }
54 return string;
55 }
56
readBytes(const String8 & filePath,char ** buffer)57 int ReadWriteUtils::readBytes(const String8& filePath, char** buffer) {
58 FILE* file = NULL;
59 file = fopen(filePath.string(), "r");
60 off64_t length = 0;
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
writeToFile(const String8 & filePath,const String8 & data)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)) {
87 if (size != write(fd, data.string(), size)) {
88 ALOGE("Failed to write the data to: %s", filePath.string());
89 }
90 }
91 fclose(file);
92 }
93 }
94
appendToFile(const String8 & filePath,const String8 & data)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
102 int size = data.size();
103 if (size != write(fd, data.string(), size)) {
104 ALOGE("Failed to write the data to: %s", filePath.string());
105 }
106 fclose(file);
107 }
108 }
109
110