minimal copy
[mizunara.git] / mizucp / main.h
1 /*
2  * Copyright 2021 Olaf Wintermann
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a 
5  * copy of this software and associated documentation files (the "Software"), 
6  * to deal in the Software without restriction, including without limitation 
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
8  * and/or sell copies of the Software, and to permit persons to whom the 
9  * Software is furnished to do so, subject to the following conditions:
10  * 
11  * The above copyright notice and this permission notice shall be included in 
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
20  * DEALINGS IN THE SOFTWARE.
21  */
22
23 #ifndef MAIN_H
24 #define MAIN_H
25
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <sys/stat.h>
29
30 #include <ucx/string.h>
31
32 #include <pthread.h>
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 #define MZCP_ENV_HOME "HOME"
39 #define MZCP_CFG_BASE ".config"
40 #define MZCP_CFG_DIR  ".config/mizunara"
41 #define MZCP_COPY_DIR "copy"
42     
43 #define CLIENT_MSG_BUFSIZE 512
44     
45 #define MAX_COPY_THREADS 32
46 #define MZ_COPY_BUFSIZE 4 * 1024 * 1024
47     
48 typedef char CPBool;
49
50 typedef struct SrcFile SrcFile;
51 typedef struct MZLock  MZLock;
52 typedef struct MZQueue MZQueue;
53
54 typedef struct {
55     char   *from;
56     char   *to;
57     size_t num_threads;
58     CPBool url;
59     CPBool pause;
60     CPBool printsocket;
61     struct stat root_stat;
62 } CPSettings;
63
64 struct MZLock {
65     pthread_mutex_t mutex;
66     pthread_cond_t cond;
67 };
68
69 struct SrcFile {
70     /*
71      * src file path relative to CPSettings.from
72      */
73     char *path;
74     
75     /*
76      * is the file a directory
77      */
78     CPBool isdir;
79     
80     /*
81      * file successfully copied
82      */
83     CPBool finished;
84     
85     /*
86      * file mode
87      */
88     mode_t mode;
89     
90     /*
91      * processing this file depends on another file (directory)
92      */
93     SrcFile *depends_on;
94     
95     /*
96      * lock used for waiting, until this resource is successfully copied
97      */
98     MZLock *lock;
99 };
100
101 struct MZQueue {
102     SrcFile *file;
103     MZQueue *next;
104 };
105
106 const char* mzcp_get_cfgdir(void);
107 const char* mzcp_get_copydir(void);
108
109 int mzcp_copy(CPSettings *settings);
110
111 int mzcp_start_scan(CPSettings *settings);
112 void* scan_run(void *data);
113
114 int enqueue_file(SrcFile *file);
115
116 int mzcp_start_copy_threads(CPSettings *settings);
117 void* copy_run(void *data);
118
119 int mz_copy_dir(CPSettings *settings, SrcFile *file, const char *from, const char *to);
120 int mz_copy_file(CPSettings *settings, SrcFile *file, const char *from, const char *to, char *buffer, size_t bufsize);
121
122     
123 #ifdef __cplusplus
124 }
125 #endif
126
127 #endif /* MAIN_H */
128