-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_loc2rem.c
More file actions
139 lines (118 loc) · 3.35 KB
/
user_loc2rem.c
File metadata and controls
139 lines (118 loc) · 3.35 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
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
// user_loc2rem.c (rclient1)
// 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 up the file to copy remotely
int locfd = open(filename, O_RDONLY, 0000);
if (locfd < 0)
{
perror("[Local] Open Error");
return OPEN_ERROR;
}
// get permissions of local file
struct stat s;
if (0 > fstat(locfd, &s))
{
perror("[Local] Stat Error");
return STAT_ERROR;
}
// open/create the remote copy
int remfd = Open(filename, O_CREAT | O_WRONLY, s.st_mode);
if (remfd < 0)
{
perror("[Remote] Open Error");
return OPEN_ERROR;
}
// now we read locally and write remotely
int bytesRead;
unsigned char buf[BUFFER_SIZE];
while ((bytesRead = read(locfd, &buf, BUFFER_SIZE)) > 0)
{
if (0 >= Write(remfd, &buf, bytesRead))
{
perror("[Remote] Write Error");
return WRITE_ERROR;
}
}
if (bytesRead < 0)
{
perror("[Local] 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;
}
// This is code that was used for debugging during the development of the
// client and server.
// TESTING CODE:
// int rslt = Open("BULLETS", O_CREAT | O_WRONLY, 0644);
// // printf("I opened fd: %d\n", rslt);
// // perror("Open");
// // printf("I'm going to try to write 'dicks dicks dicks\\n' to a file\n");
// int writeval = Write(rslt,"dicks dicks dicks\n", 19);
// perror("Write");
// // printf("Write return: %d\n", writeval);
// int closeval = Close(rslt);
// perror("Close");
// // printf("Close return: %d\n",closeval);
// // now lets try to read it back and print it out
// int fd = Open("BULLETS", O_RDONLY, 0000);
// perror("Open");
// // printf("I opened fd %d for reading.\n", fd);
// char buf[18];
// int readval = Read(fd, &buf, 17);
// perror("Read");
// // printf("I read %d bytes.\n", readval);
// buf[17] = 0;
// closeval = Close(fd);
// perror("Close");
// // printf("I closed with return val %d\n", closeval);
// // printf("The buffer I read was:<%s>\n", buf);
// fd = Open("BULLETS", O_WRONLY, 0644);
// perror("Open");
// Lseek(fd, 0, SEEK_SET);
// perror("Lseek");
// Write(fd, "DICK", 4);
// perror("Write");
// Close(fd);
// perror("Close");