summaryrefslogtreecommitdiff
path: root/src/path.h
blob: 1fa2ebfd1500d65bb5abd01e76e3d20c2ac6e857 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#ifndef _PATH_H_
#define _PATH_H_

#include <limits.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/stat.h>

#include <list.h>
#include <atomic.h>
#include <ssh.h>

struct path {
	struct list_head	list;	/* mscp->path_list */

	char	path[PATH_MAX];		/* file path */
	size_t	size;			/* size of file on this path */
	mode_t	mode;			/* permission */

	char	dst_path[PATH_MAX];	/* copy dst path */

	int	state;
	lock	lock;
	refcnt	refcnt;
};
#define FILE_STATE_INIT         0
#define FILE_STATE_OPENED       1
#define FILE_STATE_DONE         2

struct chunk {
	struct list_head	list;	/* mscp->chunk_list */

	struct path *p;
	size_t	off;	/* offset of this chunk on the file on path p */
	size_t	len;	/* length of this chunk */
	size_t	done;	/* copied bytes for this chunk by a thread */
};



/* recursivly walk through src_path and fill path_list for each file */
int walk_src_path(sftp_session src_sftp, const char *src_path,
		  struct list_head *path_list);

/* fill path->dst_path for all files */
int resolve_dst_path(const char *src_path, const char *dst_path,
		     struct list_head *path_list,
		     bool src_path_is_dir, bool dst_path_is_dir);

/* resolve chunks from files in the path_list */
int resolve_chunk(struct list_head *path_list, struct list_head *chunk_list,
		  int nr_conn, int min_chunk_sz, int max_chunk_sz);

/* prepare dst file. mkdir -p and touch dst file */
int prepare_dst_path(struct path *p, sftp_session dst_sftp);

/* copy a chunk. either src_sftp or dst_sftp is not null, and another is null */
int copy_chunk(struct chunk *c, sftp_session src_sftp, sftp_session dst_sftp,
	       int nr_ahead, int buf_sz, size_t *counter);

/* just print contents. just for debugging */
void path_dump(struct list_head *path_list);
void chunk_dump(struct list_head *chunk_list);




/* wrap DIR/dirent and sftp_dir/sftp_attribute. not thread safe */
struct mscp_dir {
        DIR *l;
        sftp_dir r;
        sftp_session sftp;
};
typedef struct mscp_dir mdir;

struct mscp_dirent {
        struct dirent *l;
        sftp_attributes r;
};
typedef struct mscp_dirent mdirent;

#define mdirent_name(e) ((e->l) ? e->l->d_name : e->r->name)
#define mdirent_is_dir(e) ((e->l) ?                                     \
                           (e->l->d_type == DT_DIR) :                   \
                           (e->r->type == SSH_FILEXFER_TYPE_DIRECTORY))
#define mdirent_is_null(e) (e->l == NULL && e->r == NULL)

static mdir *mscp_opendir(const char *path, sftp_session sftp)
{
        mdir *d;

        if (!(d = malloc(sizeof(*d))))
                return NULL;
        memset(d, 0, sizeof(*d));

        d->sftp = sftp;

        if (sftp) {
                d->r = sftp_opendir(sftp, path);
                if (!d->r) {
                        pr_err("sftp_opendir: %s: %s\n", path, sftp_get_ssh_error(sftp));
                        free(d);
                        return NULL;
                }
        } else {
                d->l = opendir(path);
                if (!d->l) {
                        pr_err("opendir: %s: %s\n", path, strerrno());
                        free(d);
                        return NULL;
                }
        }
        return d;
}

static int mscp_closedir(mdir *d)
{
        int ret;
        if (d->r)
                ret = sftp_closedir(d->r);
        else
                ret = closedir(d->l);
        free(d);
        return ret;
}

static mdirent *mscp_readdir(mdir *d)
{
        static mdirent e;

        memset(&e, 0, sizeof(e));
        if (d->r)
                e.r = sftp_readdir(d->sftp, d->r);
        else
                e.l = readdir(d->l);
        return &e;
}

/* wrap retriving error */
static const char *mscp_strerror(sftp_session sftp)
{
	if (sftp)
		return sftp_get_ssh_error(sftp);
	return strerrno();
}

/* warp stat/sftp_stat */
struct mscp_stat {
        struct stat l;
        sftp_attributes r;
};
typedef struct mscp_stat mstat;

static int mscp_stat(const char *path, mstat *s, sftp_session sftp)
{
        memset(s, 0, sizeof(*s));

        if (sftp) {
                s->r = sftp_stat(sftp, path);
                if (!s->r)
                        return -1;
        } else {
                if (stat(path, &s->l) < 0)
                        return -1;
        }

        return 0;
}

static int mscp_stat_check_err_noent(sftp_session sftp)
{
	if (sftp) {
		if (sftp_get_error(sftp) == SSH_FX_NO_SUCH_PATH ||
		    sftp_get_error(sftp) == SSH_FX_NO_SUCH_FILE)
			return 0;
	} else {
		if (errno == ENOENT)
			return 0;
	}
	return -1;
}

static void mscp_stat_free(mstat s) {
        if (s.r)
                sftp_attributes_free(s.r);
}

#define mstat_size(s) ((s.r) ? s.r->size : s.l.st_size)
#define mstat_mode(s) ((s.r) ?                                  \
                       s.r->permissions :                       \
                       s.l.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO))
#define mstat_is_regular(s) ((s.r) ?                                    \
                             (s.r->type == SSH_FILEXFER_TYPE_REGULAR) : \
                             S_ISREG(s.l.st_mode))
#define mstat_is_dir(s) ((s.r) ?                        \
                         (s.r->type == SSH_FILEXFER_TYPE_DIRECTORY) :   \
                         S_ISDIR(s.l.st_mode))

/* wrap mkdir */
static int mscp_mkdir(const char *path, mode_t mode, sftp_session sftp)
{
	int ret;

	if (sftp) {
		ret = sftp_mkdir(sftp, path, mode);
		if (ret < 0 &&
		    sftp_get_error(sftp) != SSH_FX_FILE_ALREADY_EXISTS) {
			pr_err("failed to create %s: %s\n",
			       path, sftp_get_ssh_error(sftp));
			return -1;
		}
	} else {
		if (mkdir(path, mode) == -1 && errno != EEXIST) {
			pr_err("failed to create %s: %s\n",
			       path, strerrno());
			return -1;
		}
	}

	return 0;
}

/* wrap open/sftp_open */
struct mscp_file_handle {
	int fd;
	sftp_file sf;
};
typedef struct mscp_file_handle mfh;

static mfh mscp_open(const char *path, mode_t mode, int flags, size_t off,
		      sftp_session sftp)
{
	mfh h;

	h.fd = -1;
	h.sf = NULL;

	if (sftp) {
		h.sf = sftp_open(sftp, path, flags, mode);
		if (!h.sf) {
			pr_err("sftp_open %s: %s\n", path, sftp_get_ssh_error(sftp));
			return h;
		}

		if (sftp_seek64(h.sf, off) < 0) {
			pr_err("sftp_seek64 %s: %s\n", path, sftp_get_ssh_error(sftp));
			sftp_close(h.sf);
			h.sf = NULL;
			return h;
		}
	} else {
		h.fd = open(path, flags, mode);
		if (h.fd < 0) {
			pr_err("open %s: %s\n", path, strerrno());
			return h;
		}
		if (lseek(h.fd, off, SEEK_SET) < 0) {
			pr_err("lseek %s: %s\n", path, strerrno());
			close(h.fd);
			h.fd = -1;
			return h;
		}
	}

	return h;
}

#define mscp_open_is_failed(h) (h.fd < 0 && h.sf == NULL)

static void mscp_close(mfh h)
{
	if (h.sf)
		sftp_close(h.sf);
	if (h.fd > 0)
		close(h.fd);
	h.sf = NULL;
	h.fd = -1;
}

/* wrap chmod/sftp_chmod */

static int mscp_chmod(const char *path, mode_t mode, sftp_session sftp)
{
	if (sftp) {
		if (sftp_chmod(sftp, path, mode) < 0)  {
			pr_err("sftp_chmod %s: %s\n", path, sftp_get_ssh_error(sftp));
			return -1;
		}
	} else {
		if (chmod(path, mode) < 0) {
			pr_err("chmod %s: %s\n", path, strerrno());
			return -1;
		}
	}

	return 0;
}

#endif /* _PATH_H_ */