Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "uunxapi.h"
21 : #include "system.h"
22 : #include <limits.h>
23 : #include <rtl/ustring.hxx>
24 : #include <osl/thread.h>
25 :
26 : #ifdef ANDROID
27 : #include <osl/detail/android-bootstrap.h>
28 : #endif
29 :
30 : //###########################
31 23404 : inline rtl::OString OUStringToOString(const rtl_uString* s)
32 : {
33 : return rtl::OUStringToOString(
34 : rtl::OUString(const_cast<rtl_uString*>(s)),
35 23404 : osl_getThreadTextEncoding());
36 : }
37 :
38 : //###########################
39 : #ifdef MACOSX
40 : /*
41 : * Helper function for resolving Mac native alias files (not the same as unix alias files)
42 : * and to return the resolved alias as rtl::OString
43 : */
44 : inline rtl::OString macxp_resolveAliasAndConvert(const rtl_uString* s)
45 : {
46 : rtl::OString p = OUStringToOString(s);
47 : sal_Char path[PATH_MAX];
48 : if (p.getLength() < PATH_MAX)
49 : {
50 : strcpy(path, p.getStr());
51 : macxp_resolveAlias(path, PATH_MAX);
52 : p = rtl::OString(path);
53 : }
54 : return p;
55 : }
56 : #endif /* MACOSX */
57 :
58 : //###########################
59 : //access_u
60 18058 : int access_u(const rtl_uString* pustrPath, int mode)
61 : {
62 : #ifndef MACOSX // not MACOSX
63 18058 : rtl::OString fn = OUStringToOString(pustrPath);
64 : #ifdef ANDROID
65 : if (strncmp(fn.getStr(), "/assets", sizeof("/assets")-1) == 0 &&
66 : (fn.getStr()[sizeof("/assets")-1] == '\0' ||
67 : fn.getStr()[sizeof("/assets")-1] == '/'))
68 : {
69 : struct stat stat;
70 : if (lo_apk_lstat(fn.getStr(), &stat) == -1)
71 : return -1;
72 : if (mode & W_OK)
73 : {
74 : errno = EACCES;
75 : return -1;
76 : }
77 : return 0;
78 : }
79 : #endif
80 18058 : return access(fn.getStr(), mode);
81 : #else
82 : return access(macxp_resolveAliasAndConvert(pustrPath).getStr(), mode);
83 : #endif
84 : }
85 :
86 : //#########################
87 : //realpath_u
88 1216 : sal_Bool realpath_u(const rtl_uString* pustrFileName, rtl_uString** ppustrResolvedName)
89 : {
90 : #ifndef MACOSX // not MACOSX
91 1216 : rtl::OString fn = OUStringToOString(pustrFileName);
92 : #ifdef ANDROID
93 : if (strncmp(fn.getStr(), "/assets", sizeof("/assets")-1) == 0 &&
94 : (fn.getStr()[sizeof("/assets")-1] == '\0' ||
95 : fn.getStr()[sizeof("/assets")-1] == '/'))
96 : {
97 : if (access_u(pustrFileName, F_OK) == -1)
98 : return sal_False;
99 :
100 : rtl_uString silly(*pustrFileName);
101 : rtl_uString_assign(ppustrResolvedName, &silly);
102 :
103 : return sal_True;
104 : }
105 : #endif
106 : #else
107 : rtl::OString fn = macxp_resolveAliasAndConvert(pustrFileName);
108 : #endif
109 : char rp[PATH_MAX];
110 1216 : bool bRet = realpath(fn.getStr(), rp);
111 :
112 1216 : if (bRet)
113 : {
114 : rtl::OUString resolved = rtl::OStringToOUString(
115 : rtl::OString(static_cast<sal_Char*>(rp)),
116 1216 : osl_getThreadTextEncoding());
117 :
118 1216 : rtl_uString_assign(ppustrResolvedName, resolved.pData);
119 : }
120 1216 : return bRet;
121 : }
122 :
123 : //#########################
124 : //stat_c
125 0 : int stat_c(const char* cpPath, struct stat* buf)
126 : {
127 : #ifdef ANDROID
128 : if (strncmp(cpPath, "/assets", sizeof("/assets")-1) == 0 &&
129 : (cpPath[sizeof("/assets")-1] == '\0' ||
130 : cpPath[sizeof("/assets")-1] == '/'))
131 : return lo_apk_lstat(cpPath, buf);
132 : #endif
133 0 : return stat(cpPath, buf);
134 : }
135 :
136 : //#########################
137 : //lstat_c
138 36932 : int lstat_c(const char* cpPath, struct stat* buf)
139 : {
140 : #ifdef ANDROID
141 : if (strncmp(cpPath, "/assets", sizeof("/assets")-1) == 0 &&
142 : (cpPath[sizeof("/assets")-1] == '\0' ||
143 : cpPath[sizeof("/assets")-1] == '/'))
144 : return lo_apk_lstat(cpPath, buf);
145 : #endif
146 36932 : return lstat(cpPath, buf);
147 : }
148 :
149 : //#########################
150 : //lstat_u
151 4080 : int lstat_u(const rtl_uString* pustrPath, struct stat* buf)
152 : {
153 : #ifndef MACOSX // not MACOSX
154 4080 : rtl::OString fn = OUStringToOString(pustrPath);
155 4080 : return lstat_c(fn.getStr(), buf);
156 : #else
157 : return lstat(macxp_resolveAliasAndConvert(pustrPath).getStr(), buf);
158 : #endif
159 : }
160 :
161 : //#########################
162 : // @see mkdir
163 50 : int mkdir_u(const rtl_uString* path, mode_t mode)
164 : {
165 50 : return mkdir(OUStringToOString(path).getStr(), mode);
166 : }
167 :
168 :
169 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|