dav/system.c

Sat, 20 Oct 2018 11:03:38 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 20 Oct 2018 11:03:38 +0200
branch
v1.2
changeset 484
9435cb1ddf76
parent 454
fefddd41387e
child 574
c36eddf167a8
permissions
-rw-r--r--

fixes wrong integer type

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 *
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2018 Olaf Wintermann. All rights reserved.
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
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <dirent.h>
413
241db665477a fixes build on non-windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 412
diff changeset
33 #include <sys/stat.h>
241db665477a fixes build on non-windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 412
diff changeset
34 #include <sys/types.h>
241db665477a fixes build on non-windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 412
diff changeset
35
241db665477a fixes build on non-windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 412
diff changeset
36 #ifndef _WIN32
241db665477a fixes build on non-windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 412
diff changeset
37 #include <unistd.h>
241db665477a fixes build on non-windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 412
diff changeset
38 #endif
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #include "system.h"
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 void sys_freedirent(SysDirEnt *ent) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 free(ent->name);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 free(ent);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 }
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 #ifndef _WIN32
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 /* ---------- POSIX implementation ---------- */
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 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
51 DIR *dir = opendir(path);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
52 if(!dir) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
53 return NULL;
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
54 }
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
55 SysDir *d = malloc(sizeof(SysDir));
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
56 d->dir = dir;
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
57 d->ent = NULL;
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
58 return d;
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 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
62 if(dir->ent) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
63 free(dir->ent->name);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
64 free(dir->ent);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
65 dir->ent = 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 struct dirent *ent = readdir(dir->dir);
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 if(ent) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 SysDirEnt *e = malloc(sizeof(SysDirEnt));
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 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
71 dir->ent = e;
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 return e;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 return NULL;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 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
78 closedir(dir->dir);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
79 if(dir->ent) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
80 free(dir->ent->name);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
81 free(dir->ent);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
82 }
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
83 free(dir);
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 }
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 FILE* sys_fopen(const char *path, const char *mode) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 return fopen(path, mode);
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
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 int sys_stat(const char *path, SYS_STAT *s) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 return stat(path, s);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93
411
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
94 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
95 return rename(oldpath, newpath);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
96 }
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
97
412
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
98 int sys_unlink(const char *path) {
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
99 return unlink(path);
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
100 }
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
101
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
102 int sys_mkdir(const char *path) {
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
103 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
104 }
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 #else
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 /* ---------- Windows implementation ---------- */
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 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
110 size_t len = strlen(path);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 size_t lenadd = dir ? 2 : 0;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113
453
68a786fa4695 fixes wrong buffer allocation on windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 425
diff changeset
114 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
115 int wlen = MultiByteToWideChar(
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 CP_UTF8,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 0,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 path,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 len,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 wpath,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 len+1
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 );
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 if(newlen) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 *newlen = wlen;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 for(int i=0;i<wlen;i++) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 if(wpath[i] == L'/') {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 wpath[i] = L'\\';
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 if(dir) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 if(wpath[wlen-1] != L'\\') {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 wpath[wlen++] = L'\\';
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 wpath[wlen++] = L'*';
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 wpath[wlen] = 0;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 return wpath;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 SYS_DIR sys_opendir(const char *path) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 struct WinDir *dir = malloc(sizeof(struct WinDir));
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 wchar_t *dirpath = path2winpath(path, TRUE, NULL);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 if(!dirpath) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 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
148 free(dir);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 return NULL;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 dir->first = 1;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 dir->handle = FindFirstFileW(dirpath, &dir->finddata);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 free(dirpath);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 if(dir->handle == INVALID_HANDLE_VALUE) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 free(dir);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 return NULL;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 }
411
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
158 dir->ent = NULL;
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 return dir;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 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
163 if(dir->ent) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
164 free(dir->ent->name);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
165 free(dir->ent);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
166 dir->ent = NULL;
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
167 }
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 if(dir->first) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 dir->first = 0;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 } else {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 if(FindNextFileW(dir->handle, &dir->finddata) == 0) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 return NULL;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 size_t namelen = wcslen(dir->finddata.cFileName);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 char *name = malloc((namelen+1)*4);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 int nlen = WideCharToMultiByte(
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 CP_UTF8,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 0,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 dir->finddata.cFileName,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 -1,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 name,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 256,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 NULL,
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 NULL);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 if(nlen > 0) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 name[nlen] = 0;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 SysDirEnt *ent = malloc(sizeof(SysDirEnt));
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 ent->name = name;
411
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
192 dir->ent = ent;
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 return ent;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 } else {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 return NULL;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 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
200 if(dir->ent) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
201 free(dir->ent->name);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
202 free(dir->ent);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
203 }
410
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 FindClose(dir->handle);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 free(dir);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 }
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 FILE* sys_fopen(const char *path, const char *mode) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 wchar_t *fpath = path2winpath(path, FALSE, NULL);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 wchar_t *fmode = path2winpath(mode, FALSE, NULL);
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 FILE *file = (fpath && fmode) ? _wfopen(fpath, fmode) : NULL;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 free(fpath);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 free(fmode);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 return file;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 }
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 int sys_stat(const char *path, SYS_STAT *s) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 wchar_t *fpath = path2winpath(path, FALSE, NULL);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 if(!fpath) {
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 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
222 return -1;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 int ret = _wstat64(fpath, s);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 free(fpath);
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 return ret;
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 }
0b9bea2d7283 adds file io abstraction functions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228
411
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
229 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
230 wchar_t *o = path2winpath(oldpath, FALSE, NULL);
412
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
231 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
232 if(!o || !n) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
233 return -1;
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
234 }
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
235
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
236 struct __stat64 s;
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
237 if(!_wstat64(n, &s)) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
238 if(_wunlink(n)) {
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
239 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
240 }
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
241 }
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
242
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
243 int ret = _wrename(o, n);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
244 free(o);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
245 free(n);
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
246 return ret;
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
247 }
a182e503617b using new io abstraction in dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 410
diff changeset
248
412
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
249 int sys_unlink(const char *path) {
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
250 wchar_t *wpath = path2winpath(path, FALSE, NULL);
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
251 if(!wpath) {
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
252 fprintf(stderr, "sys_unlink: cannot convert path\n");
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
253 return -1;
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
254 }
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
255 int ret = _wunlink(wpath);
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
256 free(wpath);
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
257 return ret;
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
258 }
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
259
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
260 int sys_mkdir(const char *path) {
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
261 wchar_t *wpath = path2winpath(path, FALSE, NULL);
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
262 if(!wpath) {
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
263 fprintf(stderr, "sys_mkdir: cannot convert path\n");
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
264 return -1;
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
265 }
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
266 int ret = _wmkdir(wpath);
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
267 free(wpath);
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
268 return ret;
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
269 }
dc74f736aea1 adds more fs abstraction
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 411
diff changeset
270
425
ae4d89c3477c fixes dav list return value
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 413
diff changeset
271 #endif

mercurial