Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
jami-daemon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
jami-daemon
Commits
f023e270
Commit
f023e270
authored
10 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
fileutils: add loadFile, saveFile
Change-Id: Ia65ebf8b9b593a8b56dde8db7b9c14e86d8903ac
parent
aef76520
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
daemon/src/dht/dhtaccount.cpp
+8
-47
8 additions, 47 deletions
daemon/src/dht/dhtaccount.cpp
daemon/src/fileutils.cpp
+31
-0
31 additions, 0 deletions
daemon/src/fileutils.cpp
daemon/src/fileutils.h
+3
-0
3 additions, 0 deletions
daemon/src/fileutils.h
with
42 additions
and
47 deletions
daemon/src/dht/dhtaccount.cpp
+
8
−
47
View file @
f023e270
...
...
@@ -345,35 +345,11 @@ DHTAccount::checkIdentityPath()
dht
::
crypto
::
Identity
DHTAccount
::
loadIdentity
()
const
{
std
::
vector
<
char
>
buffer
;
std
::
vector
<
char
>
buffer_crt
;
std
::
vector
<
uint8_t
>
buffer
;
std
::
vector
<
uint8_t
>
buffer_crt
;
try
{
{
std
::
ifstream
file
(
privkeyPath_
,
std
::
ios
::
binary
);
if
(
!
file
)
throw
std
::
runtime_error
(
"Can't read private key file."
);
file
.
seekg
(
0
,
std
::
ios
::
end
);
std
::
streamsize
size
=
file
.
tellg
();
if
(
size
>
std
::
numeric_limits
<
unsigned
>::
max
())
throw
std
::
runtime_error
(
"Can't read private key file."
);
buffer
.
resize
(
size
);
file
.
seekg
(
0
,
std
::
ios
::
beg
);
if
(
!
file
.
read
(
buffer
.
data
(),
size
))
throw
std
::
runtime_error
(
"Can't load private key."
);
}
{
std
::
ifstream
file
(
certPath_
,
std
::
ios
::
binary
);
if
(
!
file
)
throw
std
::
runtime_error
(
"Can't read certificate file."
);
file
.
seekg
(
0
,
std
::
ios
::
end
);
std
::
streamsize
size
=
file
.
tellg
();
if
(
size
>
std
::
numeric_limits
<
unsigned
>::
max
())
throw
std
::
runtime_error
(
"Can't read certificate file."
);
buffer_crt
.
resize
(
size
);
file
.
seekg
(
0
,
std
::
ios
::
beg
);
if
(
!
file
.
read
(
buffer_crt
.
data
(),
size
))
throw
std
::
runtime_error
(
"Can't load certificate."
);
}
buffer
=
fileutils
::
loadFile
(
privkeyPath_
);
buffer_crt
=
fileutils
::
loadFile
(
certPath_
);
}
catch
(
const
std
::
exception
&
e
)
{
SFL_ERR
(
"Error loading identity: %s"
,
e
.
what
());
...
...
@@ -421,25 +397,10 @@ DHTAccount::loadIdentity() const
void
DHTAccount
::
saveIdentity
(
const
dht
::
crypto
::
Identity
id
)
const
{
if
(
id
.
first
)
{
auto
buffer
=
id
.
first
->
serialize
();
std
::
ofstream
file
(
privkeyPath_
,
std
::
ios
::
trunc
|
std
::
ios
::
binary
);
if
(
!
file
.
is_open
())
{
SFL_ERR
(
"Could not write key to %s"
,
privkeyPath_
.
c_str
());
return
;
}
file
.
write
((
char
*
)
buffer
.
data
(),
buffer
.
size
());
}
if
(
id
.
second
)
{
auto
buffer
=
id
.
second
->
getPacked
();
std
::
ofstream
file
(
certPath_
,
std
::
ios
::
trunc
|
std
::
ios
::
binary
);
if
(
!
file
.
is_open
())
{
SFL_ERR
(
"Could not write key to %s"
,
certPath_
.
c_str
());
return
;
}
file
.
write
((
char
*
)
buffer
.
data
(),
buffer
.
size
());
}
if
(
id
.
first
)
fileutils
::
saveFile
(
privkeyPath_
,
id
.
first
->
serialize
());
if
(
id
.
second
)
fileutils
::
saveFile
(
certPath_
,
id
.
second
->
getPacked
());
}
template
<
typename
T
>
...
...
This diff is collapsed.
Click to expand it.
daemon/src/fileutils.cpp
+
31
−
0
View file @
f023e270
...
...
@@ -54,6 +54,8 @@
#include
<sstream>
#include
<fstream>
#include
<iostream>
#include
<stdexcept>
#include
<limits>
#include
<cstdlib>
#include
<cstring>
...
...
@@ -211,6 +213,35 @@ bool isDirectoryWritable(const std::string &directory)
return
access
(
directory
.
c_str
(),
W_OK
)
==
0
;
}
std
::
vector
<
uint8_t
>
loadFile
(
const
std
::
string
&
path
)
{
std
::
vector
<
uint8_t
>
buffer
;
std
::
ifstream
file
(
path
,
std
::
ios
::
binary
);
if
(
!
file
)
throw
std
::
runtime_error
(
"Can't read file: "
+
path
);
file
.
seekg
(
0
,
std
::
ios
::
end
);
std
::
streamsize
size
=
file
.
tellg
();
if
(
size
>
std
::
numeric_limits
<
unsigned
>::
max
())
throw
std
::
runtime_error
(
"File is too big: "
+
path
);
buffer
.
resize
(
size
);
file
.
seekg
(
0
,
std
::
ios
::
beg
);
if
(
!
file
.
read
((
char
*
)
buffer
.
data
(),
size
))
throw
std
::
runtime_error
(
"Can't load file: "
+
path
);
return
buffer
;
}
void
saveFile
(
const
std
::
string
&
path
,
const
std
::
vector
<
uint8_t
>&
data
)
{
std
::
ofstream
file
(
path
,
std
::
ios
::
trunc
|
std
::
ios
::
binary
);
if
(
!
file
.
is_open
())
{
SFL_ERR
(
"Could not write data to %s"
,
path
.
c_str
());
return
;
}
file
.
write
((
char
*
)
data
.
data
(),
data
.
size
());
}
static
size_t
dirent_buf_size
(
DIR
*
dirp
)
{
...
...
This diff is collapsed.
Click to expand it.
daemon/src/fileutils.h
+
3
−
0
View file @
f023e270
...
...
@@ -65,6 +65,9 @@ namespace fileutils {
*/
std
::
vector
<
std
::
string
>
readDirectory
(
const
std
::
string
&
dir
);
std
::
vector
<
uint8_t
>
loadFile
(
const
std
::
string
&
path
);
void
saveFile
(
const
std
::
string
&
path
,
const
std
::
vector
<
uint8_t
>&
data
);
struct
FileHandle
{
int
fd
;
const
std
::
string
name
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment