Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AndroidSystemCore
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Werner Sembach
AndroidSystemCore
Commits
6a29fe93
Commit
6a29fe93
authored
9 years ago
by
Calin Juravle
Committed by
Android (Google) Code Review
9 years ago
Browse files
Options
Downloads
Plain Diff
Merge "Add utility to prepare files in a similar way to directories" into nyc-dev
parents
fe01f56c
9812105b
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/cutils/fs.h
+7
-0
7 additions, 0 deletions
include/cutils/fs.h
libcutils/fs.c
+24
-9
24 additions, 9 deletions
libcutils/fs.c
with
31 additions
and
9 deletions
include/cutils/fs.h
+
7
−
0
View file @
6a29fe93
...
@@ -51,6 +51,13 @@ extern int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid);
...
@@ -51,6 +51,13 @@ extern int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid);
*/
*/
extern
int
fs_prepare_dir_strict
(
const
char
*
path
,
mode_t
mode
,
uid_t
uid
,
gid_t
gid
);
extern
int
fs_prepare_dir_strict
(
const
char
*
path
,
mode_t
mode
,
uid_t
uid
,
gid_t
gid
);
/*
* Ensure that file exists with given mode and owners. If it exists
* with different owners, they are not fixed and -1 is returned.
*/
extern
int
fs_prepare_file_strict
(
const
char
*
path
,
mode_t
mode
,
uid_t
uid
,
gid_t
gid
);
/*
/*
* Read single plaintext integer from given file, correctly handling files
* Read single plaintext integer from given file, correctly handling files
* partially written with fs_write_atomic_int().
* partially written with fs_write_atomic_int().
...
...
This diff is collapsed.
Click to expand it.
libcutils/fs.c
+
24
−
9
View file @
6a29fe93
...
@@ -37,10 +37,11 @@
...
@@ -37,10 +37,11 @@
#define ALL_PERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
#define ALL_PERMS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
#define BUF_SIZE 64
#define BUF_SIZE 64
static
int
fs_prepare_
dir
_impl
(
const
char
*
path
,
mode_t
mode
,
uid_t
uid
,
gid_t
gid
,
static
int
fs_prepare_
path
_impl
(
const
char
*
path
,
mode_t
mode
,
uid_t
uid
,
gid_t
gid
,
int
allow_fixup
)
{
int
allow_fixup
,
int
prepare_as_dir
)
{
// Check if path needs to be created
// Check if path needs to be created
struct
stat
sb
;
struct
stat
sb
;
int
create_result
=
-
1
;
if
(
TEMP_FAILURE_RETRY
(
lstat
(
path
,
&
sb
))
==
-
1
)
{
if
(
TEMP_FAILURE_RETRY
(
lstat
(
path
,
&
sb
))
==
-
1
)
{
if
(
errno
==
ENOENT
)
{
if
(
errno
==
ENOENT
)
{
goto
create
;
goto
create
;
...
@@ -51,10 +52,12 @@ static int fs_prepare_dir_impl(const char* path, mode_t mode, uid_t uid, gid_t g
...
@@ -51,10 +52,12 @@ static int fs_prepare_dir_impl(const char* path, mode_t mode, uid_t uid, gid_t g
}
}
// Exists, verify status
// Exists, verify status
if
(
!
S_ISDIR
(
sb
.
st_mode
))
{
int
type_ok
=
prepare_as_dir
?
S_ISDIR
(
sb
.
st_mode
)
:
S_ISREG
(
sb
.
st_mode
);
ALOGE
(
"Not a directory: %s"
,
path
);
if
(
!
type_ok
)
{
ALOGE
(
"Not a %s: %s"
,
(
prepare_as_dir
?
"directory"
:
"regular file"
),
path
);
return
-
1
;
return
-
1
;
}
}
int
owner_match
=
((
sb
.
st_uid
==
uid
)
&&
(
sb
.
st_gid
==
gid
));
int
owner_match
=
((
sb
.
st_uid
==
uid
)
&&
(
sb
.
st_gid
==
gid
));
int
mode_match
=
((
sb
.
st_mode
&
ALL_PERMS
)
==
mode
);
int
mode_match
=
((
sb
.
st_mode
&
ALL_PERMS
)
==
mode
);
if
(
owner_match
&&
mode_match
)
{
if
(
owner_match
&&
mode_match
)
{
...
@@ -74,13 +77,21 @@ static int fs_prepare_dir_impl(const char* path, mode_t mode, uid_t uid, gid_t g
...
@@ -74,13 +77,21 @@ static int fs_prepare_dir_impl(const char* path, mode_t mode, uid_t uid, gid_t g
}
}
create:
create:
if
(
TEMP_FAILURE_RETRY
(
mkdir
(
path
,
mode
))
==
-
1
)
{
create_result
=
prepare_as_dir
?
TEMP_FAILURE_RETRY
(
mkdir
(
path
,
mode
))
:
TEMP_FAILURE_RETRY
(
open
(
path
,
O_CREAT
|
O_CLOEXEC
|
O_NOFOLLOW
|
O_RDONLY
));
if
(
create_result
==
-
1
)
{
if
(
errno
!=
EEXIST
)
{
if
(
errno
!=
EEXIST
)
{
ALOGE
(
"Failed to mkdir(%s): %s"
,
path
,
strerror
(
errno
));
ALOGE
(
"Failed to %s(%s): %s"
,
(
prepare_as_dir
?
"mkdir"
:
"open"
),
path
,
strerror
(
errno
));
return
-
1
;
return
-
1
;
}
}
}
else
if
(
!
prepare_as_dir
)
{
// For regular files we need to make sure we close the descriptor
if
(
close
(
create_result
)
==
-
1
)
{
ALOGW
(
"Failed to close file after create %s: %s"
,
path
,
strerror
(
errno
));
}
}
}
fixup:
fixup:
if
(
TEMP_FAILURE_RETRY
(
chmod
(
path
,
mode
))
==
-
1
)
{
if
(
TEMP_FAILURE_RETRY
(
chmod
(
path
,
mode
))
==
-
1
)
{
ALOGE
(
"Failed to chmod(%s, %d): %s"
,
path
,
mode
,
strerror
(
errno
));
ALOGE
(
"Failed to chmod(%s, %d): %s"
,
path
,
mode
,
strerror
(
errno
));
...
@@ -95,11 +106,15 @@ fixup:
...
@@ -95,11 +106,15 @@ fixup:
}
}
int
fs_prepare_dir
(
const
char
*
path
,
mode_t
mode
,
uid_t
uid
,
gid_t
gid
)
{
int
fs_prepare_dir
(
const
char
*
path
,
mode_t
mode
,
uid_t
uid
,
gid_t
gid
)
{
return
fs_prepare_
dir
_impl
(
path
,
mode
,
uid
,
gid
,
1
);
return
fs_prepare_
path
_impl
(
path
,
mode
,
uid
,
gid
,
/*allow_fixup*/
1
,
/*prepare_as_dir*/
1
);
}
}
int
fs_prepare_dir_strict
(
const
char
*
path
,
mode_t
mode
,
uid_t
uid
,
gid_t
gid
)
{
int
fs_prepare_dir_strict
(
const
char
*
path
,
mode_t
mode
,
uid_t
uid
,
gid_t
gid
)
{
return
fs_prepare_dir_impl
(
path
,
mode
,
uid
,
gid
,
0
);
return
fs_prepare_path_impl
(
path
,
mode
,
uid
,
gid
,
/*allow_fixup*/
0
,
/*prepare_as_dir*/
1
);
}
int
fs_prepare_file_strict
(
const
char
*
path
,
mode_t
mode
,
uid_t
uid
,
gid_t
gid
)
{
return
fs_prepare_path_impl
(
path
,
mode
,
uid
,
gid
,
/*allow_fixup*/
0
,
/*prepare_as_dir*/
0
);
}
}
int
fs_read_atomic_int
(
const
char
*
path
,
int
*
out_value
)
{
int
fs_read_atomic_int
(
const
char
*
path
,
int
*
out_value
)
{
...
...
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