dav/system.c

Fri, 04 Jun 2021 18:24:55 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 04 Jun 2021 18:24:55 +0200
changeset 728
35a421f441d5
parent 612
66dc8b992d8d
child 747
efbd59642577
permissions
-rw-r--r--

add stream API

410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
576
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 574
diff changeset
4 * Copyright 2019 Olaf Wintermann. All rights reserved.
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
29 #include <libidav/utils.h>
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
30
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <stdio.h>
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <stdlib.h>
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <string.h>
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <dirent.h>
413
241db665477a fixes build on non-windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 412
diff changeset
35 #include <sys/stat.h>
241db665477a fixes build on non-windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 412
diff changeset
36 #include <sys/types.h>
578
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
37 #include <errno.h>
413
241db665477a fixes build on non-windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 412
diff changeset
38
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
39 #include <ucx/string.h>
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
40
413
241db665477a fixes build on non-windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 412
diff changeset
41 #ifndef _WIN32
241db665477a fixes build on non-windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 412
diff changeset
42 #include <unistd.h>
241db665477a fixes build on non-windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 412
diff changeset
43 #endif
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 #include "system.h"
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 void sys_freedirent(SysDirEnt *ent) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 free(ent->name);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 free(ent);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 #ifndef _WIN32
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 /* ---------- POSIX implementation ---------- */
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
55 void sys_init(void) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
56
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
57 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
58 void sys_uninit(void) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
59
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
60 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
61
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 SYS_DIR sys_opendir(const char *path) {
411
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
63 DIR *dir = opendir(path);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
64 if(!dir) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
65 return NULL;
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
66 }
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
67 SysDir *d = malloc(sizeof(SysDir));
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
68 d->dir = dir;
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
69 d->ent = NULL;
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
70 return d;
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 SysDirEnt* sys_readdir(SYS_DIR dir) {
411
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
74 if(dir->ent) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
75 free(dir->ent->name);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
76 free(dir->ent);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
77 dir->ent = NULL;
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
78 }
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
79 struct dirent *ent = readdir(dir->dir);
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 if(ent) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 SysDirEnt *e = malloc(sizeof(SysDirEnt));
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 e->name = strdup(ent->d_name);
411
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
83 dir->ent = e;
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 return e;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 return NULL;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 void sys_closedir(SYS_DIR dir) {
411
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
90 closedir(dir->dir);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
91 if(dir->ent) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
92 free(dir->ent->name);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
93 free(dir->ent);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
94 }
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
95 free(dir);
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 FILE* sys_fopen(const char *path, const char *mode) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 return fopen(path, mode);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 int sys_stat(const char *path, SYS_STAT *s) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 return stat(path, s);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105
574
c36eddf167a8 read link on LocalResource creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 448
diff changeset
106 int sys_lstat(const char *path, SYS_STAT *s) {
c36eddf167a8 read link on LocalResource creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 448
diff changeset
107 return lstat(path, s);
c36eddf167a8 read link on LocalResource creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 448
diff changeset
108 }
c36eddf167a8 read link on LocalResource creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 448
diff changeset
109
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
110 int sys_islink(const char *path) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
111 struct stat s;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
112 if(!lstat(path, &s)) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
113 return S_ISLNK(s.st_mode);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
114 }
612
66dc8b992d8d fix build on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 611
diff changeset
115 return 0;
66dc8b992d8d fix build on linux
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 611
diff changeset
116 }
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
117
411
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
118 int sys_rename(const char *oldpath, const char *newpath) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
119 return rename(oldpath, newpath);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
120 }
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
121
412
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
122 int sys_unlink(const char *path) {
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
123 return unlink(path);
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
124 }
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
125
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
126 int sys_mkdir(const char *path) {
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
127 return mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
413
241db665477a fixes build on non-windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 412
diff changeset
128 }
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
130 char* sys_readlink(const char *path, SYS_STAT *s) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
131 char *ret = NULL;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
132
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
133 off_t l_sz = s->st_size + 16;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
134 size_t lnksize = l_sz > 256 ? l_sz : 256;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
135 char *lnkbuf = malloc(lnksize);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
136
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
137 ssize_t len = 0;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
138 for(int i=0;i<4;i++) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
139 // we try to read the link at most 4 times
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
140 // only repeat if the buffer is too small
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
141 len = readlink(path, lnkbuf, lnksize);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
142 if(len < lnksize) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
143 ret = lnkbuf; // success
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
144 lnkbuf[len] = 0; // terminate buffer
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
145 break;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
146 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
147 lnksize *= 2; // retry with bigger buffer
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
148 lnkbuf = realloc(lnkbuf, lnksize);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
149 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
150
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
151 if(!ret) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
152 free(lnkbuf);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
153 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
154 return ret;
574
c36eddf167a8 read link on LocalResource creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 448
diff changeset
155 }
c36eddf167a8 read link on LocalResource creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 448
diff changeset
156
578
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
157 int sys_symlink(const char *target, const char *linkpath) {
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
158 int err = symlink(target, linkpath);
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
159 if(err && errno == EEXIST) {
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
160 if(unlink(linkpath)) {
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
161 return 1;
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
162 }
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
163 return sys_symlink(target, linkpath);
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
164 }
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
165 return err;
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
166 }
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
167
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 #else
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 /* ---------- Windows implementation ---------- */
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
171 #include <windows.h>
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
172 #include <winnls.h>
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
173 #include <shobjidl.h>
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
174 #include <objbase.h>
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
175 #include <objidl.h>
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
176
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
177 #include <direct.h>
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
178 #include <wchar.h>
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
179
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
180 void sys_init(void) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
181 HRESULT res = CoInitialize(NULL);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
182 if(res != S_OK) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
183 fprintf(stderr, "Error: CoInitialize failed\n");
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
184 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
185 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
186
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
187 void sys_uninit(void) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
188 CoUninitialize();
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
189 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
190
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 static wchar_t* path2winpath(const char *path, int dir, int *newlen) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 size_t len = strlen(path);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 size_t lenadd = dir ? 2 : 0;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195
447
a4a71ecfb9d6 fixes wrong buffer allocation on windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 425
diff changeset
196 wchar_t *wpath = calloc(len+lenadd+1, sizeof(wchar_t));
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 int wlen = MultiByteToWideChar(
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 CP_UTF8,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 0,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 path,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 len,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 wpath,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 len+1
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 );
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 if(newlen) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 *newlen = wlen;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 for(int i=0;i<wlen;i++) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 if(wpath[i] == L'/') {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 wpath[i] = L'\\';
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 if(dir) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 if(wpath[wlen-1] != L'\\') {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 wpath[wlen++] = L'\\';
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 wpath[wlen++] = L'*';
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 wpath[wlen] = 0;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 return wpath;
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
223 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
224
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
225 static char* winpath2multibyte(const wchar_t *wpath, size_t wlen) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
226 size_t maxlen = wlen * 4;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
227 char *ret = malloc(maxlen + 1);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
228 int ret_len = WideCharToMultiByte(
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
229 CP_UTF8,
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
230 0,
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
231 wpath,
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
232 wlen,
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
233 ret,
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
234 maxlen,
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
235 NULL,
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
236 NULL);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
237 ret[ret_len] = 0;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
238 return ret;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
239 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
240
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
241
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 SYS_DIR sys_opendir(const char *path) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 struct WinDir *dir = malloc(sizeof(struct WinDir));
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 wchar_t *dirpath = path2winpath(path, TRUE, NULL);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 if(!dirpath) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 fprintf(stderr, "Cannot convert path \"%s\" to UTF16\n", path);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 free(dir);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 return NULL;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 dir->first = 1;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 dir->handle = FindFirstFileW(dirpath, &dir->finddata);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 free(dirpath);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 if(dir->handle == INVALID_HANDLE_VALUE) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 free(dir);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 return NULL;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257 }
411
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
258 dir->ent = NULL;
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 return dir;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 SysDirEnt* sys_readdir(SYS_DIR dir) {
411
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
263 if(dir->ent) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
264 free(dir->ent->name);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
265 free(dir->ent);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
266 dir->ent = NULL;
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
267 }
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 if(dir->first) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 dir->first = 0;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 } else {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271 if(FindNextFileW(dir->handle, &dir->finddata) == 0) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 return NULL;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 size_t namelen = wcslen(dir->finddata.cFileName);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278 char *name = malloc((namelen+1)*4);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 int nlen = WideCharToMultiByte(
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 CP_UTF8,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 0,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 dir->finddata.cFileName,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 -1,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284 name,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 256,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 NULL,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 NULL);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 if(nlen > 0) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 name[nlen] = 0;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290 SysDirEnt *ent = malloc(sizeof(SysDirEnt));
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 ent->name = name;
411
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
292 dir->ent = ent;
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 return ent;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 } else {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 return NULL;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 void sys_closedir(SYS_DIR dir) {
411
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
300 if(dir->ent) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
301 free(dir->ent->name);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
302 free(dir->ent);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
303 }
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 FindClose(dir->handle);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 free(dir);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 FILE* sys_fopen(const char *path, const char *mode) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 wchar_t *fpath = path2winpath(path, FALSE, NULL);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 wchar_t *fmode = path2winpath(mode, FALSE, NULL);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 FILE *file = (fpath && fmode) ? _wfopen(fpath, fmode) : NULL;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 free(fpath);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314 free(fmode);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 return file;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318 int sys_stat(const char *path, SYS_STAT *s) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 wchar_t *fpath = path2winpath(path, FALSE, NULL);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 if(!fpath) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321 fprintf(stderr, "Cannot convert path \"%s\" to UTF16\n", path);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 return -1;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 int ret = _wstat64(fpath, s);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 free(fpath);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 return ret;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328
574
c36eddf167a8 read link on LocalResource creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 448
diff changeset
329 int sys_lstat(const char *path, SYS_STAT *s) {
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
330 return sys_stat(path, s); // unsupported on windows
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
331 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
332
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
333 int sys_islink(const char *path) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
334 // don't use symlinks on windows, because it is not really useful
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
335 // however, we interpret .lnk files as symlinks
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
336 int ret = 0;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
337
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
338 scstr_t path_s = scstr(path);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
339 if(scstrsuffix(path_s, SC(".lnk"))) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
340 // looks like a .lnk file
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
341 // check content
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
342 IShellLink *sl;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
343 HRESULT hres;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
344 hres = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLinkW, (LPVOID*)&sl);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
345 if(SUCCEEDED(hres)) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
346 IPersistFile *file;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
347 hres = sl->lpVtbl->QueryInterface(sl, &IID_IPersistFile, (void**)&file);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
348 if(!SUCCEEDED(hres)) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
349 sl->lpVtbl->Release(sl);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
350 return ret;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
351 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
352
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
353 int newlen = 0;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
354 wchar_t *wpath = path2winpath(path, 0, &newlen);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
355
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
356 hres = file->lpVtbl->Load(file, wpath, STGM_READ);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
357 if(SUCCEEDED(hres)) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
358 ret = 1;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
359 file->lpVtbl->Release(file);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
360 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
361 free(wpath);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
362
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
363 sl->lpVtbl->Release(sl);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
364 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
365 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
366 return ret;
574
c36eddf167a8 read link on LocalResource creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 448
diff changeset
367 }
c36eddf167a8 read link on LocalResource creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 448
diff changeset
368
411
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
369 int sys_rename(const char *oldpath, const char *newpath) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
370 wchar_t *o = path2winpath(oldpath, FALSE, NULL);
412
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
371 wchar_t *n = path2winpath(newpath, FALSE, NULL);
411
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
372 if(!o || !n) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
373 return -1;
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
374 }
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
375
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
376 struct __stat64 s;
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
377 if(!_wstat64(n, &s)) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
378 if(_wunlink(n)) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
379 fprintf(stderr, "sys_rename: cannot delete existing file: %ls\n", n);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
380 }
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
381 }
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
382
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
383 int ret = _wrename(o, n);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
384 free(o);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
385 free(n);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
386 return ret;
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
387 }
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
388
412
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
389 int sys_unlink(const char *path) {
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
390 wchar_t *wpath = path2winpath(path, FALSE, NULL);
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
391 if(!wpath) {
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
392 fprintf(stderr, "sys_unlink: cannot convert path\n");
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
393 return -1;
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
394 }
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
395 int ret = _wunlink(wpath);
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
396 free(wpath);
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
397 return ret;
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
398 }
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
399
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
400 int sys_mkdir(const char *path) {
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
401 wchar_t *wpath = path2winpath(path, FALSE, NULL);
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
402 if(!wpath) {
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
403 fprintf(stderr, "sys_mkdir: cannot convert path\n");
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
404 return -1;
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
405 }
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
406 int ret = _wmkdir(wpath);
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
407 free(wpath);
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
408 return ret;
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
409 }
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
410
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
411 char* sys_readlink(const char *path, SYS_STAT *s) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
412 char *ret_link = NULL;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
413
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
414 // create COM object for using the ShellLink interface
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
415 IShellLinkW *sl;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
416 HRESULT hres = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLinkW, (LPVOID*)&sl);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
417 if(!SUCCEEDED(hres)) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
418 return NULL;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
419 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
420
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
421 IPersistFile *file;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
422 hres = sl->lpVtbl->QueryInterface(sl, &IID_IPersistFile, (void**)&file);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
423 if(!SUCCEEDED(hres)) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
424 sl->lpVtbl->Release(sl);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
425 return NULL;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
426 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
427
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
428 // load .lnk file
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
429 int newlen = 0;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
430 wchar_t *wpath = path2winpath(path, 0, &newlen);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
431 hres = file->lpVtbl->Load(file, wpath, STGM_READ);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
432 if(SUCCEEDED(hres)) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
433 WCHAR link_path[MAX_PATH];
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
434 memset(link_path, 0, MAX_PATH);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
435
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
436 hres = sl->lpVtbl->Resolve(sl, 0, SLR_NO_UI);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
437 if(SUCCEEDED(hres)) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
438 hres = sl->lpVtbl->GetPath(sl, link_path, MAX_PATH, NULL, SLGP_SHORTPATH);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
439 if(SUCCEEDED(hres)) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
440 ret_link = winpath2multibyte(link_path, wcslen(link_path));
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
441 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
442 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
443 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
444 // cleanup
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
445 free(wpath);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
446 file->lpVtbl->Release(file);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
447 sl->lpVtbl->Release(sl);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
448
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
449 return ret_link;
574
c36eddf167a8 read link on LocalResource creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 448
diff changeset
450 }
c36eddf167a8 read link on LocalResource creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 448
diff changeset
451
578
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
452 int sys_symlink(const char *target, const char *linkpath) {
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
453 // convert relative target to absolut path
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
454 char *link_parent = util_parent_path(linkpath);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
455 char *target_unnormalized = util_concat_path(link_parent, target);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
456 char *target_normalized = util_path_normalize(target_unnormalized);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
457
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
458 free(link_parent);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
459 free(target_unnormalized);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
460
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
461 // convert to wchar_t*
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
462 int wtargetlen = 0;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
463 wchar_t *wtarget = path2winpath(target_normalized, FALSE, &wtargetlen);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
464 free(target_normalized);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
465 if(!wtarget) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
466 return 1;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
467 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
468
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
469 int wlinkpathlen = 0;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
470 wchar_t *wlinkpath = path2winpath(linkpath, FALSE, &wlinkpathlen);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
471 if(!wlinkpath) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
472 free(wtarget);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
473 return 1;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
474 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
475
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
476 int ret = 1;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
477
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
478 // create COM object for using the ShellLink interface
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
479 IShellLinkW *sl;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
480 HRESULT hres = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLinkW, (LPVOID*)&sl);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
481 if(SUCCEEDED(hres)) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
482 IPersistFile *file;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
483 hres = sl->lpVtbl->QueryInterface(sl, &IID_IPersistFile, (void**)&file);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
484 if(SUCCEEDED(hres)) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
485 // try to load the shortcut
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
486 file->lpVtbl->Load(file, wlinkpath, STGM_READ); // ignore error
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
487
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
488 // set path
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
489 hres = sl->lpVtbl->SetPath(sl, wtarget);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
490 if(SUCCEEDED(hres)) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
491 hres = file->lpVtbl->Save(file, wlinkpath, TRUE);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
492 if(SUCCEEDED(hres)) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
493 // successfully created/modified shortcut
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
494 ret = 0; // ok
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
495 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
496 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
497
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
498 file->lpVtbl->Release(file);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
499 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
500
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
501 sl->lpVtbl->Release(sl);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
502 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
503
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
504 free(wtarget);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
505 free(wlinkpath);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
506
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
507 return ret;
578
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
508 }
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
509
425
ae4d89c3477c fixes dav list return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 413
diff changeset
510 #endif

mercurial