|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU Lesser General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU Lesser General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU Lesser General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 |
|
19 #include <fcntl.h> |
|
20 #include <sys/types.h> |
|
21 #include <unistd.h> |
|
22 #include <cassert> |
|
23 |
|
24 #include "elfromerror.h" |
|
25 #include "inputfile.h" |
|
26 |
|
27 InputFile & InputFile::operator=(const InputFile & aInputFile) { |
|
28 // Don't copy fd. Force a new to be got. |
|
29 iOpened = false; |
|
30 // Force size to be re-computed |
|
31 iSizeValid = false; |
|
32 iOffset = aInputFile.iOffset; |
|
33 iPathName = aInputFile.iPathName; |
|
34 return *this; |
|
35 } |
|
36 |
|
37 InputFile::InputFile(const InputFile & aInputFile){ |
|
38 *this = aInputFile; |
|
39 } |
|
40 |
|
41 InputFile::~InputFile(){ |
|
42 Close(); |
|
43 } |
|
44 |
|
45 void InputFile::Open(){ |
|
46 if (iOpened) return; |
|
47 if ((iFd = open(iPathName.c_str(), O_RDONLY|O_BINARY, 0)) < 0) |
|
48 errx(EX_NOINPUT, "open \"%s\" failed\n", iPathName.c_str()); |
|
49 else |
|
50 iOpened = true; |
|
51 } |
|
52 |
|
53 void InputFile::Close(){ |
|
54 if (!iOpened) return; |
|
55 close(iFd); |
|
56 iOpened = false; |
|
57 iSizeValid = false; |
|
58 } |
|
59 |
|
60 size_t InputFile::Size(){ |
|
61 if (iSizeValid) return iSize - iOffset; |
|
62 SetSize(); |
|
63 return iSize - iOffset; |
|
64 } |
|
65 |
|
66 void InputFile::SetSize(){ |
|
67 Open(); |
|
68 if ((iSize = lseek(iFd, (size_t)0, SEEK_END)) == (size_t)-1) |
|
69 errx(EX_NOINPUT, "failed to get size of \"%s\"", iPathName.c_str()); |
|
70 if (lseek(iFd, (size_t)0, SEEK_SET) != 0) |
|
71 errx(EX_NOINPUT, "failed to get size of \"%s\"", iPathName.c_str()); |
|
72 Close(); |
|
73 iSizeValid = true; |
|
74 } |
|
75 |
|
76 char * InputFile::GetData(){ |
|
77 size_t nbytes = Size(); |
|
78 return GetData(nbytes); |
|
79 } |
|
80 |
|
81 char * InputFile::GetData(size_t nbytes) { |
|
82 char * data = new char[nbytes]; |
|
83 GetData(data, nbytes); |
|
84 return data; |
|
85 } |
|
86 |
|
87 void InputFile::GetData(void * data, size_t nbytes){ |
|
88 char * d = (char *)data; |
|
89 size_t done = 0; |
|
90 ssize_t n; |
|
91 |
|
92 Open(); |
|
93 |
|
94 if (iOffset > 0) |
|
95 if ((size_t)lseek(iFd, iOffset, SEEK_SET) != iOffset) { |
|
96 Close(); |
|
97 errx(EX_NOINPUT, "failed to read from \"%s\"", iPathName.c_str()); |
|
98 } |
|
99 |
|
100 while (nbytes){ |
|
101 n = read(iFd, d+done,nbytes); |
|
102 // n must be greater than 0 |
|
103 if (n <= 0){ |
|
104 Close(); |
|
105 errx(EX_NOINPUT, "failed to read from \"%s\"", iPathName.c_str()); |
|
106 } |
|
107 done += n; |
|
108 nbytes -= n; |
|
109 } |
|
110 |
|
111 Close(); |
|
112 } |