@@ -27,18 +27,13 @@ static int container_populate_volume(char *src, char *dest)
27
27
struct stat st ;
28
28
29
29
fprintf (stdout , "populate volumes from %s to %s\n" , src , dest );
30
- /* FIXME: check if has data in volume, (except lost+found) */
31
30
32
31
if (stat (dest , & st ) == 0 ) {
33
32
if (!S_ISDIR (st .st_mode )) {
34
33
fprintf (stderr , "the _data in volume %s is not directory\n" , dest );
35
34
return -1 ;
36
35
}
37
-
38
- return 0 ;
39
- }
40
-
41
- if (errno != ENOENT ) {
36
+ } else if (errno != ENOENT ) {
42
37
perror ("access to volume failed\n" );
43
38
return -1 ;
44
39
}
@@ -53,23 +48,29 @@ static int container_populate_volume(char *src, char *dest)
53
48
54
49
const char * INIT_VOLUME_FILENAME = ".hyper_file_volume_data_do_not_create_on_your_own" ;
55
50
56
- static int container_check_file_volume (char * hyper_path , const char * * filename )
51
+ const char * LOST_AND_FOUND_DIR = "lost+found" ;
52
+
53
+ static int container_check_volume (char * hyper_path , const char * * filename , bool * newvolume )
57
54
{
58
55
struct dirent * * list ;
59
56
struct stat stbuf ;
60
57
int i , num , found = 0 ;
61
58
char path [PATH_MAX ];
62
59
63
60
* filename = NULL ;
61
+ * newvolume = false;
64
62
num = scandir (hyper_path , & list , NULL , NULL );
65
63
if (num < 0 ) {
66
- /* No data in the volume yet, treat as non-file- volume */
64
+ /* No data in the volume yet, treat as new volume */
67
65
if (errno == ENOENT ) {
68
- return 0 ;
66
+ * newvolume = true;
67
+ goto out ;
69
68
}
70
69
perror ("scan path failed" );
71
70
return -1 ;
72
- } else if (num != 3 ) {
71
+ } else if (num == 2 ) {
72
+ * newvolume = true;
73
+ } else if (num > 3 ) {
73
74
fprintf (stdout , "%s has %d files/dirs\n" , hyper_path , num - 2 );
74
75
for (i = 0 ; i < num ; i ++ ) {
75
76
free (list [i ]);
@@ -78,20 +79,36 @@ static int container_check_file_volume(char *hyper_path, const char **filename)
78
79
return 0 ;
79
80
}
80
81
81
- sprintf ( path , "%s/%s" , hyper_path , INIT_VOLUME_FILENAME );
82
+ /* num is either 2 or 3 */
82
83
for (i = 0 ; i < num ; i ++ ) {
83
- if (strcmp (list [i ]-> d_name , "." ) != 0 &&
84
- strcmp (list [i ]-> d_name , ".." ) != 0 &&
85
- strcmp (list [i ]-> d_name , INIT_VOLUME_FILENAME ) == 0 &&
86
- stat (path , & stbuf ) == 0 && S_ISREG (stbuf .st_mode )) {
87
- found ++ ;
84
+ if (strcmp (list [i ]-> d_name , "." ) == 0 ||
85
+ strcmp (list [i ]-> d_name , ".." ) == 0 ) {
86
+ free (list [i ]);
87
+ continue ;
88
+ }
89
+
90
+ if (strcmp (list [i ]-> d_name , INIT_VOLUME_FILENAME ) == 0 ) {
91
+ sprintf (path , "%s/%s" , hyper_path , INIT_VOLUME_FILENAME );
92
+ if (stat (path , & stbuf ) == 0 && S_ISREG (stbuf .st_mode ))
93
+ found ++ ;
94
+ } else if (strcmp (list [i ]-> d_name , LOST_AND_FOUND_DIR ) == 0 ) {
95
+ sprintf (path , "%s/%s" , hyper_path , LOST_AND_FOUND_DIR );
96
+ if (stat (path , & stbuf ) == 0 && S_ISDIR (stbuf .st_mode ) &&
97
+ hyper_empty_dir (path )) {
98
+ * newvolume = true;
99
+ }
88
100
}
89
101
free (list [i ]);
90
102
}
91
103
free (list );
92
104
93
- fprintf (stdout , "%s %s a file volume\n" , hyper_path , found > 0 ?"is" :"is not" );
94
- * filename = found > 0 ? INIT_VOLUME_FILENAME : NULL ;
105
+ out :
106
+ if (found > 0 ) {
107
+ * filename = INIT_VOLUME_FILENAME ;
108
+ fprintf (stdout , "%s is a file volume\n" , hyper_path );
109
+ } else if (* newvolume ) {
110
+ fprintf (stdout , "%s is a new volume\n" , hyper_path );
111
+ }
95
112
return 0 ;
96
113
}
97
114
@@ -107,6 +124,7 @@ static int container_setup_volume(struct hyper_pod *pod,
107
124
char mountpoint [512 ];
108
125
char * options = NULL ;
109
126
const char * filevolume = NULL ;
127
+ bool newvolume = false;
110
128
vol = & container -> vols [i ];
111
129
112
130
if (vol -> scsiaddr )
@@ -149,16 +167,18 @@ static int container_setup_volume(struct hyper_pod *pod,
149
167
sprintf (volume , "/%s/_data" , path );
150
168
}
151
169
152
- if (container_check_file_volume (volume , & filevolume ) < 0 )
170
+ if (container_check_volume (volume , & filevolume , & newvolume ) < 0 )
153
171
return -1 ;
154
172
155
173
if (filevolume == NULL ) {
156
174
if (hyper_mkdir_at ("." , mountpoint , sizeof (mountpoint )) < 0 ) {
157
175
perror ("create map dir failed" );
158
176
return -1 ;
159
177
}
178
+ fprintf (stdout , "docker vol %d initialize %d newvolume %d\n" ,
179
+ vol -> docker , container -> initialize , newvolume );
160
180
if (vol -> docker ) {
161
- if (container -> initialize &&
181
+ if (container -> initialize && newvolume &&
162
182
(container_populate_volume (mountpoint , volume ) < 0 )) {
163
183
fprintf (stderr , "fail to populate volume %s\n" , mountpoint );
164
184
return -1 ;
0 commit comments