Changed the GDI driver interface to pass an opaque PHYSDEV pointer
[wine] / win32 / file.c
1 /*
2  * Win32 kernel functions
3  *
4  * Copyright 1995 Martin von Loewis, Sven Verdoolaege, and Cameron Heide
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <errno.h>
25 #ifdef HAVE_SYS_ERRNO_H
26 #include <sys/errno.h>
27 #endif
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #ifdef HAVE_SYS_MMAN_H
33 #include <sys/mman.h>
34 #endif
35 #include <fcntl.h>
36 #include <string.h>
37 #include <time.h>
38
39 #include "winbase.h"
40 #include "winerror.h"
41
42 #include "wine/winbase16.h"
43 #include "file.h"
44
45 #include "wine/debug.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(file);
48
49 /**************************************************************************
50  *              SetFileAttributes       (KERNEL.421)
51  */
52 BOOL16 WINAPI SetFileAttributes16( LPCSTR lpFileName, DWORD attributes )
53 {
54     return SetFileAttributesA( lpFileName, attributes );
55 }
56
57
58 /**************************************************************************
59  *              SetFileAttributesA      (KERNEL32.@)
60  */
61 BOOL WINAPI SetFileAttributesA(LPCSTR lpFileName, DWORD attributes)
62 {
63     struct stat buf;
64     DOS_FULL_NAME full_name;
65
66     if (!DOSFS_GetFullName( lpFileName, TRUE, &full_name ))
67         return FALSE;
68
69     TRACE("(%s,%lx)\n",lpFileName,attributes);
70     if (attributes & FILE_ATTRIBUTE_NORMAL) {
71       attributes &= ~FILE_ATTRIBUTE_NORMAL;
72       if (attributes)
73         FIXME("(%s):%lx illegal combination with FILE_ATTRIBUTE_NORMAL.\n",
74               lpFileName,attributes);
75     }
76     if(stat(full_name.long_name,&buf)==-1)
77     {
78         FILE_SetDosError();
79         return FALSE;
80     }
81     if (attributes & FILE_ATTRIBUTE_READONLY)
82     {
83         if(S_ISDIR(buf.st_mode))
84             /* FIXME */
85             WARN("FILE_ATTRIBUTE_READONLY ignored for directory.\n");
86         else
87             buf.st_mode &= ~0222; /* octal!, clear write permission bits */
88         attributes &= ~FILE_ATTRIBUTE_READONLY;
89     }
90     else
91     {
92         /* add write permission */
93         buf.st_mode |= 0600 | ((buf.st_mode & 044) >> 1);
94     }
95     if (attributes & FILE_ATTRIBUTE_DIRECTORY)
96     {
97         if (!S_ISDIR(buf.st_mode))
98             FIXME("SetFileAttributes expected the file '%s' to be a directory",
99                   lpFileName);
100         attributes &= ~FILE_ATTRIBUTE_DIRECTORY;
101     }
102     attributes &= ~(FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM);
103     if (attributes)
104         FIXME("(%s):%lx attribute(s) not implemented.\n",
105               lpFileName,attributes);
106     if (-1==chmod(full_name.long_name,buf.st_mode))
107     {
108         if(GetDriveTypeA(lpFileName) == DRIVE_CDROM) {
109            SetLastError( ERROR_ACCESS_DENIED );
110            return FALSE;
111         }
112
113         /*
114         * FIXME: We don't return FALSE here because of differences between
115         *        Linux and Windows privileges. Under Linux only the owner of
116         *        the file is allowed to change file attributes. Under Windows,
117         *        applications expect that if you can write to a file, you can also
118         *        change its attributes (see GENERIC_WRITE). We could try to be 
119         *        clever here but that would break multi-user installations where
120         *        users share read-only DLLs. This is because some installers like
121         *        to change attributes of already installed DLLs.
122         */
123         FIXME("Couldn't set file attributes for existing file \"%s\".\n"
124               "Check permissions or set VFAT \"quiet\" mount flag\n", full_name.long_name);
125     }
126     return TRUE;
127 }
128
129
130 /**************************************************************************
131  *              SetFileAttributesW      (KERNEL32.@)
132  */
133 BOOL WINAPI SetFileAttributesW(LPCWSTR lpFileName, DWORD attributes)
134 {
135     BOOL res;
136     DWORD len = WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, NULL, 0, NULL, NULL );
137     LPSTR afn = HeapAlloc( GetProcessHeap(), 0, len );
138
139     WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, afn, len, NULL, NULL );
140     res = SetFileAttributesA( afn, attributes );
141     HeapFree( GetProcessHeap(), 0, afn );
142     return res;
143 }