-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_rem2loc.c
More file actions
87 lines (77 loc) · 2.02 KB
/
user_rem2loc.c
File metadata and controls
87 lines (77 loc) · 2.02 KB
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
// user_rem2loc.c (rclient2)
// Andrew Mauragis
// Due 4/25/13
//
// This is a sample client program for linking with rclient.
// This program copies a remote file specified by the argument array to the
// local machine in the current working directory.
//
// Note, because there are no provisions for a remote Stat command, file
// permissions are not perserved.
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include "rclient.h"
#include "rpcdefs.h"
#define BUFFER_SIZE 512
// entry function
//
// To be called from rclient's main function, argc and argv are trimmed
// in rclient to remove the host address and port number
//
// returns an error code like a main() would.
int entry(int argc, char* argv[])
{
// first we pull in the file name from the argument array
if (argc < 2)
{
fprintf(stderr,"Not enough arguments. Specify a filename\n");
return ARGS_ERROR;
}
char* filename = argv[1];
// open the remote copy
int remfd = Open(filename, O_RDONLY, 000);
if (remfd < 0)
{
perror("[Remote] Open Error");
return OPEN_ERROR;
}
// open up the file to write to
int locfd = open(filename, O_CREAT| O_WRONLY, 0644);
if (locfd < 0)
{
perror("[Local] Open Error");
return OPEN_ERROR;
}
// now we read remotely and write locally
int bytesRead;
unsigned char buf[BUFFER_SIZE];
while ((bytesRead = Read(remfd, &buf, BUFFER_SIZE)) > 0)
{
if (0 >= write(locfd, &buf, bytesRead))
{
perror("[Local] Write Error");
return WRITE_ERROR;
}
}
if (bytesRead < 0)
{
perror("[Remote] Read Error");
return READ_ERROR;
}
// now we close the local and remote fds
if (0 > close(locfd))
{
perror("[Local] Close Error");
return CLOSE_ERROR;
}
if (0 > Close(remfd))
{
perror("[Remote] Close Error");
return CLOSE_ERROR;
}
return 0;
}