@@ -4,24 +4,18 @@ import (
44 "errors"
55 "fmt"
66 "log"
7- "os"
8- "path/filepath"
97 "time"
108
119 "github.com/Arm-Debug/remoteproc-simulator/internal/dirwatcher"
1210)
1311
1412type Remoteproc struct {
15- rootDir string
16- index uint
17- name string
18-
19- instanceDir string
20- firmwareDir string
21- watcher * dirwatcher.DirWatcher
22- state state
23- firmware string
24- stopChan chan struct {}
13+ name string
14+ fs * FileSystemManager
15+ watcher * dirwatcher.DirWatcher
16+ state state
17+ firmware string
18+ stopChan chan struct {}
2519}
2620
2721const (
@@ -59,74 +53,73 @@ func NewRemoteproc(config Config) (*Remoteproc, error) {
5953 }
6054
6155 r := & Remoteproc {
62- rootDir : config .RootDir ,
63- index : config .Index ,
6456 name : config .Name ,
57+ fs : NewFileSystemManager (config .RootDir , config .Index ),
6558 firmware : initialFirmware ,
6659 state : initialState ,
6760 }
6861
69- instanceDirName := fmt . Sprintf ( "remoteproc%d" , r . index )
70- r . instanceDir = filepath . Join ( r . rootDir , "sys" , "class" , "remoteproc" , instanceDirName )
71- r . firmwareDir = filepath . Join ( r . rootDir , "lib" , "firmware" )
72-
73- return r , r . start ()
62+ err := r . start ( )
63+ if err != nil {
64+ r . Close ( )
65+ }
66+ return r , err
7467}
7568
7669func (r * Remoteproc ) start () error {
77- files := map [string ]string {
78- stateFileName : r .state .String (),
79- firmwareFileName : r .firmware ,
80- nameFileName : r .name ,
81- }
82- if err := r .bootstrapInstanceDir (files ); err != nil {
83- return fmt .Errorf ("failed to bootstrap sysfs: %w" , err )
70+ if err := r .bootstrapDirectoryStructure (); err != nil {
71+ return fmt .Errorf ("failed to bootstrap directory structure: %w" , err )
8472 }
8573
86- if err := r .bootstrapFirmwareDir (); err != nil {
87- return fmt .Errorf ("failed to bootstrap firmware dir: %w" , err )
88- }
89-
90- watcher , err := dirwatcher .New (r .instanceDir )
74+ watcher , err := dirwatcher .New (r .fs .InstanceDir ())
9175 if err != nil {
9276 return fmt .Errorf ("failed to setup directory watcher: %w" , err )
9377 }
94-
9578 r .watcher = watcher
9679
9780 r .stopChan = make (chan struct {})
9881 go r .loop ()
9982
100- log .Printf ("Remoteproc initialized at %s" , r .instanceDir )
83+ log .Printf ("Remoteproc initialized at %s" , r .fs . InstanceDir () )
10184 return nil
10285}
10386
10487func (r * Remoteproc ) Close () error {
105- var err error
88+ var watcherErr error
10689 if r .watcher != nil {
107- err = r .watcher .Close ()
90+ watcherErr = r .watcher .Close ()
10891 }
92+
10993 if r .stopChan != nil {
11094 close (r .stopChan )
11195 }
112- return err
96+
97+ var fsErr error
98+ if r .fs != nil {
99+ fsErr = r .fs .Cleanup ()
100+ }
101+
102+ return errors .Join (watcherErr , fsErr )
113103}
114104
115- func (r * Remoteproc ) bootstrapInstanceDir (files map [string ]string ) error {
116- err := os .MkdirAll (r .instanceDir , 0755 )
117- if err != nil {
105+ func (r * Remoteproc ) bootstrapDirectoryStructure () error {
106+ if err := r .fs .BootstrapDirectories (); err != nil {
118107 return err
119108 }
109+
110+ files := map [string ]string {
111+ stateFileName : r .state .String (),
112+ firmwareFileName : r .firmware ,
113+ nameFileName : r .name ,
114+ }
115+
120116 for filename , content := range files {
121- if err := r .writeFile (filename , content ); err != nil {
117+ if err := r .fs . WriteInstanceFile (filename , content ); err != nil {
122118 return err
123119 }
124120 }
125- return nil
126- }
127121
128- func (r * Remoteproc ) bootstrapFirmwareDir () error {
129- return os .MkdirAll (r .firmwareDir , 0755 )
122+ return nil
130123}
131124
132125func (r * Remoteproc ) loop () {
@@ -170,8 +163,8 @@ func (r *Remoteproc) handleStateChange(value string) {
170163 return
171164 }
172165
173- if ! fileExists ( filepath . Join (r .firmwareDir , r . firmware ) ) {
174- log .Printf ("Cannot start: firmware file not found in %s directory" , r .firmwareDir )
166+ if ! r . fs . FirmwareExists (r .firmware ) {
167+ log .Printf ("Cannot start: firmware file not found in %s directory" , r .fs . FirmwareDir () )
175168 r .setState (r .state )
176169 return
177170 }
@@ -205,37 +198,23 @@ func (r *Remoteproc) handleStateChange(value string) {
205198
206199func (r * Remoteproc ) setState (state state ) {
207200 r .state = state
208- r .writeFile (stateFileName , state .String ())
201+ r .fs . WriteInstanceFile (stateFileName , state .String ())
209202}
210203
211204func (r * Remoteproc ) handleFirmwareChange (value string ) {
212205 if r .state == StateRunning {
213206 log .Printf ("Cannot change firmware while Remoteproc is %s" , r .state )
214- r .writeFile (firmwareFileName , r .firmware )
207+ r .fs . WriteInstanceFile (firmwareFileName , r .firmware )
215208 return
216209 }
217210 r .firmware = value
218211 log .Printf ("Firmware set to %s" , value )
219212}
220213
221- func (r * Remoteproc ) writeFile (filename , content string ) error {
222- path := filepath .Join (r .instanceDir , filename )
223- err := os .WriteFile (path , []byte (content ), 0644 )
224- if err != nil {
225- return fmt .Errorf ("failed to write %s: %v" , filename , err )
226- }
227- return nil
228- }
229-
230214func isStateSelfInflicted (value string ) bool {
231215 switch value {
232216 case StateRunning .String (), StateCrashed .String (), StateOffline .String ():
233217 return true
234218 }
235219 return false
236220}
237-
238- func fileExists (filename string ) bool {
239- _ , err := os .Stat (filename )
240- return ! os .IsNotExist (err )
241- }
0 commit comments