Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
028fdbb7
Commit
028fdbb7
authored
Jul 02, 2013
by
antirez
Browse files
getAbsolutePath() moved into utils.c
parent
8090c37f
Changes
3
Show whitespace changes
Inline
Side-by-side
src/redis.c
View file @
028fdbb7
...
...
@@ -2761,58 +2761,6 @@ void redisSetProcTitle(char *title) {
#endif
}
/* Given the filename, return the absolute path as an SDS string, or NULL
* if it fails for some reason. Note that "filename" may be an absolute path
* already, this will be detected and handled correctly.
*
* The function does not try to normalize everything, but only the obvious
* case of one or more "../" appearning at the start of "filename"
* relative path. */
sds getAbsolutePath(char *filename) {
char cwd[1024];
sds abspath;
sds relpath = sdsnew(filename);
relpath = sdstrim(relpath," \r\n\t");
if (relpath[0] == '/') return relpath; /* Path is already absolute. */
/* If path is relative, join cwd and relative path. */
if (getcwd(cwd,sizeof(cwd)) == NULL) {
sdsfree(relpath);
return NULL;
}
abspath = sdsnew(cwd);
if (sdslen(abspath) && abspath[sdslen(abspath)-1] != '/')
abspath = sdscat(abspath,"/");
/* At this point we have the current path always ending with "/", and
* the trimmed relative path. Try to normalize the obvious case of
* trailing ../ elements at the start of the path.
*
* For every "../" we find in the filename, we remove it and also remove
* the last element of the cwd, unless the current cwd is "/". */
while (sdslen(relpath) >= 3 &&
relpath[0] == '.' && relpath[1] == '.' && relpath[2] == '/')
{
relpath = sdsrange(relpath,3,-1);
if (sdslen(abspath) > 1) {
char *p = abspath + sdslen(abspath)-2;
int trimlen = 1;
while(*p != '/') {
p--;
trimlen++;
}
abspath = sdsrange(abspath,0,-(trimlen+1));
}
}
/* Finally glue the two parts together. */
abspath = sdscatsds(abspath,relpath);
sdsfree(relpath);
return abspath;
}
int main(int argc, char **argv) {
struct timeval tv;
...
...
src/util.c
View file @
028fdbb7
...
...
@@ -405,6 +405,58 @@ void getRandomHexChars(char *p, unsigned int len) {
fclose
(
fp
);
}
/* Given the filename, return the absolute path as an SDS string, or NULL
* if it fails for some reason. Note that "filename" may be an absolute path
* already, this will be detected and handled correctly.
*
* The function does not try to normalize everything, but only the obvious
* case of one or more "../" appearning at the start of "filename"
* relative path. */
sds
getAbsolutePath
(
char
*
filename
)
{
char
cwd
[
1024
];
sds
abspath
;
sds
relpath
=
sdsnew
(
filename
);
relpath
=
sdstrim
(
relpath
,
"
\r\n\t
"
);
if
(
relpath
[
0
]
==
'/'
)
return
relpath
;
/* Path is already absolute. */
/* If path is relative, join cwd and relative path. */
if
(
getcwd
(
cwd
,
sizeof
(
cwd
))
==
NULL
)
{
sdsfree
(
relpath
);
return
NULL
;
}
abspath
=
sdsnew
(
cwd
);
if
(
sdslen
(
abspath
)
&&
abspath
[
sdslen
(
abspath
)
-
1
]
!=
'/'
)
abspath
=
sdscat
(
abspath
,
"/"
);
/* At this point we have the current path always ending with "/", and
* the trimmed relative path. Try to normalize the obvious case of
* trailing ../ elements at the start of the path.
*
* For every "../" we find in the filename, we remove it and also remove
* the last element of the cwd, unless the current cwd is "/". */
while
(
sdslen
(
relpath
)
>=
3
&&
relpath
[
0
]
==
'.'
&&
relpath
[
1
]
==
'.'
&&
relpath
[
2
]
==
'/'
)
{
relpath
=
sdsrange
(
relpath
,
3
,
-
1
);
if
(
sdslen
(
abspath
)
>
1
)
{
char
*
p
=
abspath
+
sdslen
(
abspath
)
-
2
;
int
trimlen
=
1
;
while
(
*
p
!=
'/'
)
{
p
--
;
trimlen
++
;
}
abspath
=
sdsrange
(
abspath
,
0
,
-
(
trimlen
+
1
));
}
}
/* Finally glue the two parts together. */
abspath
=
sdscatsds
(
abspath
,
relpath
);
sdsfree
(
relpath
);
return
abspath
;
}
#ifdef UTIL_TEST_MAIN
#include <assert.h>
...
...
src/util.h
View file @
028fdbb7
...
...
@@ -30,6 +30,8 @@
#ifndef __REDIS_UTIL_H
#define __REDIS_UTIL_H
#include "sds.h"
int
stringmatchlen
(
const
char
*
p
,
int
plen
,
const
char
*
s
,
int
slen
,
int
nocase
);
int
stringmatch
(
const
char
*
p
,
const
char
*
s
,
int
nocase
);
long
long
memtoll
(
const
char
*
p
,
int
*
err
);
...
...
@@ -37,5 +39,6 @@ int ll2string(char *s, size_t len, long long value);
int
string2ll
(
const
char
*
s
,
size_t
slen
,
long
long
*
value
);
int
string2l
(
const
char
*
s
,
size_t
slen
,
long
*
value
);
int
d2string
(
char
*
buf
,
size_t
len
,
double
value
);
sds
getAbsolutePath
(
char
*
filename
);
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment