include: Introduce combaseapi.h.
[wine.git] / server / symlink.c
blob3879bb685f73decd20823c30476696a6ba0ea340
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 static const WCHAR symlink_name[] = {'S','y','m','b','o','l','i','c','L','i','n','k'};
43 struct type_descr symlink_type =
45 { symlink_name, sizeof(symlink_name) }, /* name */
46 SYMBOLIC_LINK_ALL_ACCESS, /* valid_access */
47 { /* mapping */
48 STANDARD_RIGHTS_READ | SYMBOLIC_LINK_QUERY,
49 STANDARD_RIGHTS_WRITE,
50 STANDARD_RIGHTS_EXECUTE | SYMBOLIC_LINK_QUERY,
51 SYMBOLIC_LINK_ALL_ACCESS
55 struct symlink
57 struct object obj; /* object header */
58 WCHAR *target; /* target of the symlink */
59 data_size_t len; /* target len in bytes */
62 static void symlink_dump( struct object *obj, int verbose );
63 static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
64 unsigned int attr, struct object *root );
65 static void symlink_destroy( struct object *obj );
67 static const struct object_ops symlink_ops =
69 sizeof(struct symlink), /* size */
70 &symlink_type, /* type */
71 symlink_dump, /* dump */
72 no_add_queue, /* add_queue */
73 NULL, /* remove_queue */
74 NULL, /* signaled */
75 NULL, /* satisfied */
76 no_signal, /* signal */
77 no_get_fd, /* get_fd */
78 default_map_access, /* map_access */
79 default_get_sd, /* get_sd */
80 default_set_sd, /* set_sd */
81 default_get_full_name, /* get_full_name */
82 symlink_lookup_name, /* lookup_name */
83 directory_link_name, /* link_name */
84 default_unlink_name, /* unlink_name */
85 no_open_file, /* open_file */
86 no_kernel_obj_list, /* get_kernel_obj_list */
87 no_close_handle, /* close_handle */
88 symlink_destroy /* destroy */
91 static void symlink_dump( struct object *obj, int verbose )
93 struct symlink *symlink = (struct symlink *)obj;
94 assert( obj->ops == &symlink_ops );
96 fputs( "Symlink target=\"", stderr );
97 dump_strW( symlink->target, symlink->len, stderr, "\"\"" );
98 fputs( "\"\n", stderr );
101 static struct object *symlink_lookup_name( struct object *obj, struct unicode_str *name,
102 unsigned int attr, struct object *root )
104 struct symlink *symlink = (struct symlink *)obj;
105 struct unicode_str target_str, name_left;
106 struct object *target;
108 assert( obj->ops == &symlink_ops );
110 if (!name) return NULL;
111 if (!name->len && (attr & OBJ_OPENLINK)) return NULL;
112 if (obj == root) return NULL;
114 target_str.str = symlink->target;
115 target_str.len = symlink->len;
116 if ((target = lookup_named_object( NULL, &target_str, attr, &name_left )))
118 if (name_left.len)
120 release_object( target );
121 target = NULL;
122 set_error( STATUS_OBJECT_PATH_NOT_FOUND );
125 return target;
128 static void symlink_destroy( struct object *obj )
130 struct symlink *symlink = (struct symlink *)obj;
131 assert( obj->ops == &symlink_ops );
132 free( symlink->target );
135 struct object *create_symlink( struct object *root, const struct unicode_str *name,
136 unsigned int attr, const struct unicode_str *target,
137 const struct security_descriptor *sd )
139 struct symlink *symlink;
141 if (!target->len)
143 set_error( STATUS_INVALID_PARAMETER );
144 return NULL;
146 if (!(symlink = create_named_object( root, &symlink_ops, name, attr, sd ))) return NULL;
147 if (get_error() != STATUS_OBJECT_NAME_EXISTS && !(symlink->target = memdup( target->str, target->len )))
149 release_object( symlink );
150 return NULL;
152 symlink->len = target->len;
153 return &symlink->obj;
156 /* create a symlink pointing to an existing object */
157 struct object *create_obj_symlink( struct object *root, const struct unicode_str *name,
158 unsigned int attr, struct object *target,
159 const struct security_descriptor *sd )
161 struct symlink *symlink;
162 data_size_t len;
163 WCHAR *target_name;
165 if (!(target_name = target->ops->get_full_name( target, &len )))
167 set_error( STATUS_INVALID_PARAMETER );
168 return NULL;
170 if ((symlink = create_named_object( root, &symlink_ops, name, attr, sd )) &&
171 (get_error() != STATUS_OBJECT_NAME_EXISTS))
173 symlink->target = target_name;
174 symlink->len = len;
176 else free( target_name );
178 return &symlink->obj;
182 /* create a symbolic link object */
183 DECL_HANDLER(create_symlink)
185 struct object *symlink;
186 struct unicode_str name, target;
187 struct object *root;
188 const struct security_descriptor *sd;
189 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
191 if (!objattr) return;
193 target.str = get_req_data_after_objattr( objattr, &target.len );
194 target.len = (target.len / sizeof(WCHAR)) * sizeof(WCHAR);
196 if ((symlink = create_symlink( root, &name, objattr->attributes, &target, sd )))
198 reply->handle = alloc_handle( current->process, symlink, req->access, objattr->attributes );
199 release_object( symlink );
202 if (root) release_object( root );
205 /* open a symbolic link object */
206 DECL_HANDLER(open_symlink)
208 struct unicode_str name = get_req_unicode_str();
210 reply->handle = open_object( current->process, req->rootdir, req->access,
211 &symlink_ops, &name, req->attributes | OBJ_OPENLINK );
214 /* query a symbolic link object */
215 DECL_HANDLER(query_symlink)
217 struct symlink *symlink;
219 symlink = (struct symlink *)get_handle_obj( current->process, req->handle,
220 SYMBOLIC_LINK_QUERY, &symlink_ops );
221 if (!symlink) return;
223 reply->total = symlink->len;
224 if (get_reply_max_size() < symlink->len)
225 set_error( STATUS_BUFFER_TOO_SMALL );
226 else
227 set_reply_data( symlink->target, symlink->len );
228 release_object( symlink );