@@ -27,18 +27,13 @@ static int container_populate_volume(char *src, char *dest)
2727 struct stat st ;
2828
2929 fprintf (stdout , "populate volumes from %s to %s\n" , src , dest );
30- /* FIXME: check if has data in volume, (except lost+found) */
3130
3231 if (stat (dest , & st ) == 0 ) {
3332 if (!S_ISDIR (st .st_mode )) {
3433 fprintf (stderr , "the _data in volume %s is not directory\n" , dest );
3534 return -1 ;
3635 }
37-
38- return 0 ;
39- }
40-
41- if (errno != ENOENT ) {
36+ } else if (errno != ENOENT ) {
4237 perror ("access to volume failed\n" );
4338 return -1 ;
4439 }
@@ -53,23 +48,29 @@ static int container_populate_volume(char *src, char *dest)
5348
5449const char * INIT_VOLUME_FILENAME = ".hyper_file_volume_data_do_not_create_on_your_own" ;
5550
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 )
5754{
5855 struct dirent * * list ;
5956 struct stat stbuf ;
6057 int i , num , found = 0 ;
6158 char path [PATH_MAX ];
6259
6360 * filename = NULL ;
61+ * newvolume = false;
6462 num = scandir (hyper_path , & list , NULL , NULL );
6563 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 */
6765 if (errno == ENOENT ) {
68- return 0 ;
66+ * newvolume = true;
67+ goto out ;
6968 }
7069 perror ("scan path failed" );
7170 return -1 ;
72- } else if (num != 3 ) {
71+ } else if (num == 2 ) {
72+ * newvolume = true;
73+ } else if (num > 3 ) {
7374 fprintf (stdout , "%s has %d files/dirs\n" , hyper_path , num - 2 );
7475 for (i = 0 ; i < num ; i ++ ) {
7576 free (list [i ]);
@@ -78,20 +79,36 @@ static int container_check_file_volume(char *hyper_path, const char **filename)
7879 return 0 ;
7980 }
8081
81- sprintf ( path , "%s/%s" , hyper_path , INIT_VOLUME_FILENAME );
82+ /* num is either 2 or 3 */
8283 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+ }
88100 }
89101 free (list [i ]);
90102 }
91103 free (list );
92104
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+ }
95112 return 0 ;
96113}
97114
@@ -107,6 +124,7 @@ static int container_setup_volume(struct hyper_pod *pod,
107124 char mountpoint [512 ];
108125 char * options = NULL ;
109126 const char * filevolume = NULL ;
127+ bool newvolume = false;
110128 vol = & container -> vols [i ];
111129
112130 if (vol -> scsiaddr )
@@ -149,16 +167,18 @@ static int container_setup_volume(struct hyper_pod *pod,
149167 sprintf (volume , "/%s/_data" , path );
150168 }
151169
152- if (container_check_file_volume (volume , & filevolume ) < 0 )
170+ if (container_check_volume (volume , & filevolume , & newvolume ) < 0 )
153171 return -1 ;
154172
155173 if (filevolume == NULL ) {
156174 if (hyper_mkdir_at ("." , mountpoint , sizeof (mountpoint )) < 0 ) {
157175 perror ("create map dir failed" );
158176 return -1 ;
159177 }
178+ fprintf (stdout , "docker vol %d initialize %d newvolume %d\n" ,
179+ vol -> docker , container -> initialize , newvolume );
160180 if (vol -> docker ) {
161- if (container -> initialize &&
181+ if (container -> initialize && newvolume &&
162182 (container_populate_volume (mountpoint , volume ) < 0 )) {
163183 fprintf (stderr , "fail to populate volume %s\n" , mountpoint );
164184 return -1 ;
0 commit comments