-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.c
128 lines (108 loc) · 2.82 KB
/
storage.c
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
/**
* Project 1
* storage.c
*
* Author: CS3113
*
*/
#include "storage.h"
/**
* Initialize the storage file
*
* @param name Name of the storage file
* @return NULL if there is an error;
* otherwise, a poiner to the initialized STORAGE object
*/
STORAGE * init_storage(char * name, char *pipe_name_base)
{
// Open the file
int fd = open(name, O_RDWR | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
// Is there an error?
if(fd <= 0) {
fprintf(stderr, "Unable to open %s\n", name);
return NULL;
}
// Allocate the STORAGE object and populate it
STORAGE *s = malloc(sizeof(STORAGE));
s->fd = fd;
// Success
return s;
};
/**
* Close an open storage object
*
* @param storage Pointer to an initialized storage object
* @return -1 on error; 0 on success
*
*/
int close_storage(STORAGE *storage)
{
// Close the storage file
int ret = close(storage->fd);
// Was there an error?
if(ret < 0) {
fprintf(stderr, "Unable to close storage.\n");
return(-1);
};
// Closed: now free the allocated space
free(storage);
// Success
return(0);
}
/**
* Read a set of bytes from the storage file.
*
* @param storage A pointer to an initialized storage object
* @param buf The buffer to place the read bytes into
* @param location The point in the file to start reading from
* @param len The number of bytes to read
* @return -1 if an error;
* otherwise, the number of bytes read from the storage file
*/
int get_bytes(STORAGE *storage, unsigned char *buf, int location, int len)
{
// Seek to the starting location
int ret = lseek(storage->fd, location, SEEK_SET);
// Was there an error?
if(ret < 0) {
fprintf(stderr, "Unable to seek\n");
return(-1);
};
// Read the bytes
if((ret = read(storage->fd, buf, len)) < 0){
// There was a reading error
fprintf(stderr, "Error reading fd\n");
return(-1);
};
// Success: return the number of bytes read
return(ret);
};
/**
* Write a set of bytes to the storage file
*
* @param storage A pointer to an initialized storage object
* @param buf The buffer containing the bytes to be written
* @param location The point in the file to start writing to
* @param len The number of bytes to write
* @return -1 if an error;
* otherwise, the number of bytes written to the storage file
*/
int put_bytes(STORAGE *storage, unsigned char *buf, int location, int len)
{
// Seek to the point in the file
int ret = lseek(storage->fd, location, SEEK_SET);
// Was there an error?
if(ret < 0) {
fprintf(stderr, "Unable to seek\n");
return(-1);
};
// Write te bytes to the file
if((ret = write(storage->fd, buf, len)) < 0){
// There was an error
fprintf(stderr, "Error reading fd\n");
return(-1);
};
// Success: return the number of bytes written
return(ret);
};