wined3d: Fix handling of a special case in IWineD3DImpl_FillGLCaps() and adjust type...
[wine/wine64.git] / server / symlink.c
blob183497ce40b35d68bba572820434ec33d7770790
1 /*
2 * Server-side symbolic link object management
4 * Copyright (C) 2005 Vitaliy Margolen
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <sys/types.h>
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "winternl.h"
34 #include "ddk/wdm.h"
36 #include "handle.h"
37 #include "request.h"
38 #include "object.h"
39 #include "unicode.h"
41 struct symlink
43 struct object obj; /* object header */
44 WCHAR *target; /* target of the symlink */
45 data_size_t len; /* target len in bytes */
48 static void symlink_dump( struct object *obj, int verbose );
49 static unsigned int symlink_map_access( struct object *obj, unsigned int access );
50 static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
51 unsigned int attr );
52 static void symlink_destroy( struct object *obj );
54 static const struct object_ops symlink_ops =
56 sizeof(struct symlink), /* size */
57 symlink_dump, /* dump */
58 no_add_queue, /* add_queue */
59 NULL, /* remove_queue */
60 NULL, /* signaled */
61 NULL, /* satisfied */
62 no_signal, /* signal */
63 no_get_fd, /* get_fd */
64 symlink_map_access, /* map_access */
65 default_get_sd, /* get_sd */
66 default_set_sd, /* set_sd */
67 symlink_lookup_name, /* lookup_name */
68 no_open_file, /* open_file */
69 no_close_handle, /* close_handle */
70 symlink_destroy /* destroy */
73 static void symlink_dump( struct object *obj, int verbose )
75 struct symlink *symlink = (struct symlink *)obj;
76 assert( obj->ops == &symlink_ops );
78 fprintf( stderr, "Symlink " );
79 dump_object_name( obj );
80 fprintf( stderr, " -> L\"" );
81 dump_strW( symlink->target, symlink->len / sizeof(WCHAR), stderr, "\"\"" );
82 fprintf( stderr, "\"\n" );
85 static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
86 unsigned int attr )
88 struct symlink *symlink = (struct symlink *)obj;
89 struct unicode_str target_str, name_left;
90 struct object *target;
92 assert( obj->ops == &symlink_ops );
93 if (attr & OBJ_OPENLINK) return NULL;
95 target_str.str = symlink->target;
96 target_str.len = symlink->len;
97 if ((target = find_object_dir( NULL, &target_str, attr, &name_left )))
99 if (name_left.len)
101 release_object( target );
102 target = NULL;
103 set_error( STATUS_OBJECT_PATH_NOT_FOUND );
106 return target;
109 static unsigned int symlink_map_access( struct object *obj, unsigned int access )
111 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYMBOLIC_LINK_QUERY;
112 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE;
113 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
114 if (access & GENERIC_ALL) access |= SYMBOLIC_LINK_ALL_ACCESS;
115 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
118 static void symlink_destroy( struct object *obj )
120 struct symlink *symlink = (struct symlink *)obj;
121 assert( obj->ops == &symlink_ops );
122 free( symlink->target );
125 struct symlink *create_symlink( struct directory *root, const struct unicode_str *name,
126 unsigned int attr, const struct unicode_str *target )
128 struct symlink *symlink;
130 if (!target->len)
132 set_error( STATUS_INVALID_PARAMETER );
133 return NULL;
135 if ((symlink = create_named_object_dir( root, name, attr, &symlink_ops )) &&
136 (get_error() != STATUS_OBJECT_NAME_EXISTS))
138 if ((symlink->target = memdup( target->str, target->len )))
139 symlink->len = target->len;
140 else
142 release_object( symlink );
143 symlink = NULL;
146 return symlink;
150 /* create a symbolic link object */
151 DECL_HANDLER(create_symlink)
153 struct symlink *symlink;
154 struct unicode_str name, target;
155 struct directory *root = NULL;
157 if (req->name_len > get_req_data_size())
159 set_error( STATUS_INVALID_PARAMETER );
160 return;
162 name.str = get_req_data();
163 target.str = name.str + req->name_len / sizeof(WCHAR);
164 name.len = (target.str - name.str) * sizeof(WCHAR);
165 target.len = ((get_req_data_size() - name.len) / sizeof(WCHAR)) * sizeof(WCHAR);
167 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
168 return;
170 if ((symlink = create_symlink( root, &name, req->attributes, &target )))
172 reply->handle = alloc_handle( current->process, symlink, req->access, req->attributes );
173 release_object( symlink );
176 if (root) release_object( root );
179 /* open a symbolic link object */
180 DECL_HANDLER(open_symlink)
182 struct unicode_str name;
183 struct directory *root = NULL;
184 struct symlink *symlink;
186 get_req_unicode_str( &name );
187 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
188 return;
190 if ((symlink = open_object_dir( root, &name, req->attributes | OBJ_OPENLINK, &symlink_ops )))
192 reply->handle = alloc_handle( current->process, &symlink->obj, req->access, req->attributes );
193 release_object( symlink );
196 if (root) release_object( root );
199 /* query a symbolic link object */
200 DECL_HANDLER(query_symlink)
202 struct symlink *symlink;
204 symlink = (struct symlink *)get_handle_obj( current->process, req->handle,
205 SYMBOLIC_LINK_QUERY, &symlink_ops );
206 if (!symlink) return;
208 if (get_reply_max_size() < symlink->len)
209 set_error( STATUS_BUFFER_TOO_SMALL );
210 else
211 set_reply_data( symlink->target, symlink->len );
212 release_object( symlink );