1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
use path_absolutize;

use curl::easy::{Easy, Form};
use curl::Error;
use path_absolutize::*;

use cpclib_sna as sna;
use cpclib_disc as disc;

use crate::disc::amsdos::AmsdosFileType;

use std::fs;

use std::path::Path;

use custom_error::custom_error;

use crate::sna::{Snapshot, SnapshotVersion};

custom_error! {#[allow(missing_docs)] pub XferError
    ConnectionError{source: Error} = "There is a connection error with the Cpc Wifi.",
    ConnectionError2{source: reqwest::Error} = "There is a connection error with the Cpc Wifi.",

    CdError{from: String, to: String} = @ {
        format!(
            "Unable to move in {}. Current working directory is {}.",
            from, to)
    },
    InternalError{reason: String} = @ {
        format!("Internal error: {}", reason)
    }
}

#[derive(Debug)]
/// File in a list of files generated by the m4
pub struct M4File {
    /// File name
    fname: String,
    /// TODO search what it is
    unknown: String,
    /// File size
    size: String,
}

impl M4File {
    /// Return the filname
    pub fn fname(&self) -> &str {
        &self.fname
    }
}

impl From<&str> for M4File {
    fn from(line: &str) -> Self {
        let mut splitted = line.split(',');
        Self {
            fname: splitted.next().unwrap().into(),
            unknown: splitted.next().unwrap().into(),
            size: splitted.next().unwrap().into(),
        }
    }
}

/// List of files in the M4
#[derive(Debug)]
pub struct M4FilesList {
    /// Directory of the list
    cwd: String,
    /// List of files
    files: Vec<M4File>,
}

impl From<&str> for M4FilesList {
    fn from(buffer: &str) -> Self {
        let mut iter = buffer.lines();
        let mut path = iter.next().unwrap();
        if path == "//" {
            path = "/";
        }
        let files = iter.map(|s| s.into()).collect::<Vec<M4File>>();
        Self {
            cwd: path.into(),
            files,
        }
    }
}

#[allow(missing_docs)]
impl M4FilesList {
    pub fn cwd(&self) -> &String {
        &self.cwd
    }

    pub fn nb_files(&self) -> usize {
        self.files.len()
    }

    // TODO do not give acces to the vector
    pub fn files(&self) -> &Vec<M4File> {
        &self.files
    }
}

/// Bridget the the CPC Wifi card
#[derive(Debug)]
pub struct CpcXfer {
    /// CPC Wifi hostname
    hostname: String,
}

#[allow(missing_docs)]
impl CpcXfer {
    /// Create the CpcXfer given an address
    pub fn new<S: AsRef<str>>(hostname: S) -> Self {
        Self {
            hostname: String::from(hostname.as_ref()),
        }
    }

    pub fn hostname(&self) -> &str {
        &self.hostname
    }

    /// Return the appropriate uri
    fn uri(&self, path: &str) -> String {
        format!("http://{}/{}", self.hostname, path)
    }

    /// Make a simple query
    fn simple_query(&self, query: &[(&str, &str)]) -> reqwest::Result<reqwest::Response> {
        reqwest::Client::new()
            .get(&self.uri("config.cgi"))
            .query(query)
            .header("User-Agent", "User-Agent: cpcxfer")
            .send()
    }

    /// Reset the M4
    pub fn reset_m4(&self) -> Result<(), XferError> {
        self.simple_query(&[("mres", "")])?;
        Ok(())
    }

    /// Reset the Cpc
    pub fn reset_cpc(&self) -> Result<(), XferError> {
        self.simple_query(&[("cres", "")])?;
        Ok(())
    }

    /// Run the file from the current selected path
    /// TODO debug this
    pub fn run_rom_current_path(&self, fname: &str) -> Result<(), XferError> {
        self.simple_query(&[("run", fname)])?;
        Ok(())
    }

    /// Run the file whose complete path is provided
    pub fn run(&self, path: &str) -> Result<(), XferError> {
        let absolute = self.absolute_path(path)?;
        self.simple_query(&[("run2", &absolute)])?;
        Ok(())
    }

    /// Remove the file whose complete path is provided
    pub fn rm<S: AsRef<str>>(&self, path: S) -> Result<(), XferError> {
        self.simple_query(&[("rm", path.as_ref())])?;
        Ok(())
    }

    /// upload a file on the M4
    pub fn upload<P>(
        &self,
        path: P,
        m4_path: &str,
        header: Option<(AmsdosFileType, u16, u16)>,
    ) -> Result<(), XferError>
    where
        P: AsRef<Path>,
    {
        self.upload_impl(path.as_ref(), m4_path, header)
    }

    #[allow(clippy::similar_names)]
    pub fn upload_impl(
        &self,
        path: &Path,
        m4_path: &str,
        header: Option<(AmsdosFileType, u16, u16)>,
    ) -> Result<(), XferError> {
        let local_fname = path.to_str().unwrap();

        if m4_path.len() > 255 {
            panic!(
                "{} path is too long (should be limited to 255 chars)",
                m4_path
            );
        }
        let _file_contents = fs::read(local_fname).expect("Unable to read PC file");

        let local_fname = match header {
            Some(_header) => {
                unimplemented!();
                // Need to build header and compute checksum
                // Need to inject header
            }
            None => {
                // Header is already included within the file
                // TODO check that the header is correct
                local_fname
            }
        };

        // TODO manage more cases in order to allow to provide a destination folder or a destination filename or a different name
        let destination = Path::new(m4_path).join(
            Path::new(local_fname)
                .file_name()
                .expect("Unable to retreive the filename of the file to upload"),
        );
        let destination = destination.to_str().unwrap().to_owned();

        println!("Destination : {:?}", destination);

        let mut form = Form::new();
        form.part("upfile")
            .file(local_fname)
            .filename(&destination)
            .add()
            .unwrap();
        let mut easy = Easy::new();
        easy.url(&self.uri("files.shtml"))?;
        easy.httppost(form)?;
        easy.perform()?;

        Ok(())
    }

    /// Directly sends the SNA to the M4. SNA is first saved as a V2 version as M4 is unable to read other ones
    pub fn upload_and_run_sna(&self, sna: &Snapshot) -> Result<(), XferError> {
        use tempfile::Builder;
        let file = Builder::new()
            .prefix("xfer")
            .suffix(".sna")
            .rand_bytes(4)
            .tempfile()
            .or_else(|e|{
                Err(XferError::InternalError{reason: e.to_string()})
            })?;
        let temp_path = file.into_temp_path();

        sna.save(
            &temp_path, 
            SnapshotVersion::V2)
            .or_else(|e|{
                Err(XferError::InternalError{reason: format!("Unable to save the snapshot. {}", e)})
            })?;
        self.upload_and_run(
            &temp_path, 
            None)?;

        // sleep a bit to be sure the file is not deleted BEFORE sending it to CPC
        std::thread::sleep(std::time::Duration::from_secs(5));
        temp_path.close();
        Ok(())
    }

    pub fn upload_and_run<P: AsRef<Path>>(
        &self,
        path: P,
        header: Option<(AmsdosFileType, u16, u16)>,
    ) -> Result<(), XferError> {
        self.upload_and_run_impl(path.as_ref(), header)
    }

    fn upload_and_run_impl(
        &self,
        path: &Path,
        header: Option<(AmsdosFileType, u16, u16)>,
    ) -> Result<(), XferError> {
        // We are sure it is not a snapshot there
        self.upload_impl(path, "/tmp", header)?;
        self.run(&format!(
            "/tmp/{}",
            path.file_name().unwrap().to_str().unwrap()
        ))?;
        Ok(())
    }

    pub fn current_folder_content(&self) -> Result<M4FilesList, XferError> {
        self.download_dir()
    }

    pub fn current_working_directory(&self) -> Result<String, XferError> {
        let data = self.download_dir()?;
        Ok(data.cwd().clone())
    }

    fn download_dir(&self) -> Result<M4FilesList, XferError> {
        let mut dst = Vec::new();
        {
            {
                let mut easy = Easy::new();
                easy.url(&self.uri("sd/m4/dir.txt"))?;
                let mut easy = easy.transfer();
                easy.write_function(|data| {
                    dst.extend_from_slice(data);
                    Ok(data.len())
                })?;
                easy.perform()?;
            }
        }

        let content =
            std::str::from_utf8(&dst).expect("Unable to create an UTF8 string for M4 content");

        Ok(M4FilesList::from(content))
    }

    /// Change the current directory
    pub fn cd(&self, directory: &str) -> Result<(), XferError> {
        // Get the absolute directory
        let mut directory = if let Some('/') = directory.chars().next() {
            directory.to_owned()
        } else {
            self.absolute_path(directory)?
        };

        self.ls_request(&directory)?;

        // Ensure theire is a / at the end
        if directory.chars().rev().next().unwrap() != '/' {
            directory.push('/');
        }
        let cwd = self.current_working_directory()?;

        if cwd == directory {
            Ok(())
        } else {
            Err(XferError::CdError {
                from: directory,
                to: cwd,
            })
        }
    }

    fn absolute_path(&self, relative: &str) -> Result<String, XferError> {
        match relative.chars().next() {
            None => Err(XferError::InternalError {
                reason: "No path provided".into(),
            }),
            Some('/') => Ok(relative.to_owned()),
            _ => {
                let cwd = self.current_working_directory()?;
                let absolute = Path::new(&cwd).join(relative);

                let absolute = absolute.absolutize().unwrap();
                let path: String = absolute.to_str().unwrap().into();
                if cfg!(target_os = "windows")
                {
                    return Ok(path.replace("C:\\", "/"));
                }

                Ok(path)
            }
        }
    }

    pub fn ls_request(&self, folder: &str) -> Result<(), XferError> {
        let mut easy = Easy::new();
        let folder = easy.url_encode(folder.as_bytes());
        easy.get(true)?;
        let url = format!("{}?ls={}", self.uri("config.cgi"), folder);
        easy.url(&url)?;
        easy.perform()?;
        Ok(())
    }
}