1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*87d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*87d2adbcSAndrew Rist * distributed with this work for additional information
6*87d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*87d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at
10*87d2adbcSAndrew Rist *
11*87d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist *
13*87d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist * KIND, either express or implied. See the License for the
17*87d2adbcSAndrew Rist * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist * under the License.
19*87d2adbcSAndrew Rist *
20*87d2adbcSAndrew Rist *************************************************************/
21*87d2adbcSAndrew Rist
22*87d2adbcSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "osl/file.h"
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include "system.h"
30cdf0e10cSrcweir #include <sys/types.h>
31cdf0e10cSrcweir #include <dirent.h>
32cdf0e10cSrcweir #include <errno.h>
33cdf0e10cSrcweir #include <limits.h>
34cdf0e10cSrcweir #include <unistd.h>
35cdf0e10cSrcweir
36cdf0e10cSrcweir #include "file_impl.hxx"
37cdf0e10cSrcweir #include "file_error_transl.h"
38cdf0e10cSrcweir #include "file_path_helper.hxx"
39cdf0e10cSrcweir #include "file_url.h"
40cdf0e10cSrcweir #include "uunxapi.hxx"
41cdf0e10cSrcweir
42cdf0e10cSrcweir namespace /* private */
43cdf0e10cSrcweir {
44cdf0e10cSrcweir
set_file_type(const struct stat & file_stat,oslFileStatus * pStat)45cdf0e10cSrcweir inline void set_file_type(const struct stat& file_stat, oslFileStatus* pStat)
46cdf0e10cSrcweir {
47cdf0e10cSrcweir /* links to directories state also to be a directory */
48cdf0e10cSrcweir if (S_ISLNK(file_stat.st_mode))
49cdf0e10cSrcweir pStat->eType = osl_File_Type_Link;
50cdf0e10cSrcweir else if (S_ISDIR(file_stat.st_mode))
51cdf0e10cSrcweir pStat->eType = osl_File_Type_Directory;
52cdf0e10cSrcweir else if (S_ISREG(file_stat.st_mode))
53cdf0e10cSrcweir pStat->eType = osl_File_Type_Regular;
54cdf0e10cSrcweir else if (S_ISFIFO(file_stat.st_mode))
55cdf0e10cSrcweir pStat->eType = osl_File_Type_Fifo;
56cdf0e10cSrcweir else if (S_ISSOCK(file_stat.st_mode))
57cdf0e10cSrcweir pStat->eType = osl_File_Type_Socket;
58cdf0e10cSrcweir else if (S_ISCHR(file_stat.st_mode) || S_ISBLK(file_stat.st_mode))
59cdf0e10cSrcweir pStat->eType = osl_File_Type_Special;
60cdf0e10cSrcweir else
61cdf0e10cSrcweir pStat->eType = osl_File_Type_Unknown;
62cdf0e10cSrcweir
63cdf0e10cSrcweir pStat->uValidFields |= osl_FileStatus_Mask_Type;
64cdf0e10cSrcweir }
65cdf0e10cSrcweir
set_file_access_mask(const struct stat & file_stat,oslFileStatus * pStat)66cdf0e10cSrcweir inline void set_file_access_mask(const struct stat& file_stat, oslFileStatus* pStat)
67cdf0e10cSrcweir {
68cdf0e10cSrcweir // user permissions
69cdf0e10cSrcweir if (S_IRUSR & file_stat.st_mode)
70cdf0e10cSrcweir pStat->uAttributes |= osl_File_Attribute_OwnRead;
71cdf0e10cSrcweir
72cdf0e10cSrcweir if (S_IWUSR & file_stat.st_mode)
73cdf0e10cSrcweir pStat->uAttributes |= osl_File_Attribute_OwnWrite;
74cdf0e10cSrcweir
75cdf0e10cSrcweir if (S_IXUSR & file_stat.st_mode)
76cdf0e10cSrcweir pStat->uAttributes |= osl_File_Attribute_OwnExe;
77cdf0e10cSrcweir
78cdf0e10cSrcweir // group permissions
79cdf0e10cSrcweir if (S_IRGRP & file_stat.st_mode)
80cdf0e10cSrcweir pStat->uAttributes |= osl_File_Attribute_GrpRead;
81cdf0e10cSrcweir
82cdf0e10cSrcweir if (S_IWGRP & file_stat.st_mode)
83cdf0e10cSrcweir pStat->uAttributes |= osl_File_Attribute_GrpWrite;
84cdf0e10cSrcweir
85cdf0e10cSrcweir if (S_IXGRP & file_stat.st_mode)
86cdf0e10cSrcweir pStat->uAttributes |= osl_File_Attribute_GrpExe;
87cdf0e10cSrcweir
88cdf0e10cSrcweir // others permissions
89cdf0e10cSrcweir if (S_IROTH & file_stat.st_mode)
90cdf0e10cSrcweir pStat->uAttributes |= osl_File_Attribute_OthRead;
91cdf0e10cSrcweir
92cdf0e10cSrcweir if (S_IWOTH & file_stat.st_mode)
93cdf0e10cSrcweir pStat->uAttributes |= osl_File_Attribute_OthWrite;
94cdf0e10cSrcweir
95cdf0e10cSrcweir if (S_IXOTH & file_stat.st_mode)
96cdf0e10cSrcweir pStat->uAttributes |= osl_File_Attribute_OthExe;
97cdf0e10cSrcweir
98cdf0e10cSrcweir pStat->uValidFields |= osl_FileStatus_Mask_Attributes;
99cdf0e10cSrcweir }
100cdf0e10cSrcweir
set_file_access_rights(const struct stat & file_stat,int S_IR,int S_IW,int S_IX,oslFileStatus * pStat)101cdf0e10cSrcweir inline void set_file_access_rights(const struct stat& file_stat, int S_IR, int S_IW, int S_IX, oslFileStatus* pStat)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir /* we cannot really map osl_File_Attribute_ReadOnly to
104cdf0e10cSrcweir the Unix access rights, it's a Windows only flag
105cdf0e10cSrcweir that's why the following hack. We set osl_FileStatus_Mask_Attributes
106cdf0e10cSrcweir but if there is no read access for a file we clear the flag
107cdf0e10cSrcweir again to signal to the caller that there are no file attributes
108cdf0e10cSrcweir to read because that's better than to give them incorrect one.
109cdf0e10cSrcweir */
110cdf0e10cSrcweir pStat->uValidFields |= osl_FileStatus_Mask_Attributes;
111cdf0e10cSrcweir
112cdf0e10cSrcweir if ((0 == (S_IW & file_stat.st_mode)) && (S_IR & file_stat.st_mode))
113cdf0e10cSrcweir pStat->uAttributes |= osl_File_Attribute_ReadOnly;
114cdf0e10cSrcweir
115cdf0e10cSrcweir if (S_IX & file_stat.st_mode)
116cdf0e10cSrcweir pStat->uAttributes |= osl_File_Attribute_Executable;
117cdf0e10cSrcweir }
118cdf0e10cSrcweir
119cdf0e10cSrcweir /* a process may belong to up to NGROUPS_MAX groups, so when
120cdf0e10cSrcweir checking group access rights, we have to check all belonging
121cdf0e10cSrcweir groups */
is_in_process_grouplist(const gid_t file_group)122cdf0e10cSrcweir inline bool is_in_process_grouplist(const gid_t file_group)
123cdf0e10cSrcweir {
124cdf0e10cSrcweir // check primary process group
125cdf0e10cSrcweir
126cdf0e10cSrcweir if (getgid() == file_group)
127cdf0e10cSrcweir return true;
128cdf0e10cSrcweir
129cdf0e10cSrcweir // check supplementary process groups
130cdf0e10cSrcweir
131cdf0e10cSrcweir gid_t grplist[NGROUPS_MAX];
132cdf0e10cSrcweir int grp_number = getgroups(NGROUPS_MAX, grplist);
133cdf0e10cSrcweir
134cdf0e10cSrcweir for (int i = 0; i < grp_number; i++)
135cdf0e10cSrcweir {
136cdf0e10cSrcweir if (grplist[i] == file_group)
137cdf0e10cSrcweir return true;
138cdf0e10cSrcweir }
139cdf0e10cSrcweir return false;
140cdf0e10cSrcweir }
141cdf0e10cSrcweir
142cdf0e10cSrcweir /* Currently we are determining the file access right based
143cdf0e10cSrcweir on the real user ID not the effective user ID!
144cdf0e10cSrcweir We don't use access(...) because access follows links which
145cdf0e10cSrcweir may cause performance problems see #97133.
146cdf0e10cSrcweir */
set_file_access_rights(const struct stat & file_stat,oslFileStatus * pStat)147cdf0e10cSrcweir inline void set_file_access_rights(const struct stat& file_stat, oslFileStatus* pStat)
148cdf0e10cSrcweir {
149cdf0e10cSrcweir if (getuid() == file_stat.st_uid)
150cdf0e10cSrcweir {
151cdf0e10cSrcweir set_file_access_rights(file_stat, S_IRUSR, S_IWUSR, S_IXUSR, pStat);
152cdf0e10cSrcweir }
153cdf0e10cSrcweir else if (is_in_process_grouplist(file_stat.st_gid))
154cdf0e10cSrcweir {
155cdf0e10cSrcweir set_file_access_rights(file_stat, S_IRGRP, S_IWGRP, S_IXGRP, pStat);
156cdf0e10cSrcweir }
157cdf0e10cSrcweir else
158cdf0e10cSrcweir {
159cdf0e10cSrcweir set_file_access_rights(file_stat, S_IROTH, S_IWOTH, S_IXOTH, pStat);
160cdf0e10cSrcweir }
161cdf0e10cSrcweir }
162cdf0e10cSrcweir
set_file_hidden_status(const rtl::OUString & file_path,oslFileStatus * pStat)163cdf0e10cSrcweir inline void set_file_hidden_status(const rtl::OUString& file_path, oslFileStatus* pStat)
164cdf0e10cSrcweir {
165cdf0e10cSrcweir pStat->uAttributes = osl::systemPathIsHiddenFileOrDirectoryEntry(file_path) ? osl_File_Attribute_Hidden : 0;
166cdf0e10cSrcweir pStat->uValidFields |= osl_FileStatus_Mask_Attributes;
167cdf0e10cSrcweir }
168cdf0e10cSrcweir
169cdf0e10cSrcweir /* the set_file_access_rights must be called after set_file_hidden_status(...) and
170cdf0e10cSrcweir set_file_access_mask(...) because of the hack in set_file_access_rights(...) */
set_file_attributes(const rtl::OUString & file_path,const struct stat & file_stat,const sal_uInt32 uFieldMask,oslFileStatus * pStat)171cdf0e10cSrcweir inline void set_file_attributes(
172cdf0e10cSrcweir const rtl::OUString& file_path, const struct stat& file_stat, const sal_uInt32 uFieldMask, oslFileStatus* pStat)
173cdf0e10cSrcweir {
174cdf0e10cSrcweir set_file_hidden_status(file_path, pStat);
175cdf0e10cSrcweir set_file_access_mask(file_stat, pStat);
176cdf0e10cSrcweir
177cdf0e10cSrcweir // we set the file access rights only on demand
178cdf0e10cSrcweir // because it's potentially expensive
179cdf0e10cSrcweir if (uFieldMask & osl_FileStatus_Mask_Attributes)
180cdf0e10cSrcweir set_file_access_rights(file_stat, pStat);
181cdf0e10cSrcweir }
182cdf0e10cSrcweir
set_file_access_time(const struct stat & file_stat,oslFileStatus * pStat)183cdf0e10cSrcweir inline void set_file_access_time(const struct stat& file_stat, oslFileStatus* pStat)
184cdf0e10cSrcweir {
185cdf0e10cSrcweir pStat->aAccessTime.Seconds = file_stat.st_atime;
186cdf0e10cSrcweir pStat->aAccessTime.Nanosec = 0;
187cdf0e10cSrcweir pStat->uValidFields |= osl_FileStatus_Mask_AccessTime;
188cdf0e10cSrcweir }
189cdf0e10cSrcweir
set_file_modify_time(const struct stat & file_stat,oslFileStatus * pStat)190cdf0e10cSrcweir inline void set_file_modify_time(const struct stat& file_stat, oslFileStatus* pStat)
191cdf0e10cSrcweir {
192cdf0e10cSrcweir pStat->aModifyTime.Seconds = file_stat.st_mtime;
193cdf0e10cSrcweir pStat->aModifyTime.Nanosec = 0;
194cdf0e10cSrcweir pStat->uValidFields |= osl_FileStatus_Mask_ModifyTime;
195cdf0e10cSrcweir }
196cdf0e10cSrcweir
set_file_size(const struct stat & file_stat,oslFileStatus * pStat)197cdf0e10cSrcweir inline void set_file_size(const struct stat& file_stat, oslFileStatus* pStat)
198cdf0e10cSrcweir {
199cdf0e10cSrcweir if (S_ISREG(file_stat.st_mode))
200cdf0e10cSrcweir {
201cdf0e10cSrcweir pStat->uFileSize = file_stat.st_size;
202cdf0e10cSrcweir pStat->uValidFields |= osl_FileStatus_Mask_FileSize;
203cdf0e10cSrcweir }
204cdf0e10cSrcweir }
205cdf0e10cSrcweir
206cdf0e10cSrcweir /* we only need to call stat or lstat if one of the
207cdf0e10cSrcweir following flags is set */
is_stat_call_necessary(sal_uInt32 field_mask,oslFileType file_type=osl_File_Type_Unknown)208cdf0e10cSrcweir inline bool is_stat_call_necessary(sal_uInt32 field_mask, oslFileType file_type = osl_File_Type_Unknown)
209cdf0e10cSrcweir {
210cdf0e10cSrcweir return (
211cdf0e10cSrcweir ((field_mask & osl_FileStatus_Mask_Type) && (file_type == osl_File_Type_Unknown)) ||
212cdf0e10cSrcweir (field_mask & osl_FileStatus_Mask_Attributes) ||
213cdf0e10cSrcweir (field_mask & osl_FileStatus_Mask_CreationTime) ||
214cdf0e10cSrcweir (field_mask & osl_FileStatus_Mask_AccessTime) ||
215cdf0e10cSrcweir (field_mask & osl_FileStatus_Mask_ModifyTime) ||
216cdf0e10cSrcweir (field_mask & osl_FileStatus_Mask_FileSize) ||
217cdf0e10cSrcweir (field_mask & osl_FileStatus_Mask_LinkTargetURL) ||
218cdf0e10cSrcweir (field_mask & osl_FileStatus_Mask_Validate));
219cdf0e10cSrcweir }
220cdf0e10cSrcweir
set_link_target_url(const rtl::OUString & file_path,oslFileStatus * pStat)221cdf0e10cSrcweir inline oslFileError set_link_target_url(const rtl::OUString& file_path, oslFileStatus* pStat)
222cdf0e10cSrcweir {
223cdf0e10cSrcweir rtl::OUString link_target;
224cdf0e10cSrcweir if (!osl::realpath(file_path, link_target))
225cdf0e10cSrcweir return oslTranslateFileError(OSL_FET_ERROR, errno);
226cdf0e10cSrcweir
227cdf0e10cSrcweir oslFileError osl_error = osl_getFileURLFromSystemPath(link_target.pData, &pStat->ustrLinkTargetURL);
228cdf0e10cSrcweir if (osl_error != osl_File_E_None)
229cdf0e10cSrcweir return osl_error;
230cdf0e10cSrcweir
231cdf0e10cSrcweir pStat->uValidFields |= osl_FileStatus_Mask_LinkTargetURL;
232cdf0e10cSrcweir return osl_File_E_None;
233cdf0e10cSrcweir }
234cdf0e10cSrcweir
setup_osl_getFileStatus(DirectoryItem_Impl * pImpl,oslFileStatus * pStat,rtl::OUString & file_path)235cdf0e10cSrcweir inline oslFileError setup_osl_getFileStatus(
236cdf0e10cSrcweir DirectoryItem_Impl * pImpl, oslFileStatus* pStat, rtl::OUString& file_path)
237cdf0e10cSrcweir {
238cdf0e10cSrcweir if ((NULL == pImpl) || (NULL == pStat))
239cdf0e10cSrcweir return osl_File_E_INVAL;
240cdf0e10cSrcweir
241cdf0e10cSrcweir file_path = rtl::OUString(pImpl->m_ustrFilePath);
242cdf0e10cSrcweir OSL_ASSERT(file_path.getLength() > 0);
243cdf0e10cSrcweir if (file_path.getLength() <= 0)
244cdf0e10cSrcweir return osl_File_E_INVAL;
245cdf0e10cSrcweir
246cdf0e10cSrcweir pStat->uValidFields = 0;
247cdf0e10cSrcweir return osl_File_E_None;
248cdf0e10cSrcweir }
249cdf0e10cSrcweir
250cdf0e10cSrcweir } // end namespace private
251cdf0e10cSrcweir
252cdf0e10cSrcweir
253cdf0e10cSrcweir /****************************************************************************
254cdf0e10cSrcweir * osl_getFileStatus
255cdf0e10cSrcweir ****************************************************************************/
256cdf0e10cSrcweir
osl_getFileStatus(oslDirectoryItem Item,oslFileStatus * pStat,sal_uInt32 uFieldMask)257cdf0e10cSrcweir oslFileError SAL_CALL osl_getFileStatus(oslDirectoryItem Item, oslFileStatus* pStat, sal_uInt32 uFieldMask)
258cdf0e10cSrcweir {
259cdf0e10cSrcweir DirectoryItem_Impl * pImpl = static_cast< DirectoryItem_Impl* >(Item);
260cdf0e10cSrcweir
261cdf0e10cSrcweir rtl::OUString file_path;
262cdf0e10cSrcweir oslFileError osl_error = setup_osl_getFileStatus(pImpl, pStat, file_path);
263cdf0e10cSrcweir if (osl_File_E_None != osl_error)
264cdf0e10cSrcweir return osl_error;
265cdf0e10cSrcweir
266cdf0e10cSrcweir #if defined(__GNUC__) && (__GNUC__ < 3)
267cdf0e10cSrcweir struct ::stat file_stat;
268cdf0e10cSrcweir #else
269cdf0e10cSrcweir struct stat file_stat;
270cdf0e10cSrcweir #endif
271cdf0e10cSrcweir
272cdf0e10cSrcweir bool bStatNeeded = is_stat_call_necessary(uFieldMask, pImpl->getFileType());
273cdf0e10cSrcweir if (bStatNeeded && (0 != osl::lstat(file_path, file_stat)))
274cdf0e10cSrcweir return oslTranslateFileError(OSL_FET_ERROR, errno);
275cdf0e10cSrcweir
276cdf0e10cSrcweir if (bStatNeeded)
277cdf0e10cSrcweir {
278cdf0e10cSrcweir // we set all these attributes because it's cheap
279cdf0e10cSrcweir set_file_type(file_stat, pStat);
280cdf0e10cSrcweir set_file_access_time(file_stat, pStat);
281cdf0e10cSrcweir set_file_modify_time(file_stat, pStat);
282cdf0e10cSrcweir set_file_size(file_stat, pStat);
283cdf0e10cSrcweir set_file_attributes(file_path, file_stat, uFieldMask, pStat);
284cdf0e10cSrcweir
285cdf0e10cSrcweir // file exists semantic of osl_FileStatus_Mask_Validate
286cdf0e10cSrcweir if ((uFieldMask & osl_FileStatus_Mask_LinkTargetURL) && S_ISLNK(file_stat.st_mode))
287cdf0e10cSrcweir {
288cdf0e10cSrcweir osl_error = set_link_target_url(file_path, pStat);
289cdf0e10cSrcweir if (osl_error != osl_File_E_None)
290cdf0e10cSrcweir return osl_error;
291cdf0e10cSrcweir }
292cdf0e10cSrcweir }
293cdf0e10cSrcweir #ifdef _DIRENT_HAVE_D_TYPE
294cdf0e10cSrcweir else if (uFieldMask & osl_FileStatus_Mask_Type)
295cdf0e10cSrcweir {
296cdf0e10cSrcweir pStat->eType = pImpl->getFileType();
297cdf0e10cSrcweir pStat->uValidFields |= osl_FileStatus_Mask_Type;
298cdf0e10cSrcweir }
299cdf0e10cSrcweir #endif /* _DIRENT_HAVE_D_TYPE */
300cdf0e10cSrcweir
301cdf0e10cSrcweir if (uFieldMask & osl_FileStatus_Mask_FileURL)
302cdf0e10cSrcweir {
303cdf0e10cSrcweir if ((osl_error = osl_getFileURLFromSystemPath(file_path.pData, &pStat->ustrFileURL)) != osl_File_E_None)
304cdf0e10cSrcweir return osl_error;
305cdf0e10cSrcweir
306cdf0e10cSrcweir pStat->uValidFields |= osl_FileStatus_Mask_FileURL;
307cdf0e10cSrcweir }
308cdf0e10cSrcweir
309cdf0e10cSrcweir if (uFieldMask & osl_FileStatus_Mask_FileName)
310cdf0e10cSrcweir {
311cdf0e10cSrcweir osl_systemPathGetFileNameOrLastDirectoryPart(file_path.pData, &pStat->ustrFileName);
312cdf0e10cSrcweir pStat->uValidFields |= osl_FileStatus_Mask_FileName;
313cdf0e10cSrcweir }
314cdf0e10cSrcweir return osl_File_E_None;
315cdf0e10cSrcweir }
316cdf0e10cSrcweir
317cdf0e10cSrcweir /****************************************************************************/
318cdf0e10cSrcweir /* osl_setFileAttributes */
319cdf0e10cSrcweir /****************************************************************************/
320cdf0e10cSrcweir
osl_psz_setFileAttributes(const sal_Char * pszFilePath,sal_uInt64 uAttributes)321cdf0e10cSrcweir static oslFileError osl_psz_setFileAttributes( const sal_Char* pszFilePath, sal_uInt64 uAttributes )
322cdf0e10cSrcweir {
323cdf0e10cSrcweir oslFileError osl_error = osl_File_E_None;
324cdf0e10cSrcweir mode_t nNewMode = 0;
325cdf0e10cSrcweir
326cdf0e10cSrcweir OSL_ENSURE(!(osl_File_Attribute_Hidden & uAttributes), "osl_File_Attribute_Hidden doesn't work under Unix");
327cdf0e10cSrcweir
328cdf0e10cSrcweir if (uAttributes & osl_File_Attribute_OwnRead)
329cdf0e10cSrcweir nNewMode |= S_IRUSR;
330cdf0e10cSrcweir
331cdf0e10cSrcweir if (uAttributes & osl_File_Attribute_OwnWrite)
332cdf0e10cSrcweir nNewMode|=S_IWUSR;
333cdf0e10cSrcweir
334cdf0e10cSrcweir if (uAttributes & osl_File_Attribute_OwnExe)
335cdf0e10cSrcweir nNewMode|=S_IXUSR;
336cdf0e10cSrcweir
337cdf0e10cSrcweir if (uAttributes & osl_File_Attribute_GrpRead)
338cdf0e10cSrcweir nNewMode|=S_IRGRP;
339cdf0e10cSrcweir
340cdf0e10cSrcweir if (uAttributes & osl_File_Attribute_GrpWrite)
341cdf0e10cSrcweir nNewMode|=S_IWGRP;
342cdf0e10cSrcweir
343cdf0e10cSrcweir if (uAttributes & osl_File_Attribute_GrpExe)
344cdf0e10cSrcweir nNewMode|=S_IXGRP;
345cdf0e10cSrcweir
346cdf0e10cSrcweir if (uAttributes & osl_File_Attribute_OthRead)
347cdf0e10cSrcweir nNewMode|=S_IROTH;
348cdf0e10cSrcweir
349cdf0e10cSrcweir if (uAttributes & osl_File_Attribute_OthWrite)
350cdf0e10cSrcweir nNewMode|=S_IWOTH;
351cdf0e10cSrcweir
352cdf0e10cSrcweir if (uAttributes & osl_File_Attribute_OthExe)
353cdf0e10cSrcweir nNewMode|=S_IXOTH;
354cdf0e10cSrcweir
355cdf0e10cSrcweir if (chmod(pszFilePath, nNewMode) < 0)
356cdf0e10cSrcweir osl_error = oslTranslateFileError(OSL_FET_ERROR, errno);
357cdf0e10cSrcweir
358cdf0e10cSrcweir return osl_error;
359cdf0e10cSrcweir }
360cdf0e10cSrcweir
osl_setFileAttributes(rtl_uString * ustrFileURL,sal_uInt64 uAttributes)361cdf0e10cSrcweir oslFileError SAL_CALL osl_setFileAttributes( rtl_uString* ustrFileURL, sal_uInt64 uAttributes )
362cdf0e10cSrcweir {
363cdf0e10cSrcweir char path[PATH_MAX];
364cdf0e10cSrcweir oslFileError eRet;
365cdf0e10cSrcweir
366cdf0e10cSrcweir OSL_ASSERT( ustrFileURL );
367cdf0e10cSrcweir
368cdf0e10cSrcweir /* convert file url to system path */
369cdf0e10cSrcweir eRet = FileURLToPath( path, PATH_MAX, ustrFileURL );
370cdf0e10cSrcweir if( eRet != osl_File_E_None )
371cdf0e10cSrcweir return eRet;
372cdf0e10cSrcweir
373cdf0e10cSrcweir #ifdef MACOSX
374cdf0e10cSrcweir if ( macxp_resolveAlias( path, PATH_MAX ) != 0 )
375cdf0e10cSrcweir return oslTranslateFileError( OSL_FET_ERROR, errno );
376cdf0e10cSrcweir #endif/* MACOSX */
377cdf0e10cSrcweir
378cdf0e10cSrcweir return osl_psz_setFileAttributes( path, uAttributes );
379cdf0e10cSrcweir }
380cdf0e10cSrcweir
381cdf0e10cSrcweir /****************************************************************************/
382cdf0e10cSrcweir /* osl_setFileTime */
383cdf0e10cSrcweir /****************************************************************************/
384cdf0e10cSrcweir
osl_psz_setFileTime(const sal_Char * pszFilePath,const TimeValue *,const TimeValue * pLastAccessTime,const TimeValue * pLastWriteTime)385cdf0e10cSrcweir static oslFileError osl_psz_setFileTime (
386cdf0e10cSrcweir const sal_Char* pszFilePath,
387cdf0e10cSrcweir const TimeValue* /*pCreationTime*/,
388cdf0e10cSrcweir const TimeValue* pLastAccessTime,
389cdf0e10cSrcweir const TimeValue* pLastWriteTime )
390cdf0e10cSrcweir {
391cdf0e10cSrcweir int nRet=0;
392cdf0e10cSrcweir struct utimbuf aTimeBuffer;
393cdf0e10cSrcweir struct stat aFileStat;
394cdf0e10cSrcweir #ifdef DEBUG_OSL_FILE
395cdf0e10cSrcweir struct tm* pTM=0;
396cdf0e10cSrcweir #endif
397cdf0e10cSrcweir
398cdf0e10cSrcweir nRet = lstat(pszFilePath,&aFileStat);
399cdf0e10cSrcweir
400cdf0e10cSrcweir if ( nRet < 0 )
401cdf0e10cSrcweir {
402cdf0e10cSrcweir nRet=errno;
403cdf0e10cSrcweir return oslTranslateFileError(OSL_FET_ERROR, nRet);
404cdf0e10cSrcweir }
405cdf0e10cSrcweir
406cdf0e10cSrcweir #ifdef DEBUG_OSL_FILE
407cdf0e10cSrcweir fprintf(stderr,"File Times are (in localtime):\n");
408cdf0e10cSrcweir pTM=localtime(&aFileStat.st_ctime);
409cdf0e10cSrcweir fprintf(stderr,"CreationTime is '%s'\n",asctime(pTM));
410cdf0e10cSrcweir pTM=localtime(&aFileStat.st_atime);
411cdf0e10cSrcweir fprintf(stderr,"AccessTime is '%s'\n",asctime(pTM));
412cdf0e10cSrcweir pTM=localtime(&aFileStat.st_mtime);
413cdf0e10cSrcweir fprintf(stderr,"Modification is '%s'\n",asctime(pTM));
414cdf0e10cSrcweir
415cdf0e10cSrcweir fprintf(stderr,"File Times are (in UTC):\n");
416cdf0e10cSrcweir fprintf(stderr,"CreationTime is '%s'\n",ctime(&aFileStat.st_ctime));
417cdf0e10cSrcweir fprintf(stderr,"AccessTime is '%s'\n",ctime(&aTimeBuffer.actime));
418cdf0e10cSrcweir fprintf(stderr,"Modification is '%s'\n",ctime(&aTimeBuffer.modtime));
419cdf0e10cSrcweir #endif
420cdf0e10cSrcweir
421cdf0e10cSrcweir if ( pLastAccessTime != 0 )
422cdf0e10cSrcweir {
423cdf0e10cSrcweir aTimeBuffer.actime=pLastAccessTime->Seconds;
424cdf0e10cSrcweir }
425cdf0e10cSrcweir else
426cdf0e10cSrcweir {
427cdf0e10cSrcweir aTimeBuffer.actime=aFileStat.st_atime;
428cdf0e10cSrcweir }
429cdf0e10cSrcweir
430cdf0e10cSrcweir if ( pLastWriteTime != 0 )
431cdf0e10cSrcweir {
432cdf0e10cSrcweir aTimeBuffer.modtime=pLastWriteTime->Seconds;
433cdf0e10cSrcweir }
434cdf0e10cSrcweir else
435cdf0e10cSrcweir {
436cdf0e10cSrcweir aTimeBuffer.modtime=aFileStat.st_mtime;
437cdf0e10cSrcweir }
438cdf0e10cSrcweir
439cdf0e10cSrcweir /* mfe: Creation time not used here! */
440cdf0e10cSrcweir
441cdf0e10cSrcweir #ifdef DEBUG_OSL_FILE
442cdf0e10cSrcweir fprintf(stderr,"File Times are (in localtime):\n");
443cdf0e10cSrcweir pTM=localtime(&aFileStat.st_ctime);
444cdf0e10cSrcweir fprintf(stderr,"CreationTime now '%s'\n",asctime(pTM));
445cdf0e10cSrcweir pTM=localtime(&aTimeBuffer.actime);
446cdf0e10cSrcweir fprintf(stderr,"AccessTime now '%s'\n",asctime(pTM));
447cdf0e10cSrcweir pTM=localtime(&aTimeBuffer.modtime);
448cdf0e10cSrcweir fprintf(stderr,"Modification now '%s'\n",asctime(pTM));
449cdf0e10cSrcweir
450cdf0e10cSrcweir fprintf(stderr,"File Times are (in UTC):\n");
451cdf0e10cSrcweir fprintf(stderr,"CreationTime now '%s'\n",ctime(&aFileStat.st_ctime));
452cdf0e10cSrcweir fprintf(stderr,"AccessTime now '%s'\n",ctime(&aTimeBuffer.actime));
453cdf0e10cSrcweir fprintf(stderr,"Modification now '%s'\n",ctime(&aTimeBuffer.modtime));
454cdf0e10cSrcweir #endif
455cdf0e10cSrcweir
456cdf0e10cSrcweir nRet=utime(pszFilePath,&aTimeBuffer);
457cdf0e10cSrcweir if ( nRet < 0 )
458cdf0e10cSrcweir {
459cdf0e10cSrcweir nRet=errno;
460cdf0e10cSrcweir return oslTranslateFileError(OSL_FET_ERROR, nRet);
461cdf0e10cSrcweir }
462cdf0e10cSrcweir
463cdf0e10cSrcweir return osl_File_E_None;
464cdf0e10cSrcweir }
465cdf0e10cSrcweir
osl_setFileTime(rtl_uString * ustrFileURL,const TimeValue * pCreationTime,const TimeValue * pLastAccessTime,const TimeValue * pLastWriteTime)466cdf0e10cSrcweir oslFileError SAL_CALL osl_setFileTime (
467cdf0e10cSrcweir rtl_uString* ustrFileURL,
468cdf0e10cSrcweir const TimeValue* pCreationTime,
469cdf0e10cSrcweir const TimeValue* pLastAccessTime,
470cdf0e10cSrcweir const TimeValue* pLastWriteTime )
471cdf0e10cSrcweir {
472cdf0e10cSrcweir char path[PATH_MAX];
473cdf0e10cSrcweir oslFileError eRet;
474cdf0e10cSrcweir
475cdf0e10cSrcweir OSL_ASSERT( ustrFileURL );
476cdf0e10cSrcweir
477cdf0e10cSrcweir /* convert file url to system path */
478cdf0e10cSrcweir eRet = FileURLToPath( path, PATH_MAX, ustrFileURL );
479cdf0e10cSrcweir if( eRet != osl_File_E_None )
480cdf0e10cSrcweir return eRet;
481cdf0e10cSrcweir
482cdf0e10cSrcweir #ifdef MACOSX
483cdf0e10cSrcweir if ( macxp_resolveAlias( path, PATH_MAX ) != 0 )
484cdf0e10cSrcweir return oslTranslateFileError( OSL_FET_ERROR, errno );
485cdf0e10cSrcweir #endif/* MACOSX */
486cdf0e10cSrcweir
487cdf0e10cSrcweir return osl_psz_setFileTime( path, pCreationTime, pLastAccessTime, pLastWriteTime );
488cdf0e10cSrcweir }
489