2 * This code was written by Peter Lawthers, and placed in the public
3 * domain for use by DMAPI implementors and app writers.
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
7 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
10 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
11 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
12 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
14 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
15 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
18 * October 2, 2003: Dean Roehrich
19 * - Adapted to test dm_get_bulkattr + dm_get_dmattr, from migfind.c.
33 extern int optind, optopt, opterr;
36 extern void print_victim(void *, size_t, dm_off_t);
37 extern void err_msg(char *, ...);
38 extern void errno_msg(char *, ...);
40 int setup_dmapi(dm_sessid_t *);
41 int scan_fs(dm_sessid_t, void *, size_t, dm_attrname_t*,
49 fprintf(stderr, "Usage: %s ", prog);
50 fprintf(stderr, " [-a attrname] [-b bufsz] [-v]");
51 fprintf(stderr, " [-q] filesystem\n");
55 #define V_VERBOSE 0x02
72 dm_attrname_t *dmattrp = NULL;
73 int verbose = V_PRINT;
76 memset(&dmattr, 0, sizeof(dmattr));
78 while ((c = getopt(argc, argv, "a:b:vq")) != EOF) {
81 if (strlen(optarg) > (DM_ATTR_NAME_SIZE-1)){
82 printf("Arg for -a too long\n");
85 strcpy((char*)dmattr.an_chars, optarg);
105 if (optind >= argc) {
110 fsname = argv[optind];
113 * Now we have our filesystem name and possibly a size threshold
114 * to look for. Init the dmapi, and get a filesystem handle so
115 * we can scan the filesystem
117 error = setup_dmapi(&sid);
121 if (dm_path_to_fshandle(fsname, &fs_hanp, &fs_hlen) == -1) {
122 errno_msg("Can't get filesystem handle");
129 * Get the attributes of all files in the filesystem
131 error = scan_fs(sid, fs_hanp, fs_hlen, dmattrp, bufsz, verbose);
137 * We're done, so we can shut down our session.
139 if (dm_destroy_session(sid) == -1) {
140 errno_msg("Can't close session");
153 dm_attrname_t *dm_attrp,
157 size_t buflen = 10000;
160 if (hlen > HANDLE_LEN) {
161 if (verbose & V_PRINT)
162 printf("-- invalid length --\n");
165 char handle_str[HANDLE_STR];
166 if (verbose & V_PRINT) {
167 printf("%zd\t", hlen);
168 hantoa(hanp, hlen, handle_str);
169 printf("%s ", handle_str);
172 if (dm_get_dmattr(sid, hanp, hlen, DM_NO_TOKEN, dm_attrp,
173 buflen, (char*)buf, &rlen)) {
174 if (verbose & V_PRINT) {
175 if( errno == E2BIG ){
178 else if (errno == ENOMEM){
186 else if (verbose & V_PRINT) {
187 if( isalpha(buf[0]) )
188 printf("(%s)\n", buf);
190 printf("<len=%zd>\n",rlen);
196 * Get the attributes for all the files in a filesystem in bulk,
197 * and print out the handles and sizes of any that meet our target
200 * We are not interested in file names; if we were, then we would
201 * have to do a dm_get_dirattrs() on each directroy, then use
202 * dm_handle_to_path() to get the pathname.
209 dm_attrname_t *dmattrp,
213 u_int mask; /* attributes to scan for */
214 dm_stat_t *dm_statbuf, *sbuf; /* attributes buffer */
215 dm_attrloc_t locp; /* opaque location in fs */
216 size_t rlenp; /* ret length of stat info */
217 void *hanp; /* file handle */
218 size_t hlen; /* file handle */
219 int more; /* loop terminator */
227 dm_statbuf = (dm_stat_t *)calloc(1, buflen);
228 if (dm_statbuf == NULL) {
229 err_msg("Can't get memory for stat buffer");
235 * Initialize the offset opaque offset cookie that
236 * we use in successive calls to dm_get_bulkattr()
238 error = dm_init_attrloc(sid, fs_hanp, fs_hlen, DM_NO_TOKEN, &locp);
240 errno_msg("%s/%d: Can't initialize offset cookie (%d)", __FILE__, __LINE__, errno);
246 * Set our stat mask so that we'll only get the normal stat(2)
247 * info and the file's handle
249 mask = DM_AT_HANDLE | DM_AT_STAT;
251 more = dm_get_bulkattr(sid, fs_hanp, fs_hlen, DM_NO_TOKEN,
252 mask, &locp, buflen, dm_statbuf, &rlenp);
253 if (verbose & V_VERBOSE)
254 fprintf(stderr, "BULKATTR more=%d, rlen=%zd\n",
257 errno_msg("%s/%d: Can't get bulkattr for filesystem", __FILE__, __LINE__, errno);
262 * Walk through the stat buffer and pull out files
263 * that are of interest
265 * The stat buffer is variable length, so we must
266 * use the DM_STEP_TO_NEXT macro to access each individual
267 * dm_stat_t structure in the returned buffer.
270 while (sbuf != NULL) {
271 if (S_ISREG(sbuf->dt_mode)) {
272 hanp = DM_GET_VALUE(sbuf, dt_handle, void *);
273 hlen = DM_GET_LEN(sbuf, dt_handle);
276 my_print_victim(sid, hanp, hlen,
279 else if (verbose & V_PRINT){
280 print_victim(hanp, hlen, sbuf->dt_size);
283 sbuf = DM_STEP_TO_NEXT(sbuf, dm_stat_t *);