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 : #ifdef UNX
21 : #include <errno.h>
22 : #endif
23 :
24 : #include <limits.h>
25 : #include <string.h>
26 :
27 : #include "comdep.hxx"
28 : #include <tools/fsys.hxx>
29 :
30 13255 : FileStat::FileStat( const DirEntry& rDirEntry, FSysAccess nAccess )
31 : : // don't use Default-Ctors!
32 : aDateCreated( sal_uIntPtr(0) ),
33 : aTimeCreated( sal_uIntPtr(0) ),
34 : aDateModified( sal_uIntPtr(0) ),
35 : aTimeModified( sal_uIntPtr(0) ),
36 : aDateAccessed( sal_uIntPtr(0) ),
37 13255 : aTimeAccessed( sal_uIntPtr(0) )
38 : {
39 13255 : sal_Bool bCached = FSYS_ACCESS_CACHED == (nAccess & FSYS_ACCESS_CACHED);
40 13255 : sal_Bool bFloppy = FSYS_ACCESS_FLOPPY == (nAccess & FSYS_ACCESS_FLOPPY);
41 :
42 : #ifdef FEAT_FSYS_DOUBLESPEED
43 13255 : const FileStat *pStatFromDir = bCached ? rDirEntry.ImpGetStat() : 0;
44 13255 : if ( pStatFromDir )
45 : {
46 0 : nError = pStatFromDir->nError;
47 0 : nKindFlags = pStatFromDir->nKindFlags;
48 0 : nSize = pStatFromDir->nSize;
49 0 : aCreator = pStatFromDir->aCreator;
50 0 : aType = pStatFromDir->aType;
51 0 : aDateCreated = pStatFromDir->aDateCreated;
52 0 : aTimeCreated = pStatFromDir->aTimeCreated;
53 0 : aDateModified = pStatFromDir->aDateModified;
54 0 : aTimeModified = pStatFromDir->aTimeModified;
55 0 : aDateAccessed = pStatFromDir->aDateAccessed;
56 0 : aTimeAccessed = pStatFromDir->aTimeAccessed;
57 : }
58 : else
59 : #endif
60 13255 : Update( rDirEntry, bFloppy );
61 13255 : }
62 :
63 12072 : sal_Bool FileStat::IsKind( DirEntryKind nKind ) const
64 : {
65 : sal_Bool bRet = ( ( nKind == FSYS_KIND_UNKNOWN ) &&
66 : ( nKindFlags == FSYS_KIND_UNKNOWN ) ) ||
67 12072 : ( ( nKindFlags & nKind ) == nKind );
68 12072 : return bRet;
69 : }
70 :
71 732 : sal_Bool FileStat::GetReadOnlyFlag( const DirEntry &rEntry )
72 : {
73 732 : rtl::OString aFPath(rtl::OUStringToOString(rEntry.GetFull(), osl_getThreadTextEncoding()));
74 : #if defined WNT
75 : DWORD nRes = GetFileAttributes( (LPCTSTR) aFPath.getStr() );
76 : return ULONG_MAX != nRes &&
77 : ( FILE_ATTRIBUTE_READONLY & nRes ) == FILE_ATTRIBUTE_READONLY;
78 : #elif defined UNX
79 : /* could we stat the object? */
80 : struct stat aBuf;
81 732 : if (stat(aFPath.getStr(), &aBuf))
82 32 : return sal_False;
83 : /* jupp, is writable for user? */
84 700 : return((aBuf.st_mode & S_IWUSR) != S_IWUSR);
85 : #else
86 : return sal_False;
87 : #endif
88 : }
89 :
90 0 : sal_uIntPtr FileStat::SetReadOnlyFlag( const DirEntry &rEntry, sal_Bool bRO )
91 : {
92 :
93 0 : rtl::OString aFPath(rtl::OUStringToOString(rEntry.GetFull(), osl_getThreadTextEncoding()));
94 :
95 : #if defined WNT
96 : DWORD nRes = GetFileAttributes( (LPCTSTR) aFPath.getStr() );
97 : if ( ULONG_MAX != nRes )
98 : nRes = SetFileAttributes( (LPCTSTR) aFPath.getStr(),
99 : ( nRes & ~FILE_ATTRIBUTE_READONLY ) |
100 : ( bRO ? FILE_ATTRIBUTE_READONLY : 0 ) );
101 : return ( ULONG_MAX == nRes ) ? ERRCODE_IO_UNKNOWN : 0;
102 : #elif defined UNX
103 : /* first, stat the object to get permissions */
104 : struct stat aBuf;
105 0 : if (stat(aFPath.getStr(), &aBuf))
106 0 : return ERRCODE_IO_NOTEXISTS;
107 : /* set or clear write bit for user */
108 : mode_t nMode;
109 0 : if (bRO)
110 : {
111 0 : nMode = aBuf.st_mode & ~S_IWUSR;
112 0 : nMode = aBuf.st_mode & ~S_IWGRP;
113 0 : nMode = aBuf.st_mode & ~S_IWOTH;
114 : }
115 : else
116 0 : nMode = aBuf.st_mode | S_IWUSR;
117 : /* change it on fs */
118 0 : if (chmod(aFPath.getStr(), nMode))
119 : {
120 0 : switch (errno)
121 : {
122 : case EPERM :
123 : case EROFS :
124 0 : return ERRCODE_IO_ACCESSDENIED;
125 : default :
126 0 : return ERRCODE_IO_NOTEXISTS;
127 : }
128 : }
129 : else
130 0 : return ERRCODE_NONE;
131 : #else
132 : return ERRCODE_IO_NOTSUPPORTED;
133 : #endif
134 : }
135 :
136 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|