File: trunc.c

package info (click to toggle)
reiser4progs 1.0.6-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,348 kB
  • ctags: 3,714
  • sloc: ansic: 33,468; sh: 8,489; makefile: 1,012
file content (51 lines) | stat: -rw-r--r-- 1,147 bytes parent folder | download | duplicates (4)
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
/* Copyright (C) 2001-2005 by Hans Reiser, licensing governed by
   reiser4progs/COPYING.
   
   trunc.c -- a demo program that truncates the file to the given size. */

#include "busy.h"

errno_t trunc_cmd(busy_ctx_t *ctx) {
	reiser4_object_t *object;

	aal_assert("vpf-1712", ctx != NULL);

	if (!ctx->in.fs) {
		aal_error("Fs is not openned. Wrong PAth is specified: %s.",
			  ctx->in.path);
		return -EINVAL;
	}
	
	if (ctx->in.path[0] == 0) {
		aal_error("NULL path is given.");
		return -EINVAL;
	}
	
	if (!(object = reiser4_semantic_open(ctx->in.fs->tree, 
					     ctx->in.path, NULL, 1))) 
	{
		aal_error("Can't open file %s.", ctx->in.path);
		return -EIO;
	}
	
	if (reiser4_psobj(object)->p.id.group != REG_OBJECT) {
		aal_error("The object %s is not a regular file.", 
			  ctx->in.path);
		goto error_close_object;
	}
	
	/* Truncate the object. */
	if (reiser4_object_truncate(object, ctx->count)) {
		aal_error("Can't truncate object %s to %lld bytes.", 
			  ctx->in.path, ctx->count);
		goto error_close_object;
	}
	
	reiser4_object_close(object);
	return 0;
	
 error_close_object:
	reiser4_object_close(object);
	return -EIO;
}