@@ -74,16 +74,43 @@ int storage_init(const char *storage_root, bool read_only, bool use_partitions)
7474 return 0 ;
7575}
7676
77- struct rmtfd * storage_open (unsigned node , const char * path )
77+ static int fd_open (struct rmtfd * rmtfd , const char * fspath , const struct partition * part )
78+ {
79+ int saved_errno ;
80+ int ret ;
81+ int fd ;
82+
83+ if (!storage_read_only ) {
84+ fd = open (fspath , O_RDWR );
85+ if (fd < 0 ) {
86+ saved_errno = errno ;
87+ fprintf (stderr , "[storage] failed to open '%s' (requested '%s'): %s\n" ,
88+ fspath , part -> path , strerror (saved_errno ));
89+ return saved_errno ;
90+ }
91+ rmtfd -> fd = fd ;
92+ rmtfd -> shadow_len = 0 ;
93+ } else {
94+ ret = storage_populate_shadow_buf (rmtfd , fspath );
95+ if (ret < 0 ) {
96+ saved_errno = errno ;
97+ fprintf (stderr , "[storage] failed to open '%s' (requested '%s'): %s\n" ,
98+ fspath , part -> path , strerror (saved_errno ));
99+ return saved_errno ;
100+ }
101+ }
102+
103+ return 0 ;
104+ }
105+
106+ struct rmtfd * storage_open (unsigned node , const char * path , const char * slot_suffix )
78107{
79108 char * fspath ;
80109 const struct partition * part ;
81110 struct rmtfd * rmtfd = NULL ;
82111 const char * file ;
83112 size_t pathlen ;
84- int saved_errno ;
85113 int ret ;
86- int fd ;
87114 int i ;
88115
89116 for (part = partition_table ; part -> path ; part ++ ) {
@@ -119,29 +146,19 @@ struct rmtfd *storage_open(unsigned node, const char *path)
119146 else
120147 file = part -> actual ;
121148
122- pathlen = strlen (storage_dir ) + strlen (file ) + 2 ;
149+ pathlen = strlen (storage_dir ) + strlen (file ) + 2 + strnlen ( slot_suffix , SLOT_SUFFIX_LEN ) ;
123150 fspath = alloca (pathlen );
124151 snprintf (fspath , pathlen , "%s/%s" , storage_dir , file );
125- if (!storage_read_only ) {
126- fd = open (fspath , O_RDWR );
127- if (fd < 0 ) {
128- saved_errno = errno ;
129- fprintf (stderr , "[storage] failed to open '%s' (requested '%s'): %s\n" ,
130- fspath , part -> path , strerror (saved_errno ));
131- errno = saved_errno ;
152+ ret = fd_open (rmtfd , fspath , part );
153+ if (ret ) {
154+ /* Try again with the slot suffix before giving up */
155+ if (!slot_suffix )
132156 return NULL ;
133- }
134- rmtfd -> fd = fd ;
135- rmtfd -> shadow_len = 0 ;
136- } else {
137- ret = storage_populate_shadow_buf (rmtfd , fspath );
138- if (ret < 0 ) {
139- saved_errno = errno ;
140- fprintf (stderr , "[storage] failed to open '%s' (requested '%s'): %s\n" ,
141- fspath , part -> path , strerror (saved_errno ));
142- errno = saved_errno ;
157+
158+ snprintf (fspath , pathlen , "%s/%s%s" , storage_dir , file , slot_suffix );
159+ ret = fd_open (rmtfd , fspath , part );
160+ if (ret )
143161 return NULL ;
144- }
145162 }
146163
147164 rmtfd -> node = node ;
0 commit comments