diff options
author | Chris Sherlock <chris.sherlock79@gmail.com> | 2017-05-14 17:35:56 +1000 |
---|---|---|
committer | Chris Sherlock <chris.sherlock79@gmail.com> | 2017-05-14 17:39:51 +1000 |
commit | e7982510d23c4b6047f0b81bfe1c684ecb1fff8a (patch) | |
tree | c1d58e9529c325aa60ff9aa0b7b714dafe1e6b08 /sal | |
parent | 100895cf07a4c01f3b784c6eab344b7667b145e1 (diff) |
osl: cleanup file_url.cxx
Change-Id: I636b0a7765be8e6e96e90ff5a004dc452048541a
Diffstat (limited to 'sal')
-rw-r--r-- | sal/osl/unx/file_url.cxx | 202 |
1 files changed, 94 insertions, 108 deletions
diff --git a/sal/osl/unx/file_url.cxx b/sal/osl/unx/file_url.cxx index 69ba4349b968..8b4eb69c752a 100644 --- a/sal/osl/unx/file_url.cxx +++ b/sal/osl/unx/file_url.cxx @@ -47,7 +47,7 @@ #include "uunxapi.hxx" -/*************************************************** +/** @file General note @@ -60,7 +60,7 @@ way. In rtl/uri there is already an URI parser etc. so this code should be consolidated. - **************************************************/ +*/ using namespace osl; @@ -134,16 +134,15 @@ oslFileError getSystemPathFromFileUrl( } } // Handle query or fragment: - if (url.indexOf('?', i) != -1 || url.indexOf('#', i) != -1) { + if (url.indexOf('?', i) != -1 || url.indexOf('#', i) != -1) return osl_File_E_INVAL; - } // Handle authority: - if (url.getLength() - i >= 2 && url[i] == '/' && url[i + 1] == '/') { + if (url.getLength() - i >= 2 && url[i] == '/' && url[i + 1] == '/') + { i += 2; sal_Int32 j = url.indexOf('/', i); - if (j == -1) { + if (j == -1) j = url.getLength(); - } if (j != i && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths( url.pData->buffer + i, j - i, @@ -159,45 +158,53 @@ oslFileError getSystemPathFromFileUrl( i = j; } // Handle empty path: - if (i == url.getLength()) { + if (i == url.getLength()) + { *path = "/"; return osl_File_E_None; } // Path must not contain %2F: - if (url.indexOf("%2F", i) != -1 || url.indexOf("%2f", i) != -1) { + if (url.indexOf("%2F", i) != -1 || url.indexOf("%2f", i) != -1) return osl_File_E_INVAL; - } + *path = rtl::Uri::decode( url.copy(i), rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8); // Path must not contain %2F: - if (path->indexOf('\0') != -1) { + if (path->indexOf('\0') != -1) return osl_File_E_INVAL; - } + // Handle ~ notation: - if (resolveHome && path->getLength() >= 2 && (*path)[1] == '~') { + if (resolveHome && path->getLength() >= 2 && (*path)[1] == '~') + { sal_Int32 j = path->indexOf('/', 2); - if (j == -1) { + if (j == -1) j = path->getLength(); - } - if (j == 2) { + + if (j == 2) + { OUString home; - if (!osl::Security().getHomeDir(home)) { + if (!osl::Security().getHomeDir(home)) + { SAL_WARN("sal.file", "osl::Security::getHomeDir failed"); return osl_File_E_INVAL; } + i = url.indexOf('/', i + 1); - if (i == -1) { + + if (i == -1) i = url.getLength(); - } else { + else ++i; - } + //TODO: cheesy way of ensuring home's path ends in slash: - if (!home.isEmpty() && home[home.getLength() - 1] != '/') { + if (!home.isEmpty() && home[home.getLength() - 1] != '/') home += "/"; - } - try { + try + { home = rtl::Uri::convertRelToAbs(home, url.copy(i)); - } catch (rtl::MalformedUriException & e) { + } + catch (rtl::MalformedUriException & e) + { SAL_WARN("sal.file", "rtl::MalformedUriException " << e.getMessage()); return osl_File_E_INVAL; } @@ -215,15 +222,19 @@ oslFileError SAL_CALL osl_getSystemPathFromFileURL( rtl_uString *ustrFileURL, rt { OUString path; oslFileError e; - try { + try + { e = getSystemPathFromFileUrl( OUString::unacquired(&ustrFileURL), &path, true); - } catch (std::length_error) { + } + catch (std::length_error) + { e = osl_File_E_RANGE; } - if (e == osl_File_E_None) { + + if (e == osl_File_E_None) rtl_uString_assign(pustrSystemPath, path.pData); - } + return e; } @@ -240,27 +251,7 @@ oslFileError SAL_CALL osl_getFileURLFromSystemPath( rtl_uString *ustrSystemPath, /* temporary hack: if already file url, return ustrSystemPath */ if( rtl_ustr_ascii_shortenedCompare_WithLength( ustrSystemPath->buffer, ustrSystemPath->length,"file:", 5 ) == 0 ) - { - /* - if( 0 == rtl_ustr_ascii_shortenedCompare_WithLength( ustrSystemPath->buffer, ustrSystemPath->length,"file://", 7 ) ) - { - OSL_FAIL( "osl_getFileURLFromSystemPath: input is already file URL" ); - rtl_uString_assign( pustrFileURL, ustrSystemPath ); - } - else - { - rtl_uString *pTmp2 = NULL; - - OSL_FAIL( "osl_getFileURLFromSystemPath: input is wrong file URL" ); - rtl_uString_newFromStr_WithLength( pustrFileURL, ustrSystemPath->buffer + 5, ustrSystemPath->length - 5 ); - rtl_uString_newFromAscii( &pTmp2, "file://" ); - rtl_uString_newConcat( pustrFileURL, *pustrFileURL, pTmp2 ); - rtl_uString_release( pTmp2 ); - } - return osl_File_E_None; - */ return osl_File_E_INVAL; - } /* check if system path starts with ~ or ~user and replace it with the appropriate home dir */ if( ustrSystemPath->buffer[0] == '~' ) @@ -366,10 +357,9 @@ oslFileError osl_getSystemPathFromFileURL_Ex( namespace { - /****************************************************** - * Helper function, return a pinter to the final '\0' - * of a string - ******************************************************/ + /** Helper function, return a pinter to the final '\0' + of a string + */ sal_Unicode* ustrtoend(sal_Unicode* pStr) { @@ -392,24 +382,24 @@ namespace return (*p == Chr); } - /****************************************************** - * Remove the last part of a path, a path that has - * only a '/' or no '/' at all will be returned - * unmodified - ******************************************************/ + /** + Remove the last part of a path, a path that has + only a '/' or no '/' at all will be returned + unmodified + */ sal_Unicode* _rmlastpathtoken(sal_Unicode* aPath) { - /* we always may skip -2 because we - may at least stand on a '/' but - either there is no other character - before this '/' or it's another - character than the '/' + /* we may always skip -2 because we + may at least stand on a '/' but + either there is no other character + before this '/' or it's another + character than the '/' */ sal_Unicode* p = ustrtoend(aPath) - 2; - // move back to the next path separator - // or to the start of the string + /* move back to the next path separator + or to the start of the string */ while ((p > aPath) && (*p != '/')) p--; @@ -422,7 +412,7 @@ namespace } else { - *p = '\0'; + *p = '\0'; } } @@ -446,7 +436,6 @@ namespace { if (!TextToUnicode(resolved_path, strlen(resolved_path), path, PATH_MAX)) return oslTranslateFileError(OSL_FET_ERROR, ENAMETOOLONG); - } else { @@ -460,15 +449,14 @@ namespace return ferr; } - /****************************************************** - * Works even with non existing paths. The resulting - * path must not exceed PATH_MAX else - * osl_File_E_NAMETOOLONG is the result - ******************************************************/ + /** + Works even with non existing paths. The resulting path must not exceed + PATH_MAX else osl_File_E_NAMETOOLONG is the result + */ oslFileError osl_getAbsoluteFileURL_impl_(const rtl::OUString& unresolved_path, rtl::OUString& resolved_path) { - // the given unresolved path must not exceed PATH_MAX + /* the given unresolved path must not exceed PATH_MAX */ if (unresolved_path.getLength() >= (PATH_MAX - 2)) return oslTranslateFileError(OSL_FET_ERROR, ENAMETOOLONG); @@ -476,13 +464,13 @@ namespace const sal_Unicode* punresolved = unresolved_path.getStr(); sal_Unicode* presolvedsf = path_resolved_so_far; - // reserve space for leading '/' and trailing '\0' - // do not exceed this limit + /* reserve space for leading '/' and trailing '\0' + do not exceed this limit */ sal_Unicode* sentinel = path_resolved_so_far + PATH_MAX - 2; - // if realpath fails with error ENOTDIR, EACCES or ENOENT - // we will not call it again, because _osl_realpath should also - // work with non existing directories etc. + /* if realpath fails with error ENOTDIR, EACCES or ENOENT + we will not call it again, because _osl_realpath should also + work with non existing directories etc. */ bool realpath_failed = false; oslFileError ferr; @@ -490,8 +478,7 @@ namespace while (*punresolved != '\0') { - // ignore '/.' , skip one part back when '/..' - + /* ignore '/.' , skip one part back when '/..' */ if ((*punresolved == '.') && (*presolvedsf == '/')) { if (*(punresolved + 1) == '\0') @@ -517,8 +504,8 @@ namespace continue; } - // a file or directory name may start with '.' + /* a file or directory name may start with '.' */ if ((presolvedsf = ustrtoend(path_resolved_so_far)) > sentinel) return oslTranslateFileError(OSL_FET_ERROR, ENAMETOOLONG); @@ -589,24 +576,26 @@ namespace } -oslFileError osl_getAbsoluteFileURL(rtl_uString* ustrBaseDirURL, rtl_uString* ustrRelativeURL, rtl_uString** pustrAbsoluteURL) +oslFileError osl_getAbsoluteFileURL( + rtl_uString* ustrBaseDirURL, + rtl_uString* ustrRelativeURL, + rtl_uString** pustrAbsoluteURL) { - // Work around the below call to getSystemPathFromFileURL rejecting input - // that starts with "/" (for whatever reason it behaves that way; but - // changing that would start to break lots of tests at least): + /* Work around the below call to getSystemPathFromFileURL rejecting input + that starts with "/" (for whatever reason it behaves that way; but + changing that would start to break lots of tests at least) */ rtl::OUString relUrl(ustrRelativeURL); - if (relUrl.startsWith("//")) { + if (relUrl.startsWith("//")) relUrl = "file:" + relUrl; - } else if (relUrl.startsWith("/")) { + else if (relUrl.startsWith("/")) relUrl = "file://" + relUrl; - } FileBase::RC rc; rtl::OUString unresolved_path; rc = FileBase::getSystemPathFromFileURL(relUrl, unresolved_path); - if(rc != FileBase::E_None) + if (rc != FileBase::E_None) return oslFileError(rc); if (systemPathIsRelativePath(unresolved_path)) @@ -634,17 +623,16 @@ oslFileError osl_getAbsoluteFileURL(rtl_uString* ustrBaseDirURL, rtl_uString* u return oslFileError(rc); } -namespace osl { namespace detail { - /********************************************* - No separate error code if unicode to text - conversion or getenv fails because for the - caller there is no difference why a file - could not be found in $PATH - ********************************************/ +namespace osl { +namespace detail { + /** + No separate error code if unicode to text conversion or getenv fails because for the + caller there is no difference why a file could not be found in $PATH + */ bool find_in_PATH(const rtl::OUString& file_path, rtl::OUString& result) { - bool bfound = false; + bool bfound = false; rtl::OUString path("PATH"); rtl::OUString env_path; @@ -653,17 +641,15 @@ namespace osl { namespace detail { return bfound; } -} } +} +} namespace { - /********************************************* - No separate error code if unicode to text - conversion or getcwd fails because for the - caller there is no difference why a file - could not be found in CDW - ********************************************/ - + /** + No separate error code if unicode to text conversion or getcwd fails because for the + caller there is no difference why a file could not be found in CDW + */ bool find_in_CWD(const rtl::OUString& file_path, rtl::OUString& result) { bool bfound = false; @@ -824,15 +810,15 @@ namespace int TextToUnicode( const char* text, - size_t text_buffer_size, + size_t text_buffer_size, sal_Unicode* unic_text, - sal_Int32 unic_text_buffer_size) + sal_Int32 unic_text_buffer_size) { sal_uInt32 nInfo = 0; - sal_Size nSrcChars = 0; + sal_Size nSrcChars = 0; sal_Size nDestBytes = TextToUnicodeConverter_Impl::getInstance().convert( - text, text_buffer_size, unic_text, unic_text_buffer_size, + text, text_buffer_size, unic_text, unic_text_buffer_size, OSTRING_TO_OUSTRING_CVTFLAGS | RTL_TEXTTOUNICODE_FLAGS_FLUSH, &nInfo, &nSrcChars); if (nInfo & RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL) |